led灯是给0亮,蜂鸣器和继电器是给1工作
#include <STC15F2K60S2.H>
#include <INTRINS.H>
unsigned char buzzer,relay;
typedef struct
{
unsigned char b0 : 1;
unsigned char b1 : 1;
unsigned char b2 : 1;
unsigned char b3 : 1;
unsigned char b4 : 1;
unsigned char b5 : 1;
unsigned char b6 : 1;
unsigned char b7 : 1;
}bits;
typedef union
{
unsigned char hex;
bits b;
}HexToBin;
HexToBin led_ctrl,buzzer_ctrl;
void Delay200ms() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 10;
j = 31;
k = 147;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void Device_Ctrl(unsigned char position,unsigned char dat)
{
P0 = dat;
P2 = position;
P2=0x00;
}
void System_Init(void)
{
Device_Ctrl(0xa0,(buzzer<<6)|(relay<<4));
}
void main(void)
{
System_Init();
led_ctrl.hex = 0x55;
Device_Ctrl(0x80,led_ctrl.hex);
while(1)
{
led_ctrl.b.b1 = 1;
Device_Ctrl(0x80,led_ctrl.hex);
buzzer_ctrl.b.b4 = 0;
Device_Ctrl(0xa0,buzzer_ctrl.hex);
Delay200ms();Delay200ms();Delay200ms();Delay200ms();Delay200ms();
led_ctrl.b.b1 = 0;
Device_Ctrl(0x80,led_ctrl.hex);
buzzer_ctrl.b.b4 = 1;
Device_Ctrl(0xa0,buzzer_ctrl.hex);
Delay200ms();Delay200ms();Delay200ms();Delay200ms();Delay200ms();
}
}