How to Program Timer-0 as Counter in PIC16F877A
The following code demonstrate how to use timer-0 as counter-0. The counting pulses is applied on T0CLK pin and 8-bit output is shown on port-b. The code is written in “mikroC PRO for PIC v.6.6.3” IDE and simulation is done with Proteus 8.0 SP0. At the end of code, you can find complete project files for download.
Code in mikroC
void T0AsCounterInit(void);
void main(void)
{
TRISB = 0x00; // portb as output
PORTB = 0x00; // portb init to 0x00
T0AsCounterInit();
while(1)
{
PORTB = TMR0; // timer0 reg assign to portb
}
}
void T0AsCounterInit(void)
{
// timer0 configration
TMR0 = 0; // timer0 register init
OPTION_REG.T0CS = 1; // select as counter
OPTION_REG.T0SE = 0; // Low to High Edge
//OPTION_REG.T0SE = 1; // High to low Edge
OPTION_REG.PSA = 1; // Prescaler is assigned to the WDT
}
void main(void)
{
TRISB = 0x00; // portb as output
PORTB = 0x00; // portb init to 0x00
T0AsCounterInit();
while(1)
{
PORTB = TMR0; // timer0 reg assign to portb
}
}
void T0AsCounterInit(void)
{
// timer0 configration
TMR0 = 0; // timer0 register init
OPTION_REG.T0CS = 1; // select as counter
OPTION_REG.T0SE = 0; // Low to High Edge
//OPTION_REG.T0SE = 1; // High to low Edge
OPTION_REG.PSA = 1; // Prescaler is assigned to the WDT
}
Download Files
For download “mikroC PRO for PIC” project and “Proteus 8.0” simulation files, click here.
Comments
Post a Comment