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 CCS Technical Support

1s int TIMER1 external 32.768 Real-Time Clock finally work

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



Joined: 14 Feb 2007
Posts: 46
Location: Greece & Cyprus

View user's profile Send private message

1s int TIMER1 external 32.768 Real-Time Clock finally work
PostPosted: Wed Apr 16, 2008 9:57 am     Reply with quote

HI
I'm posting this code as help for people who need some help starting with Real-Time Clock Counts Seconds, Minutes using interrupt TIMER1 with External asynchronous crystal 32.768Khz and 33pF caps on Pins between pins T1OSI (input) and T1OSO (amplifier output). (Look at data sheets).

I had had an issue of my self with this code. I couldn't have exactly 1sec interrupt. I was always 1-2 sec forward with my (casio) watch. Searching the forum didn't find exactly where was the issue, until i look at the data sheets picture and found that the oscillator circuit, shown in Figure 12-3, should be located as close as possible to the microcontroller. Grounded with Vss <<LOOK AT THE PICTURE>> so i give a try and noticed that there was "NO MORE ERROR". Cool
This code use internal 8MHZ crystal with 32.768 kHz crystal for RTC, flexy_lcd.c driver for 2x16 LCD can be found in the forum.




Code:

#include <18F2550.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 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                     //NO Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //NO 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                 //NO USB clock source comes from primary oscillator
#FUSES NOVREGEN                 //NO USB voltage regulator disabled


#use delay(clock=8000000)
#include <flexy_lcd.c>

int sec=0;
int min=0;
int hour=0;
int1 flg=false;
long  t=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); //<-- internal                             //8MHZ crystal with PLL ON 8X4=32MHZ
 
 for(;;){
 
 if(flg==true){
 
   set_timer1(32768);                //2^16=  65536/2 = 32768 * 1/32.768Hz <--- (1sec)                                           
   setup_timer_1(T1_EXTERNAL|T1_CLK_OUT);
   
       if (++sec>59){
                       sec=0;
         if(++min>59){
                       min=0;
                       hour++;
        if(++hour>23){
                             
                     hour=0;
                             }
                     }
         }
                     
                     
    lcd_gotoxy(1,1);
 printf(lcd_putc"%02u:%02u:%02u  %6Lu"hour,min,sec,t);
 flg=false;
 }
   
         }
           }
                   
#int_TIMER1
void  TIMER1_isr(void)
 {

   flg=true;

    }


_________________
---- GREECE ----
Douglas Kennedy



Joined: 07 Sep 2003
Posts: 755
Location: Florida

View user's profile Send private message AIM Address

PostPosted: Thu Apr 17, 2008 8:12 am     Reply with quote

An I missing something? The internal osc is 8Mhz and the pll takes it up to 32Mhz but your use delay is 8Mhz. Now I know there aren't any situations in your code where the use delay would be critical but it may be an accident just waiting to happen.
E_KARVELAs



Joined: 14 Feb 2007
Posts: 46
Location: Greece & Cyprus

View user's profile Send private message

PostPosted: Thu Apr 17, 2008 8:28 am     Reply with quote

Douglas Kennedy wrote:
An I missing something? The internal osc is 8Mhz and the pll takes it up to 32Mhz but your use delay is 8Mhz. Now I know there aren't any situations in your code where the use delay would be critical but it may be an accident just waiting to happen.


Nop u are Not Very Happy Xexexex I now Smile I should put delay(32000000) but did'nt work and i dont now why.
So i change it back to 8Mhz delay and the code again work .
Very strange Idea but works Wink
_________________
---- GREECE ----
E_KARVELAs



Joined: 14 Feb 2007
Posts: 46
Location: Greece & Cyprus

View user's profile Send private message

Thanks goes to Neutone.
PostPosted: Thu Apr 17, 2008 8:51 am     Reply with quote

Code Now improved for more performance and accuracy
This will improve performance and accuracy. There will be no error introduced by setting the timer to 32768 after the timer has incremented to 3 or 4.
Thanks to Neutone Smile for the modification.


Code:

#include <18F2550.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 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                     //NO Internal External Switch Over mode enabled
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOPBADEN                 //NO 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                 //NO USB clock source comes from primary oscillator
#FUSES NOVREGEN                 //NO USB voltage regulator disabled


#use delay(clock=8000000)
#include <flexy_lcd.c>

#Byte TMR1H = 0xFCF   // TIMER1 HIGH BYTE LOOK DATASHEET
#Byte T1CON = 0xFCD  //TIMER1 CONFIG REGISTER LOOK DATASHEET

