下面是自動打鈴器c程序
#include<reg51.h> #define shum P1 //數(shù)碼 #define saom P2 //數(shù)碼掃描 #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; /*-----------------------走時函數(shù)--------------------------*/ void init_timer() { TMOD=0x01; TH0=(65536-5000)/256; TL0=(65536-5000)%256; IE=0x82; TR0=1; } /*-----------------------5ms定時中斷服務函數(shù)--------------------------*/ 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;} //自動調(diào)時沒過9分鐘(tauto=9)秒變量sec自增1 if(min==60){hour++;min=0;} if(hour==24){hour=0;} } void display() //數(shù)碼管模塊 {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; } } /*-------------------調(diào)整走時時間函數(shù)部分------------------------*/ 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); } } } |