熱門(mén): 51單片機(jī) | 24小時(shí)必答區(qū) | 單片機(jī)教程 | 單片機(jī)DIY制作 | STM32 | Cortex M3 | 模數(shù)電子 | 電子DIY制作 | 音響/功放 | 拆機(jī)樂(lè)園 | Arduino | 嵌入式OS | 程序設(shè)計(jì)
![]() |
發(fā)布時(shí)間: 2018-10-30 15:06
正文摘要:請(qǐng)問(wèn)為什么設(shè)好初值后點(diǎn)擊啟動(dòng)按鈕,不會(huì)倒計(jì)時(shí)。 #include<reg52.h> //定義函數(shù)變量 int sec; unsigned char beepset; unsigned char num; unsigned char n; unsigned char minu10; unsigned char mi ... |
HC6800-ES-V2.0 發(fā)表于 2018-10-31 08:13 您好,能把整個(gè)程序發(fā)我嗎 |
程序太亂了,可以簡(jiǎn)化一下,再看語(yǔ)句問(wèn)題 |
最好打斷點(diǎn),一步步看哪里出錯(cuò)了 |
可以學(xué)習(xí)一下定時(shí) |
在main函數(shù)里沒(méi)有啟動(dòng)定時(shí)器;初值里沒(méi)有TR0=1;while里也沒(méi)有啟動(dòng)TR0=1的語(yǔ)句。再void Ex()interrupt 0 {TR0=0;}你用中斷停了定時(shí)器也不知你是什么意思,是實(shí)再什么功能。 |
由于時(shí)間關(guān)系,沒(méi)法研究你的程序,不過(guò)我有一個(gè)現(xiàn)成的,你可以對(duì)照一下,找找原因。 /* 使用定時(shí)器中斷進(jìn)行的倒計(jì)時(shí)程序。 */ #include <STC89C5xRC.H>//單片機(jī)所有資源說(shuō)明文件 #include <intrins.h>//循環(huán)移位函數(shù)所在的頭文件 #define SMG P0//數(shù)碼管接口 sbit K1=P3^4;//獨(dú)立按鍵接口 sbit K2=P3^5; sbit K3=P3^6; sbit K4=P3^7; sbit DUAN=P2^6;//數(shù)碼管顯示段碼的鎖存 sbit WEI=P2^7;//選擇數(shù)碼管位的鎖存 sbit FMQ=P2^3;//蜂鳴器 unsigned char ZF[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F, 0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71}; unsigned char Wei[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f}; unsigned char PLAY[8]={0,0,0,0,0,0,0,0}; unsigned char n=0,TT=0; void Delay1ms() //@11.0592MHz,用于數(shù)碼管動(dòng)態(tài)顯示時(shí)間間隔 { unsigned char i, j; _nop_(); i = 2; j = 199; do { while (--j); } while (--i); } void Delay10ms() //@11.0592MHz,用于按鍵消抖時(shí)間 { unsigned char i, j; i = 18; j = 235; do { while (--j); } while (--i); } void Delay100ms() //@11.0592MHz,用于蜂鳴器響的時(shí)間 { unsigned char i, j, k; _nop_(); _nop_(); i = 5; j = 52; k = 195; do { do { while (--k); } while (--j); } while (--i); } void Timer0Init(void) //50毫秒@11.0592MHz,定時(shí)器設(shè)置 { AUXR &= 0x7F; //定時(shí)器時(shí)鐘12T模式 TMOD &= 0xF0; //設(shè)置定時(shí)器模式 TMOD |= 0x01; //設(shè)置定時(shí)器模式 TL0 = 0x00; //設(shè)置定時(shí)初值 TH0 = 0x4C; //設(shè)置定時(shí)初值 EA = 1; //總中斷開(kāi)啟 ET0 = 1; //定時(shí)器0中斷開(kāi)啟 TF0 = 0; //清除TF0標(biāo)志 TR0 = 0; //定時(shí)器0不計(jì)時(shí) } void XS(void) { unsigned char i; for (i=0;i<8;i++) { SMG=Wei; WEI=1; WEI=0; SMG=PLAY; DUAN=1; DUAN=0; Delay1ms(); SMG=0x00; DUAN=1; DUAN=0; } } void DISPLAY(void) { PLAY[5]=ZF[n/100]; PLAY[6]=ZF[(n%100)/10]; PLAY[7]=ZF[(n%10)/1]; } void main(void) { unsigned char m; bit KS=0; Timer0Init(); while(1) { if (K1==0) { Delay10ms(); if (K1==0) { n=n+1; } for(m=0;((m<50)&&(K1==0));m++) { Delay10ms(); } } if (K2==0) { Delay10ms(); if (K2==0) { n=n-1; } for(m=0;((m<50)&&(K2==0));m++) { Delay10ms(); } } if (K3==0) { Delay10ms(); if (K3==0) { n=0; } for(m=0;((m<50)&&(K3==0));m++) { Delay10ms(); } } if (K4==0) { Delay10ms(); if (K4==0) { if (n!=0) { TR0=1; } } for(m=0;((m<50)&&(K4==0));m++) { Delay10ms(); } } DISPLAY(); XS(); } } void Time0 (void) interrupt 1//定時(shí)器0中斷服務(wù)函數(shù) { unsigned char m; TL0 = 0x00; TH0 = 0x4C; TT++; if (TT==20) { n--; if (n==0) { for (m=0;m<100;m++) { DISPLAY(); XS(); } TR0=0; FMQ=0; Delay100ms(); FMQ=1; } TT=0; } } 程序?qū)?yīng)的電路: ![]() ![]() ![]() |
sec設(shè)置初值為0了,后邊也沒(méi)看到有其他的動(dòng)作啊 |
Powered by 單片機(jī)教程網(wǎng)