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

Similar documents
ELCT 912: Advanced Embedded Systems

MicroToys Guide: Motors N. Pinckney April 2005

Pulse Width Modulation

Physics 335 Lab 7 - Microcontroller PWM Waveform Generation

PROCESS. Object. Block diagram of our design. DISPLAY THE DISTANCE (7 segment display) PIC 16F873

;;;;;;; Variables ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; cblock Bank0RAM ;Temporary storage for STATUS during interrupts

Sensor Interface Using PIC12CXXX as a Sensor Interface for Metal Detection

AN594. Using the CCP Modules. Using the CCP Modules CCP OPERATION. PWM Mode PWM MODE BLOCK DIAGRAM TABLE 1: CCP MODE - TIMER RESOURCE

Building an Analog Communications System

IST TSic Temperature Sensor IC Application Notes ZACwire Digital Output

Hashemite University Faculty of Engineering Mechatronics Engineering Department. Microprocessors and Microcontrollers Laboratory

TKT-3500 Microcontroller systems

CHAPTER 3 PIC Microcontroller CCP and ECCP Tips n Tricks

TECHNICAL NOTE. A COMPACT ALGORITHM USING THE ADXL202 DUTY CYCLE OUTPUT by Harvey Weinberg

PIC16F716 Data Sheet. 8-bit Flash-based Microcontroller with A/D Converter and Enhanced Capture/Compare/PWM

PIC Analog Voltage to PWM Duty Cycle

PIC Functionality. General I/O Dedicated Interrupt Change State Interrupt Input Capture Output Compare PWM ADC RS232

PIC16F716 Data Sheet. 8-bit Flash-based Microcontroller with A/D Converter and Enhanced Capture/Compare/PWM

Development of a Low Cost MPPT Circuit for Solar Panel

GCE A level 1145/01 ELECTRONICS ET5. P.M. THURSDAY, 31 May hours. Centre Number. Candidate Number. Surname. Other Names

PICmicro CCP and ECCP Tips n Tricks

νµθωερτψυιοπασδφγηϕκλζξχϖβνµθωερτ ψυιοπασδφγηϕκλζξχϖβνµθωερτψυιοπα σδφγηϕκλζξχϖβνµθωερτψυιοπασδφγηϕκ χϖβνµθωερτψυιοπασδφγηϕκλζξχϖβνµθ

MCV18E Data Sheet. 18-Pin Flash Microcontroller Microchip Technology Inc. DS41399A

Laboratory Exercise 1 Microcontroller Board with Driver Board

Simple Bridge Stand Alone H-Bridge Data Sheet Revision 1 August 2005

EASTERN MEDITERRANEAN UNIVERSITY FACULTY OF ENGINEERING Electrical and Electronics Engineering Department

PIC ADC to PWM and Mosfet Low-Side Driver

Controlling DC Brush Motor using MD10B or MD30B. Version 1.2. Aug Cytron Technologies Sdn. Bhd.

PIC16C712/716 Data Sheet

GCE A level 1145/01 ELECTRONICS ET5

Microcontroller: Timers, ADC

Lesson 19 In-Circuit Programming

A RECTANGULAR UNIPOLAR PULSE WIDTH MEASUREMENT BY MEANS OF PIC18F2550 MCU. Konstantin Metodiev

GCE A level 1145/01 ELECTRONICS ET5

Hello and welcome to this Renesas Interactive Course that provides an overview of the timers found on RL78 MCUs.

Discrete Logic Replacement Garage Door Indicator

Lecture 12 Timer Functions

Mechatronics Project Kit - Getting Started Manual

IE1206 Embedded Electronics

Triple Stage Incubator

PIC16F72 Data Sheet. 28-Pin, 8-Bit CMOS FLASH Microcontoller with A/D Converter Microchip Technology Inc. DS39597C

Designing with a Microcontroller (v6)

