Lecture17 - Lecture #17 April 7, 2010 Outline Lab 4 Analog...

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 #17 April 7, 2010 Outline Lab 4 Analog to Digital conversion (A/D) to Digital conversion (A/D) LCD display and number pad Suggestion – look at document on RPILMS Lab 4 A compass pair and a ranger pair merge to form a blimp team. For Lab 4, one car will have both sensors. The Th car speed is controlled by the ranger. The car heading is controlled by the compass. Lab 5 – gondola_info Has details of the port pins used in lab 5 and 6 Teams can consider setting up lab 4 to match the gondola The magnetic field varies greatly with position in the LITEC studio. The car can still follow the field. Run car on paper as part of check off. car on paper as part of check off. Measure and monitor the car battery voltage. Use ADC1 as was done for lab 2. Implement LCD display with number pad – new! In addition Steering – rudder fan Speed – thrust fan power Lecture #17 - Blimp 2 April 7, 2010 April 7, 2010 Lecture #17 - Blimp 3 Lab 4, merged code If your code is modular, i.e. it uses functions, then merging codes is quick and easy. Things to check Lab 4, merged code The PCA ISR will likely have to be modified. The main code should now have the following form: Define local variables Initialize everything Call steering calibration Maybe have a switch that allows you to by-pass the calibration if the center and extreme PWM signals are known. Variable names – are they repeated in the codes and will result in erroneous operation? Initialization functions – the merged code must initialize: Sys_Init (Do this first.) Port_Init SMB_Init PCA_Init (This will have to change because you need to initialize two CCMs - Capture/Compare Modules.) A_D_Init (Bring this back from the game lab.) Start infinite loop If new_heading – call steering control function If new_ranger – call speed control function At a reasonable rate – print the battery voltage, the desired heading, the actual heading and the range. A few times per second is suggested End infinite loop Put the rest of the code in the functions. 4 April 7, 2010 Lecture #17 - Blimp 5 April 7, 2010 Lecture #17 - Blimp 1 Lecture #17 April 7, 2010 Lab 4, merged code Code space – not an issue Variable space - will be precious Lab 4, merged hardware Easy But remember remember 128 bytes of space for variables i2c.h uses a few bytes xdata is available if needed. xdata int variable1; /* puts variable1 into xdata */ Best used for variables that are only changed a limited number of times. xdata is flash that is written in 512 byte increments. Changing the value of a xdata variable can be slow and there is a 100 100,000 write cycle life expectancy. write cycle life expectancy Reading values from xdata isn’t a problem Good variable to put into xdata – center_pw, gain constants, … Not so good – error, heading, range, temp_pw, … Both sensors and the LCD display use the same SMBus Daisy chain the SDA line from one sensor to the next. Daisy chain the SCL line from one sensor to the next. There should only be one pull-up resistor per line. Add a 100uF (or 10uF) capacitor between the +5V line and ground next to the ranger. The ranger draws a surge of current for each ping. ranger draws surge of current for each ping. That surge adds noise to the power line, which the capacitor helps clean up. Remember that 3 other sections will also use the car, so you may have to reconfigure the sensors each class. 6 April 7, 2010 Lecture #17 - Blimp 7 April 7, 2010 Lecture #17 - Blimp LCD and Keypad New fall 2009 semester: Liquid Crystal Di Display and number keypad. Layout for LCD + Uses I2C, so set up is simple Velcro to your car But return it to the center table at the end of class. Use for: Desired heading - input Gain constants - input Actual heading, range, pulse widths, … - output Add Velcro to protoboard. Mount LCD/keypad. Return at end of class!!! April 7, 2010 Lecture #17 - Blimp 8 April 7, 2010 Lecture #17 - Blimp 9 2 Lecture #17 April 7, 2010 Wiring for LCD LCD and Keypad Functions i2c.h + 4-wire ribbon cable Black is GND White is SCL SC Gray is SDA Violet is +5V void lcd_print(const char *fmt, ...) lcd_print(const *fmt, Example to print to LCD: LCD lcd_print(“My lcd_print(“My heading: %d”, heading); void lcd_clear(); lcd_clear(); Call lcd_clear(); to clear LCD text lcd_clear(); char read_keypad(); /* returns ASCII */ read_keypad(); Example to read key: myVariable = read_keypad(); read_keypad(); April 7, 2010 Lecture #17 - Blimp 10 April 7, 2010 Lecture #17 - Blimp 11 Full ASCII Character Set Keypad Keypad returns ASCII representation 0 is returned as a decimal 48, 0x30 1 is returned as a decimal 49, 0x31 … 9 is returned as a decimal 57, 0x39 # is returned as a decimal 35, 0x23 * Is returned as a decimal 42, 0x2A Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex | Char Dec Oct Hex ------------------------------------------------------------------------------------(nul) nul) 0 0000 0x00 | (sp) 32 0040 0x20 | @ 64 0100 0x40 | ` 96 0140 0x60 (soh) soh) 1 0001 0x01 | ! 33 0041 0x21 | A 65 0101 0x41 | a 97 0141 0x61 (stx) stx) 2 0002 0x02 | " 34 0042 0x22 | B 66 0102 0x42 | b 98 0142 0x62 (etx) etx) 3 0003 0x03 | # 35 0043 0x23 | C 67 0103 0x43 | c 99 0143 0x63 (eot) eot) 4 0004 0x04 | $ 36 0044 0x24 | D 68 0104 0x44 | d 100 0144 0x64 (enq) enq) 5 0005 0x05 | % 37 0045 0x25 | E 69 0105 0x45 | e 101 0145 0x65 (ack) ack) 6 0006 0x06 | & 38 0046 0x26 | F 70 0106 0x46 | f 102 0146 0x66 (bel) bel) 7 0007 0x07 | ' 39 0047 0x27 | G 71 0107 0x47 | g 103 0147 0x67 (bs) bs) 8 0010 0x08 | ( 40 0050 0x28 | H 72 0110 0x48 | h 104 0150 0x68 (ht) (ht) 9 0011 0x09 | ) 41 0051 0x29 | I 73 0111 0x49 | i 105 0151 0x69 (nl) nl) 10 0012 0x0a | * 42 0052 0x2a | J 74 0112 0x4a | j 106 0152 0x6a (vt) vt) 11 0013 0x0b | + 43 0053 0x2b | K 75 0113 0x4b | k 107 0153 0x6b (np) np) 12 0014 0x0c | , 44 0054 0x2c | L 76 0114 0x4c | l 108 0154 0x6c (cr) cr) 13 0015 0x0d | 45 0055 0x2d | M 77 0115 0x4d | m 109 0155 0x6d (so) 14 0016 0x0e | . 46 0056 0x2e | N 78 0116 0x4e | n 110 0156 0x6e (si) si) 15 0017 0x0f | / 47 0057 0x2f | O 79 0117 0x4f | o 111 0157 0x6f (dle) 16 0020 0x10 | 0 dle) 48 0060 0x30 | P 80 0120 0x50 | p 112 0160 0x70 (dc1) 17 0021 0x11 | 1 49 0061 0x31 | Q 81 0121 0x51 | q 113 0161 0x71 (dc2) 18 0022 0x12 | 2 50 0062 0x32 | R 82 0122 0x52 | r 114 0162 0x72 (dc3) 19 0023 0x13 | 3 51 0063 0x33 | S 83 0123 0x53 | s 115 0163 0x73 (dc4) 20 0024 0x14 | 4 52 0064 0x34 | T 84 0124 0x54 | t 116 0164 0x74 (nak) 21 0025 0x15 | 5 21 0025 0x15 53 0065 0x35 | U 53 0065 0x35 85 0125 0x55 | u 85 0125 0x55 117 0165 0x75 117 0165 0x75 (syn) 22 0026 0x16 | 6 syn) 54 0066 0x36 | V 86 0126 0x56 | v 118 0166 0x76 (etb) 23 0027 0x17 | 7 etb) 55 0067 0x37 | W 87 0127 0x57 | w 119 0167 0x77 (can) 24 0030 0x18 | 8 56 0070 0x38 | X 88 0130 0x58 | x 120 0170 0x78 (em) em) 25 0031 0x19 | 9 57 0071 0x39 | Y 89 0131 0x59 | y 121 0171 0x79 (sub) 26 0032 0x1a | : 58 0072 0x3a | Z 90 0132 0x5a | z 122 0172 0x7a (esc) 27 0033 0x1b | ; 59 0073 0x3b | [ 91 0133 0x5b | { 123 0173 0x7b (fs) fs) 28 0034 0x1c | < 60 0074 0x3c | \ 92 0134 0x5c | | 124 0174 0x7c (gs) gs) 29 0035 0x1d | = 61 0075 0x3d | ] 93 0135 0x5d | } 125 0175 0x7d (rs) rs) 30 0036 0x1e | > 62 0076 0x3e | ^ 94 0136 0x5e | ~ 126 0176 0x7e (us) 31 0037 0x1f | ? 63 0077 0x3f | _ 95 0137 0x5f | (del) 127 0177 0x7f April 7, 2010 Lecture #17 - Blimp 12 April 7, 2010 Lecture #17 - Blimp 13 3 Lecture #17 April 7, 2010 Character & Decimal Values unsigned char a, b, c; a = getchar(); getchar(); // Hit ’5’ key printf(”Key: ASCII = %c, DECIMAL = %d”, a, a); %c %d // Prints: Key: ASCII = 5, DECIMAL = 53 c = a – 0x30; // Subtract 48 decimal // c = a – ’0’ also works because the character ’0’ = 48 decimal printf(”Key: printf(”Key: ASCII = %c, DECIMAL = %d”, c, c); %c %d // Prints: Key: ASCII = , DECIMAL = 5 // The ASCII symbol for 5 is a non-printing character non// Hit ’8’ key // b = getchar(); c = c * 10 + (b – 0x30); (b printf(”c = %d”,c); %d”,c); // Prints: c = 58 // The 2 keystrokes were converted into a decimal value of 58 // read_keypad() works the same way as getchar() read_keypad() getchar() Lab 4 Use the LCD functions Input desired heading Can still be “menu”. 2 is north, 3 is north-east, … north Input steering gain – one option is to have use keys to increment or decrement the steering gain. After initialization – output relevant values Actual heading, actual range, battery voltage Pulsewidths (maybe just for debugging.) April 7, 2010 Lecture #17 - Blimp 14 April 7, 2010 Lecture #17 - Blimp 15 Lab 4 A/D (review) analog to digital conversion Monitoring the Blimp battery voltage will allow one to retrieve the blimp before loss allow one to retrieve the blimp before loss of of control To prepare, you will monitor the car battery voltage. There are two A/D converters on our version of the C8051. We will use ADC1, th C8051 ADC1 Analog to Digital Converter #1. SFRs associated with ADC1 P1MDIN – Port 1 Analog Input (C8051 manual pg 165, 174) (C8051 Set pin(s) of P1 to be analog input by writing a “0” to them in this register A “1” sets them for digital input Don’t use P1, pins 0 -3. These are wired for PCA on blimp. CEXn pins of Port 1 should be push/pull, A/D pin of Port 1 should be “open drain” in P1MDOUT (“0”s) and write a “1” to them in P1 8-bit A/D conversion Connected to Port 1 Lecture #17 - Blimp 16 April 7, 2010 Lecture #17 - Blimp 17 April 7, 2010 4 Lecture #17 April 7, 2010 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 April 7, 2010 Lecture #17 - Blimp 18 April 7, 2010 Lecture #17 - Blimp 19 SFRs associated with ADC1 AMX1SL – AMUX1 Channel Select Register (C8051 (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. Use Port1, pin 5. This is the pin used in the gondola. Don’t use P1 bits 0 – 3, these are wired for the PCA. Enable ADC1 Set “Start of conversion mode” Start conversion Flag to say conversion is done 21 21 April 7, 2010 Lecture #17 - Blimp 20 April 7, 2010 Lecture #17 - Blimp 5 Lecture #17 April 7, 2010 SFRs associated with ADC1 ADC1 – ADC1 Data Word Register (C8051 manual pg 81) manual pg 81) A/D Conversion SFR Summary In program initialization: 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 Set pin to convert with AMX1SL: AMX1SL = X; AMX1SL //X: 0-7 0Clear conversion complete bit: ADC1CN &= ~0x20; ADC1CN Start conversion: ADC1CN |= 0x10; ADC1CN Wait for conversion complete: while((ADC1CN & 0x20) == 0x00); while((ADC1CN Access results register: ADresult = ADC1; ADresult Lecture #17 - Blimp 23 Conduct A/D conversion: Read Results: April 7, 2010 Lecture #17 - Blimp 22 22 April 7, 2010 Lab 4 We can do an A/D conversion over the voltage range of 0 – 2.4V. The car battery is at 14V when on the charger. The blimp battery is at about 7.4V when fully charged. We need to reduce the measured voltage to be proportional to the battery voltage but in the range of 0 to 2.4V We will use a voltage divider. Lab 4 For a voltage divider: + v1 – + v2 – Vcc R1 i R2 v R i 1 1 V R1 R2 cc V R1 R2 cc R 2 R 1 v R i 2 2 We need v2 < 2.4V when vCC ~15V We also want 0.5mA < i < 2mA Pick R2 to meet these requirements. requirements. (R1 is fixed at 10kΩ on the car.) April 7, 2010 Lecture #17 - Blimp 25 April 7, 2010 Lecture #17 - Blimp 24 24 6 Lecture #17 April 7, 2010 What should you know about I2C? Slide 1 Synchronous, two wire serial communication. At any one time, only one Master with one or more Slaves What should you know about I2C? Slide 2 SMBus and I2C are the same for our application All devices on the bus must be configured as open drain All devices on the bus must be configured as open drain, so the SDA and SCL lines need pull up resistors. We will run our bus at 100kHZ There are five sfr, special function registers associated with the I2C bus. While the C8051 can be a slave, we won’t use it that way C8051 is the Master and the sensors are slaves Sends the clock Starts the communication by sending A Start signal, slave address, and a read/write bit Master If it is a write, this is followed by one or more data bytes If it is a read, then the Master looks for data from the slave Slave Looks for its address and R/W bit Either accepts data or sends data as appropriate Any device receiving data must send an ACK, acknowledged signal. SMB0CN – SMBus 0 Control – Used to control the bus SMB0STA SMB SMB0STA – SMBus 0 Status – Read to know present status St SMB0CR – SMBus 0 Clock Register – Set for 100kHz clock SMB0ADR – SMBus 0 Address – We don’t use, slave address SMB0DAT – SMBus 0 data – data for reads and writes Lecture #17 - Blimp 27 April 7, 2010 Lecture #17 - Blimp 26 April 7, 2010 What should you know about I2C? Slide 3 Initialize I2C void SMB0_Init() { SMB0CR = 0x93; // set SCL to 100KHz ENSMB = 1; // enable SMBUS0 } Functions will read and write to bits of SMB0CN Today and Next Class Today Work on Lab 4 No lecture Monday/Tuesday – in-class LMS part of Exam 2 inTuesday evening – 7-8:30, written Exam 2 DCC 308 Next 2 classes April 19 and 20 – Exam 2 There are sbits for SMB0CN – BUSY, ENSMB, STA, STO, SI, AA, SMBFTE, SMBTOE (we don’t use the last 2.) I2C_read_data(…), I2C_write_data(…) Understand Understand how to use your higher level functions: April 7, 2010 Lecture #17 - Blimp 28 April 7, 2010 Lecture #17 - Blimp 29 7 ...
View Full Document

This note was uploaded on 04/08/2011 for the course ENGR 2350 taught by Professor Fukanari during the Spring '08 term at Rensselaer Polytechnic Institute.

Ask a homework question - tutors are online