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

RTC ... Timers ... ???

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



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

RTC ... Timers ... ???
PostPosted: Thu Jul 09, 2009 12:12 pm     Reply with quote

Hi,

I'm not used to use timers, so I've searched and found: http://www.ccsinfo.com/forum/viewtopic.php?t=22467&highlight=timers

That I found very good... but I think that I didn't get the point about the connection between 'RTC and Timer0'. I know that they are linked but the code
Code:
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_256);

I didn't understand.

I also looked to 'EX_STWT.C' but I also didn't understand.

I'm using :

Compiler Version : 4.057
PIC18F452
5V power supply
4MHz Crystal

My goal is to have 'tick' every 2 seconds. This is my code :
Code:

#include <18F452.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C1, rcv=PIN_C0)

#define test PIN_B7

int32 count;
int flag;

#int_RTCC
void RTCC_isr(void) {
      count++;
      if(count>=100){
         count=0;
         flag=!flag;
   }
}



void main(){

   int i;

   setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_64);
   enable_interrupts(INT_RTCC);
   enable_interrupts(GLOBAL);

   count=0;

   while(1){
      if(flag) output_high(test);
      else output_low(test);
   }
}


With this code, I have a 'tick' near one second, but I can't explain why.
I would like to calculate, the 'exact' value the RTCC interrupt call.
Can anybody help me.

Thank you in advance.
Jacob
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Jul 09, 2009 12:31 pm     Reply with quote

RTCC is the same as timer0. If you look at the 18f452.h header file in your devices folder you will see:
Code:

////////////////////////////////////////////////////////////////// Timer 0
// Timer 0 (AKA RTCC)Functions: SETUP_COUNTERS() or SETUP_TIMER_0(),
//                              SET_TIMER0() or SET_RTCC(),
//                              GET_TIMER0() or GET_RTCC()
// Constants used for SETUP_TIMER_0() are:
#define RTCC_INTERNAL   0
#define RTCC_EXT_L_TO_H 32
#define RTCC_EXT_H_TO_L 48

#define RTCC_DIV_1      8
#define RTCC_DIV_2      0
#define RTCC_DIV_4      1
#define RTCC_DIV_8      2
#define RTCC_DIV_16     3
#define RTCC_DIV_32     4
#define RTCC_DIV_64     5
#define RTCC_DIV_128    6
#define RTCC_DIV_256    7

#define RTCC_OFF        0x80 

#define RTCC_8_BIT      0x40 


You see the 'Timer 0 (AKA RTCC)" in the first comments line? Looking at your program you should get a toggle on the test pin every 1.6 seconds right? You are using a 4Mhz crystal so your basic timer0 tick (the time it takes the timer to go up one count) is 1us. You are using it in 8 bit mode and a prescaler of 64. So timer0 overflows in 1us*2^8*64 = 0.0164 seconds. Everytime it overflows you enter the ISR and increase the overflow count by one. Once counts hits 100 the pin is toggled. So 0.0164*100 = 1.64 seconds.
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Thu Jul 09, 2009 2:39 pm     Reply with quote

Thank you for your fast reply.

The 'tick' is really 1.6s Smile

Now I understand. I got confused with the prescale and most strange I got confused with 'RTC' (Real Time Clock') and the RTCC !!!!

Your explanation was very easy to understand.

Thank you very much.
Jacob
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Thu Jul 09, 2009 3:11 pm     Reply with quote

Just one more question ...
How do i put the RTCC in 16 bits mode ?

If instead of :
Code:
setup_timer_0(RTCC_INTERNAL|RTCC_8_BIT|RTCC_DIV_64);


i write :
Code:
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);


the compiler assumes 16 bits ?

If so, if i write :
Code:
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_16);


then i would 'tick' : 1us*2^16*16 = 1.048576 seconds
Then for 2 seconds, it would be enough in my ISR count until 2 ...

But my question is about how i put 16 bits in RTCC.

Thank you for your help.
Jacob
mkuang



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

PostPosted: Thu Jul 09, 2009 3:13 pm     Reply with quote

Once again the header file is where you should look. Further down you will see:

Code:

////////////////////////////////////////////////////////////////// INT
// Interrupt Functions: ENABLE_INTERRUPTS(), DISABLE_INTERRUPTS(),
//                      CLEAR_INTERRUPT(), INTERRUPT_ACTIVE(),
//                      EXT_INT_EDGE()
//
// Constants used in EXT_INT_EDGE() are:
#define L_TO_H              0x40
#define H_TO_L                 0
// Constants used in ENABLE/DISABLE_INTERRUPTS() are:
#define GLOBAL                    0xF2C0
#define INT_RTCC                  0xF220
#define INT_TIMER0                0xF220


Notice that INT_RTCC and INT_TIMER0 have the same hex value, so they are interchangeable. Also note that only TIMER2 on most of these PIC18 devices have a pre and post scaler, others only have a prescaler.

You already have the answer. If you write:
Code:

setup_timer_0(RTCC_INTERNAL|RTCC_DIV_64);


you get the default which is 16 bit mode.
jjacob



Joined: 08 Mar 2008
Posts: 54
Location: PORTUGAL (PORTO)

View user's profile Send private message

PostPosted: Thu Jul 09, 2009 3:32 pm     Reply with quote

Thank you for your VERY FAST answer Very Happy

If they have the same address, of course, they are the same...
I really, must look with more carefull to the header files.

Now I have my problem solved Very Happy Very Happy Very Happy

I'll use the 16 bits modes.

Thank you once again.
Jacob
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