51单片机主动打铃C言语程序:
#include “reg51.h”
#include “intrins.h” #define uchar unsigned char #define uint unsigned int
uchar code DSY_CODE[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff};//共阴极总段//
uchar DSY_BUFFER[]={0,0,0xBF,0,0,0xff,0,0};//时刻显现时-分 形式
uchar DSY_BUFFER1[]={0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff}; //形式显现段//
uchar Scan_BIT;//位挑选//
uchar DSY_IDX;//段挑选//
uchar Key_State; //按键操控//
uchar s,s100,h1 ;
char h,m;
sbit dd=P1^7; //喇叭和led输出端口//
sbit cc=P1^6;
sbit ss=P1^5; //时刻和秒转化显现键//
void DelayMS(uchar x)
{
uchar i;
while(x–) for(i = 0;i《120;i++);
}
void Increase_Hour()
{
if (++h》23) h=0;
DSY_BUFFER[0] =DSY_CODE[h/10];
DSY_BUFFER[1] =DSY_CODE[h];
}
void Decrease_Hour()
{ h–;
if (h《0) h=23;
DSY_BUFFER[0] =DSY_CODE[h/10];
DSY_BUFFER[1] =DSY_CODE[h];
}
void Increase_Minute()
{
if(++m》59)
{
m=0;Increase_Hour();
}
DSY_BUFFER[3] =DSY_CODE[m/10];
DSY_BUFFER[4] =DSY_CODE[m];
}
void Decrease_Minute()
{ m–;
if(m《0) m=59;
DSY_BUFFER[3] =DSY_CODE[m/10];
DSY_BUFFER[4] =DSY_CODE[m];
}
void Increase_Hour1()
{
if (++h1》7) h1=1;
DSY_BUFFER[6] =DSY_CODE[h1/10];
DSY_BUFFER[7] =DSY_CODE[h1];
}
void Increase_Second()
{
if(++s》59)
{
s=0;Increase_Minute();
}
DSY_BUFFER1[0] =DSY_CODE[s/10];
DSY_BUFFER1[1] =DSY_CODE[s];
}
void T0_INT() interrupt 1 //显现屏显现//
{
TH0=(65536-1000)/256;
TL0=(65536-1000)%6;
if(ss==1) //P1.5键没按下,显现时刻//
{
P3=Scan_BIT;
P2=~DSY_BUFFER[DSY_IDX];
}
else { //P1.5键按下,闪//
P3=Scan_BIT;
P2=~DSY_BUFFER[DSY_IDX];
}
Scan_BIT=_crol_(Scan_BIT,1);//位显现移动//
DSY_IDX=(DSY_IDX+1)%8; //段挑选每次改变//
if(h1==3)
{ if(h==6&m==0)cc=dd=0;else cc=dd=1;//时刻到闹钟响,led亮,一分钟//
if(h==6&m==15)cc=dd=0; else cc=dd=1;
if(h==7&m==0)cc=dd=0; else cc=dd=1;
if(h==12&m==30)cc=dd=0; else cc=dd=1;
if(h==14&m==0)cc=dd=0; else cc=dd=1;
if(h==16&m==30)cc=dd=0; else cc=dd=1;
if(h==18&m==0)cc=dd=0; else cc=dd=1;
if(h==18&m==45)cc=dd=0; else cc=dd=1;
if(h==19&m==30)cc=dd=0; else cc=dd=1;
if(h==21&m==30)cc=dd=0; else cc=dd=1;
if(h==22&m==0)cc=dd=0;else cc=dd=1; }
else cc=dd=1;
if(h1==1|h1==4|h1==2|h1==5)
{ if(h==6&m==0)cc=dd=0;else cc=dd=1;//时刻到闹钟响,led亮,一分钟//
if(h==7&m==0)cc=dd=0; else cc=dd=1;
if(h==12&m==30)cc=dd=0; else cc=dd=1;
if(h==14&m==0)cc=dd=0; else cc=dd=1;
if(h==16&m==30)cc=dd=0; else cc=dd=1;
if(h==18&m==0)cc=dd=0; else cc=dd=1;
if(h==18&m==45)cc=dd=0; else cc=dd=1;
if(h==19&m==30)cc=dd=0; else cc=dd=1;
if(h==21&m==30)cc=dd=0; else cc=dd=1;
if(h==22&m==0)cc=dd=0;else cc=dd=1; }
else cc=dd=1;
if(h1==6)
{ if(h==6&m==0)cc=dd=0;else cc=dd=1;//时刻到闹钟响,led亮,一分钟//
if(h==7&m==0)cc=dd=0; else cc=dd=1;
if(h==12&m==30)cc=dd=0; else cc=dd=1;
if(h==14&m==30)cc=dd=0; else cc=dd=1;
if(h==18&m==0)cc=dd=0; else cc=dd=1;
if(h==21&m==0)cc=dd=0; else cc=dd=1;
if(h==21&m==30)cc=dd=0; else cc=dd=1;
if(h==22&m==30)cc=dd=0;else cc=dd=1; }
else cc=dd=1;
if(h1==7)
{ if(h==6&m==30)cc=dd=0;else cc=dd=1;//时刻到闹钟响,led亮,一分钟//
if(h==7&m==0)cc=dd=0; else cc=dd=1;
if(h==11&m==30)cc=dd=0; else cc=dd=1;
if(h==12&m==30)cc=dd=0; else cc=dd=1;
if(h==15&m==30)cc=dd=0; else cc=dd=1;
if(h==16&m==30)cc=dd=0; else cc=dd=1;
if(h==18&m==0)cc=dd=0; else cc=dd=1;
if(h==18&m==40)cc=dd=0; else cc=dd=1;
if(h==20&m==0)cc=dd=0; else cc=dd=1;
if(h==20&m==30)cc=dd=0; else cc=dd=1;
if(h==21&m==30)cc=dd=0; else cc=dd=1;
if(h==22&m==0)cc=dd=0;else cc=dd=1; }
else cc=dd=1;
}
void T1_INT() interrupt 3//时刻秒主动加一,走时//
{
TH1=(65536-50000)/256;
TL1=(65536-50000)%6;//延时50毫秒//
if (s100==20)
{
s100=0;
Increase_Second();
}
else s100++;
}
void main ()
{
P2=P3=0xFF;
TMOD=0x11;
TH0=(65536-1000)/256;
TL0=(65536-1000)%6;
TH1=0XDC;
TL1=0;
TCON=0x01;
EA=1;
ET0=1;
ET1=1;
h=05,m=55,s=s100,h1=1; //时刻和闹钟初值//
DSY_BUFFER[0]=DSY_CODE[h/10]; //时刻初值赋给时刻段码显现组//
DSY_BUFFER[1]=DSY_CODE[h];
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m];
DSY_BUFFER[6]=DSY_CODE[h1/10];
DSY_BUFFER[7]=DSY_CODE[h1];
DSY_BUFFER1[0]=DSY_CODE[h1/10]; //闹钟初值赋给闹钟段码显现组//
DSY_BUFFER1[1]=DSY_CODE[h1];
Scan_BIT=0xFE; //给位挑选赋初值为二进制数 11111110,即只要最终一位为低电平,之后每次左移一位,依次为11111101,11111011.。..。.01111111,11111110循环移动//
DSY_IDX=0; //段码显现组的挑选变量//
TR0=TR1=1;
Key_State=0xFF;
while(1)
{
if (P1^Key_State) //判别按键是否有按下//
{
DelayMS(10);
if (P1^Key_State)
{
Key_State=P1; EA=0;
if((Key_State&0x01)==0) Increase_Hour(); //小时加一//
else if ((Key_State&0x02)==0) Decrease_Hour(); //小时减一//
else if ((Key_State&0x04)==0) Increase_Minute(); //分钟加一//
else if ((Key_State&0x08)==0) Decrease_Minute(); //分钟减一//
else if ((Key_State&0x10)==0) //复位按键//
{h=05,m=55,s=0,h1=1;
DSY_BUFFER[0]=DSY_CODE[h/10];
DSY_BUFFER[1]=DSY_CODE[h];
DSY_BUFFER[3]=DSY_CODE[m/10];
DSY_BUFFER[4]=DSY_CODE[m];
DSY_BUFFER[6]=DSY_CODE[h1/10];
DSY_BUFFER[7]=DSY_CODE[h1];
}
else if ((Key_State&0x20)==0)Increase_Hour1(); //形式加一//
EA=1;
}
}
}
}