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

setup_timer_2(??,??,??) with Neoton zero drift RTC

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



Joined: 10 Feb 2004
Posts: 12
Location: Denmark, (Scandinavia-Europe)

View user's profile Send private message ICQ Number

setup_timer_2(??,??,??) with Neoton zero drift RTC
PostPosted: Mon Feb 23, 2004 3:29 pm     Reply with quote

Hi Guys,

I'm using a slightly minimized version of Neotone's old code, cutting out month, years and leapyears.

Code:

//RTC variables
int32 const xtl = 19660800;
int32 clock = xtl;
int8 days=0,hours=0,minutes=0,seconds=0;
int1 leapyear=0,tick=0;

// 0.83mS resolution with my xtl value
// -=Process Zero Drift Real Time Clock Information=-
// Decrement cycles per interupt
// Increment seconds counter, Set 1 second flag, Add number of ticks per second to clock
// Increment minutes counter
// Increment hours counter
// Increment days counter

#int_TIMER2
void TIMER2_isr() { clock -= 16384;
if(clock < 16384) {seconds++;clock += xtl;Tick= 1;
if(seconds == 60) {minutes++;seconds = 0;
if(minutes == 60) {hours++;minutes = 0;
if(hours == 24) {days++;hours= 0;
}}}}}


Problem:
As I am a total newbie with Timers & Interrupt I just recently discovered that the interrupt needs to be setup and enabled. I dont understand the math of it ?!?!?

I'm using:
20Mhz osc. quartz
Pic16F877
PCM 3.173

...what do I do to make the routine work as intended, with my crystal ?

I tried with:
setup_timer_2(T2_DIV_BY_16,0xff,1);
but that makes it count a little too fast: some 30sec in 20 min, hmm.
Keep in mind that to me the 3 variables are just numbers and I guessed at random to test it....not the correct way I know.

Code:





main() {

setup_timer_2(??,??,??);         // what do I put instead of the ??'s to make Neotrons code behave as intended, a RTC that puts secs, mins, hours....
enable_interrupts(int_timer2);
enable_interrupts(global);

while(1) {
}
}



side question:
what does the dear mr. Neotone mean by the following:

1) // 0.83mS resolution with my xtl value
2) int32 const xtl = 19660800; => do I need to change this to 20000000 ?

Thanks for you assistance guys, this is making my head hurt Sad

BR
Rasmus 'Izolator'


Last edited by Izolator on Mon Feb 23, 2004 4:45 pm; edited 2 times in total
Neutone



Joined: 08 Sep 2003
Posts: 839
Location: Houston

View user's profile Send private message

PostPosted: Mon Feb 23, 2004 4:40 pm     Reply with quote

I think this has better comments. 19660800 is a frequency that can be divided to get the most common baud rates. It's the frequency of the crystle I use.

Code:

/*********************************************
*    Initialize RTC                          *
*********************************************/
void Initialize_RTC(void)
{  Ticker = XTAL_FREQUENCY;
   setup_timer_2 ( T2_DIV_BY_4, 0xff, 4 );   // initialize 8 bit counter/timer 2 to interupt every 16384 clock cycles
   enable_interrupts( INT_TIMER2 );          // Start RTC
}

// Global Real Time Clock Information
#int_TIMER2                                  // Clock interrupt adjusted to occurs ~ 1ms
void TIMER2_isr()                            //  -=Process Zero Drift Real Time Clock Information=-
{  tickmSec = 1;                             // set each MS very approximately
   Ticker -= 16384;                          // Decrement ticker by clocks per interupt
   if ( Ticker < 16384 )                     // If second has expired
   {  Ticker += XTAL_FREQUENCY;              // Increment ticker by clocks per second
      second++;                              // Increment number of seconds
      tickSec = 1;                           // set each Second very accuratly
   }
}


Last edited by Neutone on Mon Feb 23, 2004 4:43 pm; edited 1 time in total
Izolator



Joined: 10 Feb 2004
Posts: 12
Location: Denmark, (Scandinavia-Europe)

View user's profile Send private message ICQ Number

PostPosted: Mon Feb 23, 2004 4:43 pm     Reply with quote

Thankyou good Sir,

It seems to work right out of the box.

Bless you, I might still pass my exam Cool

BR
Rasmus 'Izolator'
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