Embedded System Design - Chapter 7: Peripherals for embedded systems (Part 1)

1. Digital parallel input / output
• Commonly used input peripherals
– DIP switches, push‐button keys, keyboards, and A/D converters
• DIP switch
– One side of the switch is tied high
• To a power supply through a resistor called a pull‐up resistor
– The other side is grounded
– The logic level changes when the position is switched
• Push‐button key
– Same as the DIP switch except that contact is momentar
pdf 36 trang thamphan 26/12/2022 1980
Bạn đang xem 20 trang mẫu của tài liệu "Embedded System Design - Chapter 7: Peripherals for embedded systems (Part 1)", để tải tài liệu gốc về máy hãy click vào nút Download ở trên.

File đính kèm:

  • pdfembedded_system_design_chapter_7_peripherals_for_embedded_sy.pdf

Nội dung text: Embedded System Design - Chapter 7: Peripherals for embedded systems (Part 1)

  1. 8/9/2016 ĐẠI HỌC QUỐC GIA TP.HỒ CHÍ MINH TRƯỜNG ĐẠI HỌC BÁCH KHOA KHOA ĐIỆN-ĐIỆN TỬ BỘ MÔN KỸ THUẬT ĐIỆN TỬ Embedded System Design Chapter 7: Peripherals for embedded systems 7.1 Digital parallel input / output 7.2 LCD interfacing 7.3 Analog input /output 7.4 Serial communication Bộ Môn Kỹ Thuật ĐiệnTử - ĐHBK 1 1. Digital parallel input / output • Commonly used input peripherals – DIP switches, push‐button keys, keyboards, and A/D converters • DIP switch – One side of the switch is tied high • To a power supply through a resistor called a pull‐up resistor – The other side is grounded – The logic level changes when the position is switched • Push‐button key – Same as the DIP switch except that contact is momentary Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 2 1
  2. 8/9/2016 Digital input for PIC Active Low-Button Active High-Button Input Input Transistor Input Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 5 Switch hardware debouncing Switch hardware debounce (a) without debounce capacitor (b) with debounce capacitor Ex: With a 10k pull‐up resistor, a 10 nF capacitor would give a time constant of 100 ms, which should be more than adequate Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 66 3
  3. 8/9/2016 Test Circuit with Input Switch Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 9 Tests an input with IF Statement // IFIN.C Tests an input #include " 16F877A.h " void main() { int x; // Declare variable output_D(0); // Clear all outputs while(1) // Loop always { x = input(PIN_C0); // Get input state if(x = = 1)output_high(PIN_D0); // Change output } } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 10 5
  4. 8/9/2016 Matrix Keypads • Matrix keypads consist of keys interconnected in the shape of a matrix. Each key is a simple mechanical switch located at the crossing between the matrix rows and columns. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 13 The procedure to service matrix keypads The procedure to service matrix keypads is as follows: Step 1: Wait until the keypad is clear (due to an earlier key pressed). Step 2: Detect that a new key has been pressed. Step 3: Explore the matrix keypad to determine the key that was pressed in two ways: Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 14 7
  5. 8/9/2016 16‐key matrix keypad connected to port B Fig.13 A 16-key matrix keypad connected to port B in a PIC. Diodes are used to avoid short circuits between two exploration lines if two or more keys in the same column are pressed simultaneously. This configuration uses the internal pull-up (not shown) available in port B to keep a logic 1 in the return lines when no keys are pressed. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 17 Digital Output for PIC Current Sourcing Current Sinking Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 18 9
  6. 8/9/2016 Digital Output for PIC Higher Current Load Interface Driving a Lamp Using a Transistor Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 21 Digital Output for PIC Relay Interface Driving an Inductive Driving a Mains Bulb Using a Relay Load (e.g., a Relay) Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 22 11
  7. 8/9/2016 Interfacing Seven‐Segment LEDs • In a common anode seven‐ segment LED – All anodes are connected together to a power supply and cathodes are connected to data lines • Logic 0 turns on a segment. • Example: To display digit 1, all segments except b and c should be off. • Byte 11111001 = F9H will display digit 1. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 25 Interfacing Seven‐Segment LEDs • In a common cathode seven‐ segment LED – All cathodes are connected together to ground and the anodes are connected to data lines • Logic 1 turns on a segment. • Example: To display digit 1, all segments except b and c should be off. • Byte 00000110 = 06H will display digit 1. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 26 13
  8. 8/9/2016 Interfacing to Multiple 7‐Segments (2/2) Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 29 Keypad Connection to the Microcontroller Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 30 15
  9. 8/9/2016 Parallel LCDs • Following figure shows a typical parallel LCD. The programming of a parallel LCD is usually a complex task and requires a good understanding of the internal operation of the LCDs,including the timing requirements. A Typical Parallel LCD • Fortunately, the CCS C language provides special commands for displaying data on HD44780 or compatible LCDs • All the user has to do is connect the LCD to the appropriate I/O ports and then use these special commands to simply send data to the LCD. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 33 Alphanumeric LCD Interfacing • Pinout Microcontroller – 8 data pins D7:D0 E communications – RS: Data or Command R/W bus Register Select RS DB7–DB0 – R/W: Read or Write 8 LCD – E: Enable (Latch data) controller • RS –Register Select LCD Module – RS = 0 Command Register – RS = 1 Data Register • R/W = 0 Write, R/W = 1 Read • E – Enable – Used to latch the data present on the data pins. • D0 –D7 – Bi‐directional data/command pins. – Alphanumeric characters are sent in ASCII format. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 34 17
  10. 8/9/2016 LCD Commands • The LCD’s internal controller can accept several commands and modify the display accordingly. These commands would be things like: – Clear screen – Return home – Decrement/Increment cursor • After writing to the LCD, it takes some time for it to complete its internal operations. During this time, it will not accept any new commands or data. – We need to insert time delay between any two commands or data sent to LCD Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 37 Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 38 19
  11. 8/9/2016 LCD Timing Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 41 Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 42 21
  12. 8/9/2016 Converting to ASCII • The LCD can represent characters in ASCII • For example number 0x08 must be converted to 0x38 • To perform this: – If W=0x08 then ASCII=XORLW 0x30 W=38 Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 45 Interfacing LCD • Hardware – 20 x 2‐line LCD displays (two lines with 20 characters per line) – LCD has a display Data RAM (registers) that stores data in 8‐bit character code. – Each register in Data RAM has its own address that corresponds to its position on the line. PICDEMO • The address range for Line 1 is 00 to 13H and Line 2 is 40H to 53H. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 46 23
  13. 8/9/2016 Interfacing LCD • LCD Operation – When the MPU writes an instruction to IR or data to DR, the controller: • Sets the data line DB7 high as a flag indicating that the controller is busy completing the operation • Sets the data line DB7 low after the completion of the operation – The MPU should always check whether DB7 is low before sending an instruction or a data byte – After the power up, DB7 cannot be checked for the first two initialization instructions. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 49 Interfacing LCD • Writing to or reading from LCD • The MPU: • Asserts RS low to select IR • Reads from LCD by asserting the R/W signal high • Asserts the E signal high and then low (toggles) to latch a data byte or an instruction • Asserts RS high to select DR • Writes into LCD by asserting the R/W signal low • Asserts the E signal high and then low (toggles) to latch a data byte or an instruction LCD data write waveform Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 50 25
  14. 8/9/2016 Resetting LCD • In 4‐bit mode the data is sent in nibbles – First we send the higher nibble and then the lower nibble. • To enable the 4‐bit mode of LCD, we need to follow special sequence of initialization that tells the LCD controller that user has selected 4‐bit mode of operation: – Wait for about 20mS – Send the first init value (0x30) – Wait for about 10mS – Send second init value (0x30) – Wait for about 1mS – Send third init value (0x30) – Wait for 1mS – Select bus width (0x30 ‐ for 8‐bit and 0x20 for 4‐bit) – Wait for 1mS ộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 53 Display Data RAM (DDRAM) • Display data RAM (DDRAM) stores display data represented in 8‐bit character codes. Its extended capacity is 80 x 8 bits, or 80 characters. The area in display data RAM (DDRAM) that is not used for display can be used as general data RAM. See Figure 1 for the relationships between DDRAM addresses and positions on the liquid crystal display. 1-Line Display Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter2-Line 7 - p1 Display 54 27
  15. 8/9/2016 8‐bit LCD Interface (2/2) lcd_write(lcd_Command_mode, 0x38); lcd_write(lcd_Command_mode, 0x08); lcd_write(lcd_Command_mode, 0x01); // Display Clear lcd_write(lcd_Command_mode, 0x06); Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 57 Ex: 8‐bit LCD interface ‐ HW Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 58 29
  16. 8/9/2016 Ex: 8‐bit LCD interface ‐ SW (3/3) void main() { // 4‐bit mode LCD interface ( 2 x 16) char text1[]="Hello the world!"; char text2[]="Number: "; char text3[]="0123456789"; char *ptr; set_tris_c(0x00); // Port C : output set_tris_d(0x00); // Port D : output lcd_init(); lcd_write(lcd_Command_mode, 0x0E); lcd_write_string(0x00, text1, 20); // First line lcd_write_string(0x40, text2, 20); // Second line ptr = text3; while (1) { if (*ptr == 0) ptr = text3; lcd_write(lcd_Command_mode, 0x80 | 0x48 ); lcd_write(lcd_Data_mode, *ptr++); delay_ms(100); } } Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 61 LCD –4‐Bit Interface Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 62 31
  17. 8/9/2016 Using Library LCD.C • Define LCD pins: #define LCD_ENABLE_PIN PIN_E0 . (don’t need to define LCD pins if the default pins are used) • Functions – lcd_init(): Must be called before any other function. – lcd_putc(c): Will display c on the next position of the LCD. • \f Clear display • \n Go to start of second • \b Move back one position – lcd_gotoxy(x,y): Set write position on LCD (upper left is 1,1) – lcd_getc(x,y): Returns character at position x,y on LCD Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 65 Serial LCD • The LCD has its own controller, which is compatible with the Hitachi 44780 MCU, the standard for this interface. • When the system is started, the LCD takes some time to initialize itself; its own MCU needs time to get ready to receive data. A delay of about 500 ms should be allowed in the main controller before attempting to access the LCD. • The program uses library routines to generate the RS232 output, which are called up by the directive #use RS232. • The baud rate must be specified and the send (TX) and receive (RX) pins specified as arguments of this directive. • The directive must be preceded by a #use delay, which specifies the clock rate in the target system. Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 66 33
  18. 8/9/2016 Serial LCD • Control Codes Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 69 Serial LCD Bộ môn Kỹ Thuật ĐiệnTử - ĐHBK Chapter 7 - p1 70 35