Embedded Systems. Interfacing PIC with external devices Analog to digital Converter. Eng. Anis Nazer Second Semester

Design of Low Cost Embedded Power Plant Relay Testing Unit

Course Introduction. Content 20 pages 3 questions. Learning Time 30 minutes

ET301 GPS-UAV Development Platform. Part 3: Development suggestions

Timer A (0 and 1) and PWM EE3376

Microcontroller Based Inductance Capacitance Meter

PIC16F818/819. PIC16F818/819 Rev. A4 Silicon Errata Sheet. 2. Module: PORTB FIGURE 1: 1. Module: Internal RC Oscillator

Section 30. Capture/Compare/PWM/Timer (MCCP and SCCP)

Section 35. Output Compare with Dedicated Timer

GROAN DETECTOR SYSTEM

A Ballistic Chronograph

Introduction to Using the PIC16F877 Justin Rice IMDL Spring 2002

PICmicro MCU APPLICATION DESIGN AND HARDWARE INTERFACING

PIC16F818/819. PIC16F818/819 Rev. B0 Silicon Errata Sheet

2015 Technological Studies. Advanced Higher. Finalised Marking Instructions

Final Project Report E3390 Electronic Circuits Design Lab. RFID Access Control System. Jeffrey Mok Joseph Kim

Department of Mechanical and Industrial Engineering MECH 471 MICROCONTROLLERS FOR MECHATRONICS. Laboratory Specialist

Three-Stage Coil Gun

The rangefinder can be configured using an I2C machine interface. Settings control the

Grundlagen Microcontroller Counter/Timer. Günther Gridling Bettina Weiss

AN654. PWM, a Software Solution for the PIC16CXXX METHODS INTRODUCTION


Capture/Compare/PWM/Timer (MCCP and SCCP)

Sound-Seeking Robot. Abstract. An E155 Design Project at Harvey Mudd College (Claremont, California) December 15, 2003 Alex Utter Chris Wottawa

Solar Mailbox project. Pictures of the Solar Mailbox

Electromechanical Timer Replacement Solutions Cubed Real-Time Clock

FM Tuner Controller for Portable and Car Radios

PIC16F Pin, 8-Bit CMOS FLASH Microcontroller. Devices Included in this Data Sheet: Pin Diagram. Microcontroller Core Features:

DESIGNING A POSITION REGULATOR FOR AN ACTUATOR POWERED BY A CONTINUOUS CURRENT MOTOR USING THE PIC16F73 MICROCONTROLLER

PIC16C717/770/ /20-Pin, 8-Bit CMOS Microcontrollers with 10/12-Bit A/D. Microcontroller Core Features: Pin Diagram. Peripheral Features:

Chapter 6 PROGRAMMING THE TIMERS

Binary Outputs: LEDs

PIC16C925/ /68-Pin CMOS Microcontrollers with LCD Driver. High Performance RISC CPU: Analog Features: Special Microcontroller Features:

AN840. PIC16F7X/PIC16C7X Peripherals Configuration and Integration INTRODUCTION A/D MODULE CONVERSION BLOCK DIAGRAM

PIC12F752/HV Pin Flash-Based, 8-Bit CMOS Microcontrollers. Peripheral Features. High-Performance RISC CPU. Microcontroller Features

PIC16C65A. PIC16C65A Rev. A Silicon Errata Sheet. 2. Module: CCP (Compare Mode) 1. Module: CCP (Compare Mode) SWITCHING

K7QO Marker Generator

EIE/ENE 334 Microprocessors

MICROCONTROLLER TUTORIAL II TIMERS

Section 22. Basic 8-bit A/D Converter

Application Note Temperature Sensor IC

PIC12F529T39A. 14-Pin, 8-Bit Flash Microcontroller. High-Performance RISC CPU. Low-Power Features/CMOS Technology. Special Microcontroller Features

ATmega16A Microcontroller

