CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Timer0 does not work with Internal Oscillator

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cyberant



Joined: 04 Jun 2007
Posts: 27

View user's profile Send private message

Timer0 does not work with Internal Oscillator
PostPosted: Fri Aug 10, 2007 8:32 am     Reply with quote

My Timer0 worked with an external osci. - then I changed to an internal one and my Time Variable "tenthOfSeconds" is always 0
Can someone help me?

Code:


#include <18F4525.h>
#device adc=10

#fuses INTRC_IO
#FUSES H4                       //4xPLL --> 8MHz*4 = 32MHz

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES PUT                      //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                 //Stack full/underflow will not 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 NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOMCLR                   //Master Clear pin used for I/O


#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)//RS232 Initialisieren


#include "meinbootloader.h" //Bootloader (works)


unsigned long int_count=0;
unsigned long tenthOfSeconds=0;

//(timer0)
#int_rtcc                       
void clock_isr() {               
    if(--int_count==0)         
    {
      ++tenthOfSeconds;// = 1/10 seconds
      int_count=12;     //(32000000/(4*256*256))/10 = 12
    }
}





void main()
{


   //TIMER:
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); // Timer0
   setup_timer_1(T1_DISABLED);              // Timer 1
   setup_timer_2(T2_DISABLED,0,1);          // Timer 2

   //INTERRUPTS
   enable_interrupts(GLOBAL);

   setup_oscillator(OSC_32MHZ|OSC_INTRC|OSC_PLL_ON);//32MHz   





   enable_interrupts(RTCC_INTERNAL);
   set_timer0(0);
           
   while (true)
   {
      printf("%Lu",tenthOfSeconds);
   }
   
   disable_interrupts(RTCC_INTERNAL);
}

Ttelmah
Guest







PostPosted: Fri Aug 10, 2007 8:52 am     Reply with quote

Get rid of the H4 fuse. This is incorrect when using the internal oscillator. Change the #use delay statement, to 32000000.
With the internal oscillator, the PLL is accessed differently (it has to be turned on in the code, not through the fuses). Selecting 32MHz in the use delay, will automatically tell the compiler to do this. At present, the code is only running at 8MHz. What is happening, is that because the PLL bit is 'on' in the fuses, it no longer works with the internal oscillator, so the chip is ignoring the PLLEN bit in the oscillator setup, and is running at the 8MHz used in your delay statement, which allows the serial to work. The 'delay' statement, must _always_ match the actual speed the chip is being clocked at.

Then set int_count to 1 or greater in the initialisation, or move the decrement to after the variable in the ISR.
Because it is '0', and the first thing the interrupt handler does, is decrement it, it'll 'wrap round', and go to 65535. With the oscillator running at 1/4 the expected rate, it'll take 26 thousand seconds to ever reach zero, and start counting (about 7 days)...

Best Wishes
cyberant



Joined: 04 Jun 2007
Posts: 27

View user's profile Send private message

PostPosted: Mon Aug 13, 2007 12:47 am     Reply with quote

OK, I try'ed your suggested changes - but now it displays crap via RS232
My new Code:

Code:

#include <18F4525.h>
#device adc=10

#fuses INTRC_IO
//#FUSES H4                       //4xPLL --> 8MHz*4 = 32MHz

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES PUT                      //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                 //Stack full/underflow will not 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 NOEBTR                   //Memory not protected from table reads
#FUSES NOCPB                    //No Boot Block code protection
#FUSES NOEBTRB                  //Boot block not protected from table reads
#FUSES NOWRTC                   //configuration not registers write protected
#FUSES NOWRTB                   //Boot block not write protected
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES NOMCLR                   //Master Clear pin used for I/O


#use delay(clock=32000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)//RS232 Initialisieren


#include "meinbootloader.h" //Bootloader (works)


unsigned int int_count=1;
unsigned long tenthOfSeconds=1;

//(timer0)
#int_rtcc                       
void clock_isr() {               
    if(--int_count==0)         
    {
      ++tenthOfSeconds;// = 1/10 seconds
      int_count=12;     //(32000000/(4*256*256))/10 = 12
    }
}





void main()
{


   //TIMER:
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256); // Timer0
   setup_timer_1(T1_DISABLED);              // Timer 1
   setup_timer_2(T2_DISABLED,0,1);          // Timer 2

   //INTERRUPTS
   enable_interrupts(GLOBAL);

   setup_oscillator(OSC_32MHZ|OSC_INTRC|OSC_PLL_ON);//32MHz   





   enable_interrupts(RTCC_INTERNAL);
   set_timer0(0);
           
   while (true)
   {
      printf("%Lu",tenthOfSeconds);
   }
   
   disable_interrupts(RTCC_INTERNAL);
}


With " #use delay(clock=8000000) " it displays endless 0
why does this not count up?
and why do the 32MHz not work?
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Aug 13, 2007 1:32 am     Reply with quote

Quote:
With " #use delay(clock=8000000) " it displays endless 0
You initialize tenthOfSeconds to 1, so it is very strange a constant value of 0 is printed. What is your compiler version?

There is also an error in your timer0 setup. By default this timer for your chip is 16bits wide but in your timer calculations you assume an 8 bits width. Either add RTCC_8_BIT to the setup_timer0() call or remove the RTCC_DIV_256.
cyberant



Joined: 04 Jun 2007
Posts: 27

View user's profile Send private message

PostPosted: Mon Aug 13, 2007 2:06 am     Reply with quote

Oh I mean it endless displays 1.
My Compiler version is 4.032 - is there a bug?
But why does my timer not count up????? (Ive removed the RTCC_DIV_256 but nothing changed) Confused

EDIT:
OK Ive found out why my interrupt doesn't fire:
I have to use:
enable_interrupts(INT_RTCC);
instead of:
enable_interrupts(RTCC_INTERNAL);
then my counter counts up.

But I still have the problem that it only runs at 8MHz.
If I change " #use delay(clock=8000000) " to 16MHz or 32MHz only crap gets received...
How can I setup 16 or 32MHz? (normaly i only have to change the delay or?)
Compiler bug in 4.032 ?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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