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

Similar documents
Announcements Homework #3 due today. Ports. Outline for Today C8051 SFRs & Port I/O Worksheet #4 - simple I/O code. Using Ports

Lecture 9. Timer Operations and Programming

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

Before Class Install SDCC Instructions in Installing_SiLabs-SDCC- Drivers document. Solutions to Number Systems Worksheet. Announcements.

8051 Microcontroller

Outline for Today. Lab Equipment & Procedures. Teaching Assistants. Announcements

80C51 Block Diagram. CSE Overview 1

Timers and interrupts

Interrupts, timers and counters

Microprocessors B (17.384) Spring Lecture Outline

8051 Timers. Class 7 EE4380 Fall Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

Chapter 09. Programming in Assembly

Welcome to Lab! You do not need to keep the same partner from last lab. We will come around checking your prelabs after we introduce the lab

Lesson-3: Counters and Timers

Magic 8 Ball. Student's name & ID (1): Partner's name & ID (2): Your Section number & TA's name

8051 Microcontroller Interrupts

Welcome to Lab! Feel free to get started until we start talking! The lab document is located on the course website:

SANKALCHAND PATEL COLLEGE OF ENGINEERING, VISNAGAR. ELECTRONICS & COMMUNICATION DEPARTMENT Question Bank- 1

Timer0..Timer3. Interrupt Description Input Conditions Enable Flag

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

ELEG3923 Microprocessor Ch.9 Timer Programming

Lecture 10. Serial Communication

UBC104 Embedded Systems. Review: Introduction to Microcontrollers

Measuring Duty Cycles with an Intel MCS-51 Microcontroller

8051 Microcontroller memory Organization and its Applications

8051 Microcontroller

EE 354 September 16, 2016 C Sample Programs

ECE 362 Experiment 4: Interrupts

#include <stdio.h> // // Global CONSTANTS

Introduction to Embedded Systems

Interrupts on PIC18F252 Part 2. Interrupts Programming in C Language

The 8051 microcontroller has two 16-bit timers/counters called T0 and T1.

Understanding the basic building blocks of a microcontroller device in general. Knows the terminologies like embedded and external memory devices,

CHAPTER TIMER PROGRAMMING

8051 Microcontrollers

Lecture 1. Course Overview and The 8051 Architecture

Interrupts. EE4380 Fall 2001 Class 9. Pari vallal Kannan. Center for Integrated Circuits and Systems University of Texas at Dallas

Interrupts. Embedded Systems Interfacing. 08 September 2011

University of Texas at El Paso Electrical and Computer Engineering Department. EE 3176 Laboratory for Microprocessors I.

SYLLABUS UNIT - I 8086/8088 ARCHITECTURE AND INSTRUCTION SET

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

Chapter 09. Programming in Assembly

CPEG300 Embedded System Design. Lecture 8 Timer

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

ECE2049: Embedded Computing in Engineering Design C Term Spring Lecture #11: More Clocks and Timers

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

EECS 373 Midterm 2 Fall 2018

Embedded Controller Programming II. I/O Device Programming in C Part 1: Input and Interrupts

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

Using Timers of Microchip PIC18F Microcontrollers

Summer 2003 Lecture 21 07/15/03

CHAPTER 11 INTERRUPTS PROGRAMMING

8-bit Microcontroller with 8K Bytes In-System Programmable Flash AT89S52

Chapter 2. Overview of Architecture and Microcontroller-Resources

Microcontroller basics

Using programmed I/O on the M16C; and Writing an Interrupt Service Routine (ISR)

C:\CYGNAL\Examples\C8051F02x\C\Edu_Board_Source_Code\Magcard.c

Serial I-O for Dinesh K. Sharma Electrical Engineering Department I.I.T. Bombay Mumbai (version 14/10/07)

C Language Programming, Interrupts and Timer Hardware

Microcontroller and Embedded Systems:

For more notes of DAE

// and verify that there is a sine wave with frequency <FREQUENCY> and

MICROPROCESSORS AND MICROCONTROLLERS MATERIAL. Features of 8051:

Department of Electronics and Instrumentation Engineering Question Bank

EE251: Thursday November 30

