1

I am currently working on programming a PIC16F883 with a 3.2768 MHz oscilator. I need to make some LED blink at the right time, but that is really not relevant here.

The problem is that have set up Timer0, but it isn't working. I am going to post my code and initialization here so you can see :). By the way I am programming in MpLap IDE, in normal C with the Hi-Tech C Compiler.

Initialization:

T0CS = 0x00;            //Set Timer0 to Timer-Mode
GIE = 0x01;             //Enable all interrupts
PSA = 0x00;             //Prescaler enable
PS0 = 0x01;             //Prescaler set
PS1 = 0x00;             //Prescaler set
PS2 = 0x01;             //Prescaler set

The interrupt service routine itself:

void interrupt timer()
{
    T0IF = 0x00;             //Reset timer
    millicounter++;          //Add one to the helper variable
    PORTA = 0x00;

    if (millicounter == 25)  //Check if one second has passed.
    {
        millicounter = 0;    //Reset helper variable
        seconds++;           //Add one to elapsed seconds.
    }
}

The problem is that it doesn't look like the timer is running. I have now simulated the program various times with different settings, the latest to make a pin open when the interrupt is being run, and then turned on again in the main. The problem was it never happend. The timer isn't running I think. Why?

4

2 回答 2

3

您已设置全局中断使能位。但要使定时器中断工作,您还需要设置定时器中断使能位(T0IE)。

根据您的定时器寄存器值和晶体频率,“秒”变量将每秒增加 256 次。即,如果您使用此“秒”变量来提供闪烁延迟,那么您的 LED 开启时间大约为 3.9 毫秒。人眼无法检测到这种快速眨眼。

于 2015-09-04T12:11:39.270 回答
0

谢谢你的帮助,我让计时器工作了。我删除了整个配置并重写了计时器,现在它工作正常。我确实有另一个问题,我已经写了一个新帖子:) 如果你愿意,请查看。

最新帖子

于 2015-09-08T12:32:16.743 回答