rfpic12f675 FLASH-Based Microcontroller with ASK/FSK Transmitter High Performance RISC CPU: Pin Diagram: UHF ASK/FSK Transmitter: Peripheral Features:

PIC16F627A/628A/648A Data Sheet

Hardware Flags. and the RTI system. Microcomputer Architecture and Interfacing Colorado School of Mines Professor William Hoff

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

8-bit Microcontroller with 512/1024 Bytes In-System Programmable Flash. ATtiny4/5/9/10

MICROBOARD ADVANCED MANUAL

Lab 5 Timer Module PWM ReadMeFirst

PIC PWM. Robert Ralston KJ6HFR September 2013

Timer 0 Modes of Operation. Normal Mode Clear Timer on Compare Match (CTC) Fast PWM Mode Phase Corrected PWM Mode

Stepper Motors & Look Up Table

ME 333 Assignment 7 and 8 PI Control of LED/Phototransistor Pair. Overview

Development of a Matlab-Based Graphical User Interface Environment for PIC Microcontroller Projects

8-Bit CMOS Microcontrollers. PIC16C6X Features A R62 63 R A R A R Program Memory 1K 2K 2K 4K 2K 2K 4K 4K 8K 8K

Section 3. Reset HIGHLIGHTS. Reset. This section of the manual contains the following major topics:

ECED3204: Microprocessor Part IV--Timer Function

Transcription:

Timers and CCP Modules Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.twcgu

PIC18 Timers Timer2, Timer4 8-bit timers use instruction cycle clock as the clock source Timer0, Timer1, Timer3 16-bit timers also use external clock input as the clock source When a timer rolls over, an interrupt may be generated if it is enabled. 2

Timer0 8-bit or 16-bit timer or counter Clock signal: internal instruction cycle clock or T0CKI Divide the clock signal by a prescaler 3

T0CON register 4

Timer0 can operate as a timer or as a counter When the clock source is the instruction cycle clock, it operates as a timer. When the clock source is the T0CKI pin, it operates as a counter. 5

Example: Write a subroutine to create a time delay that is equal to 100 ms times the contents of the PRODL register assuming that the crystal oscillator is running at 32 MHz. delay movlw 0x83 ; enable TMR0, select internal clock, movwf T0CON,A ; set prescaler to 16 loopd movlw 0x3C ; load 15535 into TMR0 so that it will movwf TMR0H,A ; roll over in 50000 clock cycles movlw 0xAF ; " movwf TMR0L,A ; " bcf INTCON,TMR0IF,A ; clear the TMR0IF flag wait btfss INTCON,TMR0IF,A ; bra wait ; wait until 100 ms is over decfsz PRODL,F,A, bra loopd return 6

Timer1 Timer1 can be reset when the CCP module is configured to compare mode to generate a special event trigger. Configured to oscillator through T1OSO and T1OSI pins 7

T1CON register 8

Example: Use Timer0 as a timer to create a 1-s delay and use Timer1 as a counter to count the rising edges of an unknown signal (at the T1CKI pin). (PIC18 MCU is running with a 32 MHz crystal oscillator) #include <p18f8680.inc> t1ov_cnt set 0x00 ; Timer1 rollover interrupt count freq set 0x01 ; to save the contents of Timer1 at the end org 0x00 goto start org 0x08 ; high priority interrupt service routine btfss PIR1,TMR1IF,A ; skip if Timer1 roll-over interrupt retfie ; return if not Timer1 interrupt bcf PIR1,TMR1IF,A ; clear the interrupt flag incf t1ov_cnt,f,a ; increment Timer1 roll-over count retfie org 018 0x18 ; dummy low priority it interrupt t service routine retfie start clrf t1ov_cnt,a ; initialize Timer1 overflow cnt to 0 clrf freq,a ; initialize frequency to 0 clrf freq+1,a ; " clrf TMR1H ; initialize Timer1 to 0 clrf TMR1L ; " clrf PIR1 ; clear all interrupt flags bsf RCON,IPEN,A ; enable priority interrupt 9

