/ PIC16F877A Configuration Bit Settings
// CONFIG
//MPLAB XC8
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00010000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
}
}
I didn't understand where to disable timer?You need a foreverloop…. place a line in the main
You also need to disable the timer just before the delay as it'll still be running..
I want to flash LED when the sensor activates 10 timeThe other thing.. If you need a specific timebase then you will need to pre-load the timer every cycle...
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00010000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1);
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
TMR1ON = 0; // Stops Timer1
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
}
}
Now restart the timer in the interrupt..
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00010000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1);
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
TMR1ON = 0; // Stops Timer1
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1ON = 1; // Start Timer1
}
}
modified version of code but LED doesn't triggeredI'll take a looksee.... I'll have to create a project.... I'll report back soon.
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disable)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
PORTD = 0b00000000;
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00010000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1);
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
TMR1ON = 0; // Stops Timer1
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1ON = 1; // Start Timer1
}
}
code is not working as expectedTRISD = 0; You have RD4 set to input!!
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disable)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
PORTD = 0b00000000;
TRISC0 = 1; //PORTC R0 pins are used as Input.
TRISD4 = 0; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1);
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
TMR1ON = 0; // Stops Timer1
RD4 = 1; // LED ON
__delay_ms(5000); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1ON = 1; // Stops Timer1
}
}
Scenario:-- Count to 10.. flash LED.. reload the timer to count 10 flash LED repeat process continuouslyWhat is your trigger?? If your trigger is on RC0 ( ext int ) then you need to change your code to fire the interrupt when RC0 goes high or low..
Then in the while loop in main use a control flag to flash the LED. But! What do you want to happen once the timer hits 10... Do you need to reload the timer to count 10 again?? The reason I ask is: you have your timer setup as a timer and not a counter???
Scenario:-- Count to 10.. flash LED.. then what??
// PIC16F877A Configuration Bit Settings
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disable)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
PORTD = 0b00000000;
TRISC0 = 1; //PORTC R0 pins are used as Input.
TRISD4 = 0; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6 ; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 0; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 0; // Oscillator is shut-off
T1CKPS0 = 1; // 1:8 prescale value
T1CKPS1 = 1;
TMR1ON = 1; //Start Timer1
while(1)
{
if(TMR1IF ==1)
{
TMR1ON = 0; // Stops Timer1
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1ON = 1; // Stops Timer1
}
}
}
// PIC16F877A Configuration Bit Settings
// CONFIG
//MPLAB XC8
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00000000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 1; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 1; // Oscillator is shut-off
T1CKPS0 = 0; // 1:8 prescale value
T1CKPS1 = 0;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1);
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6; // Low Byte F6
}
}
Try this;-
T1OSCEN = 1; // Oscillator is shut-off
datasheet said:Counter mode is selected by setting bit TMR1CS. In
this mode, the timer increments on every rising edge of
clock input on pin RC1/T1OSI/CCP2 when bit
T1OSCEN is set, or on pin RC0/T1OSO/T1CKI when
bit T1OSCEN is cleared.
No!! Read the datasheet... T1OSCEN can be used ( when in counter mode )
But without the OSC bit, the input is unstable..
Modification
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISB = 0b00000001; //PORTB R0 pins are used as Input.
TRISD = 0b00000000; //PORTD R4 pins are used as output.
// PIC16F877A Configuration Bit Settings
// CONFIG
//MPLAB XC8
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#define _XTAL_FREQ 20000000 //Specify the XTAL crystal FREQ
#include<xc.h>
void main(void)
{
TRISB = 0b00000001; //PORTB R0 pins are used as Input.
TRISC = 0b00000001; //PORTC R0 pins are used as Input.
TRISD = 0b00000000; //PORTD R4 pins are used as output.
//Loading starting value of counter 65536- 10 = 65426 = FFF6 hexa decimal
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6; // Low Byte F6
//Set T1CON Register
TMR1ON = 0; // Stops Timer1
TMR1CS = 1; // Internal clock (FOSC/4)
T1SYNC = 0; // Synchronize external clock input
T1OSCEN = 1; // Oscillator is shut-off
T1CKPS0 = 0; // 1:8 prescale value
T1CKPS1 = 0;
TMR1IE=1; //Enable timer interrupt bit in PIE1 register
GIE=1; //Enable Global Interrupt
PEIE=1; //Enable the Peripheral Interrupt
TMR1ON = 1; //Start Timer1
while(1)
{
if (RB0 == 1)
{
TMR1ON = 1; //Start Timer1
}
else
{
TMR1ON = 0; //Stop Timer1
}
}
void interrupt timer_isr()
{
if(TMR1IF ==1)
{
RD4 = 1; // LED ON
__delay_ms(500); //
RD4 = 0; // LED OFF
TMR1IF=0; // Clear timer interrupt flag
TMR1H = 0xFF ; // High byte FF
TMR1L = 0xF6; // Low Byte F6
}
} [code]
When I ran the program in post #17. the counter doesn't work when the sensor is highWhen you stop the counter... It may be a good idea to reset the timer so it will count to 10... I also found that the T1OSCEN needs to be 0 if you are counting on RC0.. I had it set to 1 as I was testing the count on RC1...
**NOTE** One pitfall. The timer has to see a falling edge before it will count a rising edge.. so if the RC0 pin is high it will count the next rising.. If the pin RC0 is low when you start, then the first rising edge will be missed so it will see 11 pulses and not 10.. Before starting the count, read RC0... If it is low preload with -9, if its high then preload with -10...
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?