This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Timer Flag 0 is raised when timer starts

Hi!

I am trying to run two servos with one timer, checking pin state and set servo angle on a nRF24LE1.

I'm not a skilled programmer, but this is what I've got:

#include <Nordic/reg24le1.h>
#include <intrins.h>
#include <stdint.h>

void timer0_init();
void servo_pulse();
void read_pin1();
void read_pin2();

void main()
{
        P0DIR = 0x00;           // P0 = Servo signal outputs
        P1DIR = 0x00;           // P1 = LEDS

        P0 = 0x00;              // Turn of LEDs
        P11 = TF0;

        timer0_init();          // initialize timer 0

        IEN0 = 0x82;            // Enable all interrupts and enable Timer0 overflow interrupt

        while (1)
        {
                read_pin1();
                read_pin2();
//              P10 = TF0;
        }
}

void timer0_init()
{
        TMOD = 0x02;            // Timer0 mode 2, auto-reload 8-bit timer

        /* Kalkulerer initielle verdier med 65536 (eller 255) - (ønsket delay)/(12/Fcpu) */
        /* Timer oppdaterer hver 0.75uS ved 16MHz klokkefrekvens */

        TH0 = 0xBF;             // initial value cnt0, 50uS to count to 0xFF,  255-64 = 0xBF
}

void read_pin1()
{
        /* verdi som hentes fra pinne til korresponderende delay på servo */
        P00 = 1;
        servo_pulse();  // holds P00 = 1 until servo_pulse is finished...
        P00 = 0;
}

void read_pin2()
{
//      somethingsomething;
}

void servo_pulse()
{

        uint8_t TIMER = 0;
        uint8_t time = 25;                // 50µs * 25 = 1.25ms
        TR0 = 1;                          // Run timer while time counts up
        while(TIMER < time)
        {
                while(TF0 == 0);
                TIMER ++;
        }
        TR0 = 0;



}

//void ISR_Timer0_Overflow()    interrupt 1
//{
//      P0 ^= (1 << 0);
//}

My problem is that in the servo_pulse function, the TF0 seem to be constantly 1 after starting the timer (TR0 = 1), and my program gets stuck in this loop.
Initial value loaded in TH0 is to let the counter count 50µs (16MHz clock) before raising timer overflow flag.

Tried to toggle with the interrupt and it worked perfectly, toggled each 50µs.

Any suggestions on the matter?

Thank you very much in advance!

Parents
  • IEN0 = 0x82;            // Enable all interrupts and enable Timer0 overflow interrupt
    ...
    ...
    ...
    //void ISR_Timer0_Overflow()    interrupt 1
    //{
    //      P0 ^= (1 << 0);
    //}
    

    you enable the interrupt and do not have an ISR

    fortunately I can read comments in Norwegian :)

    Erik

Reply
  • IEN0 = 0x82;            // Enable all interrupts and enable Timer0 overflow interrupt
    ...
    ...
    ...
    //void ISR_Timer0_Overflow()    interrupt 1
    //{
    //      P0 ^= (1 << 0);
    //}
    

    you enable the interrupt and do not have an ISR

    fortunately I can read comments in Norwegian :)

    Erik

Children