Chris_WMS
Newbie level 4
- Joined
- Oct 2, 2020
- Messages
- 7
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1
- Activity points
- 67
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF);
int Count = 0;
void main ()
{
TRISD=0x00; //configurate PORTD as output
T1CON=0b00000000; //disable Timer 1
TMR1H=0x3C; //setting initial value as 15535
TMR1L=0xAF;
T1CON=0b00000001; //starting Timer 1
while (1)
{
while(TMR1IF=1)
{
TMR1IF = 0; //clear interrupt flag
Count ++; //increase count bit by 1
if (Count ==100)
{
Count = 0; //clear count bit
PORTD=~PORTD; //toggle PORT D
}
TMR1H=0x3C;
TMR1L=0xAF;
}
}
}
OPTION = 0b11010100; // configure Timer0:
//--0----- timer mode (T0CS = 0)
//----0--- prescaler assigned to Timer0 (PSA = 0)
//-----100 prescale = 32 (PS = 100)
// -> increment every 32 us
* *
* Architecture: Baseline PIC *
* Processor: 12F508/509 *
* Compiler: MPLAB XC8 v1.01 (Free mode) *
* *
*************************************************************************
* *
* Files required: none *
* *
*************************************************************************
* *
* Description: *
* *
* Demonstrates use of Timer0 to maintain timing of background actions *
* while performing other actions in response to changing inputs *
* *
* One LED simply flashes at 1 Hz (50% duty cycle). *
* The other LED is only lit when the pushbutton is pressed *
* *
* Uses symbols to represent port bit positions *
* *
*************************************************************************
* *
* Pin assignments: *
* GP1 = "button pressed" indicator LED *
* GP2 = flashing LED *
* GP3 = pushbutton switch (active low) *
* *
************************************************************************/
#include <xc.h>
#include <stdint.h>
/***** CONFIGURATION *****/
// int reset, no code protect, no watchdog, int RC clock
__CONFIG(MCLRE_OFF & CP_OFF & WDT_OFF & OSC_IntRC);
// Pin assignments
#define nFLASH 2 // flashing LED on GP2
#define nPRESS 1 // "button pressed" indicator LED on GP1
#define BUTTON GPIObits.GP3 // pushbutton
/***** GLOBAL VARIABLES *****/
uint8_t sGPIO; // shadow copy of GPIO
/***** MAIN PROGRAM *****/
void main()
{
uint8_t dc; // delay counter
//*** Initialisation
// configure port
GPIO = 0; // start with all LEDs off
sGPIO = 0; // update shadow
TRIS = ~(1<<nFLASH|1<<nPRESS); // configure LEDs (only) as outputs
// configure Timer0
OPTION = 0b11010100; // configure Timer0:
//--0----- timer mode (T0CS = 0)
//----0--- prescaler assigned to Timer0 (PSA = 0)
//-----100 prescale = 32 (PS = 100)
// -> increment every 32 us
//*** Main loop
for (;;)
{
// delay 500 ms while responding to button press
for (dc = 0; dc < 125; dc++) // repeat 125 times (125 x 4 ms = 500 ms)
{
TMR0 = 0; // clear timer0
while (TMR0 < 125) // repeat for 4 ms (125 x 32 us)
{ // check and respond to button press
sGPIO &= ~(1<<nPRESS); // assume button up -> indicator LED off
if (BUTTON == 0) // if button pressed (low)
sGPIO |= 1<<nPRESS; // turn on indicator LED
GPIO = sGPIO; // update port (copy shadow to GPIO)
}
}
// toggle flashing LED
sGPIO ^= 1<<nFLASH; // toggle flashing LED, using shadow register
}
}
It should be 'while(TMR1IF == 1)'while(TMR1IF=1)
T1CON=0b00000000; //disable Timer 1(bit 0),Prescaler (bit4-5)
You do not set the prescaler or postscaler in your code. I have an example for a lower processor, but you should check your OPTION register and the datasheet for PIC16F877A in order to see the necessary bits:
It should be 'while(TMR1IF == 1)'
or better still 'if(TMR1IF)'
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF);
int Count = 0;
void main ()
{
TRISD=0x00; //configurate PORTD as output
T1CON=0b00000000; //disable Timer 1(bit 0),Prescaler (bit4-5)
TMR1H=0x3C; //setting initial value as 15535
TMR1L=0xAF;
T1CON=0b00000001; //starting Timer 1
while (1)
{
while(TMR1IF==1)
{
TMR1IF = 0; //clear interrupt flag
Count ++; //increase count bit by 1
if (Count ==100)
{
Count = 0; //clear count bit
PORTD=~PORTD; //toggle PORT D
}
TMR1H=0x3C;
TMR1L=0xAF;
}
}
}
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS&WDTE_OFF&PWRTE_ON&BOREN_OFF&LVP_OFF);
int Count = 0;
void interrupt isr()
{
if (TMR1IF==1)
{
TMR1IF = 0; //clear interrupt flag
Count ++; //increase count bit by 1
if (Count == 100)
{
Count = 0; //clear count bit
PORTD=~PORTD; //toggle PORT D
}
TMR1H=0x3C; //setting initial value as 15535
TMR1L=0xAF;
}
}
void main ()
{
T1CON=0x00; //initialise Timer 1, prescare=1:1, unable timer1
TMR1H=0x3C; //setting initial value as 15535
TMR1L=0xAF;
T1CON = 0b00000001; //starting TImer 1
TRISD=0x00; //configurate PORTD as output
INTCONbits.GIE=1; //enable all global interrupts
INTCONbits.PEIE=1; //enable periferal int
PIE1bits.TMR1IE=1; //enable timer 1 overflow interrupt
PIR1bits.TMR1IF=0; //timer register 1 is not overflow
while (1)
{
}
}
Yes!
The interrupt flag will be set at the end of the first count above 0xFFFF but that leaves TMR1 with 0x0000 in it. You have to re-load the counter with it's start value again to shorten the remaining period. Obviously, as the timer is still running, it might have gone above 0x0000 by the time you have re-loaded it so you may have to adjust the value you use slightly to compensate. I would advise loading TMR1H before TMR1L to reduce the chance of a second roll-over before you have loaded all the value.
You can stop the timer while you reload it then start it again but there is no advantage to doing that in your application.
Brian.
Hi,
a workaround is to read the current counter value and add it to the 0x3CAF. This should be done in ISR or at least while the ISRs are disabled temprarily.
so if the counter value is already at (let´s say) 0x11 then you set the new value to 0x3CAF + 0x0011 = 0x3CC0.
I´m not familiar with the PICs, but I expect they have a better (hardware) solution.
* Either by setting a fixed "TOP" or "compare" value and automatically restart the counter with 0 (by hardware) => counting from 0 to TOP value
* or there is an option that the counter "presets" it´s value to 0x3CAF on each overflow.
Klaus
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?