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

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

Section 14. Compare/Capture/PWM (CCP)

ETEC Digital Controls PIC Lab 10 Pulse Width Modulation

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

Section 8. Interrupts

Using The PIC I/O Ports

PIC Programming in Assembly. (

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

PIC16F627A/628A/648A Data Sheet

PIC16F627A/628A/648A Data Sheet

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

10-bit Σ ADC from a PIC16F84

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

PIC12F629/675 Data Sheet

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

Section bit A/D Converter

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:

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

Section 29. Instruction Set

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

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

PIC16F87/88 Data Sheet

Section bit A/D Converter

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

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

Pololu P-Star 25K50 Micro User s

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

PIC12F752/HV752 Data Sheet

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

Timer A (0 and 1) and PWM EE3376

Specification: 1 Specification

PIC16F87XA Data Sheet

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

Lab P442 LABORATORY MANUAL page 1

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

I/O Pins. 10-bit A/D (ch) EEPROM (bytes) PIC16F N/A 1 Y 2 Y 2/1 PIC16F Y 2 Y 2/1

PIC12F629/675 Data Sheet

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

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

PIC16F882/883/884/886/887

PIC16F630/676 Data Sheet

Microtronics technologies Mobile:

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

Section 9. I/O Ports

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

How To Program A Microcontroller With Memory On A Microchip Microcontroller

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

Using Arduino Microcontrollers to Sense DC Motor Speed and Position

CoE3DJ4 Digital Systems Design. Chapter 4: Timer operation

1/22/16. You Tube Video. Definitions. Duty Cycle: on-time per period (specified in per cent)

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

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

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

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

Accurate Measurement of the Mains Electricity Frequency

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

Section 28. In-Circuit Serial Programming (ICSP )

Analog-to-Digital Converters

Digital Systems Based on Principles and Applications of Electrical Engineering/Rizzoni (McGraw Hill

PWM IN AVR. Developed by: Krishna Nand Gupta Prashant Agrawal Mayur Agarwal

Experiment # 9. Clock generator circuits & Counters. Eng. Waleed Y. Mousa

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

NTE2053 Integrated Circuit 8 Bit MPU Compatible A/D Converter

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

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

Computer Organization and Components

Application Note AN-1187

8254 PROGRAMMABLE INTERVAL TIMER

Lab 4: Pulse Width Modulation and Introduction to Simple Virtual Worlds (PWM) (PWM)

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

PIC12F508/509/16F505 Data Sheet

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

DP8570A DP8570A Timer Clock Peripheral (TCP)

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

AC : PRACTICAL DESIGN PROJECTS UTILIZING COMPLEX PROGRAMMABLE LOGIC DEVICES (CPLD)

INTERRUPTS. There are 2 methods for communicating between the microcontroller and the external system: POLLING INTERRUPTS

Serial Communications

ARM Thumb Microcontrollers. Application Note. Software ISO 7816 I/O Line Implementation. Features. Introduction

A simple RF/Microwave frequency counter

Flexible Active Shutter Control Interface using the MC1323x

RENESAS TECHNICAL UPDATE

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

HT46R14A Single Phase AC Induction Motor Frequency Converter Application

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

Pulse Width Modulation (PWM) LED Dimmer Circuit. Using a 555 Timer Chip

Embedded C Programming

Display Board Pulse Width Modulation (PWM) Power/Speed Controller Module

Data Acquisition Module with I2C interface «I2C-FLEXEL» User s Guide

Ultrasonic Distance Measurement Module

Small Hardware Development and Prototyping Board for the SX28

Develop a Dallas 1-Wire Master Using the Z8F1680 Series of MCUs

Key Words Student Paper, School of Professional Studies

DEVELOPMENT OF A MICROCONTROLED IRRADIATION SYSTEM BASED ON LIGHT-EMITTING DIODES (LEDs) MATRIXES FOR PHOTOTHERAPY APPLICATIONS

Implementing SPI Master and Slave Functionality Using the Z8 Encore! F083A

74LS193 Synchronous 4-Bit Binary Counter with Dual Clock

An Introduction to MPLAB Integrated Development Environment

c0003 A Simple PIC Application CHAPTER 3

Pulse Width Modulation

AN3252 Application note

Transcription:

Capture/Compare/Pulse Width Modulation (CCP) Programming Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan chanhl@mail.cgu.edu.twcgu

CCP1 control register 2

CCP high and low registers CCP pins 3

Compare mode Match occurs when the content of Timer1 (or Timer3) is equal to CCPR1H:CCPR1L. CCP1 pin perform one of the following actions Toggle the CCP1 pin Drive high the CCP1 pin Drive low the CCP1 pin Remain unaffected but generate software interrupt Trigger a special event with a hardware interrupt and clear the timer 4

Compare mode operation 5

PIR1 (Peripheral interrupt flag register1) containing CCP1IF flag 6

Timer1 Set interrupt flag TMR1IF on overflow CCP Special Event Trigger TMR1H TMR1L TMR1ON T1SYNC T1OSO/T1CKI T1OSCEN T1OSI Fosc/4 T0PS1:T0PS0 TMR1CS 7

Timer3 block diagram Set interrupt flag TMR3IF on overflow CCP Special Trigger Fosc/4 T3CKPS1:T3CKPS0 8

T3CON (Timer 3 control) register 9

A 1-Hz pulse is fed into Timer3 pin. Toggle LED every 10 pulses using CCP Compare mode MOVLW 0x02 ; Compare mode, toggle on match MOVWF CCP1CON MOVLW 0x42 ; Timer3 for compare mode, no prescale MOVWF T3CON BCF TRISC, CCP1 ; CCP1 pin as output BSF TRISC, TCKI ; T3 CLK pin as input MOVLW D 10 MOVWF CCPR1L MOVLW 0x0 MOVWF CCPR1H OVER CLRF TMR3H CLRF TMR3L BCF PIR1, CCP1IF BSF T3CON, TMR3ON ; start Timer3 B1 BTFSS PIR1, CCP1IF BRA B1 BCF T3CON, TMR3ON ; stop Timer3 GOTO OVER 10

T1CON (Timer 1 Control) Register 11

Generate a square wave with a 40 ms period and 50% duty cycle on CCP1 pin if XTAL = 10 MHz MOVLW 0x02 ; Compare mode, toggle on match MOVWF CCP1CON MOVLW 0x0 ; Use Timer1 for compare mode MOVWF T3CON MOVLW 00 0x0 ; Timer1, internal clock, no prescale MOVWF T1CON BCF TRISC, CCP1 ; CCP1 pin as output MOVLW MOVWF MOVLW 0C3 0xC3 CCPR1H 0x50 ; 20 ms / 0.4 s = 50,000 = 0xC350 MOVWF CCPR1L OVER CLRF TMR1H CLRF TMR1L BCF PIR1, CCP1IF BSF T1CON, TMR1ON ; start Timer1 B1 BTFSS PIR1, CCP1IF ; monitor Timer1 flag BRA BCF GOTO B1 T1CON, TMR1ON ; stop Timer1 OVER 12

Capture mode An event at CCP pin cause the content of Timer1 (or Timer3) be loaded into CCPR1H:CCPR1L every falling-edge pulse every rising-edge pulse every 4 th rising-edge pulse every 16 th rising-edge pulse 13

Measure the period of a pulse 14

Measure the period of a pulse (cont.) MOVLW 0x05 ; Capture mode, rising edge MOVWF CCP1CON MOVLW 0x0 ; Timer1 for capture MOVWF T3CON MOVLW 0x0 ; Timer1, internal clock, no prescale MOVWF T1CON CLRF TRISB CLRF TRISD BSF TRISC, CCP1 ; CCP1 pin as input 15

Measure the period of a pulse (cont.) OVER CLRF TMR1H CLRF TMR1L BCF PIR1, CCP1IF RE_1 BTFSS PIR1, CCP1IF BRA RE_1 BSF T1CON, TM13ON ; start Timer1 RE_2 BTFSS PIR1, CCP1IF BRA RE_2 BCF T1CON, TM13ON ; stop Timer1 MOVFF TMR1L, PORTB MOVFF TMR1H, PORTD GOTO OVER 16

Measure pulse width Pulse-width-modulated (PWM) output A fixed frequency and variable duty cycle MAX6666/6667 temperature sensors from Maxim Corp. Output is a square wave with a frequency of 35 Hz at 25 0 C Temperature ( 0 C) = 235 (400 x t1) / t2 17

Measure pulse width (cont.) FLAG EQU 0x10 ; flag register DISP EQU 0x0 ; flag for capture complete RF EQU 0x1 ; flag for rising or falling edge ORG 0x0000 GOTO MAIN ORG 0x0008 BTFSC PIR1, CCP1IF ; Is it CCP1 interrupt? GOTO CCP_ISR RETFIE MAIN MOVLW 0x05 ; Capture mode, rising edge MOVWF CCP1CON MOVLW 0x0 ; Timer1 for capture MOVWF T3CON MOVLW 0x0 ; Timer1, internal clock, no prescale MOVWF T1CON 18

Measure the period of a pulse (cont.) CLRF TRISB CLRF TRISD BSF TRISC, CCP1 ; CCP1 pin as input BSF PIE1, CCP1IE ; enable CCP1 interrupt BSF INTCON, PEIE ; enable peripheral interrupt BSF INTCON, GIE ; enable global interrupt BCF PIR1, CCP1IF OVER CLRF TMR1H CLRF TMR1L WAIT BTFSS FLAG, DISP ; Is capture complete? BRA WAIT BCF FLAG, DISP ; clear DISP flag for next capture MOVLW 0x03 SUBWF TMR1L, F ; subtract the overhead MOVFF MOVFF GOTO TMR1L, PORTB TMR1H, PORTD OVER 19

Measure the period of a pulse (cont.) CCP_ISR BTFSS FLAG, RF ; Is it rising edge GOTO RISE_ISR GOTO FALL_ISR RISE_ISR ISR BSF T1CON, TM13ON ; start Timer1 BSF FLAG, RF ; ready for falling edge BCF CCP1CON, 0 ; detect falling edge BCF PIR1, CCP1IF ; clear interrupt RETFIE FALL_ ISR BCF T1CON, TM1ON ; stop Timer1 BSF FLAG, DISP ; capture complete BCF FLAG, RF ; ready for rising edge BSF CCP1CON, 0 ; detect rising edge BCF PIR1, CCP1IF ; clear interrupt RETFIE 20

PWM mode Create pulses with variable widths DC motor control CCP module uses Timer2 21

PWM mode operation 22

Set the period and duty cycle of PWM Period of PWM Tpwm = (PR2 + 1) x 4 x Tosc x N (N: prescaler of 1, 4, or 16) Duty cycle of PWM DC1B1 DC1B0 Decimal points 0 0 0 0 1 0.25 1 0 0.5 1 1 0.75 Ex1. PR2 = 50 and 20% duty cycle 50*20%= 10 8-bit CCPR1L = 10 and 2-bit of DC1B1:0=00 00001010.00 Ex1. PR2 = 50 and 25% duty cycle 50*25%= 12.5 8-bit CCPR1L = 12 and 2-bit of DC1B1:0=00 00001100.10 23

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

T2CON (Timer2 Control) register 25

Create a 2.5 khz PWM with a 75% duty cycle CLRF CCP1CON MOVLW D 249 MOVWF PR2 MOVLW D 186 ; 75% duty cycle MOVWF CCPR1L BCF TRISC, CCP1 ; make PWM pin an output MOVLW 0x01 ; Timer2, 4 prescale, no postscaler MOVWF T2CON MOVLW 0x3C ; PWM mode, 11 for DC1B1:B0 MOVWF CCP1CON CLRF TMR2 BSF T2CON, TMR2ON ; start Timer2 AGAIN BCF PIR1, TMR2IF ; clear Timer2 flag OVER BTFSS PIR1, TMR2IF ; wait for the end of period BRA OVER GOTO AGAIN 26

Reference M.A. Mazidi, R.D. Mckinlay, D Causey, PIC Microcontroller and Embedded Systems Using Assembly and C for PIC18, Pearson Education Inc., 2008. 27