Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống
1
/ 36 trang
THÔNG TIN TÀI LIỆU
Thông tin cơ bản
Định dạng
Số trang
36
Dung lượng
3,61 MB
Nội dung
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ện Tử - ĐHBK 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ện Tử - ĐHBK Chapter - p1 8/9/2016 SWITCH ON I/O PORTS very small current needed Bộ môn Kỹ Thuật Điện Tử - ĐHBK the circuit waste a large current Chapter - p1 when the switch is open, the input floats to a noisesensitive Switch Input Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 8/9/2016 Digital input for PIC Active Low-Button Input Active High-Button Input Bộ môn Kỹ Thuật Điện Tử - ĐHBK Transistor Input Chapter - p1 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ện Tử - ĐHBK Chapter - p1 66 8/9/2016 Software Debouncing • Debouncing the key – Software technique • Wait for 20 ms • Read the port pin again • If the reading is still 0 (for eg ) , it indicates that a key is pressed Bouncing in mechanical switches When the switch opens or closes, the mechanical pieces vibrate generating fast contacts before settling This transient normally lasts less than 20 ms Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 Interfacing Dip Switches Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 8/9/2016 Test Circuit with Input Switch Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 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ện Tử - ĐHBK Chapter - p1 10 8/9/2016 Tests an input with conditional loop // WHILOOP.C Input switch controls output flashing #include " 16F877A.h " #use delay (clock = 1000000) // MCU clock = 1 MHz void main() { while(1) { while(input(PIN_C0)) // Repeat while switch open { output_high(PIN_D0); delay_ms(300); // Delay 0.3s output_low(PIN_D0); delay_ms(500); // Delay 0.5s } output_low(PIN_D0); // Switch off LED } } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 11 Tests an input with FOR loop // FORLOOP.C Repeat loop a set number of times #include " 16F877A.h " #use delay (clock = 1000000) void main() { int x; while(input(PIN_C0)) { } ; // Wait until switch closed for (x = 0; x COMMAND; 1 ‐‐> DATA output_bit(lcd_RW,0) ; // write to register output_bit(lcd_EN,1); // Enable LCD delay_ms(1); output_bit(lcd_EN, 0); // Disable LCD delay_ms(1); } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 59 Ex: 8‐bit LCD interface ‐ SW (2/3) void lcd_init() { delay_ms(40); lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode delay_ms(5); // Delay 5 ms lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode delay_us(200); // Delay 200 us lcd_write(lcd_Command_mode, 0x30); // Set as 8 bit data mode 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); } void lcd_write_string(byte position, char *ptr, int delay_time) { lcd_write(lcd_Command_mode, 0x80 | position ); while ( *ptr != 0) { lcd_write(lcd_Data_mode, *ptr++); delay_ms(delay_time); } } Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 60 30 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ện Tử - ĐHBK Chapter - p1 61 LCD – 4‐Bit Interface Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 62 31 8/9/2016 4‐bit LCD interface ‐ HW RD0/PSP0 RD1/PSP1 RD2/PSP2 RD3/PSP3 RD4/PSP4 RD5/PSP5 RD6/PSP6 RD7/PSP7 LCD1 10 11 12 13 14 15 16 17 18 23 24 25 26 D0 D1 D2 D3 D4 D5 D6 D7 LM016L RS RW E RA0/AN0 RA1/AN1 RA2/AN2/VREFRA3/AN3/VREF+ RA4/T0CKI RA5/AN4/SS RC0/T1OSO/T1CKI RC1/T1OSI/CCP2 RE0/AN5/RD RC2/CCP1 RE1/AN6/WR RC3/SCK/SCL RE2/AN7/CS RC4/SDI/SDA RC5/SDO RC6/TX/CK RC7/RX/DT 33 34 35 36 37 38 39 40 10 RB0/INT RB1 RB2 RB3/PGM RB4 RB5 RB6/PGC RB7/PGD VSS VDD VEE OSC1/CLKIN OSC2/CLKOUT MCLR/Vpp/THV U1 13 14 19 20 21 22 27 28 29 30 PIC16F877 Bộ môn Kỹ Thuật Điện Tử - ĐHBK 63 Chapter - p1 4‐bit LCD interface #include #use delay(clock=20000000) #use fast_io (D) #define LCD_ENABLE_PIN PIN_E0 #define LCD_RS_PIN PIN_E1 #define LCD_RW_PIN PIN_E2 #define LCD_DATA4 PIN_D4 #define LCD_DATA5 PIN_D5 #define LCD_DATA6 PIN_D6 #define LCD_DATA7 PIN_D7 #include void main() { // 4‐bit mode LCD ( 2 x 16) char text1[]="Hello the world!"; char text2[]="Number: "; char text3[]="0123456789"; Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 char *ptr; set_tris_d(0x00); lcd_init(); ptr = text3; lcd_gotoxy(1,1); // lcd_putc("Hello the world!"); printf(lcd_putc,"%s",text1); lcd_gotoxy(1,2); printf(lcd_putc,"%s",text2); while (1) { if (*ptr == 0) ptr = text3; lcd_gotoxy(8,2); lcd_putc(*ptr++); delay_ms(100); } } 64 32 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ện Tử - ĐHBK Chapter - 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ện Tử - ĐHBK Chapter - p1 66 33 8/9/2016 Ex: MILFORD Serial LCD Interface – HW Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 67 Serial LCD Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 68 34 8/9/2016 Serial LCD • Control Codes Bộ mơn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 69 Serial LCD Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 70 35 8/9/2016 Class Assignment Write a PIC C program for the calculator with basic operators: + - * / Bộ môn Kỹ Thuật Điện Tử - ĐHBK Chapter - p1 71 36