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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 1
    If I remember well you must set `T0IE = 1;` – LPs Sep 04 '15 at 12:13
  • I found [THIS](http://ww1.microchip.com/downloads/en/DeviceDoc/51702A.pdf) good tutorial – LPs Sep 04 '15 at 12:14
  • Making an led blink at the right time really *is* relevant. When building the code - that's your first point of reference. In this case, have the timer interrupt blink it. If that's too fast too see, use a counter mask or start with a slow timer. – Weather Vane Sep 04 '15 at 19:19

2 Answers2

3

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.

  • Unfortunatly i have tried that, and still nothing works, any other ideas? – Emil Wismann Kirkebæk-Jensen Sep 07 '15 at 08:38
  • It seems that your timer configurations are correct. You can try using simulators like picsimulatorIDE or Proteus to make sure that the configurations values are written properly in the appropriate register. Also make sure that your configuration bits are set properly. Forgetting to disable watchdog timer when it is not required used to trouble me a lot. – Sreeyesh Sreedharan Sep 07 '15 at 10:43
0

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

Community
  • 1
  • 1