Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Understanding timer

Status
Not open for further replies.
If you have no real input on RC0 then you may need to pull the signal low when it should be low... Some need to be pulled high use a 10k resistor to either pull high or low.

When I used below code LED flash only one time

C:
// 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
    TMR1ON = 0; //Stop Timer1
    
    TMR1H = 0xFF ;    // High byte FF
    TMR1L = 0xF6;    // Low Byte F6
  }
}
 
And?... How many flashes do you want?
flash led when the sensor is a high repeat process

When I use below program led doesn't flash when sensor is high
Code:
// 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)
{
    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 == 0)
	   {
	      TMR1ON = 0; //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
    TMR1ON = 0; //Stop Timer1
    
    TMR1H = 0xFF ;    // High byte FF
    TMR1L = 0xF6;    // Low Byte F6
  }
}
 
Last edited:
What am I checking??? I still don't get the gist of the program.. If you explain what you would like it to do, I'll make it so...

At the moment I see 10 counts and the led flashes once to signify 10 counts... What else should it do?
 
What am I checking??? I still don't get the gist of the program.. If you explain what you would like it to do, I'll make it so...

At the moment I see 10 counts and the led flashes once to signify 10 counts... What else should it do?

TRISC = 0b00000001; // count event RC0
TRISB = 0b00000001; //sensor
TRISD = 0b00000000; // LED

When object is detected by sensor , load TMR1 with starting value 25535 ( 63BF) so when TMR1 overflow it will generate interrupt so interrupt interrupt service routine will trigger the LED.
 
Last edited:
When object is detected by sensor , load TMR1 with starting value 25535 ( 63BF) so when TMR1 overflow it will generate interrupt so interrupt interrupt service routine will trigger the LED.
So where does the "count to10" come into it..

If you have a sensor on RB0 ( int pin ), Then what is driving the count on RC0 ?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top