I wrote the city electricity, generator set power supply automatic conversion control cabinet microcontroller program

xiaoxiao2021-03-06  24

#include

#include

#include

#define uchar unsigned char

#define com8155 xbyte [0xfdf8] // 8155 command port address

#define pa8155 xbyte [0xfdf9] // 8155 port A address

#define pb8155 xbyte [0xfdfa] // 8155 port B address

SBIT P10 = P1 ^ 0;

SBIT P11 = P1 ^ 1;

SBIT P12 = P1 ^ 2;

SBIT P13 = P1 ^ 3;

SBIT P14 = P1 ^ 4;

SBIT P15 = P1 ^ 5;

SBIT P16 = P1 ^ 6;

SBIT P17 = P1 ^ 7;

SBIT P20 = P2 ^ 0;

SBIT P21 = P2 ^ 1;

SBIT P22 = P2 ^ 2;

Bit me_flag = 0;

Bit DE_FLAG = 0;

Bit urs_flag = 0;

Bit Flag_8155 = 0;

Unsigned int IData Num = 0;

Unsigned int T = 50000;

Uchar ml, MH, HH, HL; // HH HL: MH ml

Uchar code x [10] = {0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f}; //0, 1, 2, 3, and..., 9

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

Void Timetoscrn (Uchar iData HHMM, UCHAR IDATA SEL) / / 8155 Time Processing Subfrext

