Download presentation
Presentation is loading. Please wait.
Published byMADHUKAR NIMBALKAR Modified over 4 years ago
1
Unit 2 : PIC I/O Ports and Timer Sinhgad College of Engineering Dept. of Information Technology Class - SE Processor Architecture ( 214451) Faculty – Prof. Madhukar V. Nimbalkar mvnimbalkar.scoe@sinhgad.edu 9890586765
2
I/O Port: I/O Port structure with programming: I/O Port structure, I/O Port programming, I/O Bit manipulation Programming. Timer/Counter: Registers used for Timer/Counter operation, Delay calculations, Programming of Timers using Embedded C. SYLLABUS : UNIT 2
3
Introduction PIC18 has two to five timers Depending on the family number These timers can be used as Timers to generate a time delay Counters to count events happening outside the uC Every timer needs a clock pulse to tick Clock source can be Internal 1/4th of the frequency of the crystal oscillator on OSC1 and OSC2 pins (Fosc/4) is fed into timer External: pulses are fed through one of the PIC18’s pins Counter
4
TIMER Comparison ParameterTIMER0TIMER1TIMER2TIMER3 Block Diagram Refer TIMER0 Block Diagram Refer TIMER1 Block Diagram Refer TIMER2 Block Diagram Refer TIMER3 Block Diagram Control Register Structure Refer T0CON Register structure Refer T1CON Register structure Refer T2CON Register structure Refer T3CON Register structure Special Function Registers associated TMR0H, TMR0LTMR1H, TMR1LTMR2, PR2TMR3H, TMR3L Timer Mode of Operation 8-bit / 16-bit16-bit8-bit16-bit Counter Mode of Operation 8-bit / 16-bit Counter 0 16-bit Counter 1 Not Available as Counter Counter 3 Pre-Scaling Factor 2, 4, 8, 16, 32, 64, 128, 256 1, 2, 4, 81, 4, 161, 2, 4, 8 Post-Scaling FactorNot Available 1, 2, 3, 4, 5, 6. 7, 8, 9, 10, 11, 12, 13, 14,15, 16 Not Available Clock Source for CCP Module Can't use Capture, Compare mode of CCP module PWM mode of CCP module Capture, Compare mode of CCP module
5
Timer Module. The Timer0 module timer/counter which can work as timer/ counter has the following features: 8-bit or 16 bit timer/counter 8-bit software programmable prescaler Internal or external clock select Interrupt on overflow from FFh to 00h Edge select for external clock Timer1 is 16 bit timer/ counter and cannot be operated in 8 bit. Timer2 is an 8-bit timer with a prescaler. It can be used as the PWM time-base for the PWM mode of the CCP module(s). Timer3 is 16 bit timer/ counter and cannot be operated in 8 bit. It also works in CCP mode
6
Timer0 registers and programming TMR0L & TMR0H are 8-bit Reg.
7
T0CON Reg Determine the timer operations modes Example If T0CON= 0000 1000 16-bit No prescaler T0CON (Timer0 Control) Register
8
TMR0IF flag bit Part of INTCON INTCON (Interrupt Control Register) has the TMR0IF Flag
9
Characteristics and operations of 16-bit mode 1. 16-bit timer, 0000 to FFFFH. 2. After loading TMR0H and TMR0L, the timer must be started. 3. Count up, till it reaches FFFFH, then it rolls over to 0000 and activate TMR0IF bit. 4. Then TMR0H and TMR0L must be reloaded with the original value and deactivate TMR0IF bit.
10
Steps to program Timer0 in 16-bit mode to generate time delay 1. Load the appropriate value into the T0CON register 2. Load reg. TMR0H followed by reg. TMR0L with initial value 3. Start the timer with instruction BSF T0CON, TMR0ON 4. Keep monitoring the timer flag (TMR0IF) to see if it is raised. 5. Stop the timer 6. Clear the TMR0IF flag 7. Go Back to step 2
12
Timer Delay Calculation for XTAL = 10 MHz with No Prescaler General formula for delay calculation Time Delay Generated = (Max. Count – Initial Count + 1) x Time period T ime Period = (1 / (10 MHz/4)) = 0.4 microseconds
14
Timer Calculation Calculate total delay generated by Timer 0 if (FFF2)H is loaded into it. Assume Crystal F = 10 MHz Time Delay Generated = (Max. Count – Initial Count + 1) x Time period T ime Period = (1 / (10 MHz/4)) = 0.4 microseconds Time Delay Generated = (FFFF – FFF2+1) X 0.4 microseconds = 14 X 0.4 microseconds = 5.6 microseconds
15
Calculate initial count to be loaded in timer0 with prescaler of 256 to generate time delay 0f 2 milliseconds. Assume crystal F = 10 MHz Time Delay Generated = (Max. Count – Initial Count + 1) x Time period TD = 2 ms, Max Count = 255 Time period = 1 / (10/4 ) = 1 / (2.5 MHz / 256 ) = 1 / (2500000/256) = 1/ 9765.625 Hz = 1024 microseconds = 1.024 ms 2 = (255 – IC+1) x 1.024 2/1.024 = (255 – IC+1) 1.953 = (255 – IC+1) IC = 256 – 1.953 = 254
16
Generate square wave of 10Khz frequency with Timer 1. assume crystal F = 10 MHz
17
Prescaler and generating larger delay The size of delay depend on The Crystal frequency Count in the timer’s 16-bit register. The largest time delay happens when TMR0L=TMR0H=0 Prescaler option is used to duplicate the delay by dividing the clock by a factor of 2,4, 8,16, 32,64, 128,256 If T0CON=0000 0101, then T = 4*64/f 9-17 XTAL Osc÷ 4÷ 64TMRx
20
TIMER 0 IN 8BIT MODE
21
Timer1 Programming
22
9-22
23
T1CON (Timer 1 Control ) Register
24
Timer1 Block Diagram
25
PIR1 (Interrupt Control Register 1) Contains the TMR1IF Flag
26
Counter Programming Used to counts event outside the PIC Increments the TMR0H and TMR0L registers TMR1CS in T1CON reg. determines the clock source, If TMR1CS = 1, the timer is used as a counter Counts up as pulses are fed from pin RC0 (T1CKI) What does T1CON=0110 1000 mean? If TMR1CS=1, the timer 1 counts up as clock pulses are fed into pin RC0
27
Using external Crystal for Timer1 clock Timer1 comes with two options, clock fed into T1CKI T1OSCEN=0 Clock from a crystal connected to T1OSI-T1OSO (additional) T1OSCEN=1 32 kHz Crystal is connected Used for saving power during SLEEP mode doesn’t disable Timer1 while the main crystal is shut down External Source Internal Source
29
9-29
33
9-33
35
9-35
36
Port Structure
37
Ports Available For PIC18F Family
38
Port SFRs of PIC18FXX Every Port has 3 SFRs associated with it 1.PORTX 2.LATX 3.TRISX
39
Port value after RESET Sr. No PORT Name Value 1TRISA(111 1111)b 2TRISB(11111111)b 3TRISC(11111111)b 4TRISD(11111111)b
40
Outputting (Writing) ‘0’ on PORT Setting a TRISX bit (= 1) will make the corresponding PORTX pin an input (i.e., put the corresponding output driver in a high-impedance mode). Clearing a TRISX bit (= 0) will make the corresponding PORTX pin an output (i.e., put the contents of the output latch on the selected pin). On a Power-on Reset, these pins are configured as inputs and read as ‘0’. Reading the PORTX register reads the status of the pins, whereas writing to it will write to the port latch.
41
Outputting (Writing) ‘1’ on PORT
42
Inputting (Reading) ‘0’ on PORT
43
Inputting (Reading) ‘1’ on PORT
44
I/O PORTs AND BIT ADDRESSABILITY
46
#include voidT0Delay(void); voidmain(void) { TRISB=0; //configure Port B as output While(1) { PORTB=0x55; //load Bit Pattern T0Delay(); PORTB=0xAA; T0Delay(); } voidT0Delay() { T0CON=0x07; //Timer0, 16bit, 256 prescaler TMR0H=0xFF; //load Higher byte inTMR0H TMR0L=0xFE; //Load Lower byte to TMR0L T0CONbits.TMR0ON=1; //start the timer for up count While(INTCONbits.TMR0IF==0); //Check for overflow T0CONbits.TMR0ON=0; //Turn off timer INTCONbits.TMR0IF==0; //clear the Timre0 flag Write embedded C program to toggle LEDs connected to port B with specific delay (2ms). T0CON Register Format
47
Time Delay Calculations (Assume Crystal Frequency = 10 MHz ) Time Delay Generated = (Max. Count – Initial Count + 1) x Time period TD = 2 ms = 2000 microseconds, Max Count = 65535, Prescalor = 256 Time period = 1 / (10/4 ) = 1 / (2.5 MHz / 256 ) = 1 / (2500000/256) = 1/ 9765.625 Hz = 1024 microseconds = 1.024 ms 2 = (65535 – IC+1) x 1.024 2/1.024 = (65535 – IC+1) 1.953 = (65535 – IC+1) IC = 65536 – 1.953 = 254.047 = 65534 = FFFEh
48
#include voidT0Delay(void); voidmain(void) { TRISB=0; //configure Port B as output While(1) { PORTB=0x55; //load Bit Pattern T0Delay(); PORTB=0xAA; T0Delay(); } voidT0Delay() { T0CON=0x47; //Timer0, 8bit, 256 prescaler TMR0L=0xFE; //Load count to TMR0L T0CONbits.TMR0ON=1; //start the timer for up count While(INTCONbits.TMR0IF==0); //Check for overflow T0CONbits.TMR0ON=0; //Turn off timer INTCONbits.TMR0IF==0; //clear the Timre0 flag Write embedded C program to toggle LEDs connected to port B with specific delay (2ms). T0CON Register Format
49
Time Delay Calculations (Assume Crystal Frequency = 10 MHz ) Time Delay Generated = (Max. Count – Initial Count + 1) x Time period TD = 2 ms = 2000 microseconds, Max Count = 255, Prescalor = 256 Time period = 1 / (10/4 ) = 1 / (2.5 MHz / 256 ) = 1 / (2500000/256) = 1/ 9765.625 Hz = 1024 microseconds = 1.024 ms 2 = (255 – IC+1) x 1.024 2/1.024 = (255 – IC+1) 1.953 = (255 – IC+1) IC = 256 – 1.953 = 254.047 = 254 = FEh
50
Thank you
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.