by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
LCD.cpp@0:519ae7e3077e, 2013-05-24 (annotated)
- Committer:
- robt
- Date:
- Fri May 24 21:33:46 2013 +0000
- Revision:
- 0:519ae7e3077e
by Rob Toulson and Tim Wilmshurst from textbook "Fast and Effective Embedded Systems Design: Applying the ARM mbed"
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
robt | 0:519ae7e3077e | 1 | #include "LCD.h" |
robt | 0:519ae7e3077e | 2 | |
robt | 0:519ae7e3077e | 3 | DigitalOut RS(p19); // RS |
robt | 0:519ae7e3077e | 4 | DigitalOut RW(p19); // RW |
robt | 0:519ae7e3077e | 5 | DigitalOut E(p20); // Enable |
robt | 0:519ae7e3077e | 6 | BusOut data(p21, p22, p23, p24);// DB4-DB7 |
robt | 0:519ae7e3077e | 7 | |
robt | 0:519ae7e3077e | 8 | /****initialise LCD function ****/ |
robt | 0:519ae7e3077e | 9 | void LCD_init(void){ |
robt | 0:519ae7e3077e | 10 | |
robt | 0:519ae7e3077e | 11 | wait(0.02); |
robt | 0:519ae7e3077e | 12 | RS=0; // set all low to write control/instruction data |
robt | 0:519ae7e3077e | 13 | // RW=0; |
robt | 0:519ae7e3077e | 14 | E=0; |
robt | 0:519ae7e3077e | 15 | |
robt | 0:519ae7e3077e | 16 | // Function set |
robt | 0:519ae7e3077e | 17 | data=0x2; // = 4 bit mode |
robt | 0:519ae7e3077e | 18 | toggle_enable(); |
robt | 0:519ae7e3077e | 19 | data=0x8; // = 2-line mode, 7 dot characters |
robt | 0:519ae7e3077e | 20 | toggle_enable(); |
robt | 0:519ae7e3077e | 21 | |
robt | 0:519ae7e3077e | 22 | // Display Mode |
robt | 0:519ae7e3077e | 23 | data=0x0; // |
robt | 0:519ae7e3077e | 24 | toggle_enable(); |
robt | 0:519ae7e3077e | 25 | data=0xF; // display on, cursor on, blink on |
robt | 0:519ae7e3077e | 26 | toggle_enable(); |
robt | 0:519ae7e3077e | 27 | |
robt | 0:519ae7e3077e | 28 | // Clear display |
robt | 0:519ae7e3077e | 29 | data=0x0; // |
robt | 0:519ae7e3077e | 30 | toggle_enable(); |
robt | 0:519ae7e3077e | 31 | data=0x1; // clear |
robt | 0:519ae7e3077e | 32 | toggle_enable(); |
robt | 0:519ae7e3077e | 33 | |
robt | 0:519ae7e3077e | 34 | } |
robt | 0:519ae7e3077e | 35 | |
robt | 0:519ae7e3077e | 36 | |
robt | 0:519ae7e3077e | 37 | /**** display ****/ |
robt | 0:519ae7e3077e | 38 | void display_to_LCD(char value ){ |
robt | 0:519ae7e3077e | 39 | |
robt | 0:519ae7e3077e | 40 | RS=1; |
robt | 0:519ae7e3077e | 41 | //***** display character ***************** |
robt | 0:519ae7e3077e | 42 | data=value>>4; // value shifted right 4 = upper |
robt | 0:519ae7e3077e | 43 | toggle_enable(); |
robt | 0:519ae7e3077e | 44 | data=value&0x0F; // value bitmask with 0x0F = lower |
robt | 0:519ae7e3077e | 45 | toggle_enable(); |
robt | 0:519ae7e3077e | 46 | } |
robt | 0:519ae7e3077e | 47 | |
robt | 0:519ae7e3077e | 48 | |
robt | 0:519ae7e3077e | 49 | /**** toggle enable function ****/ |
robt | 0:519ae7e3077e | 50 | void toggle_enable(void){ |
robt | 0:519ae7e3077e | 51 | E=1; |
robt | 0:519ae7e3077e | 52 | wait(0.001); |
robt | 0:519ae7e3077e | 53 | E=0; |
robt | 0:519ae7e3077e | 54 | wait(0.001); |
robt | 0:519ae7e3077e | 55 | } |
robt | 0:519ae7e3077e | 56 | |
robt | 0:519ae7e3077e | 57 | /**** set location function ****/ |
robt | 0:519ae7e3077e | 58 | void set_location(char location){ |
robt | 0:519ae7e3077e | 59 | RS=0; |
robt | 0:519ae7e3077e | 60 | data=(location|0x80)>>4; // upper nibble |
robt | 0:519ae7e3077e | 61 | toggle_enable(); |
robt | 0:519ae7e3077e | 62 | data=location&0x0F; // lower nibble |
robt | 0:519ae7e3077e | 63 | toggle_enable(); |
robt | 0:519ae7e3077e | 64 | } |