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!
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
Is that not allowed? I just used the interrupt for testing purposes, that's why I just comment it out so I can easily activate it again. And I see I forgot to translate that comment line :p
"Is that not allowed?"
Think about what will happen when the interrupt occurs...
(note that this is not specific to the 8051 - it applies to any processor with "vectored" interrupts)
You are damn right! Thank you!! No wonder my code won't execute very well. I thought if I didn't have the routine in the program it would be kind of disabled.