View previous topic :: View next topic |
Author |
Message |
mcisabel
Joined: 28 Jul 2005 Posts: 3
|
Timer interrupt problem |
Posted: Wed Aug 10, 2005 9:15 am |
|
|
Hi, I`ve got a problem. I want to set the timer0 to interrupt every 833us to send a bit, but when I use the instruction set_timer0(61371) to make the timer0 interrupt every 833us it seem that the timer is not interrupting every 833us because the data that I recived (I`m sending data via serial to PC) is not the data that I should recive, I don`t understand why.
#int_rtcc
rtcc_isr()
{
set_timer0(61371);
clear_interrupt(int_rtcc);
if(numeroentradas<=9)
{
espera=1;
numeroentradas++;
}
}
void main()
{
setup_timer_0(RTCC_INTERNAL);
set_rtcc(61371);
enable_interrupts(INT_RB);
enable_interrupts(GLOBAL);
while(TRUE)
{
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,1);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,0);
while(espera==0);
espera=0;
output_bit(TX1,1);
while(espera==0);
espera=0;
output_bit(TX1,1);
while(espera==0);
espera=0;
numeroentradas=0;
}
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 10, 2005 9:27 am |
|
|
You didn't enable the Timer0 interrupt.
Code: | clear_interrupt(int_rtcc); |
This line is not required, the CCS compiler generates code to clear the interrupt.
This code looks like a software based serial port, do you know CCS has inbuilt functions for this which have been tested, have a lot of functionality and are easy to use?
Code: | #use RS232 (BAUD=1200, XMIT=TX1)
putc(0x02); |
|
|
|
mcisabel
Joined: 28 Jul 2005 Posts: 3
|
|
Posted: Wed Aug 10, 2005 9:37 am |
|
|
Hi, thanks for the answer but I try whit the timer0 interrupt enable and nothing, and yes I know about the rs232 features but I already using that for other serial port. I just like to know why the timer doesn`t do what I want. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Wed Aug 10, 2005 10:30 am |
|
|
Quote: | and yes I know about the rs232 features but I already using that for other serial port. |
Using the STREAM parameter you can create as many serial ports as you like, only using them simultaneously is difficult.
Try changing your code to:
Code: | #int_rtcc
rtcc_isr()
{
set_timer0(61371);
if(numeroentradas<=9)
{
espera=1;
numeroentradas=0;
}
numeroentradas++;
} |
Problem with a setup like this where the data generation is seperated from the timing interrupt is that your communication routine is blocking the main program, i.e. you can't execute other functions in between without the risk of generating data jitter. Better is to have the interrupt routine do both the timing and transmitting the data.
Many interrupt driven serial port drivers have been published in this forum and on the internet. I can recommend the book Serial Communications from Roger L. Stevens which has a good desciption of several serial protocols like RS232, SPI and I2C and a lot of code examples for the PIC (assembly code). |
|
|
Mark
Joined: 07 Sep 2003 Posts: 2838 Location: Atlanta, GA
|
|
Posted: Wed Aug 10, 2005 7:25 pm |
|
|
Is this a PIC18 or PIC16?
The CCP module might be better for what you want to do.
Also if you use a timer int and are setting the timer to a value, remember to account for the overhead of the interrupt in your timing. |
|
|
|