CprE 288 Introduction to Embedded Systems (Timers/Input Capture) Instructors: Dr. Phillip Jones

Similar documents
11 General-Purpose Timers

Timers and Pulse Accumulator

CprE 288 Introduction to Embedded Systems (UART Interface Overview)

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

Marten van Dijk, Syed Kamran Haider

Microprocessors B (17.384) Spring Lecture Outline

CprE 288 Introduction to Embedded Systems Exam 1 Review. 1

Overview of Microcontroller and Embedded Systems

ECE 271 Microcomputer Architecture and Applications University of Maine

CprE 288 Introduction to Embedded Systems

Embedded Systems. October 2, 2017

CprE 288 Translating C Control Statements and Function Calls, Loops, Interrupt Processing. Instructors: Dr. Phillip Jones Dr.

EE251: Tuesday December 4

Interrupts, timers and counters

CHAPTER 11 INTERRUPTS PROGRAMMING

PIC Microcontroller Introduction

Capture Mode of Pic18F252

EE251: Thursday November 30

Introduction to the MC9S12 Hardware Subsystems

Use a semaphore to avoid the enqueue and dequeue functions from accessing and modifying count variable at the same time.

16.317: Microprocessor Systems Design I Fall Exam 3 December 15, Name: ID #:

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

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz IV

Introduction to Embedded Systems

ECE251: Thursday November 8

Chapter 2. Overview of Architecture and Microcontroller-Resources

EE 308 Spring A software delay. To enter a software delay, put in a nested loop, just like in assembly.

Physics 120B: Lecture 11. Timers and Scheduled Interrupts

Lecture notes Lectures 1 through 5 (up through lecture 5 slide 63) Book Chapters 1-4

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz V

Unit 13 Timers and Counters

These three counters can be programmed for either binary or BCD count.

ADC: Analog to Digital Conversion

ECE 4510/5530 Microcontroller Applications Week 9

MICROPROCESSORS A (17.383) Fall Lecture Outline

HC12 Built-In Hardware

Signature: 1. (10 points) Basic Microcontroller Concepts

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

// middle priority ISR Status.flag = 1; Status.y = 6;

Topics. Interfacing chips

UNIVERSITY OF CONNECTICUT. ECE 3411 Microprocessor Application Lab: Fall Quiz III

CprE 288 Introduction to Embedded Systems

CprE 288 Introduction to Embedded Systems (Project and Platform Overview)

Lecture 9. Timer Operations and Programming

CENG-336 Introduction to Embedded Systems Development. Timers

Embedded Systems. 3. Hardware Software Interface. Lothar Thiele. Computer Engineering and Networks Laboratory

CPEG300 Embedded System Design. Lecture 8 Timer

EECS 373 Midterm 2 Fall 2018

EE445L Fall 2014 Final Version A solution Page 1 of 7

NUC123xxxAN Series Errata Sheet. Errata Sheet for 32-bit NuMicro Family Rev May 19, 2016

12.1. Unit 12. Exceptions & Interrupts

Lecture test next week

EE 308 Spring Exam 1 Feb. 27

Chapter 1. Microprocessor architecture ECE Dr. Mohamed Mahmoud.

Exam 1. EE319K Spring 2013 Exam 1 (Practice 1) Page 1. Date: February 21, 2013; 9:30-10:45am. Printed Name:

Marten van Dijk Department of Electrical & Computer Engineering University of Connecticut

SquareWear Programming Reference 1.0 Oct 10, 2012

Interrupts and Timers

PPG Function of F2MC-16LX MCU Series as PWM

EE445L Fall 2014 Final Version A Page 1 of 7

Marten van Dijk Department of Electrical & Computer Engineering University of Connecticut

ECE 391 Exam 1 Review Session - Spring Brought to you by HKN

ECE2049 E17 Lecture 4 MSP430 Architecture & Intro to Digital I/O

ECE2049: Embedded Computing in Engineering Design A Term Fall Lecture #9: Exam Review w/ Solutions

