Aim:
In this tutorial, we are going to discuss the Timer module of PIC18F4520. First, we will see what are timers, their working and later we will configure the PIC18F4520 timers to generate the delay of 100ms and 500ms respectively. At the end, we will see how to use the ExploreEmdedded Timer library.
Description:
Many times, we plan and build systems that perform various processes that depend on time.
Simple example of this process is the digital wristwatch. The role of this electronic system is to display time in a very precise manner and change the display every second (for seconds), every minute (for minutes) and so on.
To perform the steps we've listed, the system must use a timer, which needs to be very accurate in order to take necessary actions.The clock is actually a core of any electronic system.
In this PIC timer module tutorial we will study the existing PIC timer modules. The microcontroller PIC18F4520 has 4 different timers:
- PIC Timer0
- PIC Timer1
- PIC Timer2
- PIC Timer3
We can use these timers for various important purposes. So far we used “delay procedure” to implement some delay in the program, that was counting up to a specific value, before the program could be continued. "Delay procedure" had two disadvantages:
- we could not say exactly how long the Delay procedure was in progress
- we could not perform any further steps while the program executes the "delay procedure"
Now, using Timers we can build a very precise time delays which will be based on the system clock and allow us to achieve our desired time delay well-known in advance.
In order for us to know how to work with these timers, we need to learn some things about each one of them. We will study each one separately.
In order for us to know how to work with these timers, we need to learn some things about each one of them. We will study each one separately.
PIC Timer0 tutorial
The Timer0 module incorporates the following features:
• Software selectable operation as a timer or counter in both 8-bit or 16-bit modes
• Readable and writable registers
• Dedicated 8-bit, software programmable prescaler
• Selectable clock source (internal or external)
• Edge select for external clock
• Interrupt-on-overflow
bit 7 TMR0ON: Timer0 On/Off Control bit
1 = Enables Timer0
0 = Stops Timer0
bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter
bit 5 T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)
bit 4 T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3 PSA: Timer0 Prescaler Assignment bit
1 = TImer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
bit 2-0 T0PS<2:0>: Timer0 Prescaler Select bits
111 = 1:256 Prescale value
110 = 1:128 Prescale value
101 = 1:64 Prescale value
100 = 1:32 Prescale value
011 = 1:16 Prescale value
010 = 1:8 Prescale value
001 = 1:4 Prescale value
000 = 1:2 Prescale value
• Software selectable operation as a timer or counter in both 8-bit or 16-bit modes
• Readable and writable registers
• Dedicated 8-bit, software programmable prescaler
• Selectable clock source (internal or external)
• Edge select for external clock
• Interrupt-on-overflow
1 = Enables Timer0
0 = Stops Timer0
bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
1 = Timer0 is configured as an 8-bit timer/counter
0 = Timer0 is configured as a 16-bit timer/counter
bit 5 T0CS: Timer0 Clock Source Select bit
1 = Transition on T0CKI pin
0 = Internal instruction cycle clock (CLKO)
bit 4 T0SE: Timer0 Source Edge Select bit
1 = Increment on high-to-low transition on T0CKI pin
0 = Increment on low-to-high transition on T0CKI pin
bit 3 PSA: Timer0 Prescaler Assignment bit
1 = TImer0 prescaler is not assigned. Timer0 clock input bypasses prescaler.
0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
bit 2-0 T0PS<2:0>: Timer0 Prescaler Select bits
111 = 1:256 Prescale value
110 = 1:128 Prescale value
101 = 1:64 Prescale value
100 = 1:32 Prescale value
011 = 1:16 Prescale value
010 = 1:8 Prescale value
001 = 1:4 Prescale value
000 = 1:2 Prescale value
Block Diagram
Schematic
Code
// **********************************************
// Project: Interfacing 8-bit timer0 to 8051
// Author: Hack Projects India
// Module description: Operate array of LED's
// **********************************************
#include <p18f4520.h>
#pragma config OSC=HS, FCMEN=ON, WDT=OFF, IESO=OFF, XINST=OFF, LVP=OFF
void Timer0_Delay()
{
T0CON = 0x04;
TMR0H =0x0B;
TMR0L =0xDC;
T0CONbits.TMR0ON =1;
//wait:
while(INTCONbits.TMR0IF == 0);
INTCONbits.TMR0IF =0;
}
void main()
{
ADCON1 =0x7F;
TRISB =0x00; //INTITALISE PORTB OUTPUT
PORTB =0X00; //SET PORTS AS LOW
while(1)
{
PORTB=0xff;
Timer0_Delay();
PORTB=0x00;
Timer0_Delay();
}
}
Downloads:
The code was compiled in Keil uvision4 and simulation was made in Proteus v7.7.
To download code and proteus simulation click here.
Further Reading suggestions:
You may also like,
- Interfacing keypad with PIC
- nterfacing DAC with PIC
- Interfacing with UART of PIC controller
- Interfacing SPI communication with PIC
- PIC Displaying Custom Characters on LCD
- PIC Graphical LCD
- RTC interfacing using I2C in PIC
- Interfacing ultrasonic sensor with PIC
- Interfacing GPS Modu with PIC
- Interfacing GSM Module with PIC
- Interfacing PWM in PIC
- Interfacing ADC with PIC
- Scrolling string on LCD using PIC
- Interfacing keypad with PIC
- nterfacing DAC with PIC
- Interfacing with UART of PIC controller
- Interfacing SPI communication with PIC
- PIC Displaying Custom Characters on LCD
- PIC Graphical LCD
- RTC interfacing using I2C in PIC
- Interfacing ultrasonic sensor with PIC
- Interfacing GPS Modu with PIC
- Interfacing GSM Module with PIC
- Interfacing PWM in PIC
- Interfacing ADC with PIC
- Scrolling string on LCD using PIC
No comments:
Post a Comment