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