Table of Figures Figure 1. High resolution PWM based DAC...2 Figure 2. Connecting the high resolution buck converter...8

Microprocessors & Interfacing

ECGR 4101/5101, Fall 2016: Lab 1 First Embedded Systems Project Learning Objectives:

Task Based Programming Revisited Real Time Operating Systems

Interrupts (I) Lecturer: Sri Notes by Annie Guo. Week8 1

ADC: Analog to Digital Conversion

2-Oct-13. the world s most energy friendly microcontrollers and radios

Bachelor of Engineering in Computer and Electronic Engineering

Interrupts and Time. Real-Time Systems, Lecture 5. Martina Maggio 28 January Lund University, Department of Automatic Control

Operating Systems. Introduction & Overview. Outline for today s lecture. Administrivia. ITS 225: Operating Systems. Lecture 1

e-pg Pathshala Subject : Computer Science Paper: Embedded System Module: Interrupt Handling Module No: CS/ES/13 Quadrant 1 e-text

MICROCONTROLLER SYSTEM CONTROL DESIGN JON PRITCHARD SCOTT VON THUN

Systems Programming. Lecture 11 Timers

Physics 124: Lecture 12. Timer Basics. Timers and Scheduled Interrupts

The MC9S12 Timer Input Capture Function

Interrupts and Time. Interrupts. Content. Real-Time Systems, Lecture 5. External Communication. Interrupts. Interrupts

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

Embedded Software TI2726 B. 4. Interrupts. Koen Langendoen. Embedded Software Group

ARM Architecture and Assembly Programming Intro

Final Exam Study Guide

KXCJK AN 039. Getting Started with the KXCJK. Introduction

I Introduction to Real-time Applications By Prawat Nagvajara

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Course Outline Introduction to C-Programming

Marten van Dijk, Syed Kamran Haider

Wed. Aug 23 Announcements

Interconnects, Memory, GPIO

Real Time Embedded Systems. Lecture 10 January 31, 2012 Interrupts

EE 308 Spring A software delay

Nano102/112 Series Errata Sheet

CSCE 236 Embedded Systems, Spring 2012 Quiz/Test 2

The MC9S12 Timer Output Compare Function Making an event happen at specific time on the HC12 The MC9S12 Output Compare Function

Capturing the Time of an External Event Input Capture Subsystem

Lab 9: On-Board Time Generation and Interrupts

ECE/CE 3720: Embedded System Design

Lab 4 Interrupts ReadMeFirst

Transcription:

CprE 288 Introduction to Embedded Systems (Timers/Input Capture) Instructors: Dr. Phillip Jones 1

Announcements HW 4, Due Wed 6/13 Quiz 5 (15 min): Wed 6/13, Textbook reading: Section 9.1, 9.2 (your one-side of 1 page of notes will be collected for Class Participation) Exam 2: Friday, June 22 This week lab will use an Ultra sound Textbook reading: Section 9.1, 9.2 2

Overview of Today s Lecture Input Capture Review Textbook reading: Section 9.1, 9.2 3

INPUT CAPTURE 4

Input Capture Capture the times of events Many applications in microcontroller applications: Measure rotation rate Remote control Sonar devices Communications Generally, any input that can be treated as a series of events, where the precise measure of event times is important 5

TIVA TM4C123GH6PM CCP (Capture/Compare/PWM) Timer Diagram for lab 7 we use T3CCP1 Which uses TIMER3B, and Pin 3 of Port B (More on pages 704-707 of datasheet) 6

Input Capture An event is a transition of binary signal Example: How many events make up the following waveform? 7

Input Capture An input digitalized and then times captured Example: The input is understood as events occurring at the following times: 220, 221, 223, 226, and 227 with initial state as low 8

Application: Speedometer How to detect the speed of a treadmill? Magnet sensor Microcontroller Waveform 9

Application: Sonar Device Ping))) sensor: ultrasound distance detection device 10

