Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan



Similar documents
Section 14. Compare/Capture/PWM (CCP)

Real-Time Clock. * Real-Time Computing, edited by Duncan A. Mellichamp, Van Nostrand Reinhold

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

Memory organization. Memory blocks: Program memory (flash-type) 16 kword (32 kbyte) (instruction 16 bit wide) Data RAM 1536 byte (1.

PIC Programming in Assembly. (

SPI. Overview and Use of the PICmicro Serial Peripheral Interface. Getting Started: SPI

Using The PIC I/O Ports

PIC in Practice. A Project-Based Approach. D. W. Smith

PIC16F627A/628A/648A Data Sheet

PIC16F627A/628A/648A Data Sheet

10-bit Σ ADC from a PIC16F84

PIC16CR7X Data Sheet. 28/40-Pin, 8-Bit CMOS ROM Microcontrollers Microchip Technology Inc. DS21993A

Pololu P-Star 25K50 Micro User s

Section 9. I/O Ports

PIC12F629/675 Data Sheet

PIC16F84A. 18-pin Enhanced Flash/EEPROM 8-Bit Microcontroller. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

Section 29. Instruction Set

c0003 A Simple PIC Application CHAPTER 3

PIC16F87X. 28/40-pin 8-Bit CMOS FLASH Microcontrollers. Devices Included in this Data Sheet: Pin Diagram PDIP. Microcontroller Core Features:

I 2 C Master Mode Overview and Use of the PICmicro MSSP I 2 C Interface with a 24xx01x EEPROM

8-bit Microcontroller. Application Note. AVR134: Real-Time Clock (RTC) using the Asynchronous Timer. Features. Theory of Operation.

CoE3DJ4 Digital Systems Design. Chapter 4: Timer operation

8-Bit CMOS Microcontrollers with A/D Converter

PIC16F62X. FLASH-Based 8-Bit CMOS Microcontrollers. Devices included in this data sheet: Special Microcontroller Features: High Performance RISC CPU:

Programming PIC Microcontrollers in PicBasic Pro Lesson 1 Cornerstone Electronics Technology and Robotics II

PIC16F84A Data Sheet. 18-pin Enhanced FLASH/EEPROM 8-bit Microcontroller Microchip Technology Inc. DS35007B

Section bit A/D Converter

Embedded C Programming

APPLICATION NOTE. Atmel AVR134: Real Time Clock (RTC) Using the Asynchronous Timer. Atmel AVR 8-bit Microcontroller. Introduction.

Microtronics technologies Mobile:

Section 8. Interrupts

PIC12F508/509/16F505 Data Sheet

Lesson-16: Real time clock DEVICES AND COMMUNICATION BUSES FOR DEVICES NETWORK

PIC16F62X Data Sheet. FLASH-Based 8-Bit CMOS Microcontroller Microchip Technology Inc. Preliminary DS40300C

How To Program A Microcontroller With Memory On A Microchip Microcontroller

AN974 APPLICATION NOTE

PIC16F630/676 Data Sheet

Flash Microcontroller. Memory Organization. Memory Organization

Specification: 1 Specification

AVR134: Real Time Clock (RTC) using the Asynchronous Timer. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

AVR Timer/Counter. Prof Prabhat Ranjan DA-IICT, Gandhinagar

PIC12F629/675 Data Sheet

PIC16F5X Data Sheet. Flash-Based, 8-Bit CMOS Microcontroller Series Microchip Technology Inc. DS41213D

AUTOMATIC NIGHT LAMP WITH MORNING ALARM USING MICROPROCESSOR

Section bit A/D Converter

PIC16F87XA Data Sheet

PIC16F872 Data Sheet. 28-Pin, 8-Bit CMOS Flash Microcontroller with 10-Bit A/D Microchip Technology Inc. DS30221C

Microcontroller Code Example Explanation and Words of Wisdom For Senior Design

RENESAS TECHNICAL UPDATE

PIC16F87/88 Data Sheet

PIC16F87X Data Sheet. 28/40-Pin 8-Bit CMOS FLASH Microcontrollers Microchip Technology Inc. DS30292C

PIC16F8X. 18-pin Flash/EEPROM 8-Bit Microcontrollers. Devices Included in this Data Sheet: Pin Diagrams. High Performance RISC CPU Features:

iclass Card Cloning using an RW300 Reader/Writer

How To Program A Microcontroller Board (Eb064) With A Psp Microcontroller (B064-74) With An Ios 2.5V (Power) And A Ppt (Power Control) (Power Supply) (

AN3371 Application note

Timer A (0 and 1) and PWM EE3376

AN108 IMPLEMENTING A REALTIME CLOCK. Relevant Devices. Introduction. Key Points. Overview

AN727. Credit Card Reader Using a PIC12C509 DATA ENCODING INTRODUCTION FIGURE 1: POSITION OF ISO TRACKS 1, 2 AND 3. Andrew M Errington

28/40/44-Pin Enhanced Flash Microcontrollers. 10-bit A/D (ch) EEPROM (Bytes) I/O

8-Bit Flash Microcontroller for Smart Cards. AT89SCXXXXA Summary. Features. Description. Complete datasheet available under NDA

AN3252 Application note

Accurate Measurement of the Mains Electricity Frequency

Four-Channel Digital Voltmeter with Display and Keyboard. 8 x 220W RB0 RB1 RB2 RB3 RB4 RB5 RB6 RB7 RA0 RA1 RA2 RA3 PIC16C71

Flow Charts & Assembly Language

DP8570A DP8570A Timer Clock Peripheral (TCP)

Chapter 13. PIC Family Microcontroller

AN857. Brushless DC Motor Control Made Easy INTRODUCTION S 001 B. Anatomy of a BLDC SIMPLIFIED BLDC MOTOR DIAGRAMS

Analog-to-Digital Converters

Hello, and welcome to this presentation of the STM32L4 reset and clock controller.

Section 29. Real-Time Clock and Calendar (RTCC)

Section 28. In-Circuit Serial Programming (ICSP )

Interrupts and the Timer Overflow Interrupts Huang Sections What Happens When You Reset the HCS12?

A simple RF/Microwave frequency counter

AVR131: Using the AVR s High-speed PWM. Introduction. Features. AVR 8-bit Microcontrollers APPLICATION NOTE

EMBEDDED SYSTEMS PROGRAMMING WITH THE PIC16F877

EXERCISE 3: String Variables and ASCII Code

PIC18F2450/4450 Data Sheet

AVR1003: Using the XMEGA Clock System. 8-bit Microcontrollers. Application Note. Features. 1 Introduction

Section 7. Oscillator

Microchip PIC18F452 Core Hardware. CPU, Memory, Interrupts, and I/O Ports

PIC16F631/677/685/687/689/690 Data Sheet

PIC12F752/HV752 Data Sheet

Flexible Active Shutter Control Interface using the MC1323x

AN585. A Real-Time Operating System for PICmicro Microcontrollers INTRODUCTION. Why do I Need a Real-Time Kernel? What is Multitasking Anyway?

ET-BASE AVR ATmega64/128

SSPBUF. Shift Clock SSPSR. START bit, STOP bit, Acknowledge Generate

Hello, welcome to this presentation of the low power timer, or LPTMR, module for Kinetis MCUs. In this session you ll learn about the LPTMR, it s

PIC16F87X. 28/40-Pin 8-Bit CMOS FLASH Microcontrollers. Pin Diagram. Devices Included in this Data Sheet: Microcontroller Core Features:

HT46R14A Single Phase AC Induction Motor Frequency Converter Application

PIC16F526 Data Sheet. 14-Pin, 8-Bit Flash Microcontroller Microchip Technology Inc. Preliminary DS41326A

AN1229. Class B Safety Software Library for PIC MCUs and dspic DSCs OVERVIEW OF THE IEC STANDARD INTRODUCTION

Serial Communications

PIC16C745/ Bit CMOS Microcontrollers with USB. Pin Diagrams. Devices included in this data sheet: Microcontroller Core Features:

71M6521 Energy Meter IC. Real Time Clock Compensation. The Challenge. The RTC in the 71M6521D/F. Theory of Operation APPLICATION NOTE

PICmicro tm Development Board

8-bit Microcontroller. Application Note. AVR415: RC5 IR Remote Control Transmitter. Features. Introduction. Figure 1.

1. Learn about the 555 timer integrated circuit and applications 2. Apply the 555 timer to build an infrared (IR) transmitter and receiver

PIC10F200/202/204/206 Data Sheet

Transcription:

PIC18 Timer Programming g Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.twcgu

Functions of PIC18 timer Functions of the timer Generate a time delay As a counter to count events happening outside the microcontroller Clock sources of the timer Internal clock pulse 1/4 th of the frequency of the crystal oscillator on the OSC1 and OSC2 pins (Fosc/4) Usually used to generate a time delay External clock pulse Usually used as a counter 2

Registers of the timer Special function registers (SFRs) Timer1 Timer0 3

Timer0 registers Timer0 can be used as an 8-bit or 16-bit timer Can be assessed like any SFRs MOVWF TMR0L MOVFF TMR0H, PORTC 4

T0CON (Timer0 control) register Set various timer operation mode 5

TMR0IF flag bit in INTCON (interrupt control register) T0CS=0 TMR0ON 6

Timer0 16-bit block diagram Fosc/4 T0CKI pin T0SE Sync with internal clock TMR0L TMR0 high byte Set interrupt flag TMR0IF T0CS T0PS2:T0PS0 PSA Parallel operations with TMR0L read or write TMR0H Should load TMR0H first, and then TMR0L 7

Find the frequency of the square wave on the PORTB.5 bit if XTAL = 10 MHz BCF TRISB, 5 MOVLW 0x08 ; Timer0, 16-bit, internal clock, no prescale MOVWF T0CON Clock period = 1 / (1/4 x 10 MHz) = 0.4 s HERE MOVLW 0x76 Counting clocks FFFFH 7634H + 1 = 35,276 MOVWF TMR0H time delay = 35,276 x 0.4 s = 14.11 ms MOVLW 0x34 frequency = 1/(14.11 ms x 2) = 35.434 Hz MOVWF TMR0L BCF INTCON, TMR0IF ; clear Timer0 interrupt flag CALL DELAY BTG PORTB, 5 BRA HERE DELAY BSF T0CON, TMR0ON ; start Timer0 AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag BRA AGAIN BCF T0CON, TMR0ON ;stoptimer0 RETURN 8

Assume that XTAL = 10 MHz, modify previous program for a period of 10 ms A period of 10ms atimedelayof5ms ms XTAL = 10 MHz ¼ fosc = 2.5 MHz Counter counts up every 0.4 s Need 5 ms / 0.4 s = 12,500 clocks Initial value for TMR0 = 65,535535 12,500 + 1 = CF2CH TMR0H = CF TMR0L = 2C 9

Use prescaler to generate a large time delay (assume that XTAL = 10 MHz) BCF TRISB, 2 MOVLW 0x05 ; Timer0, 16-bit, internal clock, prescaler 64 MOVWF T0CON HERE MOVLW 0x01 FFFFH 0108H + 1 = 65,272 MOVWF TMR0H 65,272 x 64 x 0.4 s = 1.671 sec MOVLW 0x08 MOVWF TMR0L BCF INTCON, TMR0IF ; clear Timer0 interrupt flag CALL DELAY BTG PORTB, 2 BRA HERE DELAY BSF T0CON, TMR0ON ; start Timer0 AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag BRA AGAIN BCF T0CON, TMR0ON ;stoptimer0 RETURN 10

Largest time delay that can be achieved Set TMR0 to 0000 count from 0000 to FFFF 65,536 clocks Set prescaler 256 Time delay = 4 s x 256 x 65,536 = 67,108,864 s 11

8-bit mode programming g of Timer0 Fosc/4 T0CKI pin T0SE T0PS2:T0PS0 PSA Sync with internal clock TMR0 Set interrupt flag TMR0IF T0CS 12

Find the frequency of the square wave if XTAL = 10 MHz BCF TRISB, 3 BCF INTCON, TMR0IF ; clear Timer0 interrupt flag MOVLW 0x48 ; Timer0, 8-bit, internal clock, no prescale MOVWF T0CON HERE MOVLW -D 150 MOVWF TMR0L Delay subroutine = 150 x 0.4 s = 60 s BSF PORTB, 3 T = high portion + low portion CALL DELAY = 2 x 60 s + 60 s = 180 s MOVWF TMR0L CALL DELAY BCF PORTB, 3 MOVWF TMR0L CALL DELAY BRA HERE DELAY BSF T0CON, TMR0ON ; start Timer0 AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag BRA BCF AGAIN T0CON, TMR0ON ; stop Timer0 13

Counter programming g using Timer0 Set T0CS (Timer0 clock source) in T0CON to 1 Get pulses from T0CKI (Timer0 clock input): RA4 (PORTA.4) 14

Clock pulses are fed into T0CKI and a buzzer is connected to PORTB.1. Sound the buzzer after counting 100 pulses BCF TRISB, 1 ; RB1 as an output to a buzzer BSF TRISA, 4 ; RA4 as an input for clock-in MOVLW 0x68 ; Timer0, 8-bit, external clock, no prescale MOVWF T0CON MOVLW -D 100 MOVWF TMR0L BSF T0CON, TMR0ON ; start Timer0 AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt t flag BRA BCF OVER BTG PORTB, 1 CALL DELAY GOTO OVER AGAIN T0CON, TMR0ON ; stop Timer0 15

Timer1 Only support 16-bit mode Set interrupt flag TMR1IF on overflow CCP Special Event Trigger TMR1H TMR1L TMR1ON T1SYNC T1OSO/T1CKI T1OSCEN T1OSI Fosc/4 T0PS1:T0PS0 TMR1CS 16

T1CON (Timer1 control) register 17

PIR1 (interrupt control register 1) 18

Assume XTAL = 10 MHz, use Timer1 maximum prescaler to generate a square wave of 50 Hz BCF TRISB, 5 MOVLW 0x30 ; Timer1, 16-bit, internal clock, prescaler 1:8 MOVWF T1CON HERE MOVLW 0xF3 FFFFH F3CBH + 1 = 3125 MOVWF TMR1H 3125 x 8 x 0.4 s = 10 ms MOVLW 0xCB frequency = 1/(2x10ms) = 50 Hz MOVWF TMR1L BCF PIR1, TMR1IF ; clear Timer1 interrupt flag CALL DELAY BTG PORTB, RB5 BRA HERE DELAY BSF T1CON, TMR1ON ; start Timer1 AGAIN BTFSS PIR1, TMR1IF ; monitor Timer1 interrupt flag BRA AGAIN BCF PIR1, TMR1ON ;stoptimer1 RETURN 19

Counter programming g using Timer1 Two options for the external clock source The clock fed into T1CKI pin The clock from a crystal connected to T1OSI and T1OSO pins where a 32-kHz crystal is connected to External clock source option (TMR1CS) is set to 1 Timer1 oscillator enable bit (T1OSCEN) is set to 1 SLEEP mode SLEEP instruction shut down main crystal to save power Timer1 is still enable for on-chip RTC (real-time clock) 20

Assume that a 1-Hz pulse is fed into T1CKI, display the counter values on ports B and D. BSF TRISC, RC0 ; RC0 as an input for clock-in CLRF TRISB CLRF TRISD MOVLW 0x02 ; Timer1, 16-bit, external clock, no prescale MOVWF T1CON HERE MOVLW 0x0 MOVWF TMR1H MOVLW 00 0x0 MOVWF TMR1L BCF PIR1, TMR1IF BSF T1CON, TMR1ON ; start Timer1 AGAIN MOVFF TMR1H, PORTD MOVFF TMR1L, PORTB BTFSS PIR1, TMR1IF ; monitor Timer1 flag BRA BCF GOTO AGAIN PIR1, TMR1ON ; stop Timer1 HERE 21

Timer2 block diagram 8-bit timer Set interrupt flag TMR2IF Fosc/4 T2CKPS1:T2CKPS0 TOUTPS3:TOUTPS0 CCP Special Event Trigger 22

T2CON (Timer2 control) register 23

PIR1 (interrupt control register 1) 24

Assume that XTAL = 10 MHz, turn on PORTB4 when TMR2 reach 100 BCF TRISB, 4 BCF PORTB, 4 MOVLW 0x0 ; Timer2, no prescale or postscale MOVWF T2CON MOVLW 00 0x0 MOVWF TMR2 MOVLW D 100 MOVWF PR2 BCF PIF1, TMR2IF ; clear Timer2 interrupt flag BSF T2CON, TMR2ON ; start Timer2 AGAIN BTFSS INTCON, TMR0IF ; monitor Timer0 interrupt flag BRA AGAIN BSF PORTB, 4 BCF T2CON, TMR2ON ; stop Timer2 HERE BRA HERE 25

Timer3 block diagram 16-bit timer or counter Set interrupt flag TMR3IF on overflow CCP Special Trigger Fosc/4 T3CKPS1:T3CKPS0 26

T3CON (Timer3 control) register 27

PIR2 (interrupt control register 2) 28

Generate a square wave of 50 Hz on the PORTB.5 bit if XTAL = 10 MHz BCF TRISB, 5 MOVLW 0x0 ; Timer3, 16-bit, internal clock, no prescale MOVWF T3CON HERE MOVLW 0x9E FFFFH 9E58H + 1 = 25,000 clocks MOVWF TMR3H time delay = 25,000 x 0.4 s = 1 ms MOVLW 0x58 frequency = 1/(1 ms x 2) = 50 Hz MOVWF TMR3L BCF PIR2, TMR3IF ; clear Timer3 interrupt flag CALL DELAY BTG PORTB, RB5 BRA HERE DELAY BSF T3CON, TMR3ON ; start Timer3 AGAIN BTFSS PIR2, TMR3IF ; monitor Timer3 interrupt flag BRA AGAIN BCF T3CON, TMR3ON ;stoptimer3 RETURN 29

Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 30