Lecture09_Exam_1_review - Exam 1 review lecture September...

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 September 22, 2010 Next Class: Exam1 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 Number Systems we use in LITEC: decimal (base 10) binary (base 2) hexadecimal (base 16) 9/22/2010 Exam 1 review lecture 1 9/22/2010 Exam 1 review lecture 2 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 9/22/2010 Converting: Binary to Hexadecimal 0 1 1 1 1 0 1 1 Most Significant Bit Least Significant Bit 0 1 1 1 1 0 1 1 7 01111011 = 0x7B When the answer is ‘0’, fill the rest of the byte with leading zeros Exam 1 review lecture 3 9/22/2010 Exam 1 review lecture B 4 1 Exam 1 review lecture September 22, 2010 Conversion Table Binary 0000 0001 0010 0011 0100 0101 0110 0111 1000 1001 1010 1011 1100 1101 1110 1111 9/22/2010 Common Common Digital Gates Inverter X Q Decimal 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Exam 1 review lecture Hex 0x0 0x1 0x2 0x3 0x4 0x5 0x6 0x7 0x8 0x9 0xA 0xB 0xC 0xD 0xE 0xF 5 7404 X 0 1 Q 1 0 Buffer Buffer X Q X 0 1 Q 0 1 7407 7465 OR X Y 7432 Q X 0 0 1 1 Y 0 1 0 1 Q 0 1 1 1 6 9/22/2010 Exam 1 review lecture Common Digital Gates AND X Y Q Operators Operators Q 0 0 0 1 Q 1 1 1 0 7408 X 0 0 1 1 Y 0 1 0 1 Y 0 1 0 1 There are many operators available in C to perform operations on numerical values perform operations on numerical values NAND NAND X Y Q 7400 X 0 0 1 1 There are many, many other digital circuits, but these are the common ones we will use. 9/22/2010 Exam 1 review lecture 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 7 9/22/2010 2 Exam 1 review lecture September 22, 2010 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. 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 conduct a bitwise OR operation bitwise OR operation 0x04: 0000 0100 XXXX X 1X X Note: P2.2 is high, rest are unchanged Alternate form of writing this: P2 |= 0x04; P2 9/22/2010 Exam 1 review lecture 9 Application to Microcontroller Another application – testing the status on one or more input pins or more input pins. 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; (X can be 0 or 1) P3: XXXX XXXX 0x10: 0001 0000 conduct a bitwise AND operation X 000 0 test = 000 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 9/22/2010 Exam 1 review lecture 10 Bitwise operations, example { P2 = 0x19; P2 |= 0xC1; P2 &= ~0x33; } // assignment operation // bitwise OR // bitwise AND Creating Digital Inputs The usual implementation: Vcc P2 = 0x19 0 0 0 1 1 0 0 1 To input pin R P2 |= 0xC1 1 1 0 1 1 0 0 1 P2 &= ~0x33 9/22/2010 1 1 0 0 1 0 0 0 Exam 1 review lecture 11 9/22/2010 Exam 1 review lecture 12 3 Exam 1 review lecture September 22, 2010 Application in Circuit Connect an LED and resistor in series Configuring I/O Ports Here is an example using pins 2-7 of Port 2 and pins 0-2 of 20Port 3 for digital input (open-drain, set to 0) and pins 0-1 of (open0Port 2 and 3-7 of Port 3 for digital output (push-pull, set to 1). Port and of Port for digital output (push set to 1). Choose resistor to limit Choose resistor to limit current in LED to rated value in LED to rated i<=10 mA Can use an output pin on microcontroller to turn on/off Put these commands in a function that you will call at the beginning of the program 7654 3210 7654 3210 Port 2 config: 0x03 = 0000 0011 config: Port 3 config: 0xF8 = 1111 1000 config: Power +5V Current (i) Power +5V Ground 0V Logic High – no current flow Logic Low – current flow Use buffer gate to protect microcontroller (acts as a current sink) 9/22/2010 Exam 1 review lecture 13 void Port_Init(void) Port_Init(void) { P2MDOUT = 0x03; // P2 |= 0xFC; // P3MDOUT = 0xF8; // P3 |= 0x07; // } Pins 0-1 0Set pins Pins 3-7 3Set pins pushpush-pull, pins 2-7 open-drain 2- open22-7 of P2 pushpush-pull, pins 0-2 open-drain 0- open00-2 of P3 9/22/2010 Exam 1 review lecture 14 Sample Code 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 Timers – see manual 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 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 15 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 9/22/2010 9/22/2010 Exam 1 review lecture 16 4 Exam 1 review lecture September 22, 2010 Timers Given the clock frequency CKCON: Clock Control Register Calculate the count rate (i.e., counts per second) the count rate (i counts per second) Calculate the time per counter overflow 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 Exam 8192 lecture 213 1 review counts for SYSCLK 12, use Example: clock frequency, 22.1184 MHz 9/22/2010 17 9/22/2010 Exam 1 review lecture 18 TMOD – Timer Mode Register Be able to configure either T0 or T1 Configuring Timer 0 TMOD (see pg 38 of LITEC manual or 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. 9/22/2010 Exam 1 review lecture 19 9/22/2010 Exam 1 review lecture 20 5 Exam 1 review lecture September 22, 2010 Configuring Timer 0 TCON (see p 39 of LITEC manual or p 231 of C8051 manual ) Interrupts – second major topic for today It was mentioned previously that an event can be triggered when an overflow occurs. or disable counting Bit Bit 4 – enable or disable counting 0 will disable Timer 0 1 will enable Timer 0 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. Same warning – don’t change 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. Exam 1 review lecture 22 BitBit-addressable Special Function Registers You write the code for the ISR th th ISR 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 9/22/2010 9/22/2010 Interrupt Service Routine 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 24 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 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. Exam 1 review lecture 23 9/22/2010 9/22/2010 Exam 1 review lecture 6 Exam 1 review lecture September 22, 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 IE: enable interrupt TCON: interrupt flag (handled automatically) ISR: function that runs when interrupt occurs Exam 1 review lecture 26 26 9/22/2010 Exam 1 review lecture 25 9/22/2010 Analog-toAnalog-to-Digital Conversion 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 9/22/2010 A/D Conversion on the C8051 ADC1 is connected to Port 1 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 100 ? 200 300 inp x inp L ans x ref L inp H inp L ref H ref L Exam 1 review lecture 27 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 9/22/2010 7 Exam 1 review lecture September 22, 2010 A Sample 4-Bit A/D Conversion 4with Successive Approximation 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 9/22/2010 SFRs associated with ADC1 P1MDIN – Port 1 Analog Input (LITEC manual pg 55, C8051 manual pg 165, 174) Too high 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 A “1” sets them for digital input input signal Previous best Previous best Too high Check Good digital approximations 1010 1001 1000 1000 1000 1001 1100 Too low Also, the pins of P1 to be set to “open drain” in P1MDOUT (“0”s) and write a “1” to them in P1 iterations Exam 1 review lecture 29 9/22/2010 Exam 1 review lecture 30 SFRs associated with ADC1 REF0CN – Reference Control Register (LITEC manual pg 56, C8051 manual page 92) C8051 SFRs associated with ADC1 ADC1CF – ADC1 Configuration Register (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.) Set gain of internal amplification to 1 9/22/2010 Exam 1 review lecture 31 9/22/2010 Exam 1 review lecture 32 8 Exam 1 review lecture September 22, 2010 SFRs associated with ADC1 AMX1SL – AMUX1 Channel Select Register manual pg 58 C8051 manual pg 79) (LITEC manual pg 58, C8051 manual pg 79) SFRs associated with ADC1 ADC1CN – ADC1 Control Register (LITEC manual pg 57, C8051 C8051 manual pg 80) Select which pin of P1 to use for conversion Enable ADC1 Set “Start of conversion mode” Start conversion Flag to say conversion is done 34 34 9/22/2010 Exam 1 review lecture 33 9/22/2010 Exam 1 review lecture SFRs associated with ADC1 ADC1 – ADC1 Data Word Register (C8051 manual pg 81) manual pg 81) A/D Conversion SFR Summary In program initialization: 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: = bbb; //bbb: 000-111 000 Set pin to convert with AMX1SL: AMX1SL = X; AMX1SL //X: 0-7 0 Clear conversion complete bit: ADC1CN &= ~0x20; Start conversion: ADC1CN |= 0x10; ADC1CN Wait for conversion complete: while((ADC1CN & 0x20) == 0x00); while((ADC1CN The result after an A/D conversion Code can use the value found in this register after the conversion is complete Read Results: Access results register: ADresult = ADC1; ADresult Exam 1 review lecture 36 9/22/2010 Exam 1 review lecture 35 35 9/22/2010 9 Exam 1 review lecture September 22, 2010 Basic Circuit Laws Ohm’s Law: iR + vR – R Analog Circuits Voltage Dividers By KCL, the current through R1 = the current through R2 = i Vcc By KVL: v i – v2 + + v4 – R R Ri v R R vcc v1 v2 R + By Ohm’s Law: v1 R1i v2 R2i v1 – R1 i R2 Kirchhoff’s Voltage Law (KVL): Combine the above: + v1 – – v3 + v j j 0 v R i 1 1 2 2 V R R R v R i V RR 1 2 2 1 2 R + cc 1 v2 – cc 9/22/2010 Exam 1 review lecture 37 9/22/2010 Exam 1 review lecture 38 10 ...
View Full Document

Ask a homework question - tutors are online