Sonar Principle Sound Speed in Lab Temperature: About 340m/s Pulse width proportional to round-trip distance * Temperature affects sound speed 11

Sonar Principle Assume 62.5KHz Input Capture clock 1ms <=> 62.5 clocks <=> 34cm Time Diff. Clock Count One-way Distance 2ms 125 0.34m 4ms 250 0.68m How to capture the times of rising edge and falling edge? 12

Application: Remote Control (Decoding) 13

Input Capture: Design Principle Time is important! How could a microcontroller capture the time of an event, assuming a clock count can be read? Keep polling the input pin? Use an interrupt???? Precise timing is needed! 14

Input Capture: Design Principle Time value (clock count) is captured first then read by the CPU GPTMTnV Interrupt To CPU GPTMTnR Edge Detector Noise Canceller GPTMTnV: Timer n Value Register (n is A or B) GPTMTnR: Timer Register (in Edge-Time mode, this register is loaded with the value in GPTMTnV at the last input edge event) 15

Input Capture: Design Principle What happens in hardware and software when and after an event occurs The event s time is captured in the GPTMTnR (timer register) An interrupt is raised to the CPU CPU executes the input capture ISR, which reads the timer register and completes the related processing The captured time is precise because it s captured immediately when the event occurs The ISR should read the timer register and complete its processing fast enough to avoid loss of events 16

Input Capture: Design Principle Interrupt CPU Interrupt processing CPU Foreground computation 17

Input Capture: Design Principle How to program the interrupt handler to Count the number of pulses Calculate pulse width Decode IR signals And do many other functions IntRegister(INT_TIMER3B, TIMER3B_Handler); //in main void TIMER3B_Handler(void) { // YOUR PROCESSING } 18

Tiva TM4C123GH6PM 16/32-bit Timer as Input Capture Unit Tiva TM4C123GH6PM has 6, multi-purpose 16/32-bit timer units with Input capture units (IC) Output compare units (OC) Pulse width modulation output (PWM) And other features 19

TIVA TM4C123GH6PM CCP (Capture/Compare/PWM) Timer Diagram for lab 7 we use T3CCP1 Which uses TIMER3B, and Pin 3 of Port B (More on pages 704-707 of datasheet) 20

Tiva TM4C123GH6PM 16/32-bit Timer as Input Capture Unit When an edge is detected at input capture pin, current TIMERx_TnV_R (GPTMTnV) value is captured (saved) into TIMERx_TnR_R (GPTMTnR) Time is captured immediately (when an event happens) and read by the CPU later 21

Use Input Capture: Example int last_event_time; int main(void){ //configurations and inits IntRegister(INT_TIMER3B, TIMER3B_Handler); } void TIMER3B_Handler(void) { int event_time = TIMER3_TBR_R; // read current event time // YOUR PROCESSING CODE } Notes: Use Interrupt to process input capture events Read captured time from TIMER3_TBR_R 22

Lab 7 General Idea of Programming General idea: Configure Timer3B for input capture Generate a pulse to activate the PING))) sensor Capture the time of rising edge event Capture the time of falling edge event Calculate time difference and then distance to any object 23

Application: Sonar Device PING Sensor Datasheet: /resources/docs/28 015-PING-v1.3.pdf 24

Lab 7 General Idea of Programming PB3 Config for output PB3 Config for intput PB3 = 1 5us minimum PB3 = 0 Change to GPIO mode before sending trigger pulse Send trigger Catch rising edge (store TIMER3_TBR_R in a var) Change to Input Capture mode after sending trigger pulse Catch falling edge (store TIMER3_TBR_R in a var) Remember only one pin (i.e PB3) used to communicate with the PING))) sensor 25

Timer Programming Interface GPTMCTL: GPTM (General Purpose Timer) Control GPTMCFG: GPTM Configuration GPTMTnMR: GPTM Timer n Mode (n is A or B) GPTMTnILR: GPTM Timer n Interval Load GPTMIMR: GPTM Interrupt Mask Register GPTMMIS: GPTM Masked Interrupt Status GPTMICR: GPTM Interrupt Clear Register 26

