Code used to connect the Grove RGB LCD display to an mbed microcontroller.

Dependents:   Grove_LCD_RGB_Backlight_HelloWorld Test_BERTIN LASS_LoRa_mbed Affichage_MesureEnvironnement ... more

Committer:
cmatz3
Date:
Tue Mar 15 18:05:14 2016 +0000
Revision:
2:5ce38ef7a7db
Parent:
0:253885b1f364
Fixed spelling error in docs;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
cmatz3 0:253885b1f364 1 #include "mbed.h"
cmatz3 0:253885b1f364 2 #include "Grove_LCD_RGB_Backlight.h"
cmatz3 0:253885b1f364 3 char _displayfunction;
cmatz3 0:253885b1f364 4 char _displaycontrol;
cmatz3 0:253885b1f364 5
cmatz3 0:253885b1f364 6 Grove_LCD_RGB_Backlight::Grove_LCD_RGB_Backlight(PinName sda, PinName scl):i2c(sda, scl)
cmatz3 0:253885b1f364 7 {
cmatz3 0:253885b1f364 8 this->init();
cmatz3 0:253885b1f364 9 }
cmatz3 0:253885b1f364 10
cmatz3 0:253885b1f364 11 /*This function is used to turn on the display on the LCD. It sends the
cmatz3 0:253885b1f364 12 hexadecimal code for the display on setting to the device in order to
cmatz3 0:253885b1f364 13 turn on the display*/
cmatz3 0:253885b1f364 14
cmatz3 0:253885b1f364 15 void Grove_LCD_RGB_Backlight::displayOn()
cmatz3 0:253885b1f364 16 {
cmatz3 0:253885b1f364 17 _displaycontrol |= LCD_DISPLAYON;
cmatz3 0:253885b1f364 18 this->sendCommand(LCD_DISPLAYCONTROL | _displaycontrol);
cmatz3 0:253885b1f364 19 }
cmatz3 0:253885b1f364 20
cmatz3 0:253885b1f364 21 /*This function is used to clear display on the LCD. It sends the
cmatz3 0:253885b1f364 22 hexadecimal code for the clear instruction to the device in order to
cmatz3 0:253885b1f364 23 clear the display*/
cmatz3 0:253885b1f364 24 void Grove_LCD_RGB_Backlight::clear()
cmatz3 0:253885b1f364 25 {
cmatz3 0:253885b1f364 26 this->sendCommand(LCD_CLEARDISPLAY);
cmatz3 0:253885b1f364 27 wait_us(2000);
cmatz3 0:253885b1f364 28 }
cmatz3 0:253885b1f364 29
cmatz3 0:253885b1f364 30
cmatz3 0:253885b1f364 31 /*This function is used to set the backlight color of the dispaly.
cmatz3 0:253885b1f364 32 It writes the provided r, g, and b values to the registers for the red
cmatz3 0:253885b1f364 33 value, the green value, and the blue value respectively.*/
cmatz3 0:253885b1f364 34 void Grove_LCD_RGB_Backlight::setRGB(char r, char g, char b)
cmatz3 0:253885b1f364 35 {
cmatz3 0:253885b1f364 36
cmatz3 0:253885b1f364 37 this->setReg(RED_REG, r);
cmatz3 0:253885b1f364 38 this->setReg(GREEN_REG, g);
cmatz3 0:253885b1f364 39 this->setReg(BLUE_REG, b);
cmatz3 0:253885b1f364 40
cmatz3 0:253885b1f364 41 }
cmatz3 0:253885b1f364 42
cmatz3 0:253885b1f364 43 /*This function is used to write to one of the registers for the backlight
cmatz3 0:253885b1f364 44 of the LCD display. The function takes in the address of which register to
cmatz3 0:253885b1f364 45 write to and the value to be written to that register and then sends it to the
cmatz3 0:253885b1f364 46 LCD display via the mbed I2C library*/
cmatz3 0:253885b1f364 47 void Grove_LCD_RGB_Backlight::setReg(char addr, char val)
cmatz3 0:253885b1f364 48 {
cmatz3 0:253885b1f364 49 char data[2];
cmatz3 0:253885b1f364 50 data[0] = addr;
cmatz3 0:253885b1f364 51 data[1] = val;
cmatz3 0:253885b1f364 52 i2c.write(RGB_ADDRESS, data, 2);
cmatz3 0:253885b1f364 53 }
cmatz3 0:253885b1f364 54
cmatz3 0:253885b1f364 55 /*This function is used to write to the LCD screen. It takes in a string of
cmatz3 0:253885b1f364 56 characters and writes them to the 0x40 register of the display.*/
cmatz3 0:253885b1f364 57 void Grove_LCD_RGB_Backlight::print(char *str)
cmatz3 0:253885b1f364 58 {
cmatz3 0:253885b1f364 59 char data[2];
cmatz3 0:253885b1f364 60 data[0] = 0x40;
cmatz3 0:253885b1f364 61 while(*str)
cmatz3 0:253885b1f364 62 {
cmatz3 0:253885b1f364 63 data[1] = *str;
cmatz3 0:253885b1f364 64 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 65 str++;
cmatz3 0:253885b1f364 66
cmatz3 0:253885b1f364 67 }
cmatz3 0:253885b1f364 68
cmatz3 0:253885b1f364 69
cmatz3 0:253885b1f364 70 }
cmatz3 0:253885b1f364 71
cmatz3 0:253885b1f364 72
cmatz3 0:253885b1f364 73 /*This function sets where on the screen the text will be written next. It
cmatz3 0:253885b1f364 74 takes in two values which indicate the row and column on the display that
cmatz3 0:253885b1f364 75 the cursor should be moved to*/
cmatz3 0:253885b1f364 76 void Grove_LCD_RGB_Backlight::locate(char col, char row)
cmatz3 0:253885b1f364 77 {
cmatz3 0:253885b1f364 78 if(row == 0)
cmatz3 0:253885b1f364 79 {
cmatz3 0:253885b1f364 80 col = col | 0x80;
cmatz3 0:253885b1f364 81 }
cmatz3 0:253885b1f364 82 else
cmatz3 0:253885b1f364 83 {
cmatz3 0:253885b1f364 84 col = col | 0xc0;
cmatz3 0:253885b1f364 85 }
cmatz3 0:253885b1f364 86
cmatz3 0:253885b1f364 87 char data[2];
cmatz3 0:253885b1f364 88 data[0] = 0x80;
cmatz3 0:253885b1f364 89 data[1] = col;
cmatz3 0:253885b1f364 90 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 91 }
cmatz3 0:253885b1f364 92
cmatz3 0:253885b1f364 93 /*This function sends an instruction to the LCD display using the
cmatz3 0:253885b1f364 94 built in instruction codes from the device datasheet using the mbed
cmatz3 0:253885b1f364 95 I2C library*/
cmatz3 0:253885b1f364 96 void Grove_LCD_RGB_Backlight::sendCommand(char value)
cmatz3 0:253885b1f364 97 {
cmatz3 0:253885b1f364 98 char data[2] = {0x80, value};
cmatz3 0:253885b1f364 99 i2c.write(LCD_ADDRESS, data, 2);
cmatz3 0:253885b1f364 100 }
cmatz3 0:253885b1f364 101
cmatz3 0:253885b1f364 102 void Grove_LCD_RGB_Backlight::init()
cmatz3 0:253885b1f364 103 {
cmatz3 0:253885b1f364 104 //Initialize displayfunction parameter for setting up LCD display
cmatz3 0:253885b1f364 105 _displayfunction |= LCD_2LINE;
cmatz3 0:253885b1f364 106 _displayfunction |= LCD_5x10DOTS;
cmatz3 0:253885b1f364 107
cmatz3 0:253885b1f364 108 //Wait for more than 30 ms after power rises above 4.5V per the data sheet
cmatz3 0:253885b1f364 109 wait_ms(50);
cmatz3 0:253885b1f364 110
cmatz3 0:253885b1f364 111
cmatz3 0:253885b1f364 112 // Send first function set command. Wait longer that 39 us per the data sheet
cmatz3 0:253885b1f364 113 sendCommand(LCD_FUNCTIONSET | _displayfunction);
cmatz3 0:253885b1f364 114 wait_us(45);
cmatz3 0:253885b1f364 115
cmatz3 0:253885b1f364 116 // turn the display on
cmatz3 0:253885b1f364 117 displayOn();
cmatz3 0:253885b1f364 118
cmatz3 0:253885b1f364 119 // clear the display
cmatz3 0:253885b1f364 120 clear();
cmatz3 0:253885b1f364 121
cmatz3 0:253885b1f364 122 // Initialize backlight
cmatz3 0:253885b1f364 123 setReg(0, 0);
cmatz3 0:253885b1f364 124 setReg(1, 0);
cmatz3 0:253885b1f364 125 setReg(0x08, 0xAA);
cmatz3 0:253885b1f364 126
cmatz3 0:253885b1f364 127
cmatz3 0:253885b1f364 128
cmatz3 0:253885b1f364 129
cmatz3 0:253885b1f364 130
cmatz3 0:253885b1f364 131 }
cmatz3 0:253885b1f364 132