標(biāo)題: 一個簡單的秒表單片機(jī)程序+proteus仿真工程 [打印本頁]

作者: 月默的愛    時間: 2017-6-15 08:36
標(biāo)題: 一個簡單的秒表單片機(jī)程序+proteus仿真工程
這周實訓(xùn)弄得一個簡單的秒表
仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)


單片機(jī)源程序如下:
  1. #include <reg51.H>  
  2. sbit sta_end=P0^0;
  3. sbit reset=P3^7;   
  4. unsigned char code table0[]={       
  5.                                         0x3F,/*0*/
  6.                                 0x06,/*1*/
  7.                                 0x5B,/*2*/
  8.                                 0x4F,/*3*/
  9.                                 0x66,/*4*/
  10.                                 0x6D,/*5*/
  11.                                 0x7D,/*6*/
  12.                                 0x07,/*7*/
  13.                                 0x7F,/*8*/
  14.                                 0x6F,/*9*/
  15.                                         };   
  16. unsigned char code table1[]={       
  17.                                         0x3F,/*0*/
  18.                                 0x06,/*1*/
  19.                                 0x5B,/*2*/
  20.                                 0x4F,/*3*/
  21.                                 0x66,/*4*/
  22.                                 0x6D,/*5*/
  23.                                 0x7D,/*6*/
  24.                                 0x07,/*7*/
  25.                                 0x7F,/*8*/
  26.                                 0x6F,/*9*/
  27.                                         };
  28. /*聲明second10用于計數(shù)十分之一秒變化,second1用于記錄秒的個*/       /*位,second2用于記錄秒的十位*/  
  29. unsigned int second10,second1,second2;
  30. bit bdata flag;   /*以下是設(shè)置延時功能的函數(shù)*/
  31. void delay()                     
  32. {   
  33. unsigned char i,j;   
  34. for(i=90;i>0;i--)   
  35. for(j=200;j>0;j--);  
  36. }      
  37. /*以下是設(shè)置時間的函數(shù)*/
  38. void  setTime()   
  39. {                  
  40. second10++;   
  41. if(second10==10)             /*當(dāng)十分之一秒計數(shù)到十后從零重新計時*/   
  42. {   
  43.   second10=0;                /*同時秒計時個位加一*/   
  44.   second1++;                 
  45.   if(second1==10)             /*當(dāng)秒計數(shù)個位到十后從零重新計時*/   
  46. {
  47.   second1=0;   
  48.   second2++;               /*同時秒計時十位加一*/   
  49.   if(second2==10)     
  50.   second2=0;  
  51. }   
  52. }
  53. }   
  54. /*以下是向LED管輸數(shù)據(jù)并使之顯示的函數(shù)*/  
  55. void dispact()                                       
  56. {   
  57.    P3=0xfe;                        
  58.    P1=table1[second1];        
  59.    delay();   
  60.    P3=0xfd;   
  61.    P1=table1[second2];
  62.    delay();   
  63.    P2=table0[second10];
  64. }
  65. /*十分之一秒刷新一次,實現(xiàn)十分之一秒進(jìn)一*/  
  66. Timer0 ()  interrupt 1 using 1
  67. {   
  68.   TH0 = (65535 - 50000)/256;   
  69.   TL0 = (65535 - 50000)%256;   
  70.   if(flag)     
  71.      setTime();   
  72.   dispact();
  73. }   
  74. void main(void)                     
  75. {   
  76.   TMOD = 0x01;
  77.   TH0 = (65535 - 50000)/256;         /*定時器賦初值*/   
  78.   TL0 = (65535 - 50000)%256;   
  79.   flag = 0;
  80.   EA = 1;                     /*cpu開中斷*/   
  81.   TR0 = 1;                           /*利用定時器0*/   
  82.   ET0 = 1;                           /*外部中斷允許*/   
  83.   do   
  84.   {      
  85.      if(!sta_end)  
  86.   {      
  87.      if(flag == 0)      
  88. ……………………

  89. …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
新建文件夾.rar (17.34 KB, 下載次數(shù): 12)



作者: 月默的愛    時間: 2017-6-15 08:45
一個0到99的秒表,可暫停和復(fù)位




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