標題: 單片機定時器0的方式1實現(xiàn)一個發(fā)光二極管以500ms間隔閃爍,用定時器1的方式1實現(xiàn)兩... [打印本頁]

作者: yqx4327023    時間: 2020-3-27 17:02
標題: 單片機定時器0的方式1實現(xiàn)一個發(fā)光二極管以500ms間隔閃爍,用定時器1的方式1實現(xiàn)兩...
定時器0的方式1實現(xiàn)一個發(fā)光二極管以500ms間隔閃爍,用定時器1的方式1實現(xiàn)兩位數(shù)碼...

#include <reg51.h>
#define uchar unsigned char
sbit LED=P1^0;                 //連的是LED
#define MAX_NUM     99         //最大倒計時數(shù)
#define SEGPORT     P0         //定義數(shù)碼管連接端口
sbit LE_DUAN = P2^6;           //定義573鎖存使能端口 段鎖存
sbit LE_WEI  = P2^7;           //定義573鎖存使能端口 位鎖存
unsigned char code Seg_Duan[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};   //共陰數(shù)碼管顯示段碼值 0~9
unsigned char code Seg_Wei[]  = {0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};             //數(shù)碼管的位碼,低電平有效。
static uchar second=MAX_NUM,count0,count1;   
uchar shiwei;
uchar gewei;
void delay(unsigned int cnt)
{        
while(--cnt);
}
void main()
{      
  TMOD=0x11;                                                               
  TH0=(65536-50000)/256;                                //定時時間   50ms        
  TL0=(65536-50000)%256;  
  TH1=(65536-50000)/256;                                
  TL1=(65536-50000)%256;        
  EA=1;        
  ET0=1;      
  TR0=1;
  ET1=1;      
  TR1=1;        
  SEGPORT = 0;   
  LE_DUAN = 0;        
  LE_WEI  = 0;     
  while(1)      
  {              
   shiwei=second/10;                       //數(shù)碼管十位               
   gewei=second%10;                        //數(shù)碼管個位               
   SEGPORT=0;                              //清空數(shù)據(jù)            
   LE_DUAN=1;                              //段鎖存               
   LE_DUAN=0;               
   delay(30);               
   SEGPORT = Seg_Wei[1];                   //先送位碼值               
   LE_WEI = 1;                             //573高電平鎖存               
   LE_WEI = 0;               
   SEGPORT = Seg_Duan[shiwei];             //再送段碼值               
   LE_DUAN = 1;                            //573高電平鎖存               
   LE_DUAN = 0;               
   delay(300);
   SEGPORT = Seg_Wei[0];                   //先送位碼值   
   LE_WEI = 1;                             //573高電平鎖存              
   LE_WEI = 0;         
   SEGPORT = Seg_Duan[gewei];              //再送段碼值            
   LE_DUAN = 1;                            //573高電平鎖存               
   LE_DUAN = 0;               
   delay(300);        
   }
}
void tim0(void) interrupt 1    //定時器0中斷   
{        
  TH0=(65536-50000)/256;       //定時時間   50ms        
  TL0=(65536-50000)%256;        
  count0++;   
  if(count0==10)               //50毫秒定時,50*10=500(毫秒)   
  {           
   count0=0;                       
   LED=~LED;                  //LED取反                                                      
   }                 
}
void tim1(void) interrupt 3    //定時器1中斷   
{        
  TH1=(65536-50000)/256;                                
  TL1=(65536-50000)%256;        
  count1++;   
  if(count1==20)              //50毫秒定時,50*20=1000(毫秒)=1(秒)  
  {           
   count1=0;                       
   second--;                                             
   if(second==0xff)                                             
{               
   second=MAX_NUM;               
    }                  
   }                 
}

作業(yè).rar

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






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