The Timer Modules - Timer0
Until now, we have cover the basic knowledge of PIC programming. We will start analyzing the built-in modules of the PICs. At this point, i should remind that this tutorial is based on the PIC 16F88, a very powerful 8-bit PIC. For the 8-bit PIC family the modules have more or less the same functionality. As you understand, this tutorial will not work for 16 or 32-bit PICs. I will start with the TIMER modules. These modules are by far the most important to learn.
The Timer0 Module
The PIC 16F88 has 3 built in timers. The first one is the Timer0 module. This is an 8-bit counter, which means that it can count from 0 to 255, and then it will loop back to 0. There are two sources that the Timer0 can increase the count. Each PIC module, can be controlled by bit switches in various registers. The Timer0 module uses some bits from the OPTION_REG register for setting it up.
Setting up and accessing the Timer0 module
The Timer0 module has a dedicated register that can be accessed for read and write operations. This register can be accessed from BANK 0 and BANK 2, and is named "TMR0". A direct read (MOVF TMR0,w) will transfer the value of the Timer0 counter directly to the W register, while a direct write (MOVWF TMR0) will transfer the content of the W register to the Timer0 counter.
The operation of the Timer0 module can be determined by specific bits of the OPTION_REG register. Let's see these bits one by one:
OPTION_REG register BIT <5> (T0CS):
This is the "TMR0 Clock Source Select bit". The value of this bit will determine if the TMR0 register will be increased on every internal instruction cycle of the PIC (in other words once every 4 clock pulses) (learn more about the instruction cycle here), or on every pulse on the TOCKI pin input. The possible values for the TOCS bit are:
OPTION_REG register BIT <4> (T0SE):
This is the "TMR0 Source Edge Select bit". This bit has an effect only if the T0CS bit (above) is 1, in other words if the TMR0 register increases with external pulses on the TOCKI pin. The increament is done at the EDGE of each pulse. The value of the TOSE bit will determine if the increament will be done on the POSITIVE GOING transition, or on the NEGATIVE GOING transition of the pulse. The values that this bit can get are:
OPTION_REG register BIT <3> (PSA):
This is the "Prescaler Assignment bit". The prescaler is another module of the PIC. The prescaler divides the input counts before they arrive at the TMR0 register. The prescaler can divide both the pulses from the TOCKI pin (when TOCS=1) as well as the counts from the internal instruction cycles (when TOCS-0). The PSA bit can have the following values:
OPTION_REG register BIT <2:0> (PS):
These 3 bits are the "Prescaler Rate Select bits". These bits have an effect on the prescaler division rate all the time, but they have an effect on the Timer0 module ONLY if the prescaler is assigned to the Timer0 module (when PSA=0). If the prescaler is assigned to the Timer0 module, then these 3 bits will determine the division rate. If for example the division rate is 64, then the TMR0 module will increase it's value by one, after 64 counts. The values that these bits can get are:
The Timer0 Interrupt
The Timer0 module has an 8-bit register for the counts, the TMR0 register located in BANK0 and BANK2. This means that it can count up to 255. If the TMR0 value is 255 (0xFF) and it is increased once more, then the TMR0 register will become 0 (0x00) and the TMR0 interrupt shall be raised. The TMR0IE interrupt can be controlled by the INTCON register bit 5 (TMR0IE - TMR0 Interrupt Enabled). When the TMR0 Interrupt is raised, the INTCON bit 2 (TMR0IF - TMR0 Interrupt Flag) is raised to indicate that the TMR0 interrupt is occurred. The TMR0IF must be cleared in software (BCF INTCON,TMR0IF) for the TMR0 interrupt to be re-enabled. More info about the PIC Interrupts can be found in this link.
Examples with the Timer0 Module
For the sake of understanding, find below some examples on how to setup the TMR0 module.
Example 1 - TMR0 increasing on every LOW to HIGH transition on TOCKI pin
BANK1 ; Select Bank 1 BSF OPTION_REG,TOCS ; Transition on T0CKI pin BCF OPTION_REG,T0SE ; Increment on low-to-high transition on T0CKI pin BSF OPTION_REG,PSA ; Prescaler is assigned to the Watchdog BANK0 ; Select Bank 0
Example 2 - TMR0 increasing once every 128 internal instruction cycles
BANK1 ; Select Bank 1 BCF OPTION_REG,TOCS ; Transition on T0CKI pin BCF OPTION_REG,PSA ; Prescaler is assigned to the Watchdog BSF OPTION_REG,2 ; BSF OPTION_REG,1 ; BCF OPTION_REG,0 ; PS=110 = 1/128 division rate BANK0 ; Select Bank 0
Example 3 - Read - Add 0x10 and Write the TMR0 register
BANK0 ; Select Bank 0 MOVF TMR0,w ; READ TMR0 ADDLW 0x10 ; Add 0x10 MOVWF TMR0,w ; WRITE TMR0
Confirm your knowledge
There is an online test to check your knowledge on this page. You may reveal the test with the following button:
No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise without the prior written permission of the author.
Read the Disclaimer
All trademarks used are properties of their respective owners.
Copyright © 2007-2009 Lazaridis Giorgos.
All rights reserved.