Capture Mode of Pic18F252

Lab 5: EBI and ADC: Digital Voltmeter

C Language Programming, Interrupts and Timer Hardware


1. Attempt any three of the following: 15

EE6502- MICROPROCESSOR AND MICROCONTROLLER

Embedded World Television, Radio, CD player, Washing Machine Microwave Oven Card readers, Palm devices

CENG-336 Introduction to Embedded Systems Development. Timers

8051 Training Kit Lab Book

ENE 334 Microprocessors

Timer programming

CPEG300 Embedded System Design. Lecture 6 Interrupt System

Interrupt Programming: Interrupts vs. Polling Method:

EE251: Tuesday December 4

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

Interrupts L33-1. Interrupts

Timer1 Capture Mode:

University of Texas at El Paso Electrical and Computer Engineering Department. EE 3176 Laboratory for Microprocessors I.

The Microcontroller. Lecture Set 3. Major Microcontroller Families. Example Microcontroller Families Cont. Example Microcontroller Families

UNIVERSITY OF CALIFORNIA, SANTA CRUZ BOARD OF STUDIES IN COMPUTER ENGINEERING CMPE13/L: INTRODUCTION TO PROGRAMMING IN C SPRING 2013

Mod-3: Interrupts,Timer operation,serial communication 1

UNIT V MICRO CONTROLLER PROGRAMMING & APPLICATIONS TWO MARKS. 3.Give any two differences between microprocessor and micro controller.

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

Infineon C167CR microcontroller, 256 kb external. RAM and 256 kb external (Flash) EEPROM. - Small single-board computer (SBC) with an

Laboratory 10. Programming a PIC Microcontroller - Part II

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

8051 MICROCONTROLLER

BASICS OF THE RENESAS SYNERGY TM

2. (2 pts) If an external clock is used, which pin of the 8051 should it be connected to?

Course Introduction. Purpose: Objectives: Content: 27 pages 4 questions. Learning Time: 20 minutes

Microprocessors & Interfacing

MPLAB SIM. MPLAB IDE Software Simulation Engine Microchip Technology Incorporated MPLAB SIM Software Simulation Engine

IAS0430 MICROPROCESSOR SYSTEMS

INTERRUPTS in microprocessor systems

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

Transcription:

By the end of Class Pseudo-code for Lab 1-2 due as part of prelab Homework #5 on website due before next class Outline Introduce Lab 1-2 Counting Timers on C8051 Interrupts Laboratory Worksheet #05 Copy and paste C code Compile and run Get checked off on Lab 1-1 if not already done Start Lab 1-2 Quiz 2 next class on RPILMS Timers and Interrupts 2 3 Homework 5 C8051F020 Block Diagram (pg 18) Homework 5 is a programming assignment that is due before the start of the next class A basic template is provided, edit and add to the code For your own development you will not need your protoboard, but will need a SecureCRT window You probably should come to open shop to test your code or work on it in class Submit a working version of the code on LMS 4 5

Major topic 1 Timer 0 There are counters available They can count clock cycles or pulses on an external pin. If a counter counts clock cycles, it then is a timer. Today we will use Timer0. Major topic 2 Interrupts Software or Hardware can interrupt your code. Think of it as a phone call while you are watching a DVD Stop your watch DVD code (pause) The DVD player remembers where you are Pick up the phone Take care of the interruption Interrupt Service Routine Go back to watch DVD code Pick up where you were before the interruption There are many possible interrupts: Phone (external trigger) Hunger pains (internal trigger) Time to go to class (timer interrupt type we will use today.) 6 7 Lab 1, part 2 Description Lab 1, part 2: On RPILMS Hardware: You will need to add one additional LED (LED1) to the Lab 1, part 1 hardware Keep the remaining circuitry from Lab 1, part 1. Basic idea player tries to match randomly lit LEDs by pressing buttons Wait for slide switch, then loop 10 times Light random LED combination Wait for 1 second Check for correct reaction indicate correct or wrong with Bicolor LED Keep track of correct and wrong results Pause if slide switch is off Print the results Start another loop only after the slide switch has been switched off and back on. 8 9

