View previous topic :: View next topic |
Author |
Message |
pavas
Joined: 11 Aug 2007 Posts: 4
|
Timer0 configuration problem in pic 18f4550 |
Posted: Sat Aug 11, 2007 9:46 pm |
|
|
I have problems to put my timer0 to count 1s, if I'm right this confiuration set my PIC to oscilate at 48MHz with a xtal of 20MHz. Then the cicle is
: (1/48000000)*4 ~= 8 uS to timer0 add by 1. But this code don't work, someone can tell me why???
Code: |
#include <18F4550.h>
#fuses HSPLL,NOWDT,NOPROTECT,NOLVP,NODEBUG,USBDIV,PLL5,CPUDIV1,VREGEN
#use delay(clock=48000000)
#include ".\include\usb_cdc.h"
long cont=0;
int segundos=0;
#int_timer0 //Trata a iterrupção gerada pelo TIMER 0
void trata_tmr0 () {
set_timer0(0);
cont++;
if(cont == 125000) { // ( 1s/0.000008s) = 125000
segundos++;
cont=0;
printf(usb_cdc_putc,"Seconds: %u",segundos);
printf(usb_cdc_putc,"\r\n ");
}
}
void main() {
delay_ms(300);
usb_cdc_init();
usb_init();
while(!usb_cdc_connected()) {}
setup_timer_0 (RTCC_DIV_1);
set_timer0(0);
enable_interrupts (INT_TIMER0);
while (true);
} |
Thx. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Aug 11, 2007 10:04 pm |
|
|
You also need to enable global interrupts. Example:
Code: |
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
while(1);
|
|
|
|
Ttelmah Guest
|
|
Posted: Sun Aug 12, 2007 3:09 am |
|
|
Actually, I don't think that is the problem. The interrupts should be enabled in the USB code (unless the poster has altered this to use polling, instead of interrupts).
There are several other potential problems though.
Does the terminal package being used send a 'set_line_coding' message?. If not, then the usb_cdc_connected call will never return 'true'.
The other problem though is probably that the processor has not got time to handle the USB. With the code set to use interrupts at this frequency, they will be set again only a little while after the interrupt handler exits, which could result in an effective processor 'deadlock' condition...
Best Wishes |
|
|
inservi
Joined: 13 May 2007 Posts: 128
|
|
Posted: Sun Aug 12, 2007 3:54 am |
|
|
Hello,
If i well understand, 'cont' is a long (16bits) then, it can never be equal to 125000, what do you think about ?
dro. _________________ in médio virtus |
|
|
pavas
Joined: 11 Aug 2007 Posts: 4
|
|
Posted: Sun Aug 12, 2007 6:46 am |
|
|
Quote: |
Actually, I don't think that is the problem. The interrupts should be enabled in the USB code (unless the poster has altered this to use polling, instead of interrupts).
There are several other potential problems though.
Does the terminal package being used send a 'set_line_coding' message?. If not, then the usb_cdc_connected call will never return 'true'.
|
The terminal and the PIC work right. I know because the string envoy to PC was ok.
Quote: |
The other problem though is probably that the processor has not got time to handle the USB. With the code set to use interrupts at this frequency, they will be set again only a little while after the interrupt handler exits, which could result in an effective processor 'deadlock' condition... |
Perhaps this is my problem, can you tell me what I have to do?? Use a minor frequency to clock setting a diferrent PLL scaler??? Or another sugestion???
Thx again..... |
|
|
pavas
Joined: 11 Aug 2007 Posts: 4
|
|
Posted: Mon Aug 13, 2007 6:18 am |
|
|
Hey someone can help me pls????
There´s some wrong with the compiler???? |
|
|
mskala
Joined: 06 Mar 2007 Posts: 100 Location: Massachusetts, USA
|
|
Posted: Mon Aug 13, 2007 6:45 am |
|
|
inservi wrote: | Hello,
If i well understand, 'cont' is a long (16bits) then, it can never be equal to 125000, what do you think about ?
dro. |
Please read above by inservi.
Type 'long' in CCS is same as int16. A 16-bit value cannot be higher than 65536, and you are comparing it to 125000. |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Mon Aug 13, 2007 7:07 am |
|
|
Quote: | Then the cicle is: (1/48000000)*4 ~= 8 uS to timer0 add by 1. | This is wrong, it takes about 0.08us for Timer0 to increase by 1.
The timer interrupt is generated every timer overflow, i.e. depending on the timer configuration this is every 256 or 65536 ticks (configure as 8 or 16 bit counter).
Interrupts do have a lot of overhead, so for best performance it is best to minimize the number of interrupts per second. Code: | setup_timer_0 (RTCC_DIV_1); | This will give you 48MHz / (4 * 1 * 65536) = 183 interrupts per second.
Change to RTCC_DIV_16 for about 11 interrupts per second.
In your interrupt handler you are resetting the timer to zero, this is not required. The interrupt fires on timer overflow. The timer starts counting from 0 automatically, manually resetting to zero causes an offset error.
Code: | int8 cont=0;
int8 segundos=0;
#int_timer0 //Trata a iterrupção gerada pelo TIMER 0
void trata_tmr0 () {
cont++;
if (cont >= 11) { // about 11 interrupts per second
segundos++;
cont=0;
printf(usb_cdc_putc,"Seconds: %u \r\n",segundos);
}
} |
I'm no USB expert so can't help you on the other aspects.
Please describe in more details what behavior you see and how this is different from what you expect.
What is the version number of the compiler you are using? |
|
|
pavas
Joined: 11 Aug 2007 Posts: 4
|
|
Posted: Mon Aug 13, 2007 10:28 am |
|
|
ckielstra THANK YOU VERY MUCH!!!!
My problem was so stupid!!!!
The PIC had the timer pre-configured to 16bits and I tought it was 8bits UAUHAUHAUHAUHHA
THX for help and srry about my stupid question.... |
|
|
|