|
|
View previous topic :: View next topic |
Author |
Message |
Guest
|
clock problem with pic18f2550 |
Posted: Sat Feb 14, 2009 10:16 am |
|
|
Hello everybody!
This is the fist time I am using C (ccs 4) for writing pic programs, before I always used asm. But I am having some problems with the frequency. Right now I just try to turn on/off a led with a PIC18F2550.
But the clock frequency is not working correctly.
First I used the internal clock at 8Mhz, but for a simple loop of 500 ms it needs 2 seconds!!! This code turns off and on the led again in 4 seconds (instead of 1 second).
Code: |
#int_TIMER0
void TIMER0_isr(void)
{
}
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(GLOBAL);
setup_oscillator(OSC_8MHZ|OSC_TIMER1|OSC_31250|OSC_PLL_OFF);
// TODO: USER CODE!!
while(TRUE)
{
output_toggle(PIN_A0);
delay_ms(500);
}
}
|
Code: |
#include <18F2550.h>
#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 NOPUT //No 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 IESO //Internal External Switch Over mode enabled
#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 NOMCLR //Master Clear pin used for I/O
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL12 //Divide By 12(48MHz oscillator input)
#FUSES CPUDIV4 //System Clock by 4
#FUSES USBDIV //USB clock source comes from PLL divide by 2
#FUSES VREGEN //USB voltage regulator enabled
#use delay(clock=8000000)
//#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8)
|
So, I decided to use a normal 20 Mhz Xtal.
In this time it is worse, instead of working 4 times slower, it is working 20 times slower. The led stays 10s on, and then 10s off... and this is the code of it:
Code: |
void main()
{
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_32);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_8);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
enable_interrupts(INT_TIMER0);
enable_interrupts(INT_TIMER1);
disable_interrupts(GLOBAL);
//Setup_Oscillator parameter not selected from Intr Oscillator Config tab
// TODO: USER CODE!!
while(TRUE)
{
output_toggle(PIN_A0);
delay_ms(500);
}
}
|
Code: |
#include <18F2550.h>
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV20 //Brownout reset at 2.0V
#FUSES NOPUT //No 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 IESO //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 NOMCLR //Master Clear pin used for I/O
#FUSES NOLPT1OSC //Timer1 configured for higher power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL1 //No PLL PreScaler
#FUSES CPUDIV1 //No System Clock Postscaler
#FUSES NOUSBDIV //USB clock source comes from primary oscillator
#FUSES VREGEN //USB voltage regulator enabled
#use delay(clock=2000000)
//#use rs232(baud=9600,parity=N,xmit=PIN_B6,rcv=PIN_B7,bits=8)
//#use i2c(Master,Slow,sda=PIN_B0,scl=PIN_B1,force_hw)
|
I would be very grateful if you can help me with this I am not sure on what I am doing wrong...
thanks a lot!!!
PD: I was using the wizard for the configuration |
|
|
dyeatman
Joined: 06 Sep 2003 Posts: 1934 Location: Norman, OK
|
|
Posted: Sat Feb 14, 2009 1:40 pm |
|
|
Your delay definition shows 2Mhz... |
|
|
Guest
|
|
Posted: Sun Feb 15, 2009 7:21 am |
|
|
Sorry, I confused, normally it is 20Mhz, I just changed it to 2Mhz, to see how it was affected. The conclusion was that delay_ms where 10 times faster, but the timer interruptions where as slow as before. So I do not know why the clock is going much slower (with the extarnal Xtal and the internal clock).
And I do not know why is this. :( Does anybody see anything wrong in the configuration #FUSES?
Thanks again |
|
|
Ttelmah Guest
|
|
Posted: Sun Feb 15, 2009 10:29 am |
|
|
You have not told us what you expect the timer to do.
You show no code that involves the timer at all...
However some comments:
You are enabling two interrupts, but only show a handler for one. This _will_ cause disaster. You _must_, have a handler present for any interrupt you enable.
However you then don't enable the global interrupt, so no interrupts are going to occur.
Timer0, on a system at 20MHz, with your 'setup', will call it's interrupt every:
20000000/(4*32*65536)th second
2.384 times per second.
At 8MHz, every 1.04 seconds.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|