Using Timers of Microchip PIC18F Microcontrollers

Similar documents
Section 13. Timer0 HIGHLIGHTS. Timer0. This section of the manual contains the following major topics:

Using the Digital I/O interface of STMicroelectronics STM32 Microcontrollers

Introduction to L.A.P. 1

CENG-336 Introduction to Embedded Systems Development. Timers

The I2C BUS Interface

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

Interrupts on PIC18F252 Part 2. Interrupts Programming in C Language

Using the Special Function Registers of the Digital I/O interface of STM32

Interrupts on PIC18F252 Part 2

Embedded Systems. PIC16F84A Internal Architecture. Eng. Anis Nazer First Semester

EE6008-Microcontroller Based System Design Department Of EEE/ DCE

The Analog to Digital Converter (ADC)

Using the Special Function Registers of the Digital I/O interface of STM32

Microprocessors B (17.384) Spring Lecture Outline

Additional Timer Functionalities. Capture, Compare, PWM

8051 Peripherals. On-Chip Memory Timers Serial Port Interrupts. Computer Engineering Timers

Section 11. Timer0. Timer0 HIGHLIGHTS. This section of the manual contains the following major topics:

Handling Asynchronous Events in MCUs A Light Keyboard Driver

The University of Texas at Arlington Lecture 21_Review

The Serial Peripheral Interface

Accurate Time and Interrupts

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING. BEng(Hons) Electrical and Electronics Engineering SEMESTER 1 EXAMINATION 2016/2017

PIC Discussion By Eng. Tamar Jomaa

Embedded systems. Exercise session 3. Microcontroller Programming Lab Preparation

By the end of Class. Outline. Homework 5. C8051F020 Block Diagram (pg 18) Pseudo-code for Lab 1-2 due as part of prelab

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Lesson-3: Counters and Timers

Section 14. Timer1 HIGHLIGHTS. Timer1. This section of the manual contains the following major topics:

The MCU s Pulse. Internal clock or oscillator to synchronize operation. One clock cycle = 1 TOSC = 1/fOSC. t TOSC

Human Response Timer

8051 Microcontroller

The Controller Area Network (CAN) Interface

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

ME 6405 Introduction to Mechatronics

Lecture 9. Timer Operations and Programming

Embedded Systems Lab 2 - Introduction to interrupts

Basic Embedded Software C Review Using MPLAB SIM. Loops. Embedded Systems Interfacing. 25 August 2011

Laboratory 9. Programming a PIC Microcontroller - Part I

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING B.ENG (HONS) ELECTRICAL AND ELECTRONIC ENGINEERING EXAMINATION SEMESTER /2016

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

PIC18F452 Timer adjustment

UNIVERSITY OF BOLTON SCHOOL OF ENGINEERING MSC SYSTEMS ENGINEERING AND ENGINEERING MANAGEMENT SEMESTER 2 EXAMINATION 2016/2017

PIC32&Overview& E155&

n/a PIC12F629-I/P (RC) n/a PIC12F629-I/SN (RC) n/a PIC12F675-I/P (RC) n/a PIC12F675-I/SN MICROCONTROLLER (RC)

which means that writing to a port implies that the port pins are first read, then this value is modified and then written to the port data latch.

Speed Control of a DC Motor using Digital Control

