仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
51hei.png (16.31 KB, 下載次數: 36)
下載附件
2021-4-28 21:34 上傳
單片機源程序如下:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit LED1=P2^0;
- sbit LED2=P2^1;
- sbit LED3=P2^2;
- sbit LED4=P2^3;
- sbit LED5=P0^4;
- sbit LED6=P0^5;
- sbit LED7=P0^6;
- sbit LED8=P0^7;
- sbit LEVEL=P2^6;//LEVEL,P2.4引腳上的電平值
- sbit LEVEL1=P2^5;
- sbit LEVEL2=P2^6;
- sbit LEVEL3=P2^7;
- uchar highcount; //記錄高電平定時次數
- uchar zhaowei=0,zhaoweide=0;
- uchar count;
- uchar counth,countl,count2,count3;
- const uchar tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,}; /*顯示0~9*/
- const uchar tab1[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,}; /*顯示0~9*/
- void delay()
- {
- uint i,j;
- for(i=0;i<256;i++);
- for(j=0;j<256;j++);
- }
- void timer0isr(void) interrupt 1 using 1
- {
- TH0 = 0xD8;
- TL0 = 0xF0; //重置計數初值
- highcount++; //溢出次數加1
- if(highcount == zhaowei)
- {
- LEVEL = 0;
- if(highcount==100)
- {
- highcount=0;
- }
- }
- if(highcount == zhaoweide)
- {
- LEVEL = 1;
- if(highcount==100)
- {
- highcount=0;
- }
- }
- }
- void int0( ) interrupt 0 using 1
- {
- zhaowei=zhaowei+5;
- if(zhaowei==100)
- zhaowei=0;
- }
- void int1( ) interrupt 2 using 2
- {
- zhaoweide=zhaoweide+5;
- if(zhaoweide==100)
- zhaoweide=0;
- }
- void main(void)
- {
- zhaowei=0;
- zhaoweide=0;
- highcount = 0; //記錄高電平定時次數
- TMOD = 0x01; //設定T/C0工作在定時器方式1
- LEVEL = 0; //初始電平為高
- TH0 = 0xD8;
- TL0 = 0xF0; //裝載計數初值
- EA = 1; //開總中斷
- ET0 = 1; //開T/C0中斷
- TR0 = 1; //啟動T/C0
- IT0=1;
- IT1=1;
- EX0=1;
- EX1=1;
- PX1=1;
- while(1)
- {
- counth=zhaowei/10;
- countl=zhaowei%10;
- P1=tab[counth];
- LED1=1;
- delay();
- LEVEL1=0;
- LED1=0;
- P1=tab[countl];
- LED2=1;
- delay();
- LEVEL1=1;
- LED2=0;
- //第二段數碼管
- count2=zhaoweide/10;
- count3=zhaoweide%10;
- P0=tab1[count2];
- LED3=1;
- delay();
- LEVEL3=0;
- LED3=0;
- P1=tab1[count3];
- LED4=1;
- delay();
- LEVEL3=0;
- LED4=0;
- }
- }
復制代碼
51hei.png (11.39 KB, 下載次數: 38)
下載附件
2019-11-3 15:32 上傳
由于我是新手以上資料肯定存在一些錯誤,歡迎大家批評指正,一起學習 提高技術(僅供參考 切勿照搬):
利用定時器產生脈沖.zip
(184.24 KB, 下載次數: 41)
2019-11-3 10:44 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|