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

[Resolved] Strange TMR1 32,768Khz RTC

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



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

[Resolved] Strange TMR1 32,768Khz RTC
PostPosted: Sun Mar 01, 2009 4:45 am     Reply with quote

Hello,
I've been facing strange timings when using TMR1 with external 32,768Khz XT (RC0, RC1).

PORTD,5 should have a frequency of 0.5Hz but I'm reading 2.918Hz.
Please note that i dont have capacitors on the 32,768Khz XT.

device: 18LF4620 running on 3V Li Battery
compiler: 4.086

Code:

#include <18F4620.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 NOIESO                   //Internal External Switch Over mode disabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#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 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 NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES NOPBADEN                 //PORTB pins are configured as digital I/O on RESET
#FUSES LPT1OSC                  //Timer1 configured for low-power operation
#FUSES MCLR                     //Master Clear pin enabled

#use delay(clock=8000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)
#use i2c(Master,Fast=400000,sda=PIN_D1,scl=PIN_D0, FORCE_SW)

#byte PORTA = 0xF80
#byte PORTB = 0xF81
#byte PORTC = 0xF82
#byte PORTD = 0xF83
#byte PORTE = 0xF84

#byte OSCTUNE=0xF9B
#byte OSCCON= 0xFD3
#bit  IDLEN = OSCCON.7

#Byte TMR1H = 0xFCF
#Byte T1CON = 0xFCD

#use fast_io (A)

///////////////////////////////////////////////////////////////////////////////
#int_TIMER1       //Real Time Clock
void TIMER1_isr()

   bit_clear(T1CON,7);
   Bit_Set(TMR1H,7);     
   Bit_Set(T1CON,7);     
   Sflg=true;
   #asm btg PORTD, 5 #endasm
}
///////////////////////////////////////////////////////////////////////////////
void delay_s(int8 s)
{
   while(s--)
   {
      Sflg = false;
      while(!Sflg);
   }
}
///////////////////////////////////////////////////////////////////////////////
void InitPorts (void)
{
   set_tris_a(0x04);
   PORTA = 0x21;
   set_tris_b(0x0F);
   PORTB = 0x00;
   port_b_pullups (TRUE);
   set_tris_c(0x93);
   PORTC = 0x00;
   set_tris_d(0x03);
   PORTD = 0x84;
   set_tris_e(0x00);
   PORTE = 0x00;
}
///////////////////////////////////////////////////////////////////////////////
void main()
{
   setup_oscillator(OSC_8MHZ|OSC_IDLE_MODE|OSC_31250|OSC_PLL_OFF);
   delay_cycles(4);
   
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_XMIT_L_TO_H|SPI_L_TO_H|SPI_CLK_DIV_4);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_EXTERNAL|T1_DIV_BY_1|T1_CLK_OUT);
   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);
   InitPorts();
   
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);

   while(1);
}


Any thoughts?
Thanks
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 6:25 am     Reply with quote

hmmm,

27 pF caps are a must Laughing
Ttelmah
Guest







PostPosted: Sun Mar 01, 2009 9:10 am     Reply with quote

First comment, simplify.
What you have posted, could easily be written in half the lines, loads of bloat that is not helping to see what is going on, but at the same time, is not complete, so can't be the actual code you are running (Sflg, is not defined anywhere...)...
Code should be simple, and complete.

Personally, I'd do something much simpler.
Just set the timer to zero, pause for 100mSec, and read the timer. If the value read is below 6400, set one bit, if it is above 6700, set another (I'm assuming your serial is not connected, otherwise just print the value read). If either bit comes on, you can tell that your timer clock _is_ off frequency, and start looking at this.

Best Wishes
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 9:16 am     Reply with quote

Thanks,

But I wrote the first comment Very Happy
And now it's working. I just had to add these two caps.
Ttelmah
Guest







PostPosted: Sun Mar 01, 2009 10:57 am     Reply with quote

I'm not surprised. The slim 32K crystals, though fundamental types, rather than overtone units, actually have two fundamentals. One corresponding the to width of the crystal, and the other the length. The ratio is typically about 5:1. The internal oscillator will try to lock onto the higher frequency...

Without the right loading, there is no point in ever using a crystal.

Your post didn't appear on my system, till after I posted. A common 'oddity'.

However I would suggest doing lots of things more efficiently. Why keep toggling the timer between 16bit and 8bit modes?. Just set it to 8bit mode once. Do this with something like:
Code:

#Byte TMR1H = 0xFCF
#Byte T1CON = 0xFCD
#bit T1_16bit = T1CON.7
#bit Msb =TMR1H.7
//Just set T1_16bit=false in the main code once

//The ISR then becomes:
#int_TIMER1       //Real Time Clock
void TIMER1_isr()
{
   Msb=true;
   Sflg=true;
   output_toggle(PIN_D5);
}

You can also get rid of most of the setup code shown. Large parts of this are the default settings.

Best Wishes
PICoHolic



Joined: 04 Jan 2005
Posts: 224

View user's profile Send private message

PostPosted: Sun Mar 01, 2009 11:58 am     Reply with quote

Thanks Smile
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