evsjv `k cigvyy kw³ Kwgkb BANGLADESH ATOMIC ENERGY COMMISSION

Fujitsu Microelectronics Europe Application Note MCU-AN E-V10 F²MC-FR FAMILY 32-BIT MICROCONTROLLER MB91460 RELOAD TIMER APPLICATION NOTE

Timer1 Capture Mode:

Embedded Systems Design (630470) Lecture 4. Memory Organization. Prof. Kasim M. Al-Aubidy Computer Eng. Dept.

Embedded Systems Lab Lab 1 Introduction to Microcontrollers Eng. Dalia A. Awad

Interrupts, timers and counters

ME 515 Mechatronics. A microprocessor

dspic Interrupts The Interrupt Control and Staus Registers are :- ENG721-S2 Mixed Signal Processing : Hassan Parchizadeh Page 1

Timer programming

Capture Mode of Pic18F252

ECE 354 Introduction to Lab 2. February 23 rd, 2003

Computer Hardware Requirements for ERTSs: Microprocessors & Microcontrollers

Timer2 Interrupts. NDSU Timer2 Interrupts September 20, Background:

Lecture (03) PIC16F84 (2)

/*Algorithm: This code display a centrifuge with five variable speed RPM by increaseing */

Interrupts. Embedded Systems Interfacing. 08 September 2011

Analog Output with a Digital to Analog Converter

8051 Microcontrollers

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

Embedded System Design

Embedded programming, AVR intro

8051 microcontrollers

PIC18F452 Timer adjustment

Mod-5: PIC 18 Introduction 1. Module 5

ECE Homework #10

Learning Objectives:

DERTS Design Requirements (1): Microcontroller Architecture & Programming

Chapter 11: Interrupt On Change

NH-67, TRICHY MAIN ROAD, PULIYUR, C.F , KARUR DT. DEPARTMENT OF ELECTRONICS AND COMMUNICATION ENGINEERING COURSE MATERIAL

Lecture (04) PIC 16F84A programming I

EE 361L Digital Systems and Computer Design Laboratory

AVR Timers TIMER0. Based on:

Department of EIE / Pondicherry Engineering College. Timer/Counters. Department of EIE / Pondicherry Engineering College 1

EEE394 Microprocessor and Microcontroller Laboratory Lab #6

e-pg Pathshala Subject: Computer Science Paper: Embedded System Module: Interrupt Programming in Embedded C Module No: CS/ES/20 Quadrant 1 e-text

Measuring Duty Cycles with an Intel MCS-51 Microcontroller

Section 9. Watchdog Timer (WDT) and Power-Saving Modes

General Purpose Programmable Peripheral Devices. Assistant Professor, EC Dept., Sankalchand Patel College of Engg.,Visnagar

CPEG300 Embedded System Design. Lecture 8 Timer

Microcontroller systems Lec 2 PIC18LF8722 Microcontroller s s core

BENCHMARK ST72 vs. PIC16

PIC16C84. 8-bit CMOS EEPROM Microcontroller PIC16C84. Pin Diagram. High Performance RISC CPU Features: CMOS Technology: Peripheral Features:

AVR XMEGA Product Line Introduction AVR XMEGA TM. Product Introduction.

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: 8051 Architecture Module No: CS/ES/5 Quadrant 1 e-text

Course Introduction. 2009, Renesas Technology America, Inc., All Rights Reserved

8051 Microcontroller

The MICROPROCESSOR PRINCIPLES AND APPLICATIONS Lab 7

Chapter 13. PIC Family Microcontroller

Hong Kong Institute of Vocational Education Digital Electronics & Microcontroller. 8. Microcontroller

Chapter 6 PROGRAMMING THE TIMERS

More Fun with Timer Interrupts

PIC Microcontroller Introduction

Explanation of PIC 16F84A processor data sheet Part 2: More on the PIC

Transcription:

Using Timers of Microchip PIC18F Microcontrollers ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Università di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course

What is a Timer? It is a circuit to let a software have the knowledge of flow of time It is a composed of: A clock source; usually the system clock or an external signal; A programmable frequency divisor, called prescaler, to divide clock source frequency, if needed; Some SFRs which hold a 8-, 16- or 32-bit value that is incremented in hardware using the clock source. Some SFRs which give some state information, e.g overflow (zero crossing). PIC18F family has 7 timers, called TIMER0, TIMER1,..., TIMER5, TIMER6 Each timer has different characteristics and may be used together with other peripherals.

The TIMER0 of PIC18 TIMER0 is a 8/16 bit timer/counter (figure shows the 8bit mode); TMR0L is the SFR containing the value that is incremented; All the parts to the left are the clock source circuits. T0CON (Timer 0 Control) register is used to program the timer, and includes the bits shown in figure (T0CS, PSA, T0PS, etc.)

The 16-bit version of TIMER0 In 16-bit mode, two SFR are used TMR0L and TMR0H; In write operations,tmr0h must be written beforetmr0l; In read operations, TMR0L must be read before TMR0H; However, XC8 offers a single 16-bit variable TMR0 which includes both low and high part of TMR0.

The 16-bit version of TIMER0 The T0CON (Timer 0 Control) SFR includes all the bits which control TIMER0 functioning.

TIMER0: Selecting clock source Clock source can be internal or external and is controlled by bit T0CS: T0CS = 0; clock source is internal and is taken from F osc /4. T0CS = 1; clock source is external and is taken from T0CKI pin; in this case T0SE controls the edge of the signal which triggers increment.

TIMER0: Dividing clock frequency In some cases, the clock coming from the oscillator could be too fast for our applications: we can lower it by using the frequency prescaler. The prescaler is a circuit which divides the signal frequency by 2, 4, 8, 16,..., 256. The prescaler is activated by bit PSA: PSA = 0; prescaler is selected, frequency division is controlled by bits T0PS. PSA = 1; prescaler is not selected.

TIMER0: Dividing clock frequency When the prescaler is activated (PSA = 0), division is performed as: T0PS = 111, division 1:256 T0PS = 110, division 1:128 T0PS = 101, division 1:64... T0PS = 000, division 1:2

TIMER0: controlling depth and on/off Finally, T0CON includes these other two bits: TMR0ON, turns on/off the timer; T08BIT, selects 8 (value 1 ) or 16 (value 0 ) bit mode.

A case-study: a timer to flash a LED We want to use the system clock,t0cs = 0; In our board, we have F OSC = 64MHz, therefore the basic frequency is F OSC /4 = 16MHz, the P = 62.5ns; Let s use the prescaler and divide the frequency by 256, so PSA = 0; T0PS = 0b111; The timer increments using a period P = 62.5ns 256 = 16µs.

A case-study: a timer to flash a LED... the timer increments using a period P = 62.5ns 256 = 16µs. Let us suppose we want a period of half a second 500ms Therefore 500 10 3 16 10 6 = 31250 A delay of 500ms implies 31250 counts

A case-study: a timer to flash a LED int main(void) { TRISBbits.TRISB0 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 ( 0b is a prefix for binary) TMR0 = 0; // clear timer value T0CONbits.TMR0ON = 1; // start the timer for (;;) { unsigned int t; t = TMR0; if (t >= 31250) { // equivalent of 500 ms TMR0 = 0; LATBbits.LATB0 =!LATBbits.LATB0;

Case-study 2: more LEDs flashing Let us suppose we want to: flash led in RB0 at a period of 500 ms flash led in RB1 at a period of 750 ms Do we need two timers?? NO! 1 compute the greatest common divisor, which is 250ms 2 use it as your timer period 3 toggle RB0 after two periods 4 toggle RB1 after three periods

Case-study 2: more LEDs flashing Using the same set-up of the previous example, since our period is 250ms we have 250 10 3 16 10 6 = 15625 A delay of 250ms implies 15625 counts

Case-study 2: more LEDs flashing int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 ( 0b is a prefix for binary) TMR0 = 0; // clear timer value T0CONbits.TMR0ON = 1; // start the timer for (;;) { unsigned int t; t = TMR0; if (t >= 15625) { // equivalent of 250 ms TMR0 = 0; ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 =!LATBbits.LATB0; c0 = 0; if (c1 == 3) { // flash led 1 LATBbits.LATB1 =!LATBbits.LATB1; c1 = 0;

Timer Overflow In our examples, we check the timer value and, after reaching a certain maximum, we clear it However, what does it happen if we don t modify TMR0? At a certain point, the TMR0 reaches its maximum possible value, which is 255 (0xff) at 8 bit and 65535 (0xffff) at 16 bit The next increment will overflow TMR0, which thus goes to zero This event is signalled by the hardware by setting a proper bit in a SFR The bit is called T0IF and belongs to register INTCON The bit set by the hardware and cleared by software

Timer Overflow We can exploit the overflow event as follows. Instead of clearing TMR0 and waiting for reaching our MAX (15625 in the example), we can: Set TMR0 to 65536 MAX ( 65536 15625 = 49911 in our example) Wait for overflow by checking T0IF Clear T0IF

Case-study 2: LED flashing with overflow int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 ( 0b is a prefix for binary) TMR0 = 49911; // initial timer value INTCONbits.T0IF = 0; // clear the overflow bit initially T0CONbits.TMR0ON = 1; // start the timer for (;;) { if (INTCONbits.T0IF == 1) { // overflow! TMR0 = 49911; // reload timer INTCONbits.T0IF = 0; // clear overflow ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 =!LATBbits.LATB0; c0 = 0; if (c1 == 3) { // flash led 1 LATBbits.LATB1 =!LATBbits.LATB1; c1 = 0;

Timer Overflow Let s consider the expression: 65536 MAX : We notice that 65536, in 16-bit arithmetic, does not exist and is equivalent to 0 therefore, 65536 MAX = MAX

Case-study 2: LED flashing with overflow int main(void) { char c0 = 0, c1 = 0; // why char? because they are 8 bits TRISBbits.TRISB0 = 0; // output TRISBbits.TRISB1 = 0; // output T0CONbits.TMR0ON = 0; // stop the timer T0CONbits.T08BIT = 0; // timer configured as 16-bit T0CONbits.T0CS = 0; // use system clock T0CONbits.PSA = 0; // use prescaler T0CONbits.T0PS = 0b111; // prescaler 1:256 ( 0b is a prefix for binary) TMR0 = -15625; // initial timer value INTCONbits.T0IF = 0; // clear the overflow bit initially T0CONbits.TMR0ON = 1; // start the timer for (;;) { if (INTCONbits.T0IF == 1) { // overflow! TMR0 = -15625; // reload timer INTCONbits.T0IF = 0; // clear overflow ++c0; ++c1; if (c0 == 2) { // flash led 0 LATBbits.LATB0 =!LATBbits.LATB0; c0 = 0; if (c1 == 3) { // flash led 1 LATBbits.LATB1 =!LATBbits.LATB1; c1 = 0;

Comparing the techniques Let s compare (1) unsigned int t; t = TMR0; if (t >= 15625) { // equivalent of 250 ms TMR0 = 0; to (2) if (INTCONbits.T0IF == 1) { // overflow! TMR0 = -15625; // reload timer INTCONbits.T0IF = 0; // clear overflow (1) uses a 16-bit comparison, (2) uses a single-bit comparson less code since the CPU is 8-bit (2) uses polling but can be easily transformed into a interrupt-based code since overflows can be programmed to generate interrupts

Using Timers of Microchip PIC18F Microcontrollers ARSLAB - Autonomous and Robotic Systems Laboratory Dipartimento di Matematica e Informatica - Università di Catania, Italy santoro@dmi.unict.it L.A.P. 1 Course