Textbook & Data Sheet: Read and ask questions Exam 2 will predominantly consist of questions of the form Program Configure Registers to meet these specs UART, ADC, Input Capture, Output Compare, Timers, Interrupts Each device has a section in the Datasheet and Textbook Based on a given configuration, answer questions about how the program will behave E.g. How long will something take to occur? E.g. How many time a second with something occur? Explain why a given configuration is incorrect for implementing a specified behavior Assuming a given configuration, write a short program to implement a specific behavior ADC calculation problem 27

16-bit Timer Programming Interface Inside GPTMCTL: TnPWML 6, 14 (A, B): PWM Output Level TnOTE 5, 13 (A, B): Output trigger enable TnEVENT 3:2, 11:10 (A, B): Event Mode (Edge Select) TnSTALL 1, 9 (A, B): Timer n Stall Enable TnEN 0, 8 (A, B): Timer n Enable RTCEN 4 : RTC Stall Enable 28

GPTMCTL (TIMERx_CTL_R) TnEVENT: GPTM Timer n Event Mode Which edge will trigger an interrupt? 00: Positive (rising) edge 01: Negative (falling) edge 10: Reserved 11: Both TnEN: GPTM Timer n Enable Bit Set this bit to enable Timer n. Make sure a timer is disabled before trying to change its settings. (from p. 737 of datasheet) 29

GPTMCFG (TIMERx_CFG_R) GPTMCFG: GPTM Configuration 0x0: Concatenated mode. (16/32 bit timers use 32 bits, 32/64 bit timers use 64 bits.) 0x1: Concatenated mode, and timers are set to RTC (realtime clock) counter configuration. 0x4: 16/32 bit timers are spilt into two 16-bit timers, timer A and timer B. 32/64 bit timers are split into two 32-bit timers. Other values for GPTMCFG: are reserved. (p. 727 of datasheet) 30

GPTMTnMR (TIMERx_TnMR_R) GPTMTnMR: GPTM Timer n Mode Controls Timer mode. When in concatenated mode, GPTMTAMR controls the concatenated timer and GPTMTBMR is ignored. TnMR: Timer n Mode 0x0: Reserved 0x1: One-Shot Timer Mode 0x2: Periodic Timer Mode 0x3: Capture Mode (p. 729 (Timer A) & 733 (Timer B) of datasheet) 31

TM4C123 Clock Sources 32

GPTMTnILR (TIMERx_TnILR_R) When timer n is counting up GPTMTnILR contains the upper bound. When counting down GPTMTnILR contains the initial value for timer n. Important Note: In lab 7, if TIMER3_TBILR_R is set to the max 0xFFFF (default), then the prescalar register (TIMER3_TBPS_R) will concatenate with the timer value register to make a 24-bit register. To get the correct values you must read from both TIMER3_TBPS_R & TIMER3_TBR_R and shift accordingly. TIMER3_TBPS_R is the most significant 8 bits in the 24 bit register. (p. 757, 761, 770) 33

GPTMIMR (TIMERx_IMR_R) For any interrupt in GPTMIMR write to the corresponding bit: 0 to disable the interrupt 1 to enable the interrupt CnEIM: Timer n Capture Mode Event Interrupt Mask (p. 745 of datasheet) 34

GPTMMIS (TIMERx_MIS_R) The GPTMMIS register shows the status of unmasked interrupts. Each timer n has 1 ISR vector which all of its interrupts trigger, so it is necessary to check the GPTMMIS register to see which specific interrupts were set. CnEMIS: Timer n, Capture Mode Event Flag (p. 751 of datasheet) 35

GPTMICR (TIMERx_ICR_R) To clear an interrupt flag write a 1 to the corresponding bit in GPTMICR. CnECINT: Clears the Timer n Capture Mode Event Flag (p. 754 of datasheet) 36

