下面是自动打铃器c程序
#include<reg51.h> #define shum P1 //数码 #define saom P2 //数码扫描 #define uchar unsigned char #define uint unsigned int char DATA_7SEG[10]={0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xf8,0x80,0x90,}; uchar hour=0,min=0,sec=0,tauto=0; //时 分 秒 uchar x=8,y=0,g=11,f=55,o=16,p=30; //x是定时1小时设定值 y是定时1分设定值 uchar deda=0; uchar code xsjsbb[60]; bit d_05s=0; uchar set=0; /*-----------------------走时函数--------------------------*/ void init_timer() { TMOD=0x01; TH0=(65536-5000)/256; TL0=(65536-5000)%256; IE=0x82; TR0=1; } /*-----------------------5ms定时中断服务函数--------------------------*/ void T0_srv(void) interrupt 1 { TH0=(65536-5000)/256; TL0=(65536-5000)%256; deda++ ; } /*----------------------时,分,秒单元及走时单元转换-------------------------*/ void conv() { if(deda<=100)d_05s=0; else d_05s=1; if(deda>=200){sec++;deda=0;} if(sec==60){min++;tauto++;sec=0;} if(tauto==9){sec=sec+1;tauto=0;} //自动调时没过9分钟(tauto=9)秒变量sec自增1 if(min==60){hour++;min=0;} if(hour==24){hour=0;} } void display() //数码管模块 {char e,scan; if(deda<=100)P3_4=0; else P3_4=1; if(hour==x)a=1; else a=0; //定时设定值x,y g,f o,p if(min==y)b=1; else b=0; if(sec==2)c=1; else c=0; if(a&b&c) P0_3=0; else P0_3=1; if(hour==g)la=1; else la=0; if(min==f)lb=1; else lb=0; if(sec==2)lc=1; else lc=0; if(la&lb&lc) P0_3=0; else P0_3=1; if(hour==o)ia=1; else ia=0; if(min==p)ib=1; else ib=0; if(sec==2)ic=1; else ic=0; if(ia&ib&ic) P0_3=0; else P0_3=1; disp[3]=hour/10; disp[2]=hour%10; disp[1]=min/10; disp[0]=min%10; P1=0xff; scan=0xef; for(e=0;e<4;e++) { shum=0xff; saom=scan; if(P3_6==0) { if(d_05s==1) {if(e==2)P1_7=0;else P1_7=1;} } else {if(e==2) P1_7=1;} if(P3_6==0) P1=DATA_7SEG[disp[e]]; else P1=0xff; delay(4); scan>>=1; } } /*-------------------调整走时时间函数部分------------------------*/ void set_time() {uchar m; if(P3_1==0)delay(1); if(P3_1==0)hour++; F1:if(P3_1==0)goto F1; if(hour==24)hour=0; for(m=0;m<30;m++) { display(); if(d_05s==1)P2_7=0; else P2_7=1; delay(1); } if(P3_0==0)delay(1); if(P3_0==0)min++; F2:if(P3_0==0)goto F2; if(min==60)min=0; for(m=0;m<30;m++) { display(); if(d_05s==1)P2_7=0; else P2_7=1; delay(1); } } } |