int sec=0;
int min=0;
int hour=0;
int1 flg=false;


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);
                                                  //<-- internal 8MHZ crystal with PLL ON 8X4=32MHZ                     
 
 for(;;){
 
 if(flg==true){                     
   setup_timer_1(T1_EXTERNAL|T1_CLK_OUT);       //    external TIMER1 with
                                               //    32.768Khz ,clock out for oscillator,asynchronus mode
   
       if (++sec>59){
                       sec=0;
         if(++min>59){
                       min=0;
                       hour++;
        if(++hour>23){
                             
                     hour=0;
                             }
                     }
         }
                     
                     
    lcd_gotoxy(1,1);
 printf(lcd_putc"%02u:%02u:%02u  "hour,min,sec);
 flg=false;
 }
   
         }
           }
                   
#int_TIMER1
void TIMER1_isr()    // 2^16=  65536/2 = 32768 * 1/32.768Hz <--- (1sec)                     
{

   bit_clear(T1CON,7);  //Enable access to the individual bytes of the timer register
   Bit_Set(TMR1H,7);   //Add 32768 to timer1 by setting high bit or timer register       
   Bit_Set(T1CON,7);   //Disable access to the individual bytes of the timer register
   
   flg=true;
}


_________________
---- GREECE ----
Ttelmah
Guest







PostPosted: Thu Apr 17, 2008 8:59 am     Reply with quote

Not on this chip.....
The PLL, is not available on the internal oscillator on this chip. On some other chips, there is an internal software switchable PLL, which can be enabled (the 2480 for example), but not on the 2550. There shouldn't actually be a OSC_PLL_ON setting for this chip at all (fault in the CCS file), since the only PLL present, is the one enabled by the fuses, feeding the USB clock.
If you look at the clock diagram for the chip, you will see that there is no PLL after the internal oscillator connects to control multiplexer.
8MHz is the maximum internal clock available on this chip.

Best Wishes
E_KARVELAs



Joined: 14 Feb 2007
Posts: 46
Location: Greece & Cyprus

View user's profile Send private message

PostPosted: Thu Apr 17, 2008 9:30 am     Reply with quote

Ttelmah wrote:
Not on this chip.....
The PLL, is not available on the internal oscillator on this chip. On some other chips, there is an internal software switchable PLL, which can be enabled (the 2480 for example), but not on the 2550. There shouldn't actually be a OSC_PLL_ON setting for this chip at all (fault in the CCS file), since the only PLL present, is the one enabled by the fuses, feeding the USB clock.
If you look at the clock diagram for the chip, you will see that there is no PLL after the internal oscillator connects to control multiplexer.
8MHz is the maximum internal clock available on this chip.

Best Wishes


Damn Right , Nice!!! I just have a look at the datasheets and notice there's PLL for external oscillator only NOT FOR INTERNAL .thanks


_________________
---- GREECE ----
ericne
Guest







PostPosted: Sat Apr 25, 2009 7:08 pm     Reply with quote

Let me say first of all thank you E_KARVELA for your work on this.

The question I have is over the TMR1H and T1CON values which I see that are being set to 0xFCF and 0xFCD respectively. What is the significance of these numbers? I looked on the datasheet for the 2550, but couldn't figure out why you would want a 12-bit number there, or how I would modify that value for a different device.
E_KARVELAs



Joined: 14 Feb 2007
Posts: 46
Location: Greece & Cyprus

View user's profile Send private message

PostPosted: Tue Apr 28, 2009 3:37 am     Reply with quote

ericne wrote:
Let me say first of all thank you E_KARVELA for your work on this.

The question I have is over the TMR1H and T1CON values which I see that are being set to 0xFCF and 0xFCD respectively. What is the significance of these numbers? I looked on the datasheet for the 2550, but couldn't figure out why you would want a 12-bit number there, or how I would modify that value for a different device.



Hi
Before start using TMR1H & T1CON and every SFR(Special Function Register) you have to Define the address first for the ccsc compiler. If you look at the datasheets of 2550 (address SFR ) you will see that the TMR1H address is 0xFCF and T1CON address is 0xFCD. Thats all.
Take care.

Code:

#Byte TMR1H = 0xFCF   // TIMER1 HIGH BYTE LOOK DATASHEET
#Byte T1CON = 0xFCD  //TIMER1 CONFIG REGISTER LOOK DATASHEET

_________________
---- GREECE ----
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