标题:
单片机秒表有点慢,这是什么情况?求解
[打印本页]
作者:
2906225248
时间:
2018-12-19 20:56
标题:
单片机秒表有点慢,这是什么情况?求解
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit LCD_RS=P2^6;
sbit LCD_RW=P2^5;
sbit LCD_E=P2^7;
sbit k2=P3^0;
long int count,count1;
char mil,sec,min,mil1,sec1,min1;
unsigned char code table[ ]=" miaobiao ";
unsigned char code table1[ ]=" 00:00:0 ";
void delay(uint z)
{
uchar j,k;
for(j=z;j>0;j--)
for(k=256;k>0;k--);
}
void write_com(uchar com)
{
LCD_RS=0;
LCD_E=0;
LCD_RW=0;
LCD_DATAPINS=com;
delay(5);
LCD_E=1;
delay(5);
LCD_E=0;
}
void write_date(uchar date)
{
LCD_RS=1;
LCD_E=0;
LCD_RW=0;
LCD_DATAPINS=date;
delay(5);
LCD_E=1;
delay(5);
LCD_E=0;
}
void init()
{
uchar num;
LCD_E=0;
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<20;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40);
for(num=0;num<12;num++)
{
write_date(table1[num]);
delay(5);
}
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
EX0=1;
TR0=0;
}
void write_min(uchar add,uchar date)
{
uchar one,two;
one=date/10;
two=date%10;
write_com(0x80+0x40+add);
write_date(0x30+one);
write_date(0x30+two);
}
void write_sec(uchar add,uchar date)
{
uchar three,four;
three=date/10;
four=date%10;
write_com(0x80+0x40+add);
write_date(0x30+three);
write_date(0x30+four);
}
void write_mil(uchar add,uchar date)
{
uchar five;
five=date%10;
write_com(0x80+0x40+add);
write_date(0x30+five);
}
void zjs()
{
if(count==2)
{
delay(5);
count=0;
mil++;
if(mil==10)
{
delay(5);
mil=0;
sec++;
if(sec==60)
{
delay(5);
sec=0;
min++;
if(min==60)
{
delay(5);
min=0;
mil=0;
}
}
}
write_min(4,min);
write_sec(7,sec);
write_mil(10,mil);
}
}
void main()
{
init();
while(1)
{
zjs();
}
}
void int0() interrupt 0
{
delay(10);
TR0=~TR0;
}
void timer0() interrupt 1
{
count++;
}
复制代码
作者:
wulin
时间:
2018-12-19 21:59
T0中断程序中没有重装TH0、TL0初值,当然时间慢30%。
作者:
1092648746
时间:
2018-12-19 22:02
中断里面不要放延时……,你既然开了定时器为何不用硬件计数来计时呢?为什么还要故意用循环延时呢?估计问题就是在这里,重新想想这个计时逻辑吧
作者:
通天塔
时间:
2018-12-19 22:20
定时器方式是 16位定时器/计数器
void int0() interrupt 0
{
delay(10);
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
TR0=~TR0;
}
作者:
ssk7793
时间:
2018-12-20 09:35
可参考我发的定时器代码,点我的主题
作者:
2906225248
时间:
2018-12-25 13:41
谢谢大佬
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1