Buffer working with LCD, Serial and SD Card
Dependencies: BMP280
Fork of Thread_Communication by
LCD.cpp@0:cb3a5c15b01e, 2017-12-12 (annotated)
- Committer:
- benparkes
- Date:
- Tue Dec 12 17:05:59 2017 +0000
- Revision:
- 0:cb3a5c15b01e
- Child:
- 8:ab6322afa341
Working No Mail que
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
benparkes | 0:cb3a5c15b01e | 1 | #include "mbed.h" |
benparkes | 0:cb3a5c15b01e | 2 | |
benparkes | 0:cb3a5c15b01e | 3 | #include "LCD.h" |
benparkes | 0:cb3a5c15b01e | 4 | #define busy_mask 0x8000 |
benparkes | 0:cb3a5c15b01e | 5 | |
benparkes | 0:cb3a5c15b01e | 6 | |
benparkes | 0:cb3a5c15b01e | 7 | LCD::LCD(PinName RS, PinName E, PinName d4, PinName d5, |
benparkes | 0:cb3a5c15b01e | 8 | PinName d6, PinName d7) : _RS(RS), |
benparkes | 0:cb3a5c15b01e | 9 | _E(E), _DBUS(d4, d5, d6, d7) { |
benparkes | 0:cb3a5c15b01e | 10 | |
benparkes | 0:cb3a5c15b01e | 11 | _E = 0; |
benparkes | 0:cb3a5c15b01e | 12 | _RS = 0; // command mode |
benparkes | 0:cb3a5c15b01e | 13 | |
benparkes | 0:cb3a5c15b01e | 14 | wait(0.015); // Wait 15ms to ensure powered up |
benparkes | 0:cb3a5c15b01e | 15 | |
benparkes | 0:cb3a5c15b01e | 16 | // send "Display Settings" 3 times (Only top nibble of 0x30 as we've got 4-bit bus) |
benparkes | 0:cb3a5c15b01e | 17 | |
benparkes | 0:cb3a5c15b01e | 18 | wait(0.015); // Wait 15ms to ensure powered up |
benparkes | 0:cb3a5c15b01e | 19 | |
benparkes | 0:cb3a5c15b01e | 20 | for (int i=0; i<3; i++) { |
benparkes | 0:cb3a5c15b01e | 21 | DATA(0x30, CMD); |
benparkes | 0:cb3a5c15b01e | 22 | wait(0.00164); // this command takes 1.64ms, so wait for it |
benparkes | 0:cb3a5c15b01e | 23 | } |
benparkes | 0:cb3a5c15b01e | 24 | DATA(0x02, CMD); // 4-bit mode |
benparkes | 0:cb3a5c15b01e | 25 | wait(0.000040f); // most instructions take 40us |
benparkes | 0:cb3a5c15b01e | 26 | |
benparkes | 0:cb3a5c15b01e | 27 | DATA(0x28,CMD); // Function set 001 BW N F - - |
benparkes | 0:cb3a5c15b01e | 28 | DATA(0x0C,CMD); |
benparkes | 0:cb3a5c15b01e | 29 | DATA(0x06,CMD); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
benparkes | 0:cb3a5c15b01e | 30 | Clear(); |
benparkes | 0:cb3a5c15b01e | 31 | DATA(0x01,CMD); // cls, and set cursor to 0 |
benparkes | 0:cb3a5c15b01e | 32 | wait(0.00164f); // This command takes 1.64 ms |
benparkes | 0:cb3a5c15b01e | 33 | Write("Constructed"); |
benparkes | 0:cb3a5c15b01e | 34 | } |
benparkes | 0:cb3a5c15b01e | 35 | |
benparkes | 0:cb3a5c15b01e | 36 | |
benparkes | 0:cb3a5c15b01e | 37 | |
benparkes | 0:cb3a5c15b01e | 38 | |
benparkes | 0:cb3a5c15b01e | 39 | /*int main(){ |
benparkes | 0:cb3a5c15b01e | 40 | //(PF_13, PE_9, PF_14, PF_15) |
benparkes | 0:cb3a5c15b01e | 41 | //(PF_15, PF_14, PE_9,PF_13) |
benparkes | 0:cb3a5c15b01e | 42 | LCD_DBUS = new BusInOut(PF_13, PE_9, PF_14, PF_15) ; |
benparkes | 0:cb3a5c15b01e | 43 | LCD_Init(); |
benparkes | 0:cb3a5c15b01e | 44 | while(1){ |
benparkes | 0:cb3a5c15b01e | 45 | write_string("Hi"); |
benparkes | 0:cb3a5c15b01e | 46 | wait(0.1); |
benparkes | 0:cb3a5c15b01e | 47 | } |
benparkes | 0:cb3a5c15b01e | 48 | } |
benparkes | 0:cb3a5c15b01e | 49 | */ |
benparkes | 0:cb3a5c15b01e | 50 | // Initialise LCD Pins // |
benparkes | 0:cb3a5c15b01e | 51 | /*void LCD_Init(void){ |
benparkes | 0:cb3a5c15b01e | 52 | LCD_E = 0;; //clear enable |
benparkes | 0:cb3a5c15b01e | 53 | LCD_RW = 0; // write |
benparkes | 0:cb3a5c15b01e | 54 | LCD_RS = 0; // command |
benparkes | 0:cb3a5c15b01e | 55 | |
benparkes | 0:cb3a5c15b01e | 56 | wait_ms(3); //delay for LCD to initialise. |
benparkes | 0:cb3a5c15b01e | 57 | |
benparkes | 0:cb3a5c15b01e | 58 | LCD_DATA(0x28,CMD); //set to 4 bit interface, 2 line and 5*8 font |
benparkes | 0:cb3a5c15b01e | 59 | LCD_DATA(0x0f,CMD); //cursor on, cursor position blink |
benparkes | 0:cb3a5c15b01e | 60 | LCD_DATA(0x10,CMD); |
benparkes | 0:cb3a5c15b01e | 61 | LCD_CLR; //clear display |
benparkes | 0:cb3a5c15b01e | 62 | LCD_DATA(0x06,CMD); //move cursor right after write |
benparkes | 0:cb3a5c15b01e | 63 | LCD_HOME; //return home |
benparkes | 0:cb3a5c15b01e | 64 | |
benparkes | 0:cb3a5c15b01e | 65 | LCD_E = 1; |
benparkes | 0:cb3a5c15b01e | 66 | LCD_RS = 0; // command mode |
benparkes | 0:cb3a5c15b01e | 67 | LCD_RW = 0; |
benparkes | 0:cb3a5c15b01e | 68 | wait(0.015); // Wait 15ms to ensure powered up |
benparkes | 0:cb3a5c15b01e | 69 | |
benparkes | 0:cb3a5c15b01e | 70 | for (int i=0; i<3; i++) { |
benparkes | 0:cb3a5c15b01e | 71 | LCD_DATA(0x03, TXT); |
benparkes | 0:cb3a5c15b01e | 72 | wait(0.00164); // this command takes 1.64ms, so wait for it |
benparkes | 0:cb3a5c15b01e | 73 | } |
benparkes | 0:cb3a5c15b01e | 74 | LCD_DATA(0x02, TXT); // 4-bit mode |
benparkes | 0:cb3a5c15b01e | 75 | wait(0.000040f); // most instructions take 40us |
benparkes | 0:cb3a5c15b01e | 76 | |
benparkes | 0:cb3a5c15b01e | 77 | LCD_DATA(0x28,CMD); // Function set 001 BW N F - - |
benparkes | 0:cb3a5c15b01e | 78 | LCD_DATA(0x0C,CMD); |
benparkes | 0:cb3a5c15b01e | 79 | LCD_DATA(0x06,CMD); // Cursor Direction and Display Shift : 0000 01 CD S (CD 0-left, 1-right S(hift) 0-no, 1-yes |
benparkes | 0:cb3a5c15b01e | 80 | LCD_DATA(0x01,CMD); // cls, and set cursor to 0 |
benparkes | 0:cb3a5c15b01e | 81 | wait(0.00164f); // This command takes 1.64 ms |
benparkes | 0:cb3a5c15b01e | 82 | }*/ |
benparkes | 0:cb3a5c15b01e | 83 | /*---------------------------------------------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 84 | void LCD::Clear(void){ |
benparkes | 0:cb3a5c15b01e | 85 | DATA(CLEAR,CMD); |
benparkes | 0:cb3a5c15b01e | 86 | } |
benparkes | 0:cb3a5c15b01e | 87 | |
benparkes | 0:cb3a5c15b01e | 88 | void LCD::RowSelect(int row){ |
benparkes | 0:cb3a5c15b01e | 89 | switch(row){ |
benparkes | 0:cb3a5c15b01e | 90 | case 0: |
benparkes | 0:cb3a5c15b01e | 91 | DATA(LINE1,CMD); |
benparkes | 0:cb3a5c15b01e | 92 | break; |
benparkes | 0:cb3a5c15b01e | 93 | case 1: |
benparkes | 0:cb3a5c15b01e | 94 | DATA(LINE2,CMD); |
benparkes | 0:cb3a5c15b01e | 95 | break; |
benparkes | 0:cb3a5c15b01e | 96 | default: |
benparkes | 0:cb3a5c15b01e | 97 | DATA(LINE1,CMD); |
benparkes | 0:cb3a5c15b01e | 98 | break; |
benparkes | 0:cb3a5c15b01e | 99 | } |
benparkes | 0:cb3a5c15b01e | 100 | } |
benparkes | 0:cb3a5c15b01e | 101 | |
benparkes | 0:cb3a5c15b01e | 102 | void LCD::Busy(void) |
benparkes | 0:cb3a5c15b01e | 103 | { |
benparkes | 0:cb3a5c15b01e | 104 | wait_ms(1); |
benparkes | 0:cb3a5c15b01e | 105 | } |
benparkes | 0:cb3a5c15b01e | 106 | /*---------------------------------------------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 107 | void LCD::DATA(char data,char type){ |
benparkes | 0:cb3a5c15b01e | 108 | |
benparkes | 0:cb3a5c15b01e | 109 | Busy(); //TEST LCD FOR BUSY |
benparkes | 0:cb3a5c15b01e | 110 | |
benparkes | 0:cb3a5c15b01e | 111 | _DBUS = (data>>4); |
benparkes | 0:cb3a5c15b01e | 112 | |
benparkes | 0:cb3a5c15b01e | 113 | wait(0.00040f); // most instructions take 40us |
benparkes | 0:cb3a5c15b01e | 114 | |
benparkes | 0:cb3a5c15b01e | 115 | if(type == CMD) |
benparkes | 0:cb3a5c15b01e | 116 | { |
benparkes | 0:cb3a5c15b01e | 117 | _RS = 0; //COMMAND MODE |
benparkes | 0:cb3a5c15b01e | 118 | } |
benparkes | 0:cb3a5c15b01e | 119 | else |
benparkes | 0:cb3a5c15b01e | 120 | { |
benparkes | 0:cb3a5c15b01e | 121 | _RS = 1; //CHARACTER/DATA MODE |
benparkes | 0:cb3a5c15b01e | 122 | } |
benparkes | 0:cb3a5c15b01e | 123 | wait(0.00040f); |
benparkes | 0:cb3a5c15b01e | 124 | |
benparkes | 0:cb3a5c15b01e | 125 | _E = 1; //ENABLE LCD DATA LINE |
benparkes | 0:cb3a5c15b01e | 126 | wait(0.00040f); // most instructions take 40us |
benparkes | 0:cb3a5c15b01e | 127 | _E = 0; //DISABLE LCD DATA LINE |
benparkes | 0:cb3a5c15b01e | 128 | |
benparkes | 0:cb3a5c15b01e | 129 | _DBUS = 0; |
benparkes | 0:cb3a5c15b01e | 130 | |
benparkes | 0:cb3a5c15b01e | 131 | _DBUS = (data); |
benparkes | 0:cb3a5c15b01e | 132 | |
benparkes | 0:cb3a5c15b01e | 133 | _E = 1; //ENABLE LCD DATA LINE |
benparkes | 0:cb3a5c15b01e | 134 | wait(0.00040f); // most instructions take 40us |
benparkes | 0:cb3a5c15b01e | 135 | _E = 0; //DISABLE LCD DATA LINE |
benparkes | 0:cb3a5c15b01e | 136 | } |
benparkes | 0:cb3a5c15b01e | 137 | /*---------------------------------------------------------------------*/ |
benparkes | 0:cb3a5c15b01e | 138 | void LCD::Write(char text[16]){ |
benparkes | 0:cb3a5c15b01e | 139 | int i = 0; |
benparkes | 0:cb3a5c15b01e | 140 | |
benparkes | 0:cb3a5c15b01e | 141 | |
benparkes | 0:cb3a5c15b01e | 142 | while((text[i] != 0)) |
benparkes | 0:cb3a5c15b01e | 143 | { |
benparkes | 0:cb3a5c15b01e | 144 | |
benparkes | 0:cb3a5c15b01e | 145 | char character = text[i]; |
benparkes | 0:cb3a5c15b01e | 146 | DATA (character,TXT); // Write text "a" to the LCD |
benparkes | 0:cb3a5c15b01e | 147 | |
benparkes | 0:cb3a5c15b01e | 148 | i++; |
benparkes | 0:cb3a5c15b01e | 149 | } |
benparkes | 0:cb3a5c15b01e | 150 | } |