View previous topic :: View next topic |
Author |
Message |
luizsergiorf
Joined: 03 Apr 2019 Posts: 2
|
Set timer1 offset value |
Posted: Wed Apr 03, 2019 10:53 am |
|
|
Hello,
I'm having trouble setting the values of TMR1L and TMR1H in pic 16F648A.
Here is my code:
Code: |
#include <programa.h>
#include "flex_lcd.c"
//DEFININDO OS BOTOES
#define BTN_ENTER PIN_B6
#define BTN_MAIS PIN_B5
#define BTN_MENOS PIN_B4
#int_TIMER1
void TIMER1_isr(void)
{
output_toggle(PIN_B3);
}
void main()
{
// t= 1*2*(65536-15536)
setup_timer_1 (T1_INTERNAL|T1_DIV_BY_2); //131 ms overflow
set_timer1(0x3CB0);
enable_interrupts (int_TIMER1);
enable_interrupts (GLOBAL);
lcd_init (); // Always call this first.
lcd_putc ("\fHello World\n");
while (TRUE)
{
...
}
}
|
Changing the code to set the value of TMR1L and TMR1H in the interrupt, it works. But, I'd like to set the offset before the interruption
Code: |
#int_TIMER1
void TIMER1_isr(void)
{
set_timer1(get_timer1() + 0x3CB0);
output_toggle(PIN_B3);
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 03, 2019 3:07 pm |
|
|
I'm not sure what you mean. Are you thinking there is a register that
allows you to set the reload value for Timer1 ? There isn't one.
You always have to manually reload it, if you want to start at a higher
value than 0x0000. |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19369
|
|
Posted: Thu Apr 04, 2019 1:33 am |
|
|
On the basic PIC's (16/18), the only timer that supports a programmable
reset value, is Timer2. Al the others count from zero, to 255, or 65535.
The standard reload code in the interrupt, would be something like:
Code: |
#int_TIMER1
void TIMER1_isr(void)
{
set_timer1(get_timer1()+0x3CB0);
output_toggle(PIN_B3);
}
|
Your need to do it as an addition, otherwise any counts between the
interrupt 'event', and where you do the reset, will be lost. You will lose
a little time anyway (the addition takes time, and the prescaler gets
reset when the timer is loaded). However very small. |
|
![](templates/subSilver/images/spacer.gif) |
luizsergiorf
Joined: 03 Apr 2019 Posts: 2
|
|
Posted: Thu Apr 04, 2019 2:20 pm |
|
|
Hello,
Now I understand, Thank you all for help. |
|
![](templates/subSilver/images/spacer.gif) |
|