標(biāo)題: 51單片機(jī)鬧鐘添加取消響鈴按鍵 [打印本頁]

作者: automation007    時(shí)間: 2021-1-24 11:36
標(biāo)題: 51單片機(jī)鬧鐘添加取消響鈴按鍵
本帖最后由 automation007 于 2021-1-28 20:17 編輯

51鬧鐘添加取消響鈴按鍵,有時(shí)候響鈴很煩人,所以添加了按鍵以跳過當(dāng)前響鈴,下一次再響...還添加了工作與休息按鍵,按工作鍵時(shí)就進(jìn)入工作計(jì)時(shí),按休息鍵時(shí),就進(jìn)入休息計(jì)時(shí)。默認(rèn)工作時(shí)長為30分鐘,休息時(shí)長為10分鐘。繼昨天開發(fā)了三個(gè)按鍵,今天又加入了一個(gè)切換鈴聲的按鍵...反正用的是最笨的辦法...誰有空有興趣再研究吧!可以用串口來控制的我偏用8個(gè)IO口來控制....太笨了

單片機(jī)源程序如下:
  1. #include <reg52.h>

  2. #define uint unsigned int
  3. #define uchar unsigned char
  4. sbit K1=P3^7;//跳過當(dāng)前響鈴
  5. sbit K2=P3^6;//開始休息
  6. sbit K3=P3^5;//開始工作

  7. uint working_hours;//
  8. uint rest_time;//
  9. //uint Ring_duration;//


  10. uchar code digital_table[]={//-
  11.         0x3f,0x06,0x5b,0x4f,
  12.         0x66,0x6d,0x7d,0x07,
  13.         0x7f,0x6f,0x77,0x7c,
  14.         0x39,0x5e,0x79,0x71
  15. };
  16. uchar code digital[]={//
  17.         0x1f,0x2f,0x4f,0x8f
  18. };

  19. uint num;//

  20. void init();//
  21. void display_digital();//
  22. void delay_ms(uint x);

  23. void main(){
  24.         /***可修改工作時(shí)間與休息時(shí)間,其它不用動(dòng)***/
  25.         working_hours=29;//工作時(shí)長 29
  26.         rest_time=9;//休息時(shí)長 9




  27.         init();
  28.         display_digital();
  29. }

















  30. void display_digital(){
  31.         uchar temp,m[4],up=1,lower=0,min_temp=0,max_temp=1;
  32.         uchar AlarmClock_switch=1;
  33.         uint tim;//
  34.         while(1){
  35.                 if(K1==0||K2==0||K3==0){
  36.                         delay_ms(50);
  37.                         if(K1==0||K2==0||K3==0){
  38.                                 if(K1==0){//
  39.                                         delay_ms(50);//
  40.                                         if(K1==0){//
  41.                                                 AlarmClock_switch=0;//
  42.                                         }
  43.                                 }
  44.                                 if(K2==0){//10()
  45.                                         delay_ms(50);//
  46.                                         if(K2==0){//
  47.                                                 num=0;//
  48.                                                 tim=0;//
  49.                                                 lower=1;//=1
  50.                                                 up=0;//
  51.                                                 AlarmClock_switch=0;
  52.                                                 min_temp=2;//
  53.                                                 max_temp=3;//
  54.                                         }
  55.                                 }
  56.                                 if(K3==0){//)
  57.                                         delay_ms(50);
  58.                                         if(K3==0){
  59.                                                 num=0;//
  60.                                                 tim=0;//
  61.                                                 up=1;//
  62.                                                 lower=0;//
  63.                                                 AlarmClock_switch=0;
  64.                                                 min_temp=0;//
  65.                                                 max_temp=1;//
  66.                                         }
  67.                                 }
  68.                         }
  69.                 }
  70.                 if(tim!=0&&AlarmClock_switch==0){// 1200
  71.                         AlarmClock_switch=1;//
  72.                 }


  73.                 if(num>=1200){//1  1200
  74.                         num=0;//
  75.                         tim++;//++
  76.                         if(up&&tim>working_hours){//
  77.                                 tim=0;
  78.                                 lower=1;//=1
  79.                                 up=0;//
  80.                                 min_temp=2;//
  81.                                 max_temp=3;//
  82.                         }
  83.                         if(lower&&tim>rest_time){//
  84.                                 tim=0;//
  85.                                 up=1;//
  86.                                 lower=0;//
  87.                                 min_temp=0;//
  88.                                 max_temp=1;//
  89.                         }
  90.                 }
  91.                 if(tim==0&&up==1&&num<340&&AlarmClock_switch==1){//<17   tim1
  92.                         P1=0xfd;//
  93.                 }else if(tim==0&&lower==1&&num<400&&AlarmClock_switch==1){//<26s
  94.                         P1=0xf7;//0xfb
  95.                 }else
  96.                         P1=0xff;//

  97.                 if(tim>9){//>9  
  98.                         temp++;//++
  99.                         if(temp>max_temp)//>
  100.                                 temp=min_temp;//
  101.                         m[min_temp]=tim/10;
  102.                         m[max_temp]=tim%10;
  103.                 }else{
  104.                         temp=min_temp;
  105.                         m[min_temp]=tim;//
  106.                 }
  107.                 P0=0x00;//
  108.                 P2=digital[temp];//
  109.                 P0=digital_table[m[temp]];//
  110.         }
  111. }

  112. void init(){
  113.         num=0;
  114.         TMOD=1;//T016
  115.         TH0=(65535-50000)/255;//50//
  116.         TL0=(65535-50000)%255;//
  117.         EA=1;//
  118.         ET0=1;//0
  119.         TR0=1;//0
  120.         K1=1;
  121.         K2=1;
  122. }

  123. void timer0() interrupt 1 /*using 0*/{//
  124.         TH0=(65535-50000)/255;//50
  125.         TL0=(65535-50000)%255;//50
  126.         num++;//++
  127. }


  128. void delay_ms(uint x){
  129.         uint t,m;
  130.         for(t=x;t>0;t--){
  131.                 for(m=120;m>0;m--);
  132.         }
  133. }
復(fù)制代碼

所有資料51hei提供下載:
51鬧鐘添加取消響鈴按鍵2.rar (1.7 MB, 下載次數(shù): 10)

alarm_clock2.1.rar

2.46 KB, 下載次數(shù): 6, 下載積分: 黑幣 -5






歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1