View previous topic :: View next topic |
Author |
Message |
jjacob
Joined: 08 Mar 2008 Posts: 54 Location: PORTUGAL (PORTO)
|
RTC ... Timers ... ??? |
Posted: Thu Jul 09, 2009 12:12 pm |
|
|
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
|
|
Posted: Thu Jul 09, 2009 12:31 pm |
|
|
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)
|
|
Posted: Thu Jul 09, 2009 2:39 pm |
|
|
Thank you for your fast reply.
The 'tick' is really 1.6s
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)
|
|
Posted: Thu Jul 09, 2009 3:11 pm |
|
|
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
|
|
Posted: Thu Jul 09, 2009 3:13 pm |
|
|
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)
|
|
Posted: Thu Jul 09, 2009 3:32 pm |
|
|
Thank you for your VERY FAST answer
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
I'll use the 16 bits modes.
Thank you once again.
Jacob |
|
|
|