{

PA8155 = SEL;

PB8155 = x [hhmm];

_nop _ (); _ NOP _ (); _ NOP_ ();

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

Void Interrupt_0 () Interrupt 0 Using 1 // External Interrupt 0 Sub Functions - Municipal Electricity and Generator Sets At the same time

{

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

While (t -); t = 50000; while (t -); t = 50000; // Time 200MS

/ / -------------------------------------------------------------------------------------------- -------------------------------------------

While (p17 == 0)

{

P1 = (p1 | 0x04) & 0xE4; // Turn on the city electricity, stopped, until the unit stopped successfully

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

}

Tr0 = 0;

P1 = (p1 | 0x07) & 0xe7; // Keep the main electricity

ME_FLAG = 1;

DE_FLAG = 0;

/ / -------------------------------------------------------------------------------------------- ------------------------------------------ p21 = 1; // 8155 stop working

FLAG_8155 = 0;

P2 = (p2 & 0x1f) | 0xE0; // The mains indicator light is on (111x xxxx)

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

Void Interrupt_2 () Interrupt 2 Using 2 // Emergency stop (interrupt priority)

{

IF (DE_FLAG == 1)

{

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

While (p17 == 0)

{

P1 = (p1 | 0x1c) & 0xfc; // Stop group until the generator set is downtime

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

}

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible outputs;

DE_FLAG = 0;

P21 = 1; // unit runtime display stop

FLAG_8155 = 0;

URS_FLAG = 1;

Tr0 = 0;

P2 = (p2 & 0x1f) | 0xA0; // Conversion failed alarm (101x xxxx)

}

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

Void Time0_int () Interrupt 1 Using 1 // Timer 0 Interrupt

{

TH0 = (65536-40000) / 256; // 20ms interrupt cycle, a total of 3000 times, showing 1 minute

TL0 = (65536-40000)% 256;

Num ;

IF (Num> 3000)

{

Num = 0; ml ;

IF (ml> 9)

{

ML = 0; MH ;

IF (MH> 5)

{

MH = 0; HL ;

IF (HL> 9)

{

HL = 0; HH ;

IF (hh> 9)

HH = 0;

Timetoscrn (HH, 0x08);

}

TimetosCRN (HL, 0x04);

}

TimetosCRN (MH, 0x02);

}

TimetosCRN (ml, 0x01);

}

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

void init ()

{

ME_FLAG = 0; DE_FLAG = 0; urs_flag = 0; FLAG_8155 = 0; NUM = 0; ML = 0; MH = 0; HH = 0; HL = 0;

// IE = 0x85;

EA = 1; // All interrupts allow

EX0 = 1; // External interrupt 0 allows the main electricity interrupt to be interrupted at the same time

EX1 = 1; // External interrupt 1 allows emergency stop generator sets to interrupt allow

ET0 = 1; // Timer 0 interrupt allowed

IP = 0x04; // Expedition external interrupt 1 preferred (emergency stop generator set priority)

TMOD = 0x01; // Timer 0 mode 1, only by TR0

TH0 = (65536-40000) / 256; // 20ms interrupt cycle, a total of 3000 times, indicating 1 minute

TL0 = (65536-40000)% 256;

Tr0 = 0; // Timer 0 Stopping

P1 = 0xff; // p15 = 1, p16 = 1, p17 = 1 Prepare read

P2 = 0x0f; // 8155 is prohibited, set 8155 I / O mode

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

Void main (void) // main program

{

Uchar iData startnum = 0;

Uchar I, SEL;

SEL = 0x01;

T = 50000;

INIT ();

While (1)

{

/ / -------------------------------------------------------------------------------------------- -----------------------------------------

IF (P20 == 0 && Flag_8155 == 1) // Generator set runtime display reset, please

{

COM8155 = 0x0b; // send command character A send bit code B send segment code

For (i = 0; i <4; i ) // display clear

{

Timetoscrn (0, SEL);

SEL = SEL << 1;

}

Tr0 = 0;

ET0 = 0;

Num = 0; ml = 0; MH = 0; HH = 0; HL = 0;

Tr0 = 1;

ET0 = 1;

}

/ / -------------------------------------------------------------------------------------------- ------------------------------------------

// City electric stop or no market power, and emergency stop failure has been repaired (p15 == 1 && p16 == 0 && urs_flag == 0)

{

ME_FLAG = 0; // 市 电 电 通 通 非

StartNum = 0;

/ / -------------------------------------------------------------------------------------------- -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

While (p17 == 1) // Monitoring the success of the generator set startup

{

/ / -------------------------------------------------------------------------------------------- ------------------------------------

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

Tr0 = 0;

IF (startnum> 2) // has been continuously started three times

Break;

/ / -------------------------------------------------------------------------------------------- ------------------------------------

P1 = (p1 | 0x13) & 0xf3; // Start generator set

While (t -); t = 50000; while (t -); t = 50000; // Time 1s

While (t -); t = 50000; while (t -); t = 50000;

While (t -); t = 50000; while (t -); t = 50000;

While (t -); t = 50000; while (t -); t = 50000;

While (t -); t = 50000; while (t -); t = 50000;

/ / -------------------------------------------------------------------------------------------- ------------------------------------

P1 = (p1 & 0xe0) | 0x1f;

StartNum ;

}

IF (p17 == 1) // If the generator set failed

{

P2 = (p2 & 0x1f) | 0xA0; // Conversion failed alarm (101x xxxx)

DE_FLAG = 0;

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

}

ELSE IF (p17 == 0 && de_flag == 0) // If the generator set is successful, the generator set is not connected

{

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

P1 = (p1 | 0x19) & 0xf9; // Enable generator set output

DE_FLAG = 1;

TH0 = (65536-40000) / 256; // 20ms interrupt cycle, a total of 3000 times, indicating 1 minute

TL0 = (65536-40000)% 256;

P2 = (p2 & 0x1f) | 0xc0; // generator set output indicator Light (110X xxxx) P21 = 0; // 8155 enable, generator set working time display start

FLAG_8155 = 1; // 8155 Enable flag 1

P22 = 1; // set 8155 I / O state

COM8155 = 0x0b; // send command character A send bit code B send segment code

For (i = 0; i <4; i ) // display clear

{

Timetoscrn (0, SEL);

SEL = SEL << 1;

}

ET0 = 1; // Timer 0 interrupt allowed

Tr0 = 1; // Start timer 0

}

Else IF (P17 == 0 && de_flag == 1) // If the generator set is running and the generator set is turned on

P2 = (p2 & 0x1f) | 0xc0; // generator set output indicator light (110x xxxx)

}

/ / -------------------------------------------------------------------------------------------- -----------------------------------------

// The city is electrically powered and the generator set is not started.

ELSE IF (p15 == 0 && p16 == 1 && p17 == 1 && me_flag == 0)

{

ME_FLAG = 1; // 市 电 电 电 电子 输 标 1

DE_FLAG = 0;

P1 = (p1 & 0xe0) | 0x1f; // cut off all possible output

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

P1 = (p1 | 0x07) & 0xe7; // Turn on the mains output

P2 = (p2 & 0x1f) | 0xE0; // City electricity output indicator bright (111x xxxx)

}

/ / -------------------------------------------------------------------------------------------- -----------------------------------------

// The city is electrically powered and the generator set is not started, and the main electricity is turned on.

ELSE IF (p15 == 0 && p16 == 1 && p17 == 1 && me_flag == 1)

P2 = (p2 & 0x1f) | 0xE0; // City electricity output indicator bright (111x xxxx)

/ / -------------------------------------------------------------------------------------------- -----------------------------------------

// unknown

Else

{

While (t -); t = 50000; while (t -); t = 50000; // Time 200MSP1 = (P1 & 0xE0) | 0x1f; // cut all possible output

While (t -); t = 50000; while (t -); t = 50000; // delay 200ms

P2 = 0x0f; // Turn off all indicators

Tr0 = 0;

// While (1);

}

/ / -------------------------------------------------------------------------------------------- -----------------------------------------

}

}

/ / -------------------------------------------------------------------------------------------- --------------------------------------------

转载请注明原文地址:https://www.9cbs.com/read-79505.html

New Post(0)