Use Timer0 as a timer to create a 1-s delay and use Timer1 as a counter to count the rising edges of an unknown signal (Cont.) forever movlw 0x01 ; set TMR1 interrupt to high priority movwf IPR1,A ; " movwf PIE1,A ; enable Timer1 roll-over interrupt movlw 0x87 ; enable Timer1, select external clock, set movwf T1CON,A ; prescaler to 1, disable crystal oscillator movlw 0xC0 ; enable global and peripheral interrupt movwf INTCON,A ; " movlw 0x0A movwf PRODL,A ; prepare to call delay to wait for 1 second call delay ; Timer1 overflow interrupt occur in this second movff TMR1L,freq ; save frequency low byte movff TMR1H,freq+1 1 ; save frequency high h byte bcf INTCON,GIE,A ; disable global interrupt nop bra forever end 10

Timer2 To the synchronous serial port module 8-bit timer 11

T2CON register 12

Example: Write an instruction sequence to generate periodic interrupts every 8 ms with high priority using Timer2 (32-MHz PIC18F8680). movlw D 249 ; load 249 into PR2 movwf PR2,A ; bsf RCON,IPEN,A ; enable priority interrupt bsf IPR1,TMR2IP,A ; place TMR2 interrupt at high priority bcf PIR1,TMR2IF,A ; movlw 0xC0 movwf INTCON,A ; enable global interrupt movlw 0x7E ; enable TMR2, set prescaler to 16, set movwf T2CON,A ; postscaler to 16 bsf PIE1,TMR2IE,A, ; enable TMR2 overflow interrupt 13

Timer3 Similar to Timer1 Use either internal (instruction cycle clock) or external signal as the clock source. 14

T3CON register 15

Timer4 Only available to the PIC18F8X2X and PIC6X2X devices. The contents of T4CON are identical to those of T2CON. 16

PIC18 CCP (capture, compare, and pulse width modulation) modules Capture operation Copy the contents of a timer into a capture register Compare operation Compares the contents of a CCPR register with that of Timer1 (or Timer3) in every clock cycle When they are equal, the associated pin may be pulled to high, or low, or toggled. PWM mode Generate a waveform with certain frequency and duty cycle (Timer2 or Timer4) 17

CCPxCON register 18

CPP in capture mode Capture event arrival time An event is represented by a signal edge: every falling edge every e rising edge every 4 th rising edge every 16 th rising edge 19

Example: Period measurement. Use the CCP channel 1 in capture mode to measure the period of an unknown signal assuming that the PIC18 MCU is running with a 16 MHz crystal oscillator #include <p18f8720.inc> org 0x00 goto start org 0x08 retfie org 0x18 retfie start bsf TRISC,CCP1,A ; configure CCP1 pin for input movlw 0x81 ; use Timer1 as the time base movwf T3CON,A ; of CCP1 capture bcf PIE1,CCP1IE,A ; disable CCP1 capture interrupt movlw 0x81 ; enable Timer1, prescaler set to 1, movwf T1CON,A ; 16-bit, use instruction cycle clock movlw 0x05 ; set CCP1 to capture on every rising edge movwf CCP1CON,A ; " bcf PIR1,CCP1IF,A ; clear the CCP1IF flag 20

Example: Period measurement (cont.) edge1 btfss PIR1,CCP1IF,A ; wait for the first edge to arrive bra edge1 ; " movff CCPR1H,PRODH ; save the first edge movff CCPR1L,PRODL ; bcf PIR1,CCP1IF,A ; clear the CCP1IF flag edge2 btfss PIR1,CCP1IF,A ; wait for the second edge to arrive bra edge2 ; " clrf CCP1CON ; disable CCP1 capture movf PRODL,W,A subwf CCPR1L,W,A, ; subtract first edge from 2nd edge movwf PRODL,A ; and leave the period in PRODH:PRODL movf PRODH,W,A ; " subwfb CCPR1H,W,A, ; " movwf PRODH,A ; " forever goto forever ; end 21