Configure Timer3B for Lab 7 TIMER3_CTL_R: Enable, Edge Select. TIMER3_CFG_R: 16-bit mode. TIMER3_TBMR_R: Capture Mode, Edge-Time Mode, Count up. TIMER3_TBILR_R: Set upper bound. TIMER3_IMR_R: Enable capture interrupt. Port B pin 3 (PB3) It is Timer3B s Capture/Compare/PWMv (CCP) pin, and connects to the input/output pin of the PING sensor 37

IC Programming Example volatile enum {LOW, HIGH, DONE} state; volatile unsigned rising_time; // start time of the return pulse volatile unsigned falling_time; // end time of the return pulse /* start and read the ping sensor once, return distance in mm */ unsigned ping_read() { } /* ping sensor related to ISR */ void TIMER3B_Handler(void) { } Note 1: This code does not work for Lab 7 as it is. Note 2: Does not follow timing example of slide 25. 38

/* send out a pulse on PB3 */ void send_pulse() { GPIO_PORTB_DIR_R = 0x08; // set PB3 as output GPIO_PORTB_DATA_R = 0x08; // set PB3 to high // wait at least 5 microseconds based on data sheet GPIO_PORTB_DATA_R &= 0xF7; // set PB3 to low GPIO_PORTB_DIR_R &= 0xF7; // set PB3 as input } /* convert time in clock counts to single-trip distance in mm */ unsigned time2dist(unsigned time) { } Note 1: This code does not work for Lab 7 as it is. Note 2: Does not follow timing example of slide 25. 39

unsigned ping_read() { send_pulse(); // send the starting pulse to PING // TODO get time of the rising edge of the pulse // TODO get time of the falling edge of the pulse // Calculate the width of the pulse; convert to centimeters } 40

ADD-ON SLIDES 41

CCP Programming Example Treadmill last_time current_time Assume The sensor input is connected to Timer 1A Capture Compare PWM Pin (T1CCP0) L is the circumference (length of circle) of the wheel 42

CCP Programming Example volatile unsigned last_time = 0; volatile unsigned current_time = 0; volatile int update_flag = 0; // ISR: Record the current event time void TIMER1A_Handler(void) { last_time = current_time; current_time = TIMER1_TAR_R; update_flag = 1; } Recall: We have to declare volatile for global variables changed by ISRs, otherwise a normal function may not see the changes 43

Polling- vs. Interrupt-Based Programming Polling: Your code keeps checking I/O events For Input Capture, your code may check CAE flag while ((TIMER1_MIS_R & TIMER_MIS_CAEMIS) == 0) {} print_speed(); TIMER1_ICR_R = TIMER_ICR_CAECINT; // clear ICF1 Note: CAEMIS is cleared by writing 1 to CAECINT. (Always check the datasheet for such details.) 44

Polling- vs. Interrupt-Based Programming Why polling? Program control flow looks simple Interrupts have overheads added to the processing delay Not every programmer likes writing ISRs Why NOT polling? The CPU cannot do anything else The CPU cannot sleep to save power Using ISRs can simplify the control structure of the main program 45

Overflow Are we concerned with overflow in the calculation? time_diff = current_time last_time; What happens if current_time is less than last_time? If we use the prescalar register to concatenate with the 16-bit register to make a 24-bit register: Overflow: Change from 0xFFFFFF to 0x000000 0xFFFFFF is 16777216 in decimal, it takes over 1 second for our 16MHz clock to overflow the timer. Considering the time scale of our PING))) sensor readings, we would never have more than 1 overflow at a time. One overflow can be accounted for easily. 46

Overflow unsigned long time_diff; overflow += (current_time < last_time); time_diff = ((unsigned long)overflow<<24) + current_time last_time; update_flag = 0; Overflow occurred if current_time < last_time For each overflow, increase time_diff by 16,777,216 (2 24 ) You have to use long integer which is 32-bit (0 to 2 32-1) 47