Preview only show first 10 pages with watermark. For full document please download

Ece 4510/5530 Microcontroller Applications Week 2 Lab 2 Preparations

ECE 4510/5530 Microcontroller Applications Week 2 Lab 2 Preparations Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences

   EMBED


Share

Transcript

ECE 4510/5530 Microcontroller Applications Week 2 Lab 2 Preparations Dr. Bradley J. Bazuin Associate Professor Department of Electrical and Computer Engineering College of Engineering and Applied Sciences Lab 2 Elements Hardware Development Parallel I/O ports (Chap. 7.5) LEDs (Chap. 4.11) Dip switches (Chap. 4.11) 7-segment display (Chap. 4.11, Chap. 7.6) Keypad (Chap. 7.9) Clock-recovery-generator (Chap. 6.6, 6.7) Software Development Environment Software control flow (Chap. 2.6, Chap. 5.4) Function calls (Chap. 5.6) Software delay loops (Chap. 2.10) ECE Basic Concepts of I/O I/O devices are also called peripheral devices. For microcontrollers, they are usually part of the IC For microprocessors, they are usually in separate ICs I/O devices are consist of circuitry (logic or a device) that exchange data with a computer processing unit (CPU). Examples data includes: switches, light-emitting diodes, cathoderay tube screens, printers, modems, keyboards, and disk drives. ECE 2510 Interface (Peripheral) Logic/IC (1 of 2) Logic/IC whose function is to synchronize data transfer between the CPU and I/O devices Consists of control registers, status registers, data direction latches, and control circuitry Has pins that are connected to the CPU and I/O port pins that are connected to the I/O devices Each interface chip has a chip enable signal input or inputs, when asserted, allow the interface chip to react to the data transfer request. Data transfer between an I/O device and the CPU can be proceeded bitby-bit or in multiple bits (parallel). For embedded devices, this is the peripheral register address! ECE 2510 Interface (Peripheral) Chip (2 of 2) Address decoder makes sure that each time one and only one peripheral device responds to the CPU s I/O request. Address Decoder from input device I/O pins to output device CE CE Interface chip 1 Interface chip 1 Microprocessor Data Bus ECE 2510 Figure 7.1 Interface chip, I/O devices, and microprocessor HCS12 Parallel Ports The number of pins available in each I/O port for HCS12 are (see mc9s12dp256.s and mc9s12dp256.h for names): PORTA 8 pins PA7 PA0 PORTB 8 pins PB7 PB0 PORTE 8 pins PE7 PE0 PTH 8 pins PH7 PH0 PTJ 4 pins PJ7, PJ6, PJ1, PJ0 PORTK 7 pins PK7, PK5 PK0 PTM 8 pins PM7 PM0 PTP 8 pins PP7 PP0 PTS 8 pins PS7 PS0 PTT 8 pins PT7 PT0 PORTAD1, PORTAD0 16 pins PAD15 PAD0 ECE Adapt9S12DP512 I/O Pins ECE HCS12 Parallel Ports (1 of 3) All I/O pins serve multiple functions. When a peripheral function is enabled, its associated pins cannot be used as general purpose I/O pins. Each I/O port has several registers to support its operation. Registers related to I/O ports have been assigned a mnemonic name and the user can use these names to refer to them movb #$FF, PortA ; output $FF to Port A ECE 2510 HCS12 Parallel Ports (2 of 3) All I/O ports (except PORTAD0 and PORTAD1) have an associated data direction register and a data register. The name of the data direction register is formed by adding the letters DDR as the prefix to the port name. For example, DDRA, DDRB, and DDRT. To configure a pin for output, write a 1 to the associated bit in the data direction register. To configure a pin for input, write a 0 to the associated bit in the data direction register. ECE 2510 movb #$FF,DDRA movb #0,DDRA bset DDRA,$81 ; configure port A for output ; configure port A for input ; configure Port A pin 7 and 0 for output HCS12 Parallel Ports (3 of 3) We use PORT as the prefix to the port name for ports A, B, E, and K. For the other ports, the register name is formed by adding letters PT as the prefix to the port name. For example, PTH, PTJ, PTM, PTP, PTS, and PTT. Output a value to a port is done by storing that value to the port data register address. movb #$FF,DDRH movb #$37,PTH ; configure Port H for output ; output the hex value 37 to port H Input a value from an input port is done by loading from the port data register address. movb #0,DDRH ; configure Port H for input ldaa PTH ; read data from port H into A ECE Port Integration Module (PIM) Parallel Ports the PT# ports: T, S, M, P, H, J A standard PIM port pin has the following minimum features: Input/output selection 5V output drive with two selectable drive strengths 5V digital and analog input Input with selectable pull-up or pull-down device Optional features: Open drain for wired-or connections Interrupt inputs with glitch filtering ECE 4510/ Block Diagram PT#: input/output data register PTI#: input register DDR#: data direction register ECE 4510/ Special Pin Controls Data direction (DDR) input or output Output level (I/O) same as PT describing the port value Reduced drive (RDR) full or 1/3 drive Pull enable (PER) enable a pull-up or pull-down Interrupt enable (PIE) for port P, H, and J only Pull select (PPS) - select whether pull-up or -down if interrupt is enabled, configure the sensitive interrupt edge (rising or falling. if PE is active, select either a pull-up or pull-down device. ECE 4510/ Port Registers Pin Control ECE 4510/ Memory Map Address Base: 0x240 (0x240 to 0x26F) ECE 4510/ C Include File #define PTS _P(0x0248) #define PTIS _P(0x0249) #define DDRS _P(0x024A) #define RDRS _P(0x024B) #define PERS _P(0x024C) #define PPSS _P(0x024D) #define WOMS _P(0x024E) //#define Reserved _P(0x024F) #define PTM _P(0x0250) #define PTIM _P(0x0251) #define DDRM _P(0x0252) #define RDRM _P(0x0253) #define PERM _P(0x0254) #define PPSM _P(0x0255) #define WOMM _P(0x0256) #define PTP _P(0x0258) #define PTIP _P(0x0259) #define DDRP _P(0x025A) #define RDRP _P(0x025B) #define PERP _P(0x025C) #define PPSP _P(0x025D) #define PIEP _P(0x025E) #define PIFP _P(0x025F) #define PTH _P(0x0260) #define PTIH _P(0x0261) #define DDRH _P(0x0262) #define RDRH _P(0x0263) #define PERH _P(0x0264) #define PPSH _P(0x0265) #define PIEH _P(0x0266) #define PIFH _P(0x0267) #define PTJ _P(0x0268) #define PTIJ _P(0x0269) #define DDRJ _P(0x026A) #define RDRJ _P(0x026B) #define PERJ _P(0x026C) #define PPSJ _P(0x026D) #define PIEJ _P(0x026E) #define PIFJ _P(0x026F) //#define Reserved _P(0x0270-0x027F) //#define Reserved _P(0x0257) ECE 4510/ Assembly Example This allows to use the symbolic name to access ports Ex: PORTA = $0000 PTH = $0260 PORTB = $0001 DDRH = $0262 See DDRA = $0002 PTT = $0240 mc9s12dp256.s DDRB = $0003 DDRT = $0242 LDAA #$FF STAA DDRA LDAA #$FF STAA PORTA ; Configure Port A for output ; Write to Port A ECE C Example This allows to use the symbolic name to access ports Ex: # Define PORTA 0x0000; # Define PTH 0x0260; # Define PORTB 0x0001; # Define DDRH 0x0262 # Define DDRA 0x0002; # Define PTT 0x0240 # Define DDRB 0x0003; # Define DDRT 0x0242 DDRA = 0xFF; PORTA = 0xFF; //Configure Port A for output //Write to Port A DDRH = 0xAA; PTH =0xAA; PTH &= ~0xAA; // every other bit output // set the outputs // clear the outputs ECE Interfacing with Output Devices Many embedded devices only require interfacing with simple input and output devices such as switches, light emitting devices, keypads, seven segment displays etc Interfacing with LED s: LED s are often used to indicate the system operation mode Whether the system is turned on Whether the system operation is normal Whether the system is in an error mode etc An LED can illuminate when it is forward biased and has sufficient current following through it The current required to light the LEDs that are used in the lab is nominally 10mA. (It isn t a car tail light or a traffic light!) ECE LED Driving by Port B Ex: Use Port B to drive eight LED s. Turn on each LED one at a time for 0.5s and then repeat the process. Schematic for interfacing LED bar with Port B HCS12 PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 1.5K Note: this is not the preferred way! Note: this is how the PTP bit 7 LED is connected. Figure 7.30 Circuit connection for example 7.3 ECE Interfacing with LEDs Three methods for interfacing with LED s Method A and B are only recommended for use with LED s that need only 1 to 2mA to produce enough brightness (most LEDs want 10 ma or more!) The circuit C is recommended for use with LED s that need larger current to turn on, the ECE 2510 labs, and all other ECE labs. Resistor value can be between 220 ohm and 1 Kohm Port pin R1 (a) positive direct drive Port pin R2 (b) inverse direct drive V CC Port pin V CC R3 74HC04 (c) buffered drive ECE 2510 Figure 7.29 An LED connected to a CMOS inverter through a current- limiting resistor. 21 Lab LED and Light Bar Connections Buffer the HC12 port (!!!) Inverting buffer 540 Non-inverting buffer 541 Provide current sinking for the LED Resistor between +5 and LED The resistor provides current limiting Resistor Value LED needs ~1.2 to 1.5 V drop LED wants ~ 10 ma or more I ~ from 8 to 12 ma 5V 1.2V 0.8V OL R mA ECE Interfacing with Output Devices To turn on the LED driven by pins PB7 PB0 one at a time, one should output the values $80, $40, $20, $10, $08, $04, $02, $01 every 0.5s Step1: Place the values $80, $40, , $01 in an array. Use index register X to point to the start of the array Step2: Output the value pointed to by X to Port B. Increment the pointer X Step3: Run the delay for 0.5s Step4: If X points to the end of array, reset X to point to the start of the array. Go to step2 Arbitrary light patterns of different lengths and time delays can be easily created! ECE Code in ASM ;The assembly program that performs the operation is as follows:.org $1000 led_tab:.byte $80,$40,$20,$10,$08,$04,$02,$01.byte $01,$02,$04,$08,$10,$20,$40,$80.text _main:: movb #$FF,DDRB ; configure port B for output forever: ldaa #16 ; initialize loop count to 8 ldx #led_tab ; use X as the pointer to LED pattern table led_lp: movb 1,x+,PTB ; turn on one LED ldy #5 ; wait for half a second jsr delayby100ms ; dbne a,led_lp ; reach the end of the table yet? bra forever ; start from beginning ECE Code in C (from text) The C language version of the program is as follows: #include c:\egnu091\include\hcs12.h #include c:\egnu091\include\delay.c main (void) { char led_tab[8] = {0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01, 0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; char i; DDRB = 0xFF; /* configure port B for output */ } while (1){ for (i = 0; i 16; i++) { PTB = led_tab[i]; delayby100ms(5); } } return 0; ECE Interfacing with Seven Segment Displays Seven segment displays are often used when the embedded products needs to display only a few digits Seven segment displays are mainly used to display decimal digits and a small subset of letters Although HCS12 devices have enough current to drive a seven segment display, it is not advisable to do when a HCS12 based embedded product needs to drive many other I/O devices ECE 2510 Best way is to use a buffer chip 74HC244 between microcontroller and seven segment display 74HC244 provides 5v and using a 330 ohm resistor between 74HC244 and seven segment display provides a current of 10mA, sufficient to illuminate an LED 26 7 Segment Display HCS12 PB6 PB5 PB4 PB3 PB2 PB1 PB0 74HC each a a b f c b g d e e c f d g common cathode Figure 7.31 Driving a single seven-segment display Note: 541 is an alternative buffer ECE BCD to 7 Segment Mapping ECE Done for discrete logic in an ECE2500 Lab! Example: Write to One 7-Segment Display Write a sequence of instructions to display a BCD value on the sevensegment display previously shown. Solution: Output the appropriate hex value to port B #include hcs12.inc .org $1000 BCD7:.byte $7E, $30, $6D, $79, $33, $5B, $5F, $70, $7F, $7B, $77, $1F $4E, $3D,$4F, $47 BCDVal =4.text _main:: ; BCD Value to be displayed movb #$FF,DDRB ; configure PORT B for output ldaa #BCDVal ; load the BCD Value ldx #BCD7 ldaa a,x ; load the 7 Segment Value staa PORTB ; output to 7 segment display swi What if we wanted additional Hex Outputs? ECE DIP Switches A switch is probably the simplest input device available To make input more efficient, a set of eight switches organized as a Dual inline package (DIP) is often used A DIP package can be connected to any input port with eight pins such as PortA, PortB etc When a switch is closed, the associated port input is 0, otherwise the associated port input is 1 Each port input is pulled up high via a 330 ohm or 1Kohm resistor when the associated switch is open ECE Connecting DIP Switches V CC SW DIP-8 10K PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 HCS12 Figure 7.39 Connecting a set of eight DIP switches to Port A of the HCS12 ECE Lab Dip Switch Connections Buffer the HC12 port (!!!) Inverting buffer 540 Non-inverting buffer 541 Provide isolation from switch Resistor between +5 and switch If you short the switch while probing, the resistor provides current limiting. Resistor Value 330 ohms or greater (10 kohm preferred) ECE Reading DIP Switches Steps Ex: Instruction to read data from DIP switches interfaced to PortA Step1: Define the corresponding Data Direction Register and Data Register of PortA Step2: Set the Data Direction Register of PortA to configure port as input port Step3: Read the data from the Data register of PortA according to the requirements of the program. Data from the DIP switches is always available in the Data Register of the corresponding port with which it s interfaced to ECE DIP to LED Example V CC ; Dip Switch Inputs ; LED Outputs.area prog(abs) PORTA = $00 PORTB = $01 DDRA = $02 DDRB = $03.text _main:: ldaa #$00 staa DDRA coma staa DDRB Loop: ldaa PORTA staa PORTB Bra Loop // Dip Switch Inputs // LED Outputs # define PORTA 0x00 # define PORTB 0x01 # define DDRA 0x02 # define DDRB 0x03 Char temp; void main(void) { DDRA = 0x00; // default DDRB = 0xFF; // output while(1) { temp = PORTA; PORTB = temp; } } // end main() SW DIP-8 10K PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 HCS12 Figure 7.39 Connecting a set of eight DIP switches to Port A of the HCS12 HCS12 PB7 PB6 PB5 PB4 PB3 PB2 PB1 PB0 1.5K ECE Figure 7.30 Circuit connection for example 7.3 Switch Bounce Mechanical switches have a common problem called contact bounce Mechanical switches or keypads are most popular due to their low cost and minimal strength of construction Instead of producing a single clean output pulse, pressing a mechanical switch generates a series of pulses because switch contacts do not come to rest immediately When the key is not presses, everything is fine In order to detect which key has been presses, controller needs to scan every key of keypad and may, due to contact bounce, determine that every low is a new key press (you get repeated characters when one was desired!) ECE Contact Switch Bounce The switch bounces or chatters in transitioning from one to zero. ECE De-bouncing The signal that falls and rises within a period of up to 5ms is likely a contact bounces Human being cannot press and release a switch in less than 20ms a de-bouncer could recognize that the switch is closed after the voltage is low for about 10ms and then could recognize after that the switch is open after the voltage is high for about 10ms. Both H/W and S/W solutions to the key bounce problem are available H/W solution includes an analog circuit that uses a resistor and a capacitor, and two digital solutions that uses S-R latches or CMOS buffers and double throw switches ECE Interfacing with Input Devices Set-Reset Latches A key switch can be de-bounced using the S-R latches Before being presses, the key is touching the set input and the Q voltage is high When pressed the key touches the reset position, the Q voltage will go Low If the key is bouncing and touching neither Set or Reset, both inputs are pulled low by the pull-down resistor. Since both Set and reset are low, the Q voltage will remain low and the key will be recognized as pressed ECE Interfacing with input Devices There is an alternate configuration (ECE2500): Two 2-in Nand Pull-ups to switch that shorts Setn and Resetn (negative logic) used ECE Interfacing with input Devices Non inverting CMOS buffer with high input impedance The CMOS buffer output is identical to its input When the switch is pressed, the input to the buffer chip is grounded and hence Vout is forced low When the key switch is bouncing, the resistor R keeps the output voltage low. This is due to the high input impedance of the buffer, which causes a negligible voltage drop on the feedback resistor ECE Switch Bounce Mitigation Capacitor Integrated de-bouncers The RC constant of the integrator determines the rate at which the capacitor charges up towards the supply voltage once the ground connection via the switch has been removed As long as the capacitor voltage does not exceed the logic 0 threshold value, the Vout signal will be recognized as logic 0 The cheapest approach! ECE Switch Bounce Mitigation Software Technique #1 The most popular and simple one has been the wait and see method. Wait to see if the switch value stabilizes. In this method, the program simply waits for about 10 ms and reexamines the same key again to see if it is still pressed. Technique #2 M of N detection. Make multiple periodic samples (shift the bit value into an 8-bit word). Count the number of 1 in the word and determine if M or more of N=8 are high or low (compute M for ones and 8-M for zeros) When M (or more) of N agree, declare the level as appropriate. ECE Multiplexing 7-Segment Displays a b g #0... a b g # a b g #5 PB0 74HC244 PB1 PB6 74HC367 common cathode common cathode common cathode PP0 A0 Y0 PP1 A1 Y1 HCS12 PP2 PP3 PP4 A2 A3 A4 Y2 Y3 Y4... PP5 A5 Y5 Figure 4.18 Port B and Port P together drive six seven -segment displays (MC9S12DG256) ECE 4510/ Example 4.14 Write a program to display on the six seven-segment displays shown in Figure Solution: The values 1,2,3,4,5, and 6 are displayed on display #5, #4,, #0, respectively. A table is set up to control the pattern and digit selection as shown in Table 4.8. #include c:\miniide\hcs12.inc .org $1000 DispTab:.byte $06,$1F, $5B,$2F, $4F,$37.byte $66,$3B, $6D,$3D, $7D,$3E.text _main:: Start: lds #$1500 movb #$FF,DDRB movb #$3F,DDRP Forever: ldx #DispTab ; set X to point to the display table loopi: movb 1,x+,PTB ; output segment pattern movb 1,x+,PTP ; output display select ldy #1 jsr delayby1ms ; wait for 1 ms cpx #DispTab+12 ; reach the end of the table? bne loopi bra forever Table 4.8 Table of display patterns for Example 4.14 Seven-Segment Display #5 #4 #3 #2 #1 #0 Displayed BCD Digit Port B $06 $5B $4F $66 $6D $7D Port P $1F $2F $37 $3B $3D $3E 16-Button Keypad 4200 Series by C&K Components, Inc. Web Site: Matrix connections based on which key is pressed Time multiplex pins 1-4 while reading pins 5-8 A keypad controller IC can be purchased to act as a peripheral ECE 16-Button Keypad Series 96 by Grayhill, Inc. Web Site: Matrix connections based on which key is pressed Time multiplex pins 1-4 while reading pins 5-8 A keypad controller IC can be purchased to act as a peripheral ECE Keypad Scanning Keypad scanning is usually performed row-by-row or column-by-column A 16-key keypad can be easily interfaced using any available 8-bit I/O port For the keypad application the upper four pins of the port should be configured for output and the lower four pins of the port should be configured for input (with pull-ups) The rows and columns of a keypad are simply conductors The keypad interface setup to HCS12 PortA is as shown in the next slide ECE Keypad Circuitry PA7 PA6 PA5 PA4 Selected keys HCS12 MCU PA , 4, 8, C, 1, 5, 9, D, 2, 6, A, E, and 3 and 7 and B and F Table 7.16 Sixteen-key keypad row selections outputs PA6 PA5 PA4 PA3 3 7 B F inputs PA2 PA A 9 E D PA C 10K V CC ECE Figure 7.41 Sixteen-key keypad connected to the HCS12 Keypad Operation Whenever a key switch is pressed, the corresponding row and column are shorted together In order to distinguish the row and column of the key pressed Scan a zero through the columns If an input row becomes zero, the key pressed must be connected to the zeroed column. Figure it out to determine the key! PA7 PA6 PA5 PA4 Selected keys , 4, 8, C, 1, 5, 9, D, 2, 6, A, E, and 3 and 7 and B and F Table 7.16 Sixteen-key keypad row selections ECE Keypad Input vs. Key Pressed (When