View previous topic :: View next topic |
Author |
Message |
E_KARVELAs
Joined: 14 Feb 2007 Posts: 46 Location: Greece & Cyprus
|
Need to get exactly 10ms int with internal TIMER1 18f4523 |
Posted: Sat Apr 12, 2008 6:13 pm |
|
|
Hi
I need to get exactly 10ms int with internal TIMER1 on PIC18F4523 but some how i lose 1 sec every minute.
Code: |
#include <18F4523.h>
#device ICD=TRUE
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV21 //Brownout reset at 2.1V
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#use delay(clock=4000000)
#include <flexy_lcd.c>
int dec_sec=0
int sec_time_count=0;
void main()
{
setup_oscillator(OSC_4MHZ|OSC_INTRC|OSC_IDLE_MODE|OSC_PLL_ON);
lcd_init();
set_timer1(55536); //10000 x 1ms=10ms overflow
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1); //internal 31khz crystal
enable_interrupts(INT_TIMER1);
enable_interrupts(GLOBAL);
for(;;){
lcd_gotoxy(1,1);
printf(lcd_putc,"set_time %5d.%02u \n ",sec_time_count,dec_sec) ;
} // for(;;) void
} // void main
#INT_TIMER1
void micro()
{
set_timer1(get_timer1()+55536);
if(++dec_sec>99){ // 100x10ms=1sec
dec_sec=0;
++sec_time_count;
}
}
|
I have compare the Timer1 int with My casio watch it seems to be 1 sec back for every min. is there any way i can fix the issue? _________________ ---- GREECE ---- |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Sat Apr 12, 2008 7:02 pm |
|
|
Check table 26-8 in the datasheet. Typical accuracy of the internal RC oscillator is 1%, but can be as bad as 2%.
2% of 1minute = 1.2 seconds.
If you want to improve on this you will have to use an external (crystal based) oscillator. |
|
|
E_KARVELAs
Joined: 14 Feb 2007 Posts: 46 Location: Greece & Cyprus
|
|
Posted: Sat Apr 12, 2008 7:10 pm |
|
|
ckielstra wrote: | Check table 26-8 in the datasheet. Typical accuracy of the internal RC oscillator is 1%, but can be as bad as 2%.
2% of 1minute = 1.2 seconds.
If you want to improve on this you will have to use an external (crystal based) oscillator. |
NIce.U got right...than i should change as soon as posible to external
32.768KHz crystal. _________________ ---- GREECE ----
Last edited by E_KARVELAs on Tue Apr 15, 2008 12:10 pm; edited 1 time in total |
|
|
E_KARVELAs
Joined: 14 Feb 2007 Posts: 46 Location: Greece & Cyprus
|
Can't have 1 sec external TIMER1 int with 18f2550 |
Posted: Tue Apr 15, 2008 12:07 pm |
|
|
Hi
I have changed the hardware im using external (32.768KHz crystal with 33pF )to produce
1 sec interrupt but im going forward 1 sec every min. very strange i done everything but i don't understand.im using internal 8Mhz
primary clock with PLL for cpu clock .
Ive done everything ...
Any suggestion ??
Code: |
#include <18F2550.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES PUT //Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES NOIESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES MCLR //Master Clear pin enabled
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV1
#FUSES NOUSBDIV //USB clock source comes from primary oscillator
#FUSES NOVREGEN //USB voltage regulator disabled
#use delay(clock=8000000)
#include <flexy_lcd.c>
int sec=0, min=0,hour=0;
void main(){
enable_interrupts(INT_TIMER1);
setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
enable_interrupts(GLOBAL);
lcd_init();
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_ON);
for(;;){
lcd_gotoxy(1,1);
printf(lcd_putc"%02u:%02u:%02u"hour,min,sec);
}
}
#int_TIMER1
void TIMER1_isr(void)
{
set_timer1(32768); //2^16= 65536/2 = 32768 * 1/32.768Hz <--- (1sec)
if (++sec>59){
sec=0;
if(++min>59){
min=0;
hour++;
}
}
}
|
Code: |
Timer1 is 16bit so
2^16= (65536/2)*1/32768=1.000000sec exacly
|
_________________ ---- GREECE ---- |
|
|
E_KARVELAs
Joined: 14 Feb 2007 Posts: 46 Location: Greece & Cyprus
|
|
Posted: Tue Apr 15, 2008 1:35 pm |
|
|
Hi again
Searching the forum along time for this issue i found some good info on this issue:
http://www.ccsinfo.com/forum/viewtopic.php?t=29957&highlight=timer1
They suggest stop TIMER1 before reading it or writing it because it's working on asynchronus mode. Still not sure what to do
_________________ ---- GREECE ---- |
|
|
|