Bar graph simulation with custom characters

Dependencies:   TextLCD mbed

Fork of TextLCD_HelloWorld by Simon Ford

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* Lcd_Custom_Char
00002 
00003 Up to 8 characters can be programmed in CGRAM, but because CGRAM is a RAM, characters are lost
00004 on power off and must be reloaded on power on.
00005 Assuming 2x16 LCD with HD44780 Hitachi controller compatible.
00006 
00007 */
00008 
00009 #include "mbed.h"
00010 #include "TextLCD.h"
00011 
00012 TextLCD lcd(p13, p14, p17, p18, p19, p20); // rs, e, d4, d5, d6, d7
00013 AnalogIn ain(p15);
00014 DigitalOut led1(LED1);
00015 DigitalOut led2(LED2);
00016 DigitalOut led3(LED3);
00017 DigitalOut led4(LED4);
00018 
00019 // Defines 8 bar heights
00020 char custom_char[8][8] = {
00021     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // 0
00022     {0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x1f}, // 1
00023     {0x00,0x00,0x00,0x00,0x00,0x00,0x1f,0x1f}, // 2
00024     {0x00,0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f}, // 3
00025     {0x00,0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f}, // 4
00026     {0x00,0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f}, // 5
00027     {0x00,0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f}, // 6
00028     {0x00,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f,0x1f}  // 7
00029 };
00030 
00031 BusOut Lcd_pins(p17, p18, p19, p20); // d4, d5, d6, d7
00032 
00033 DigitalOut rs_pin(p13); // rs
00034 DigitalOut e_pin(p14);  // e 
00035 
00036 // Prepare write port
00037 void writePort(int value) {
00038 
00039     Lcd_pins = value >> 4;  // Shifts 4 bit right.
00040     wait(0.000040f); // Delay
00041     e_pin = 0;
00042     wait(0.000040f);
00043     e_pin = 1;
00044     Lcd_pins = value;
00045     wait(0.000040f);
00046     e_pin = 0;
00047     wait(0.000040f);
00048     e_pin = 1;
00049 }
00050 
00051 int main() {
00052     float temp = ain;
00053     lcd.cls();
00054     //lcd.printf("Writing Characters...");
00055     wait(0.5);
00056     for (int j=0; j<8; j++) {
00057 
00058         rs_pin = 0; // Send command
00059 
00060         /* 0X40 is the initial CGRAM address. Because each character needs a total amount of 8 memory 
00061         locations, we increment addres in 8 units after each character.
00062         */
00063         writePort(0x40+8*j);
00064 // Writes custom characters to the text LCD.
00065         rs_pin = 1; // Send the data here
00066         for (int i=0; i<8; i++) {
00067             writePort(custom_char[j][i]);
00068         }
00069     }
00070     lcd.cls();
00071     wait(0.010);
00072     lcd.locate(0,0);
00073 
00074 // Prints loaded custom characters. ASCII codes 0 to 7.
00075 
00076     while(1){
00077         wait(0.2);
00078         temp = ain;
00079         if(temp>=0&&temp<=0.142){
00080         lcd.cls();
00081         lcd.putc(0);
00082         led1=0;
00083         led2=0;
00084         led3=0;
00085         led4=0;
00086         }
00087        if(temp>0.142&&temp<=0.284){
00088         lcd.cls();
00089         lcd.putc(1);
00090         led1=1;
00091         led2=0;
00092         led3=0;
00093         led4=0;
00094         }
00095        if(temp>0.284&&temp<=0.426){
00096         lcd.cls();
00097         lcd.putc(2);
00098         led1=1;
00099         led2=1;
00100         led3=0;
00101         led4=0;
00102         }
00103        if(temp>0.426&&temp<=0.568){ 
00104         lcd.cls();
00105         lcd.putc(3);
00106         led1=1;
00107         led2=1;
00108         led3=0;
00109         led4=0;
00110         }
00111        else if(temp>0.568&&temp<=0.710){   
00112         lcd.cls();
00113         lcd.putc(4);
00114         led1=1;
00115         led2=1;
00116         led3=1;
00117         led4=0;
00118         }
00119        else if(temp>0.710&&temp<=0.852){
00120         lcd.cls();
00121         lcd.putc(5);
00122         led1=1;
00123         led2=1;
00124         led3=1;
00125         led4=0;
00126         }
00127        else if(temp>0.852&&temp<=0.92){
00128         lcd.cls();
00129         lcd.putc(6);
00130         led1=1;
00131         led2=1;
00132         led3=1;
00133         led4=1;}
00134         
00135        else if(temp>0.92&&temp<=1.0){
00136         lcd.cls();
00137         lcd.putc(7);
00138         led1=1;
00139         led2=1;
00140         led3=1;
00141         led4=1;}
00142             
00143 
00144     }
00145  }