Understanding of counter

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Hello
I just want to set a counter that goes from 0 to 100 then stops

I tried to write the code. This is only one step If the code look's fine I want to flash LED when timer stop

can someone check my code ?

C:
//MPLABX  XC8
//PIC16F877A

// 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

#include <xc.h>

void main(void)
{
 
    TRISC0=00000001;    //PORTC R0  pins are used as Input.

   //65536- 100 = 65436
   //65436 decimal = FF9C hexa decimal

   TMR1H=FF ;    // High byte FF
   TMR1L=9C ;    // Low Byte 9C

    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

  while(1)
  {
   
  }
}
I hope someone helps me to understand timer 1 programming

@JohnInTX
 

Thread Starter

Fanfire174

Joined Mar 13, 2018
240
Hi Expert's

I modified my program

The output of the sensor is connected to the Timer Clock Input Pin of the microcontroller. The timer register inside the microcontroller increments each time when a sensor become high.

I do not have any idea why LED doesn't flash when 10 counts happen?

C:
/ 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

}


void interrupt timer_isr()
{
  if(TMR1IF ==1)
  {
    RD4 = 1;  // LED ON
    __delay_ms(500); //
    RD4 = 0;  // LED OFF
    TMR1IF=0; // Clear timer interrupt flag
  }
}
@JohnInTX, @AlbertHall
 
Last edited:
Top