PIC12F675 Timer0 Code tutorial

PIC12F675 Timer 0 Code tutorial

This post provides the timer0 code for PIC12F675 microcontroller. This code is written in C language using MPLAB X IDE with XC8 compiler. You can download this code from the 'Downloads' section at the bottom of this page.

Simulation:

Figure1 : PIC12F675 Timer 0 Simulation
 In this circuit, PIC12F675 is running on internal oscillator of 4MHz value.GP0 pin is toggled every time timer0 expires and executes it's ISR code.



Code:

Figure2: Circuit for PIC16F877A ADC 

Figure2: PIC12F675 Timer 0 Code
Figure3: Timer 0 Initializing code


 Calculations:

In this function, OPTION_REG is initialized to make timer0 prescalar to be 1:2. Timer0 is an 8bit timer, so it expires after reaching a value of 255. When timer0 prescalar is made 1:2 then it means that timer0 value will increment after every two clock cycles. Since PIC12F675 is running at 1MIPS speed, this means that timer0 will expire after every 256*2 = 512 usec.T0IE bit enables timer0 interrupts and GIE bit enables global interrupts.
Figure4 : Interrupt service routine code
Whenever timer0 expires, then an interrupt is generated which executes the function shown above in the Figure 4. In this function, GP0 pin is toggled every time and timer0 interrupt flag is cleared. This means that in Figure 1 when ever GP0 pin toggles than timer0 interrupt has occurred. 

This is just an example code for timer0 of PIC12F675 microcontroller. You can modify it to fulfill your circuit requirements. You can leave your comments in the comment section below.

Downloads

Timer0 code for PIC12F675 was compiled in MPLAB X IDE with XC8 compiler and simulation was made in Proteus v7.10. To download code and Proteus simulation click here.

Popular posts from this blog

555 Timer Astable mode, Lt spice simulation, and real-time testing

Exploring USB Device Detection with Arduino and USB Host Shield

PIC12F675 LED Blinking Code