51study实例-C51闹铃程序
时间:12-04 16:42 阅读:790次
*温馨提示:点击图片可以放大观看高清大图
简介:在这里为大家整理了一个基于C51的闹铃程序。
#include <atmel\at89x52.h>
#include <string.h>
#define comdir P3_2
#define wirespeaker P1_3
#define wiret0 T0
#define wiret1 T1
#define adr_delay1s 0x1086
#define adr_print10ms 0x1000
#define adr_print80ms 0x101A
#define adr_screen_black 0x10BA
#define adr_screen_clear 0x10CD
#define adr_key_search 0X10EB
#define adr_sound500hz 0x109E
#define max_timer 24*3
void (* pFunc)(void); //定义指向函数的指针
void (* Print80ms)(void);
int CodeChange(int i);
/*define a struct*/
struct time_str{
unsigned char hour;
unsigned char min;
unsigned char sec;
}today;
int depart=20;
int t0int_flag=0;
unsigned char data dispbuf[8] _at_ 0x30;
unsigned char code Sound_Date[max_timer]={
0x00 0 0 0 0 0 0 0 0 0 0 0
0x0b 0 0 0 0 0 0 0 0 0 0 0
0x0b 0 0 0 0 0 0 0 0 0 0 0
0x0b 0 0 0 0 0 0 0 0 0 0 0
0x0b 0 0 0 0 0 0 0 0 0 0 0
0x0b 0 0 0 0 0 0 0 0 0 0 0
};
/*TIMER0 INTERRUPT*/
void timer0(void) interrupt 1{
wiret1=!wiret1;
TH0=0X3C;
TL0=0XB0;
depart-=1;
if (depart%10==0){
t0int_flag=!t0int_flag;
}
if (depart==0){
depart=20;
today.sec+=1;
wiret0=!wiret0;
if (today.sec==60){
today.sec=0;
today.min+=1;
if(today.min==60){
today.min=0;
today.hour+=1;
if(today.hour==24){
today.hour=0;
}
}
}
}
}
/*SERIAL PORT INTERRUPT*/
void serial(void) interrupt 4{
}
main()
{
char *P;
int j=0;
int i=0;
int k=0;
P3_4=0; //T0=0
P3_4=1; //T0=1
P3_5=0; //T1=0
P3_5=1; //T1=1
pFunc=adr_screen_black; //screen black
(*pFunc)();
pFunc=adr_screen_clear; //clrar screen ram
(*pFunc)();
/*P=0X30;
(*P)=0X79;
P++;
(*P)=0X71;*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//SP=0XD0;
TMOD=0X21;
//TH1=0XF3;
//TL1=0XF3;
//PCON=0;
//SCON=0X40;
//TR1=1;
ET1=0;
//REN=1;
TH0=0X3C; /* 50ms interrupt */
TL0=0XB0;
TR0=1;
ET0=1;
EA=1;
//TI=0;
//SBUF=0XAA; //MON51 占用串口
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pFunc=adr_sound500hz;
(*pFunc)(); /*发声音一秒*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
today.hour=0;
today.min=0;
today.sec=0;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
for(;;)
{
pFunc=adr_print10ms;
(*pFunc)(); /*screen out*/
j=CodeChange(today.hour/10);
k=CodeChange(today.hour%10);
k=k|0x80;
P=0x30;
(*P)=j;
P++;
(*P)=k;
P++;
j=CodeChange(today.min/10);
k=CodeChange(today.min%10);
(t0int_flag==1)?(k=k|0x80):(k=k&0x7f);
(*P)=j;
P++;
(*P)=k;
P++;
j=CodeChange(today.sec/10);
k=CodeChange(today.sec%10);
(*P)=j;
P++;
(*P)=k;
for(i=0;i<max_timer/3;i++){
if((today.hour==Sound_Date[i*3])&&(today.min==Sound_Date[i*3+1])&&(today.sec==Sound_Date[i*3+2])){
pFunc=adr_sound500hz;
(* pFunc)(); /*发声音一秒*/
}
}
pFunc=adr_key_search;
(* pFunc)();
i=ACC;
if (CY==1) {
//while (CY==1){ /***等待放手***/
pFunc=adr_print80ms;
(* pFunc)();
(* pFunc)();
(* pFunc)();
//pFunc=adr_key_search;
//(* pFunc)();
//}
switch(i){
case 7:
today.hour+=1;
if(today.hour>23){today.hour=0;}
break;
case 6:
today.hour-=1;
if(today.hour==255){today.hour=23;}
break;
case 5:
today.min+=1;
if(today.min>59){today.min=0;}
break;
case 4:
today.min-=1;
if(today.min==255){today.min=59;}
break;
case 3:
today.sec+=1;
if(today.sec>59){today.sec=0;}
break;
case 2:
today.sec-=1;
if(today.sec==255){today.sec=59;}
break;
}
}
}
}
//clock table:6:00:00 6:30:00 7:00 7:45:00 0:2:08 0:3:09
int CodeChange(int i){
// 3fh 6 5 4 66 6 7 7 7 67 {
case 0:
i=0X3F;
break;
case 1:
i=0X6;
break;
case 2:
i=0X5b;
break;
case 3:
i=0X4F;
break;
case 4:
i=0X66;
break;
case 5:
i=0X6d;
break;
case 6:
i=0X7d;
break;
case 7:
i=0X7;
break;
case 8:
i=0X7f;
break;
case 9:
i=0X67;
break;
};
return i;
}