基于51单片机的可控硅调压调光程序-带过零检测
#include
#include
sbitledRS=P1^0;
sbitledRW=P1^1;
sbit ledE=P1^2;
sbit keyjia=P2^7;
sbitkeyjian=P2^6;
sbitcontrolLD=P1^3;
bitflag1=1;
bitflag2=1;
unsigned
char code table[]=" guang liangdu
";
unsignedchar code table1[]=" 00 ";
intnum,flag=0,count=0;
charliangdu=3;
voiddelaykt(void) //导通延时
{
unsigned char a;
for(a=2;a>0;a--);
}
voiddelay(int z) //可调延时
{
unsigned x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void
delay9ms(void) //误差
-0.43402777778us 延时9ms
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=224;b>0;b--)
for(a=17;a>0;a--);
}
voidwrite_com(unsigned com) //写指令
{
ledRS=0;
P0=com;
delay(10);
ledE=1;
delay(20);
ledE=0;
}
voidwrite_date(unsigned date) //写数据
{
ledRS=1;
P0=date;
delay(10);
ledE=1;
delay(20);
ledE=0;
}
voidwrite_liangdu(unsigned liangdu) //写亮度
{
int shi,ge;
shi=(liangdu-3)/10;
ge=(liangdu-3)%10;
write_com(0x80+0x40+7);
write_date(0x30+shi);
write_date(0x30+ge);
}
voidinit() //初始化
{
controlLD=1; //触发控制初始化
ledRW=0; //液晶初始化
ledE=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delaykt();
}
write_com(0x80+0x40);
for(num=0;num<15;num++)
{
write_date(table1[num]);
delaykt();
}
EA=1; //外部中断设置
EX0=1;
IT0=1;
}
voidkeyscanf() //键盘判断
{
if(keyjia==0) //加键判断
{
delaykt();
if(keyjia==0)
{
if(flag1==1)
{
flag1=0;
liangdu++;
if(liangdu==10)
liangdu=3;
write_com(0x80+0x40+7);
write_liangdu(liangdu);
}
}
}
else if(keyjia!=0)
{
delaykt();
if(keyjia!=0)
flag1=1;
}
if(keyjian==0) //减键判断
{
delaykt();
if(keyjian==0)
{
if(flag2==1)
{
flag2=0;
liangdu--;
if(liangdu==2)
liangdu=9;
write_com(0x80+0x40+7);
write_liangdu(liangdu);
}
}
}
else if(keyjian!=0)
{
delaykt();
if(keyjian!=0)
flag2=1;
}
}
voidzhongduan() interrupt 0 //外部中断
{
flag=1;
}
voidtimepd() //同步标志判断与定时器设置
{
if(flag==1)
{
flag=0;
TMOD=0x01;
TH0=(65536-1000*(10-liangdu))/256;
TL0=(65536-1000*(10-liangdu))%256;
ET0=1;
TR0=1;
}
}
voidtime0() interrupt 1 //定时中断程序
{
count=1;
}
voidchufa() //MOS3020触发
{
if(count==1)
{
count=0;
controlLD=0;
delaykt();
controlLD=1;
delay9ms();
controlLD=0;
delaykt();
controlLD=1;
}
}
voidmain()
{
init();
while(1)
{
keyscanf();
timepd();
chufa();
}
}