PIC16F883 timer won't work

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?

Upvotes: 1

Views: 256

Answers (2)

Sreeyesh Sreedharan
Sreeyesh Sreedharan

Reputation: 793

You have set the Global Interrupt Enable bit. But for the timer interrupt to work, you need to set the timer interrupt enable bit(T0IE) too.

As per your timer register values and crystal frequency, the "seconds" variable will be incremented 256 times per second. i.e, If you are using this "seconds" variable to provide blinking delay, your LED on time will be 3.9 milliseconds approximately. An human eyes cannot detect this fast blinking.

Upvotes: 3

thank you for your help, i got the timer working. I deleted my whole config and rewrited the timer, and now it's working fine. I do have another problem that i have written a new post for :) Check it out if you want.

New Post

Upvotes: 0

Related Questions