Lab 1, part 2 Representation Lab 1, part 2 C code Switches (pushbuttons, slide switch) Input Output Ports 2 and 3 Ports 2 and 3 LED0, LED1, bicolor LED, Buzzer In the software, you need to generate a random number. Refer to sample code at end of lab description Sample code is available on LMS #include <stdlib.h> for rand() function Your code will also need to keep track of time in order to implement a delay. How can we count the passage of time? 10 11 Counting Counting on Microprocessors A basic feature of the microcontroller is the ability to count something Can count some type of electronic signal that is an input to the microcontroller Signal comes from outside the microcontroller, so this is called an external event Can count the passage of time Internal clock cycles of the microprocessor represent time. We will count clock cycles for this lab assignment Start at a count of 0 Counter register has a limited size Next count after maximum causes an overflow Counter resets to 0 System can be set so that this overflow will trigger something to occur Counter may continue to be incremented after overflow Similar to a clock Minutes count from 0 to 59, then overflows to 0 and continues to count. Also triggers something else increment hours. 12 13

Timers see LITEC manual Ch. 4 - Timers T0 Mode 0 Bock Diagram In the C8051 microcontroller, Timers are used as a type of counter Five Timers are available (see C8051 manual page 225) Timer 0, Timer 1, Timer 4 Timer 0 is of interest now Timers are extra hardware on chip Don t require the CPU independent of C8051 (8 bits) Figure 4.2 in manual 14 15 Using Timers We can configure the Timer to count external event signals as well as to count clock cycles for the passage of time. When counting clock cycles, remember that the system clock is fast. Our system clock runs at 22.1184 megahertz. The timer will overflow many times Timer keeps resetting making it difficult to track longer periods of time It may be easier to keep track of the number of overflows that have passed How can overflows be counted? Interrupts later topic in this lecture. Timers Given the clock frequency Calculate the count rate (i.e., counts per second) Calculate the time per counter overflow Example: clock frequency, 22.1184 MHz 16 17

Timer 0 see Ch. 4 Timers of LITEC manual Can be a 16 bit, 13 bit, or 8 bit counter Can count: The system clock (SYSCLK) The system clock/12 External events Count can be gated, (counting can be turned on and off): By an external event By software, (using TR0) Let s look at the SFRs needed to configure Timer 0 for keeping track of the passage of time Configuring Timer 0 CKCON (see Ch. 4 - Timers of LITEC manual or pg 226 of C8051 manual) Bit 3 select counting rate: clock or clock 12 Timer 1 is used for printf, don t change the settings! Warning, don t change T1M, used for printf 18 19 Configuring Timer 0 Configuring Timer 0 TMOD (see Ch. 4 Timers of LITEC manual or pg 232 of C8051 manual) Bits 0 and 1 set number of bits (8, 13, or 16) for counter Bit 2 select increment on count clock or external pin Bit 3 control counting using TR0 with or without external input pin T0 ( /INT0 ) TR0 is a bit of TCON Warning: Don t change these bits, used for printf. TCON (see Ch. 4 - Timers of LITEC manual or p 231 of C8051 manual ) Bit 4 enable or disable counting 0 will disable Timer 0 1 will enable Timer 0 Same warning don t change Bit-addressable Special Function Registers Specific bits of bit-addressable SFRs can be accessed with their individual names Example: TR0 = 1; // Sets bit 4 of TCON 20 21

