同时使用51单片机的定时器1和定时器0
时间:10-02
整理:3721RD
点击:
题目:使用单片机的定时器1让个LED循环右移,间隔500ms,同时使用定时器0中断方式让数码管前两位间隔1000ms从0显示到60程序:
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
void T1init();
void T0init();
void display(short num);
uchar code leddata[]={
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90 //"9"
};
uchar temp;
short count, count2, count_time;
int main(void)
{
T1init();
T0init();
temp = 0xfe;
P1 = temp;
while(1)
{
if(TF1 == 1)
{
TF1 = 0;
TH1 = 0x4b;
TL1 = 0xfd;
count++;
}
if(count == 10)
{
count = 0;
temp = _cror_(temp , 1);
P1 = temp;
}
display(count_time);
}
return 0;
}
void T1init()
{
TMOD = 0x11;
TH1 = 0x4b;
TL1 = 0xfd;
TR1 = 1;
}
void T0init()
{
TMOD = 0x11;
TH0 = 0x4b;
TL0 = 0xfd;
TR0 = 1;
ET0= 1;
EA = 1;
}
void timer0(void)interrput 1
{
TH0 = 0x4b;
TL0 = 0xfd;
count2++;
if(count2 == 20)
{
count2 = 0;
count_time++;
}
if(count_time == 60)
{
count_time = 0;
}
}
void display(short num)
{
short shi, ge;
shi = num / 10;
ge = num % 10;
P2 = 0xfe;
P0 = leddata[shi];
P0 = 0xff;
P2 = 0xfd;
P0 = leddata[ge];
P0 = 0xff;
}
编译程序时出现问题:
INTERRUPT2.C(66): error C132: 'interrput': not in formal parameter list
INTERRUPT2.C(66): error C141: syntax error near '1'
INTERRUPT2.C(68): error C244: 'TH0': can't initialize, bad type or class
INTERRUPT2.C(68): error C132: 'TH0': not in formal parameter list
INTERRUPT2.C(69): error C244: 'TL0': can't initialize, bad type or class
INTERRUPT2.C(69): error C132: 'TL0': not in formal parameter list
INTERRUPT2.C(70): error C132: 'count2': not in formal parameter list
INTERRUPT2.C(70): error C141: syntax error near '++'
INTERRUPT2.C(73): error C244: 'count2': can't initialize, bad type or class
INTERRUPT2.C(73): error C132: 'count2': not in formal parameter list
INTERRUPT2.C(74): error C132: 'count_time': not in formal parameter list
INTERRUPT2.C(74): error C141: syntax error near '++'
INTERRUPT2.C(78): error C244: 'count_time': can't initialize, bad type or class
INTERRUPT2.C(78): error C132: 'count_time': not in formal parameter list
INTERRUPT2.C(79): error C141: syntax error near '}'
看了一下,不知道怎么解决,需要大家帮帮看看哪里出了问题。
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
void T1init();
void T0init();
void display(short num);
uchar code leddata[]={
0xC0, //"0"
0xF9, //"1"
0xA4, //"2"
0xB0, //"3"
0x99, //"4"
0x92, //"5"
0x82, //"6"
0xF8, //"7"
0x80, //"8"
0x90 //"9"
};
uchar temp;
short count, count2, count_time;
int main(void)
{
T1init();
T0init();
temp = 0xfe;
P1 = temp;
while(1)
{
if(TF1 == 1)
{
TF1 = 0;
TH1 = 0x4b;
TL1 = 0xfd;
count++;
}
if(count == 10)
{
count = 0;
temp = _cror_(temp , 1);
P1 = temp;
}
display(count_time);
}
return 0;
}
void T1init()
{
TMOD = 0x11;
TH1 = 0x4b;
TL1 = 0xfd;
TR1 = 1;
}
void T0init()
{
TMOD = 0x11;
TH0 = 0x4b;
TL0 = 0xfd;
TR0 = 1;
ET0= 1;
EA = 1;
}
void timer0(void)interrput 1
{
TH0 = 0x4b;
TL0 = 0xfd;
count2++;
if(count2 == 20)
{
count2 = 0;
count_time++;
}
if(count_time == 60)
{
count_time = 0;
}
}
void display(short num)
{
short shi, ge;
shi = num / 10;
ge = num % 10;
P2 = 0xfe;
P0 = leddata[shi];
P0 = 0xff;
P2 = 0xfd;
P0 = leddata[ge];
P0 = 0xff;
}
编译程序时出现问题:
INTERRUPT2.C(66): error C132: 'interrput': not in formal parameter list
INTERRUPT2.C(66): error C141: syntax error near '1'
INTERRUPT2.C(68): error C244: 'TH0': can't initialize, bad type or class
INTERRUPT2.C(68): error C132: 'TH0': not in formal parameter list
INTERRUPT2.C(69): error C244: 'TL0': can't initialize, bad type or class
INTERRUPT2.C(69): error C132: 'TL0': not in formal parameter list
INTERRUPT2.C(70): error C132: 'count2': not in formal parameter list
INTERRUPT2.C(70): error C141: syntax error near '++'
INTERRUPT2.C(73): error C244: 'count2': can't initialize, bad type or class
INTERRUPT2.C(73): error C132: 'count2': not in formal parameter list
INTERRUPT2.C(74): error C132: 'count_time': not in formal parameter list
INTERRUPT2.C(74): error C141: syntax error near '++'
INTERRUPT2.C(78): error C244: 'count_time': can't initialize, bad type or class
INTERRUPT2.C(78): error C132: 'count_time': not in formal parameter list
INTERRUPT2.C(79): error C141: syntax error near '}'
看了一下,不知道怎么解决,需要大家帮帮看看哪里出了问题。
关键字拼写错误:void timer0(void) interrput 1
正确:void timer0(void) interrupt 1
谢谢,完美的解决了我的问题