Exam 1 Review - Exam 1 review lecture January 14, 2010 Next...

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: Exam 1 review lecture January 14, 2010 Next Class: Exam1 Number Systems 90 minutes Open book, open notes book open notes No sharing, no other applications up on laptops No lab work until 90 minutes have passed. Number Systems we use in LITEC: 1/14/2010 Exam 1 review lecture 1 1/14/2010 Converting: Decimal to Binary 123 ÷2 61 rem 1 ÷2 30 rem 1 ÷2 15 rem 0 ÷2 7 rem 1 ÷2 3 rem 1 ÷2 1 rem 1 ÷2 0 rem 1 0 1 1 1 1 0 1 decimal (base 10) binary (base 2) hexadecimal (base 16) Exam 1 review lecture 2 Converting: Binary to Hexadecimal 1 Most Significant Bit 0 Least Significant Bit 1 1 1 1 0 1 1 B 7 01111011 = 0x7B When the answer is ‘0’, fill the rest of the byte with leading zeros 1/14/2010 Exam 1 review lecture 3 1/14/2010 Exam 1 review lecture 4 1 Exam 1 review lecture January 14, 2010 Common Common Digital Gates Conversion Table Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 1/14/2010 Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Inverter Hex 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF Buffer Buffer Y Q 7408 OR X Y Q 7400 There are many, many other digital circuits, but these are the common ones we will use. 1/14/2010 Exam 1 review lecture X Q 7432 5 1/14/2010 X 0 0 1 1 Q 1 0 X 0 1 Q Y Q 0 1 Y 0 1 0 1 Q 0 1 1 1 Exam 1 review lecture 6 Operators Operators X 0 0 1 1 Y 0 1 0 1 Q 0 0 0 1 X 0 0 Y 0 1 Q 1 1 1 1 0 1 1 0 There are many operators available in C to perform operations on numerical values perform operations on numerical values NAND NAND X 7407 7465 Common Common Digital Gates X X 0 1 Q 7404 Exam 1 review lecture AND X 7 1/14/2010 mathematical: +, -, *, /, % (mod - remainder) relational tests: <, >, <=, >= equality tests: ==, != logical tests: || (OR), && (AND) bitwise: & (AND), | (OR), ^ (XOR), << (left shift), >> (right shift) (right shift) unary: ++ (increment), -- (decrement), ~ (ones complement), ! (logical negation) others listed in lab manual starting on p. 20 Exam 1 review lecture 8 2 Exam 1 review lecture January 14, 2010 Application to Microcontroller Application to Microcontroller You can use these operators to read or change the state of bit or groups of bits change the state of a bit or groups of bits in in an I/O port. Another application – testing the status on one or more input pins or more input pins. Make pin 2 of Port 2 (P2.2) high and leave others as they were: P2 = P2 | 0x04; P2 P2: XXXX XXXX (X can be 0 or 1) 0x04: 0000 0100 0x04: 0000 0100 conduct a bitwise OR operation bitwise OR operation XXXX X 1X X Note: P2.2 is high, rest are unchanged Alternate form of writing this: P2 |= 0x04; P2 1/14/2010 Exam 1 review lecture 9 Test if pin 4 of Port 3 (P3.4) is high or low: Statement if(P3) will check status of entire port if(P3) entire If P3 = 0000 0000, statement is false; anything else is true To check one pin (P3.4), use: test = P3 & 0x10; P3: XXXX XXXX (X can be 0 or 1) 0x10: 0001 0000 conduct a bitwise AND operation test = 000 X 000 0 Note: just status of pin 4 is preserved test will be true if P3.4 was high, or false if it was low This is called bit-masking (the 0x10 is the mask value) bit 1/14/2010 Bitwise operations, example Exam 1 review lecture 10 Creating Digital Inputs { P2 = 0x19; P2 |= 0xC1; P2 &= ~0x33; // assignment operation // bitwise OR // bitwise AND The usual implementation: Vcc } P2 = 0x19 0 0 0 1 1 0 0 1 To input pin P2 |= 0xC1 0xC1 P2 &= ~0x33 1/14/2010 1 1 1 1 0 0 Exam 1 review lecture 1 0 1 1 0 0 0 0 R 1 0 11 1/14/2010 Exam 1 review lecture 12 3 Exam 1 review lecture January 14, 2010 Application in Circuit Configuring I/O Ports Here is an example using pins 2-7 of Port 2 and pins 0-2 20of Port 3 for digital input and pins 0-1 of P2 and 3-7 of P3 03for digital output. Connect an LED and resistor in series Choose resistor to limit Choose resistor to limit current in LED to rated value in LED to rated Can use an output pin on microcontroller to turn on/off i<=10 mA Power +5V Current (i) Ground 0V Logic High – no current flow Logic Low – current flow Power +5V Use buffer gate to protect microcontroller (acts as a current sink) 1/14/2010 Exam 1 review lecture 13 Put these commands in a function that you will call at the beginning of the program void Port_Init(void) Port_Init(void) { P2MDOUT = 0x03; 0x03; P2 |= 0xFC; P3MDOUT = 0xF8; P3 |= 0x07; } 1/14/2010 /* Pins 0-1 push-pull, pins 2-7 open-drain */ Pins push pins open /* Set pins 2-7 of P2*/ 2/* Pins 3-7 push-pull, pins 0-2 open-drain */ 3- push0- open/* Set pins 0-2 of P3 */ 0- Exam 1 review lecture Sample Code Timers – see manual Remember, we can use logic operations to change state of a pin, if P2.2 is connected to LED0 state of pin if P2 is connected to LED0 14 In 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 1/14/2010 To turn on LED0, force P2.2 to go low P2 &= ~0x04; The C8051 also has the ability to change the state of individual bits on certain ports sbit at 0xB4 LED0 ;/* LED0 associated with P2.2 */ LED0 = 0; /* Light LED0 */ Same can be done with input ports to read state of individual pins Exam 1 review lecture Timer 0, Timer 1, … Timer 4 Timer 0 is of interest now Timers are extra hardware on chip 15 1/14/2010 Don’t require the CPU – independent of C8051 Exam 1 review lecture 16 4 Exam 1 review lecture January 14, 2010 Timers CKCON: Clock Control Register Given the clock frequency Calculate the count rate (i.e., counts per second) the count rate (i counts per second) Calculate the time per counter overflow Example: clock frequency, 22.1184 MHz seconds 1 1 4.5211 10 8 freq 22.1184 106 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 Exam 8192lecture 213 1 review counts for SYSCLK 12, use 1/14/2010 17 1/14/2010 TMOD – Timer Mode Register Exam 1 review lecture 18 Configuring Timer 0 TMOD (see pg 38 of LITEC manual or Be able to configure either T0 or T1 pg 232 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 Warning: Don’t change these bits, used for printf. 1/14/2010 Exam 1 review lecture 19 1/14/2010 Exam 1 review lecture 20 5 Exam 1 review lecture January 14, 2010 Configuring Timer 0 Interrupts – second major topic for today TCON (see p 39 of LITEC manual or p 231 of C8051 manual ) It was mentioned previously that an event can be triggered when an overflow occurs. 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 1/14/2010 Specific bits of bit-addressable SFRs can be bitaccessed with their individual names Example: TR0 = 1; // Sets bit 4 of TCON TR0 Exam 1 review lecture 21 Use the Priority Number that corresponds to the desired type of interrupt as the Interrupt number in the Interrupt function Interrupt Source Source Interrupt Vector Priority Interrupt type # Order Interrupt Source Source Interrupt Vector Priority Order 10 Top Comparator 0 Falling Edge 0x0053 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 0x001B 3 Timer 3 Overflow 0x0073 14 0x0023 4 ADC0 End of Conversion 0x007B 15 Timer 2 Overflow (or RXF2) 0x002B 5 Timer 4 Overflow 0x0083 16 Serial Peripheral Interface 23 0x0000 UART0 Interrupt numbers are found in the Kiel Software Cx51 User’s Kiel Guide page 120, which matches the Interrupt Priority Number in C8051 manual pages 117-118, and the LITEC manual page 43, 117and on the inside front cover. Reset External Interrupt 0 (/INT0) Timer 1 Overflow 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 Exam 1 review lecture 22 Interrupt Number Each type of interrupt used needs a different ISR 1/14/2010 Exam 1 review lecture (C8051 (C8051 Man. pg. 117, LITEC manual pg. 43) 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 } For this example, a variable can be incremented every time the ISR is executed to keep track of how many overflows have occurred. 1/14/2010 Interrupt Service Routine Original code is resumed upon completion of the ISR function You write the code for the ISR th th ISR BitBit-addressable Special Function Registers 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. 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 1/14/2010 Exam 1 review lecture 24 6 Exam 1 review lecture January 14, 2010 IE – Interrupt Enable Timer & Interrupt Summary 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 1/14/2010 Exam 1 review lecture 25 Analog-toAnalog-to-Digital Conversion 1/14/2010 1/14/2010 Analog voltages on the pins of P1 can be converted Analog voltages on the pins of P1 can be converted to to a digital representation An internal system reference exists for Vref with a value of 2.4V inp x inp L ans x ref L inp H inp L ref H ref L Exam 1 review lecture 26 26 ADC1 is connected to Port 1 100 ? 200 300 Exam 1 review lecture A/D Conversion on the C8051 What is Analog-to-Digital (A/D) conversion? Analog-toA/D A/D conversion is the process by which a th continuous continuous signal (generally a voltage) is mapped mapped at a particular point in time to a digital value. Generic example of mapping: 0.0 2.5 5.0 IE: enable interrupt TCON: interrupt flag (handled automatically) ISR: function that runs when interrupt occurs 27 1/14/2010 0V maps to 0x00 (0) 1.2V maps to 0x80 (128) maps to 0x80 (128) 2.4V maps to 0xFF (255) An external reference may also be chosen, but we will use the internal reference. Exam 1 review lecture 28 7 Exam 1 review lecture January 14, 2010 A Sample 4-Bit A/D Conversion 4with Successive Approximation P1MDIN – Port 1 Analog Input (LITEC manual pg 55, C8051 manual pg 165, 174) Previous best Good Set pin(s) of P1 to be analog input by writing a “0” to Set pin(s) of P1 to be analog input by writing to them them in this register 1001 A “1” sets them for digital input Check 1001 Too high 1000 input signal Previous best Too low Too high Also, the pins of P1 to be set to “open drain” in P1MDOUT (“0”s) and write a “1” to them in P1 1010 1000 1100 digital approximations 1000 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 SFRs associated with ADC1 iterations 1/14/2010 Exam 1 review lecture 29 SFRs associated with ADC1 (LITEC manual pg 56, C8051 manual pg 79) manual pg 56 C8051 manual pg 79) Turn on 2.4V internal reference Uses jumper on circuit board to VREF pin to VREF1 pin (already configured on board.) 1/14/2010 Exam 1 review lecture 30 ADC1CF – ADC1 Configuration Register (LITEC manual pg 56, C8051 manual page 92) C8051 Exam 1 review lecture SFRs associated with ADC1 REF0CN – Reference Control Register 1/14/2010 31 1/14/2010 Set gain of internal amplification to 1 Exam 1 review lecture 32 8 Exam 1 review lecture January 14, 2010 SFRs associated with ADC1 SFRs associated with ADC1 AMX1SL – AMUX1 Channel Select Register (LITEC manual pg 58, C8051 manual pg 79) manual pg 58 C8051 manual pg 79) ADC1CN – ADC1 Control Register Select which pin of P1 to use for conversion (LITEC manual pg 57, C8051 C8051 manual pg 80) 1/14/2010 Exam 1 review lecture 33 SFRs associated with ADC1 34 34 In program initialization: (C8051 manual pg 81) manual pg 81) Exam 1 review lecture A/D Conversion SFR Summary ADC1 – ADC1 Data Word Register 1/14/2010 Enable ADC1 Set “Start of conversion mode” Start conversion Flag to say conversion is done The result after an A/D conversion Code can use the value found in this register after the conversion is complete Configure analog input pins – set desired A/D pins in P1MDOUT to “0” and P1 to “1” and PIMDIN to “0” “0” P1 “1” PIMDIN “0” Configure Reference – set internal reference by clearing REF0CN pin 3: REF0CN = 0x03; REF0CN Configure A/D converter gain – set to gain of 1: ADC1CF |= 0x01; ADC1CF Enable converter: ADC1CN = 0x80; ADC1CN Conduct A/D conversion: Set pin to convert with AMUX1SL: AMUX1SL = XX; AMUX1SL XX //XX: 0-7 0Clear conversion complete bit: ADC1CN &= ~0x20; ADC1CN Start conversion: ADC1CN |= 0x10; ADC1CN Wait for conversion complete: while((ADC1CN & 0x20) == 0x00); while((ADC1CN Read Results: 1/14/2010 Exam 1 review lecture 35 35 1/14/2010 Access results register: ADresult = ADC1; ADresult Exam 1 review lecture 36 9 Exam 1 review lecture January 14, 2010 Basic Circuit Laws Analog Circuits Ohm’s Law: v iR + vR R R i – Ri v Voltage Dividers By KCL, the current through R1 = the current through R2 = i Vcc By KVL: R vcc v1 v2 R R + R v2 R2i v1 R1i By Ohm’s Law: Kirchhoff’s Voltage Law (KVL): + – – v3 v1 + v4 – v j R V R R R v R i V RR v R i 0 1 1 j + cc 2 i v2 R2 – 2 2 1 Exam 1 review lecture + 1 1 2 1/14/2010 R1 – Combine the above: – v2 + v1 37 1/14/2010 cc 2 Exam 1 review lecture 38 10 ...
View Full Document

This note was uploaded on 10/22/2011 for the course ENGR 2410 taught by Professor Wallack during the Fall '09 term at Rensselaer Polytechnic Institute.

Ask a homework question - tutors are online