Timers - summary Know how to read and write the present count in a timer TL0 & TH0 for Timer 0 (or 16-bit TMR0) TL1 & TH1 for Timer 1 (or 16-bt TMR1) Example: unsigned int count; // count needs to be 16-bit variable count = TH0 * 256 + TL0; (or count = TH0<<8 + TL0;) Also: count = TMR0 // using 16-bit SFR Know which SFRs are used to configure the timers. CKCON TCON TMOD Interrupts second major topic for today It was mentioned previously that an event can be triggered when an overflow occurs. We call this event an interrupt. It is called an interrupt because the main code is stopped, and another piece of code, the Interrupt Service Routine (ISR), is executed. Original code is resumed upon completion of the ISR function You write the code for the ISR For this example, a variable can be incremented every time the ISR is executed to keep track of how many overflows have occurred. 22 23 Interrupts Configuring Interrupts Interrupts cause the code to do the following procedure: Stop Store all variables Record location in code Transfer control to the Interrupt Service Routine function, ISR. When the ISR has handled the interrupt (function has finished), control is transferred back to the original code. It picks up where it left off. Think of it like a phone ringing while you are watching a DVD. Pause player, answer phone, talk, hang up, return to DVD We can control which interrupts we allow, there are 22 available in the C8051F020 Refer pg 43 of LITEC (or inside cover,) 117 of C8051 manual We will start with the use of the Timer 0 Overflow An interrupt is triggered when both its interrupt enable & interrupt flag are set Interrupts need to be enabled & disabled because interrupts may not be needed in all applications in which Timer 0 is used The interrupt flag is used as the trigger to run the ISR Timer 0 Overflow Flag is a specific bit in a SFR It is set automatically when Timer 0 overflows It is cleared automatically when the interrupt service routine is called. 24 25