CPP in compare mode When CCPRx register and TMR1 (or TMR3) match, one of the following actions may occur on the CCPx pin: driven high driven low toggle output remains unchanged 22

Example: Use CCP1 to generate a periodic waveform with 40% duty cycle and 1 KHz frequency assuming that the instruction cycle clock frequency is 4 MHz. 23

Example: Use CCP1 to generate a periodic waveform with 40% duty cycle and 1 KHz frequency (cont.) #include <p18f8720.inc> hi_hi equ 0x06 ; number (1600) of clock cycles that signal hi_lo equ 0x40 ; is high lo_hi equ 0x09 ; number (2400) of clock cycles that signal lo_lo equ 0x60 ;islow org 0x00 goto start start bcf TRISC,CCP1 ; configure CCP1 pin for output movlw 0xC9 ; enable 16-bit Timer3, prescaler 1:1 movwf T3CON ; bcf PIR1,CCP1IF movlw 0x09 ; CCP1 pin set high initially and movwf CCP1CON ; pull low on match ; CCPR1 TMR3 + 1600 ; start a new compare operation movlw hi_lo ; addwf TMR3L,W ; movwf CCPR1L ; movlw hi_hi ; addwfc TMR3H,W ; movwf CCPR1H ; 24

Example: Use CCP1 to generate a periodic waveform with 40% duty cycle and 1 KHz frequency (cont.) bcf PIR1,CCP1IF hi_time btfss PIR1,CCP1IF ; wait until CCPR1 matches TMR3 bra hi_time ; bcf PIR1,CCP1IF movlw 0x08 ; CCP1 pin set low initially and movwf CCP1CON ; pull high on match ; CCPR1 CCPR1 + 2400 ; start another compare operation movlw lo_lo ; addwf CCPR1L,F ; movlw lo_hi ; addwfc CCPR1H,F ; lo_time btfss PIR1,CCP1IF ; wait until CCPR1 matches TMR3 bra lo_time ; bcf PIR1,CCP1IF movlw 0x09 ; CCP1 pin set high initially and movwf CCP1CON ; pull low on match movlw hi_lo ; start another new compare operation addwf CCPR1L,F ; movlw hi_hi ; addwfc CCPR1H,F ; bra hi_time end 25

CPP in PWM mode PWM period = [(PRy) + 1] 4 TOSC (TMRy prescale factor) PWM duty cycle = (CCPRxL:CCPxCON<5:4>) TOSC (TMRy prescale factor) 26

Example: Configure CCP1 in PWM mode to generate a digital waveform with 40% duty cycle and 10 KHz frequency assuming that the PIC18 MCU is running with a 32 MHz crystal oscillator. movlw 0xC7 ; set period value to 199 movwf PR2,A ; movlw 0x50 ; set duty cycle value to 80 movwf CCPR1L,A ; movlw 0x00 ; movwf CCPR1H,A ; bcf TRISC,CCP1,A ; configure CCP1 pin for output movlw 0x81 ; enable Timer3 in 16-bit mode and use movwf T3CON,A ; Timer2 as time base for PWM1 thru PWM5 clrf TMR2,A ; force TMR2 to count from 0 movlw 0x05 ; enable Timer2 and set its prescaler to 4 movwf T2CON,A ; movlw 0x0C ; enable CCP1 PWM mode movwf CCP1CON,A ; Period register value is PR2 = 32 10 6 4 4 10 4 1 = 199 Duty cycle value is CCPR1L = 200 40% = 80 27

Reference Han-Way Huang, PIC Microcontroller: An Introduction to Software and Hardware Interfacing, Thomson Delmar Learning, 2005. 28