Tuesday, January 5, 2021

PIC16F887 Timer0 in timer mode MikroC

Introduction

As mentioned earlier, Timer0 works in either timer and counter mode. Timer0 Clock Select bit toggle between these two modes. Implementation of timer of a microcontroller has already described in previous post.

In this programming example of using Timer0 module, timer creates a time tick that's a square wave output on a microcontroller digital output pin.

Configuration

Timer0 module of PIC16F887 is configured to operate in timer mode. Prescaler is assigned to WDT to get 1:1 ratio.

Microcontroller clock is its 8MHz internal oscillator. Microcontroller executing speed is,

Finstruction = Fosc/4 = (8MHz)/4 = 2MHz.

Microcontroller instruction executing cycle is,

Tinstruction = 1/2MHz =  500ns.

Input clock pulse is directly drive to Timer0 module. Timer0 overflow time is,

Timer0overflow = 256 * 500ns = 128us.

Each time Timer0 overflow RD0 of Port D toggles, creating an output square wave. The period of square wave is,

Tinterrupt = 2 * 128us = 256us.

Its frequency is,

1 / (256us) = 3906Hz.

Programming for Timer0

This programming example is simulated using Proteus.



PIC16F887 Timer0 in timer mode MikroC
Schematic Diagram

 


MikroC program lists below,

  1. #define OUTPUT PORTD.RD0
  2.  
  3. void main() {
  4. PORTD=0x00; // CLEAR PORTD
  5. TRISD=0x00; // SET PORTD AS OUTPUT
  6. OSCCON|=0x70; // SELECT INTERNAL OSCILLATOR 8MHz
  7. OPTION_REG.T0CS=0; // SELCT INTERNAL SOURCE
  8. OPTION_REG.PSA=1; // Prescaler Assigned to WDT
  9. OPTION_REG&=0xF8 ; // SELECT 1:1 Prescaler
  10. TMR0=0; // CLEAR TIMER0
  11. while(1)
  12. if(INTCON.T0IF){
  13. INTCON.T0IF=0;
  14. PORTD^=1;
  15. }
  16. }

Click here to download this programming example.  


 

No comments:

Post a Comment

Labels

ADC (10) Analog (14) Arduino (12) Atmega16 (19) Audio (2) AVR (20) Charger (1) Cortex-M0 (1) Counter (10) CPLD (25) Digital I/O (22) Display (34) EEPROM (2) Environment Sensor (1) esp8266 (2) Experiment Board (10) I2C (4) Interrupt (7) LCD (1) LDmicro (29) measurement and instrumentation (7) Microchip Studio (3) MikroC (1) One-Shot (3) OpAmp (1) PCB (31) PIC16 Microcontrollers (16) PIC16F877A (2) PIC16F887 MikroC (22) PLC (35) PWM (11) Regulator (1) RTC (2) Sensor (8) Shift Registers (5) SPI (5) Timer (34) UART (2) ultra-sonic sensor (1) USB (1) VHDL (21) xc8 (1) XC95108 (9) XC9536 (15) XC9572 (1) Xilinx (23) Xilinx ISE (22)