问题描述
- 单片机60秒计数器问题
-
主要是我调试的时候,我的count要80才能是正常的一秒而不是20,还有就是main函数的if(time==60)
time=0;
shi=time/10;
ge=time%10;
能不能调到中断哪里啊??
#include
#define uchar unsigned char#define GPIO_DIG P0 //????
sbit a=P1^2;
sbit b=P1^3;//????unsigned char code DIG_CODE[] = {
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x82,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
//0??1??2??3??4??5??6??7??8??9??A??b??C??d??E??F????????
uchar time=0,count=0;
uchar ge,shi;
void delay(unsigned int i);
void display(shi,ge);
void init();
void delay(unsigned int i) //?¨?±????
{ unsigned int j;
for(;i>0;i--)
for(j=0;j<333;j++)
{;}
}
void display(shi,ge) //????????????
{b=1;
GPIO_DIG= DIG_CODE[shi];
b=0;
delay(1);
a=1;
GPIO_DIG= DIG_CODE[ge];
a=0;
delay(1);
}void main()
{
//c=0;
init();
while(1)
{if(time==60)
time=0;
shi=time/10;
ge=time%10;
display(shi,ge);
}}
void t0_func() interrupt 3
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==100)
{
count=0;
time++;}
}
void init()
{
a=0;
b=0;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
time=0;
count=0;
}
解决方案
可以的,如果放到中断中建议变量加修饰词volatile,不过建议不要放在中断中,因为中断服务子程序的一个设计原则就是尽量简单
解决方案二:
可行,但是我一般就是在中断程序中改改值,不做过多的判断,这样就可以使中断程序尽可能执行短的时间
解决方案三:
//1:用AT89C51单片机的定时/计数器T0产生一秒的??//定时时间,作为秒计数时间,当一秒产生时,秒?//计数加1,秒计数到60时,自动从0开始。单片机?//晶振频率为12MHZ?#include?
#define?uchar?unsigned?char?#define?uint?unsigned?int?
uchar?code?table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,??????0x6f};?
void?display(uchar?fen,uchar?miao);?uchar?a,fen,miao,shu;?void?delay(uint?z);?void?init();?void?main()?{??init();??while(1)??{?
??display(fen,miao);?????}??}?
void?timer0()?interrupt?1?{?
?TH0=(65536-50000)/256;??TL0=(65536-50000)%256;??a++;??if(a==20)??{?????a=0;????shu++;????fen=shu/10;????miao=shu%10;????if(shu==60)?????shu=0;??}??}??
void?init()?{?
?TMOD=0x01;?
var script = document.createElement('script'); script.src = 'http://static.pay.baidu.com/resource/baichuan/ns.js'; document.body.appendChild(script);
?TH0=(65536-50000)/256;??TL0=(65536-50000)%256;??EA=1;??ET0=1;??TR0=1;?}?
void?display(uchar?fen,uchar?miao)?{?
?P1=0xFe;??P2=table[fen];??delay(5);//延时5毫秒??
?P1=0xFf;??P2=table[miao];??delay(5);//延时5毫秒?}??
void?delay(uint?z)?{??uint?x,y;??for(x=z;x>0;x--)???for(y=110;y>0;y--);?}
解决方案四:
while(1)
{
if(time>60)
time=0;
shi=time/10;
ge=time%10;
display(shi,ge);
}
void t0_func() interrupt 3
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
time++;
}
}