如果我在主程序写b=1234,数码管就能显示1234.所以我想应该没进中断,但都按照书上的步骤都设置了,不知道哪错了。用的是20M晶振 想定时1ms,所以定时器TMR0的初值写的是96(设置的是32分频)定时器每加1的时间((1/20000000)*4*32)=0.0000064=6.4us 若定时1ms,定时器增加的数=1024us/6.4us=160
TMR0 的初值为256-160=96
#include<pic.h>
__CONFIG(WDTDIS & LVPDIS & HS & PWRTDIS & BORDIS);
#define uchar unsigned char
#define uint unsigned int
uchar disp[] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90};
uint one,ten,hundred,thousand,b;
void delayms(uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<454;j++);
}
void display(uint data)
{
while(1)
{
PORTA=0X0C;
thousand=data/1000;
PORTD=disp[thousand];
delayms(1);
PORTA=0X10;
hundred=data%1000/100;
PORTD=disp[hundred];
delayms(1);
PORTA=0X14;
ten=data%100/10;
PORTD=disp[ten];
delayms(1);
PORTA=0X18;
one=data%10;
PORTD=disp[one];
delayms(1);
}
}
main()
{
TRISD=0;
TRISA=0XE3;
T0CS=0;
PSA=0;
PS2=1;
PS1=0;
PS0=0;
T0IF=0;
T0IE=1;
GIE=1;
TMR0=96;//此处再加一句b=1234,则数目管则显示1234
while(1)
{
display(b);
}
}
void interrupt TIMER0()
{
if(T0IE&&T0IF)
{
T0IF=0;
b=1234;
}
} |