Interrupt Service Routine - ISR When an interrupt occurs, a function needs to exist in the program this is the ISR This function is specified as void ISR_name (void) interrupt N { // code for interrupt } Where ISR_name is a name you give the function and N is a number in the range of 0-21 that corresponds to the appropriate interrupt number. For Timer 0 Overflow, N = 1 Each type of interrupt used needs a different ISR Interrupt numbers are assigned the same value as the Interrupt Priority Number in C8051 manual pages 117-118, and the LITEC manual Table 4.2, and on the inside front cover. Interrupt Number (C8051 Man. pg. 117, LITEC manual Table 4.2) Use the Priority Number that corresponds to the desired type of interrupt as the Interrupt number in the Interrupt function Interrupt Source Interrupt Vector Interrupt Priority type Order # Interrupt Source Interrupt Vector Priority Order Reset 0x0000 Top Comparator 0 Falling Edge 0x0053 10 External Interrupt 0 (/INT0) 0x0003 0 Comparator 0 Rising Edge 0x005B 11 Timer 0 Overflow 0x000B 1 Comparator 1 Falling Edge 0x0063 12 External Interrupt 1 (/INT1) 0x0013 2 Comparator 1 Rising Edge 0x006B 13 Timer 1 Overflow 0x001B 3 Timer 3 Overflow 0x0073 14 UART0 0x0023 4 ADC0 End of Conversion 0x007B 15 Timer 2 Overflow (or RXF2) 0x002B 5 Timer 4 Overflow 0x0083 16 Serial Peripheral Interface 0x0033 6 ADC1 End of Conversion 0x008B 17 SMBus Interface 0x003B 7 External Interrupt 6 0x0093 18 ADC0 Window Comparator 0x0043 8 External Interrupt 7 0x009B 19 Programmable Counter Array 0x004B 9 UART1 0x00A3 20 External Crystal OSC Ready 0x00AB 21 26 27 IE Interrupt Enable Configuring Interrupts Ch. 4 of LITEC Manual Interrupt enable bit for Timer 0 is ET0 (bit 1) in the IE register (see C8051 manual page 119) Overflow of Timer 0 will set the TF0 flag (bit 5) in the TCON register (see C8051 manual page 231) 28 29

Timer & Interrupt Summary Worksheet #5 Timers can be used to count passage of time (focus on Timer 0) CKCON: set clock rate TMOD: set how timer will count TCON: enable counter TH0: Timer 0 value high order 8 bits TL0: Timer 0 value low order 8 bits Interrupts can be used to trigger the execution of certain portions of code IE: enable interrupt TCON: interrupt flag (handled automatically) ISR: function that runs when interrupt occurs Now that we have some basic understanding of using timers and interrupts to keep track of time, lets try an application of these concepts. Laboratory Worksheet #5 for Lab 1, part 2 Use hardware already built in Lab 1, part 1 Copy code from website (Sample Codes) Compile, download and run program and answer questions on worksheet Enter your procedure and results in Lab Notebook Have a TA check your answers 30 31 Sample Code - Worksheet #5 for Lab 1, part 2 Counter Overflows This code will demonstrate the use of a T0 interrupt to count the number of T0 overflows that occur while a slide switch connected to P3.0 is in the OFF position Rate: clock cycles/12 (CKCON) Number of bits: 16 bit counting (TMOD) Uses software to start and stop, TR0 (TMOD, TCON) Enable interrupts for Timer 0 (IE) Can use this code as a reference when writing your code for Lab 1, part 2 Counters have a size based on the number of memory bits assigned to the counter Maximum count for a 16 bit counter is 0xFFFF (decimal value is 65,535) Maximum count for a 13 bit counter is 0x1FFF (decimal value is 8,191) Max count for an 8 bit counter is 0xFF (decimal 255) An overflow results in a count of zero The overflow can trigger another event. We will use a timer overflow to trigger an interrupt 32 33

TMOD Timer Mode Register Lab 1, part 2: Description Be able to configure either T0 or T1 Wait for slide switch, then loop 10 times Light random LED combination Wait for 1 second Check for correct reaction (PB held down for 1s) Keep track of correct and wrong results Print the results Start another loop only after the slide switch has been switched off and back on. 34 35 Lab 1, part 2 Pseudo-Code compiler directives declare global variables function prototypes main function declare local variables configure ports 2 and 3 Initialize Timer0, including allowing interrupts, don t start counting while (TRUE) turn off LEDs and buzzer if (slideswitch)... end if (slideswitch) wait for slideswitch to be toggled off end while (TRUE) end main function Lab 1, part 2 Pseudo-Code if (slideswitch) start Timer 0 for 10 loops get random number repeat until it is different than previous number display correct combination of LEDs reset Interrupt_counts and Timer 0 contents wait 1 second check switches if correct, increment count end for loop clear LED output stop Timer 0 print results end if (slideswitch) 36 37

Lab 1, part 2 Pseudo-Code Interrupt Service Routine increment Interrupt_counts End ISR Today s Lab Work IMPORTANT get checked off for Lab 1-1 Finish Laboratory Worksheet #5 Wire circuit for Lab 1, part 2 Start writing code for Lab 1, part 2 Next Class Homework #5 is due before class begins Quiz #2 Timers & Interrupts Check-off Lab 1, part 2 38 39 Quiz 2 -Timers Configure Timer 0 and Timer 1 Set up any mode, (fully understand mode 0 and mode 1) 13 bit counting, mode 0 16 bit counting, mode 1 8 bit, mode 2, or two 8 bit counters, mode 3 Count at a rate equal to the system clock or the (system clock) 12 Know that the timers are capable of counting external events Know how to start and stop the counting 40 42

Timers Know how to read and write the present count in a timer TL0 & TH0 for Timer 0 (or 16-bit TMR0) TL1 & TH1 for Timer 1 (or 16-bt TMR1) Example: unsigned int count; // count needs to be 16-bit variable count = TH0 * 256 + TL0; (or count = TH0<<8 + TL0;) Also: count = TMR0 // using 16-bit SFR Know which SFRs are used to configure the timers. CKCON TCON TMOD CKCON: Clock Control Register 43 44 TCON: Timer Control Register TMOD Timer Mode Register Be able to configure either T0 or T1 45 46

Quiz 2 - Interrupts Understand what an interrupt is Know what interrupts are available on the 8051 Know what is necessary in code for interrupts Enable interrupts Allow all interrupts, EA=1; or IE = 0x80; Just Timer 0 Overflow interrupt enable with ET0 of IE Just Timer 1 Overflow interrupt enable with ET1 of IE Write an Interrupt Service Routine Include correct interrupt number Be able to find the interrupt number for any type of interrupt, not just Timer interrupts. Interrupt Summary 47 48 Start Lab 1, part 2 Today s Class If you haven t finish Lab 1-1 yet, come in during open shop to complete it, and get checked-off Lab 1-2 Check-off should be done in the next class o Complete entries in lab notebook prior to check-off o Both members of a pair must be present for check-off Leave hardware intact for Lab 2 Turn in Notebook for Lab 1, after completing and getting checked off on both parts of the lab Homework 5 will become available after we post this semester s version of Lab 2! 49