Chinaunix首页 | 论坛 | 博客
  • 博客访问: 420108
  • 博文数量: 121
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1393
  • 用 户 组: 普通用户
  • 注册时间: 2014-03-11 12:17
个人简介

www.vibexie.com vibexie@qq.com

文章分类

全部博文(121)

文章存档

2015年(55)

2014年(66)

我的朋友

分类: 嵌入式

2014-06-10 19:29:27


点击(此处)折叠或打开

  1. #include<reg52.h>

  2. #define uchar unsigned char
  3. #define uint unsigned int

  4. sbit dula=P2^6;
  5. sbit wela=P2^7;
  6. sbit key1=P3^4;
  7. sbit key2=P3^5;
  8. sbit key3=P3^6;
  9. sbit key4=P3^7;

  10. uchar code table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

  11. uchar code table_dp[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef,0xf7,0xfc,0xb9,0xde,0xf9,0xf1};

  12. void display(uchar,uchar,uchar,uchar,uchar,uchar);
  13. void delayms(uint);
  14. void keyscan();
  15. void timescan();
  16. void bellscan();
  17. void setbell();
  18. void savebell();
  19. void ring();

  20. uchar num,sec_num,min_num,hou_num,sec_shi,sec_ge,min_shi,min_ge,hou_shi,hou_ge,hou_bell,min_bell,sec_bell,set_save,ring_time,ring_yes;

  21. int main()
  22. {
  23.     TMOD=0x11;//定时器0和1为工作方式1
  24.     TH0=(65535-45872)/256;
  25.     TL0=(65535-45872)%256;
  26.     TH1=(65535-45872)/256;
  27.     TL1=(65535-45872)%256;

  28.     EA=1;
  29.     ET0=1;
  30.     ET1=1;
  31.     TR0=0;
  32.     TR1=1;
  33.     
  34.     while(1)
  35.     {
  36.     keyscan();
  37.     if(set_save==1)
  38.      setbell();
  39.     else
  40.      savebell();
  41.         display(sec_shi,sec_ge,min_shi,min_ge,hou_shi,hou_ge);

  42.     if(sec_num==sec_bell && min_num==min_bell && hou_num==hou_bell)
  43.      ring_yes=1;
  44.     if(ring_yes==1)
  45.      {
  46.         for(ring_time=0;ring_time<5;ring_time++)
  47.         {
  48.          key4=0;
  49.          delayms(1000);
  50.          key4=1;
  51.          delayms(1000);
  52.          ring_yes=0;
  53.         }
  54.      }
  55.     }

  56.     return 0;
  57. }

  58. void display(uchar sec_shi,uchar sec_ge,uchar min_shi,uchar min_ge,uchar hou_shi,uchar hou_ge)
  59. {
  60.     //second
  61.     dula=1;
  62.     P0=table[sec_shi];
  63.     dula=0;
  64.     P0=0xff;

  65.     wela=1;
  66.     P0=0xef;
  67.     wela=0;
  68.     delayms(1);

  69.     dula=1;
  70.     P0=table[sec_ge];
  71.     dula=0;
  72.     P0=0xff;
  73.     
  74.     wela=1;
  75.     P0=0xdf;
  76.     wela=0;
  77.     delayms(1);

  78.     //minute
  79.     dula=1;
  80.     P0=table[min_shi];
  81.     dula=0;
  82.     P0=0xff;

  83.     wela=1;
  84.     P0=0xfb;
  85.     wela=0;
  86.     delayms(1);

  87.     dula=1;
  88.     P0=table_dp[min_ge];
  89.     dula=0;
  90.     P0=0xff;
  91.     
  92.     wela=1;
  93.     P0=0xf7;
  94.     wela=0;
  95.     delayms(1);

  96.     //hour
  97.     dula=1;
  98.     P0=table[hou_shi];
  99.     dula=0;
  100.     P0=0xff;

  101.     wela=1;
  102.     P0=0xfe;
  103.     wela=0;
  104.     delayms(1);

  105.     dula=1;
  106.     P0=table_dp[hou_ge];
  107.     dula=0;
  108.     P0=0xff;
  109.     
  110.     wela=1;
  111.     P0=0xfd;
  112.     wela=0;
  113.     delayms(1);
  114. }

  115. void delayms(uint xms)
  116. {
  117.     uint i,j;
  118.     for(i=xms;i>0;i--)
  119.         for(j=112;j>0;j--);
  120. }

  121. void T0_time() interrupt 1
  122. {
  123.     TH0=(65535-45872)/256;
  124.     TL0=(65535-45872)%256;
  125.     if(sec_bell==60)
  126.     sec_bell=0;
  127.     if(min_bell==60)
  128.         min_bell=0;
  129.     if(hou_bell==24)
  130.         hou_bell=0;
  131.         
  132.     sec_shi=sec_bell/10;
  133.     sec_ge=sec_bell%10;

  134.     min_shi=min_bell/10;
  135.     min_ge=min_bell%10;

  136.     hou_shi=hou_bell/10;
  137.     hou_ge=hou_bell%10;

  138.     num++;
  139.     if(num>=20)
  140.     {
  141.         num=0;
  142.         sec_num++;
  143.         if(sec_num==60)
  144.         {
  145.             sec_num=0;
  146.             min_num++;
  147.             if(min_num==60)
  148.             {
  149.                 min_num=0;
  150.                 hou_num++;
  151.                 if(hou_num==24)
  152.                 {
  153.                     hou_num=0;
  154.                 }
  155.             }
  156.         }
  157.     }
  158. }

  159. void T1_time() interrupt 3
  160. {
  161.     TH1=(65535-45872)/256;
  162.     TL1=(65535-45872)%256;
  163.     num++;
  164.     if(num>=20)
  165.     {
  166.         num=0;
  167.         sec_num++;
  168.         if(sec_num==60)
  169.         {
  170.             sec_num=0;
  171.             min_num++;
  172.             if(min_num==60)
  173.             {
  174.                 min_num=0;
  175.                 hou_num++;
  176.                 if(hou_num==24)
  177.                 {
  178.                     hou_num=0;
  179.                 }
  180.             }
  181.         }
  182.         
  183.         sec_shi=sec_num/10;
  184.         sec_ge=sec_num%10;

  185.         min_shi=min_num/10;
  186.         min_ge=min_num%10;

  187.         hou_shi=hou_num/10;
  188.         hou_ge=hou_num%10;
  189.     }
  190. }

  191. void keyscan()
  192. {
  193.     if(key4==0)
  194.     {
  195.     delayms(10);
  196.     if(key4==0)
  197.     {
  198.         if(set_save==0)
  199.          set_save=1;
  200.         else if(set_save==1)
  201.          set_save=0;
  202.      while(!key4);
  203.     }
  204.     }
  205. }

  206. void setbell()
  207. {
  208.     P1=0x00;
  209.     TR0=1;
  210.     TR1=0;
  211.     bellscan();
  212. }

  213. void savebell()
  214. {
  215.     P1=0xff;
  216.     TR0=0;
  217.     TR1=1;
  218.     timescan();
  219. }

  220. void timescan()
  221. {
  222.     if(key1==0)
  223.     {
  224.     delayms(10);
  225.     if(key1==0)
  226.     {
  227.      sec_num++;
  228.      if(sec_num==60)
  229.         sec_num=0;
  230.      while(!key1);
  231.     }
  232.     }

  233.     if(key2==0)
  234.     {
  235.     delayms(10);
  236.     if(key2==0)
  237.     {
  238.      min_num++;
  239.      if(min_num==60)
  240.         min_num=0;
  241.      while(!key2);
  242.     }
  243.     }

  244.     if(key3==0)
  245.     {
  246.     delayms(10);
  247.     if(key3==0)
  248.     {
  249.      hou_num++;
  250.      if(hou_num==24)
  251.         hou_num=0;
  252.      while(!key3);
  253.     }
  254.     }
  255. }

  256. void bellscan()
  257. {
  258.     if(key1==0)
  259.     {
  260.     delayms(10);
  261.     if(key1==0)
  262.     {
  263.      sec_bell++;
  264.      if(sec_bell==60)
  265.         sec_bell=0;
  266.      while(!key1);
  267.     }
  268.     }

  269.     if(key2==0)
  270.     {
  271.     delayms(10);
  272.     if(key2==0)
  273.     {
  274.      min_bell++;
  275.      if(min_bell==60)
  276.         min_bell=0;
  277.      while(!key2);
  278.     }
  279.     }

  280.     if(key3==0)
  281.     {
  282.     delayms(10);
  283.     if(key3==0)
  284.     {
  285.      hou_bell++;
  286.      if(hou_bell==24)
  287.         hou_bell=0;
  288.      while(!key3);
  289.     }
  290.     }
  291. }

阅读(1551) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~