lecture05F - Lecture #5 August 26, 2010 Before Class...

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Lecture #5 August 26, 2010 Before Class Begins... Hand in Homework #4 Quiz 1 RPILMS Remember to submit a soft copy on WebCT Homework #5 on website – due next class Log into the course RPILMS site One partner faces the room center the other One partner faces the room center, the other faces faces the wall Assessments You must be listed with this section We will give you the password when everybody is We will give you the password when everybody is ready ready and quiet Setup for Quiz 1 40 minutes; Password: ____________ Open book, open notes, Calculators OK Will start lecture & Lab 1-2 when done 1August 26, 2010 Lecture #5 2 NO sharing and NO copies of past quizzes Alternate seating, front & back desktops No other applications open on your computer Please turn off IM and all other applications that might automatically open during the quiz No talking until everybody has finished August 26, 2010 Lecture #5 3 Outline Introduce Lab 1, part 2 Counting Timers on C8051 Interrupts Laboratory Worksheet #4 C8051F020 Block Diagram (pg 18) (pg Copy and paste Compile and run Work on Lab 1, part 2 Check off of Lab 1, part 1 – if not already done August 26, 2010 Lecture #5 4 August 26, 2010 Lecture #5 5 1 Lecture #5 August 26, 2010 Major topic 1 – Timer 0 There are counters available Major topic 2 – Interrupts Software or Hardware can interrupt your code. Think of it as a phone call while you are watching a DVD They can count clock cycles or pulses on an external pin pin. If a counter counts clock cycles, it then is a timer. Today we will use Timer0 Timer0. Stop your “watch DVD code” our watch DVD 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 Phone (external trigger) Hunger pains (internal trigger) Time to go to class (timer interrupt – type we will use today.) There are many possible interrupts: August 26, 2010 Lecture #5 6 August 26, 2010 Lecture #5 7 Lab 1, part 2 Description Hardware: Lab 1, part 2: On RPILMS Basic idea – player tries to match randomly light LEDs LEDs by pressing buttons pressing buttons 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. Wait for slide switch, then loop 10 times o Light random LED combination o Wait for 1 second o Check for correct reaction indicate correct or wrong with Bicolor LED Keep track of correct and wrong results o Pause if slide switch is off if Print the results Start another loop only after the slide switch has been switched off and back on. Lecture #5 9 August 26, 2010 Lecture #5 8 August 26, 2010 2 Lecture #5 August 26, 2010 Lab 1, part 2 Representation Switches (pushbuttons, slide switch) Lab 1, part 2 In the software, you need to generate a random number. Input Ports 2 and 3 C code Refer to sample code at end of lab description Sample code is available on LMS #include <stdlib.h> for rand() function <stdlib.h> Ports 2 and 3 and Output LED0, LED1, bicolor LED, Buzzer August 26, 2010 Lecture #5 10 Your code will also need to keep track of Your code will also need to keep track of time time in order to implement a delay. How can we count the passage of time? Lecture #5 11 August 26, 2010 Counting A basic feature of the microcontroller is the ability to count something ability to count something Counting on Microprocessors Start at a count of 0 Counter register has a limited size register has limited size Next count after maximum causes an overflow overflow Can count some type of electronic signal that is an input to the microcontroller o Signal comes from outside the microcontroller, so this is called an external event external Internal clock cycles of the microprocessor represent time. We will count clock cycles for this lab assignment Counter resets to 0 System can be set so that this overflow will trigger something to occur Can count the passage of time o Counter may continue to be incremented after overflow Similar to a clock o Minutes count from 0 to 59, then overflows to 0 and overflows continues to count. Also triggers something else – increment hours. Lecture #5 13 August 26, 2010 Lecture #5 12 August 26, 2010 3 Lecture #5 August 26, 2010 Timers – see manual pages 36-41 36In the C8051 microcontroller, Timers are used Timers as type of counter as a type of counter Five Five Timers are available (see C8051 manual page 225) (see T0 Mode 0 Bock Diagram Page 37 of manual Timer 0, Timer 1, … Timer 4 Timer 0 is of interest now Don’t require the CPU – independent of C8051 Timers are extra hardware on chip (8 bits) bits) August 26, 2010 Lecture #5 14 August 26, 2010 Lecture #5 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. Timers Given the clock frequency Calculate the count rate (i.e., counts per second) Calculate the time per counter overflow th ti 1 1 seconds 4.5211 10 8 freq 22.1184 10 6 Hz count for Mode 1, 16 bits, total number of counts before overflow is : rate 216 65536 counts 0.045211s 65536counts 0.00296s 2.96ms for each overflow count overflow overflow 22.1184 106 Hz 1.8432 10 6 Hz 12 for Mode 0, 13 bit, use 213 8192 counts for SYSCLK 12, use Example: clock frequency, 22.1184 MHz The timer will overflow many times Timer keeps resetting making it difficult to track longer periods of time time It may be easier to keep track of the number of overflows that have passed Interrupts – later topic in this lecture. Lecture #5 16 How can overflows be counted? August 26, 2010 August 26, 2010 Lecture #5 17 4 Lecture #5 August 26, 2010 Timer 0 – see pages 36-40 of LITEC manual 36Can be a 16 bit, 13 bit, or 8 bit counter count: Can count: CKCON (see pg 37 of LITEC manual or pg 226 of C8051 manual) Configuring Timer 0 Bit 3 – select counting rate: clock or clock 12 is used for printf, don change the settings! Timer 1 is used for printf, don’t change the settings! The system clock (SYSCLK) (SYSCLK) The system clock/12 External events Warning, don’t change T1M, used for printf Count can be gated, (counting can be turned on and off): an external event By an external event By software, (using TR0) TR0) Let’s look at the SFRs needed to configure Timer 0 for keeping track of the passage of time August 26, 2010 Lecture #5 18 August 26, 2010 Lecture #5 19 Configuring Timer 0 TMOD (see pg 38 of LITEC manual or pg 232 of C8051 manual) Configuring Timer 0 TCON (see p 39 of LITEC manual or p 231 of C8051 manual ) Bits and Bits 0 and 1 – set number of bits (8, 13, or 16) for set number of bits (8 13 or 16) for counter 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 Bit Bit 4 – enable or disable counting or disable counting 0 will disable Timer 0 1 will enable Timer 0 Same warning – don’t change BitBit-addressable Special Function Registers Warning: Don’t change these bits, used for printf. August 26, 2010 Lecture #5 20 Specific bits of bit-addressable SFRs can be bitaccessed with their individual names Example: TR0 = 1; // Sets bit 4 of TCON TR0 Lecture #5 21 August 26, 2010 5 Lecture #5 August 26, 2010 Timers - summary Know how to read and write the present count in a timer – there are 5 counters on our chip Interrupts – second major topic for today It was mentioned previously that an event can be triggered when an overflow occurs. TL0 & TH0 for Timer 0 TL1 & TH1 for Timer 1 Example: unsigned int count; // count needs to be 16-bit variable 16count = TH0 * 256 + TL0; (or count = TH0<<8 + TL0;) (or We call this event an interrupt. interrupt It is called an interrupt because the main code is stopped, and another piece of code, the Interrupt Service Routine (ISR), is executed. Know which SFRs are used to configure the timers. Original code is resumed upon completion of the ISR function For this example, a variable can be incremented every time the ISR is executed to keep track of how many overflows have occurred. Lecture #5 23 You write the code for the ISR th th ISR CKCON TCON TMOD Lecture #5 22 22 August 26, 2010 August 26, 2010 Interrupts Interrupts cause the code to do the following procedure: Configuring Interrupts We can control which interrupts we allow, there are 22 available in the C8051F020 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 finished), control is transferred back to the original code. 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 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 interrupt 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 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. August 26, 2010 Lecture #5 24 August 26, 2010 Lecture #5 25 6 Lecture #5 August 26, 2010 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 X { ISR_name … // code for interrupt } Interrupt Number (C8051 (C8051 Man. pg. 117, LITEC manual pg. 43) Use the Priority Number that corresponds to the desired type of interrupt as the Interrupt number in the Interrupt function Interrupt Source Source Reset External Interrupt 0 (/INT0) Timer 0 Overflow External Interrupt 1 (/INT1) Timer 1 Overflow UART0 Timer 2 Overflow (or RXF2) Serial Peripheral Interface SMBus Interface ADC0 Window Comparator Programmable Counter Array Interrupt Vector 0x0000 0x0003 0x000B 0x0013 0x001B 0x0023 0x002B 0x0033 0x003B 0x0043 0x004B Priority Interrupt type # Order Top 0 1 2 3 4 5 6 7 8 9 Interrupt Source Source Comparator 0 Falling Edge Comparator 0 Rising Edge Comparator 1 Falling Edge Comparator 1 Rising Edge Timer 3 Overflow ADC0 End of Conversion Timer 4 Overflow ADC1 End of Conversion External Interrupt 6 External Interrupt 7 UART1 External Crystal OSC Ready Interrupt Vector 0x0053 0x005B 0x0063 0x006B 0x0073 0x007B 0x0083 0x008B 0x0093 0x009B 0x00A3 0x00AB Priority Order 10 11 12 13 14 15 16 17 18 19 20 21 27 Where ISR_name is a name you give the function and X is a ISR_name number in the range of 0-21 that corresponds to the appropriate 0interrupt number. For Timer 0 Overflow, X = 1 Ti 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 117manual page 43, and on the inside front cover. Lecture #5 26 August 26, 2010 August 26, 2010 Lecture #5 IE – Interrupt Enable Configuring Interrupts – pg 42 and 39 of LITEC Manual Interrupt enable bit for Timer 0 is ET0 (bit 1) in the IE register (see C8051 manual page 119) (see Overflow of Timer 0 will set the TF0 flag (bit 5) in the TCON register (see C8051 manual page 231) TCON register (see C8051 manual page 231) August 26, 2010 Lecture #5 28 August 26, 2010 Lecture #5 29 7 Lecture #5 August 26, 2010 Timer & Interrupt Summary Timers can be used to count passage of time (focus on Timer 0) Worksheet #4 Now that we have some basic understanding of using timers and interrupts to keep track of time using timers and interrupts to keep track of time, lets try an application of these concepts. Laboratory Worksheet #4 for Lab 1, part 2 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 Lecture #5 30 30 Use hardware already built in Lab 1, part 1 Copy code from website (Sample Codes) Compile download and run program and answer Compile, download and run program and answer questions questions on worksheet Enter your procedure and results in Lab Notebook Have a TA check your answers Lecture #5 31 August 26, 2010 August 26, 2010 Sample Code - Worksheet #4 for Lab 1, part 2 This code will demonstrate the use of a T0 interrupt to count the number of T0 overflows interrupt to count the number of T0 overflows that that occur while a slide switch connected to P3.0 is in the OFF position Today’s Lab Work If not done – check off for Lab 1, part 1 Finish Laboratory Worksheet #4 #4 Wire circuit for Lab 1, part 2 Start writing code for Lab 1, part 2 Rate: clock cycles/12 (CKCON) (CKCON) Number of bits: 16 bit counting (TMOD) (TMOD) Uses software to start and stop, TR0 (TMOD, TCON) software to start and stop TR0 TCON Enable interrupts for Timer 0 (IE) (IE) Next Class Class Homework #5 is due at beginning of class CheckCheck-off Lab 1, part 2 Can use this code as a reference when writing reference your code for Lab 1, part 2 August 26, 2010 Lecture #5 32 August 26, 2010 Lecture #5 33 8 Lecture #5 August 26, 2010 Counter Overflows Counters have a size based on the number of memory bits assigned to the counter memory bits assigned to the counter TMOD – Timer Mode Register Be able to configure either T0 or T1 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 interrupt Lecture #5 34 August 26, 2010 Lecture #5 35 August 26, 2010 9 ...
View Full Document

Ask a homework question - tutors are online