Lib for the LCD display on mbed lab Board

Dependents:   SprintUSBModemWebsocketTest-LCD-RO iOSAppChat Christmas-LCD led_dimm ... more

Basic information

The LCD on the mbed lab board has 128 x 32 pixels and is connected via spi. It use a ST7565R controller. The spi connection is fast, but it has one drawback - you can't read the display buffer. This is a problem, because each bit reflect a pixel. If you want to set only one bit / pixel, you have to know the status of the other seven bits / pixel. Because of this we have to use a framebuffer (128 * 32 / 8 = 512 Byte). All drawing functions are working on this framebuffer. If you use the LPC1768 based mbed, the dma channel 0 is used to speed up the transfer to the lcd. This information is only relevant if you also want to use the dma controller. You have to switch to a other channel.

There are two update mode. After startup the automode is turned on. This means that the display is automaticly updated after the drawing. For example - if you call the function

lcd.line(x0, y0, x1, y1, 1);

a line from x0,y0 to x1,y1 is drawn inside the framebuffer and after that the framebuffer is copied to the lcd. If you draw more lines, it will be faster to draw all graphics inside the framebuffer and update the lcd only once. To do so, you can use the function :

lcd.set_auto_up(0);

This switch off the autoupdate. If you want to see it, you have to refresh the lcd by calling the function :

lcd.copy_to_lcd();

lcd.set_auto_up(1);

will switch back to auto update mode.

Basic functions

To use the lcd we have to create a lcd object :

C12832_LCD lcd;

There are two drawing modes : NORMAL and XOR. At startup the mode is NORMAL. If you use

lcd.setmode(XOR);

you switch to XOR mode. In this mode a pixel in the frambuffer is inverted if you set it to 1.

lcd.setmode(NORMAL);

switch back to normal mode.

The function :

lcd.invert(1);

will invert the lcd. This is done by the lcd controller. The framebuffer is not changed.

lcd.invert(0);

will switch back.

The function :

lcd.cls();

clear the screen.

The function :

lcd.set_contrast(25);

will set the contrast. The lib start with 23. A value between 10 and 35 will be visible.

Text

To print text you simply have to use the printf function. The output of the stdout is redirected to the lcd.

lcd.printf("temperature : %3.2f F",heat);

The position can be set up with the function :

lcd.locate(x,y);

At startup a 7 pixel font is used. If you want to use a different font you can include the lib http://mbed.org/users/dreschpe/code/LCD_fonts. This lib include four additional fonts. From 6 pixel to 23 pixel. To switch the font use :

lcd.set_font((unsigned char*) Arial_9);

The names of the fonts are : Small_6, Small_7, Arial_9, Arial12x12 and Arial24x23.

The function :

lcd._putc(c);

print the char c on the actual cursor position.

The function :

lcd.character(x, y, c);

print the char c at position x,y.

Graphic

The function :

lcd.line(x0, y0, x1, y1, color);

draw a single pixel line from x0,y0 to x1,y1. If color is 1 : black, 0 : white.

The function :

lcd.rect(x0, y0, x1, y1, color);

draw a single pixel rectangle from x0, y0 to x1, y1. If color is 1 : black, 0 : white.

The function :

lcd.fillrect(x0, y0, x1, y1, color);

draw a filled rectangle from x0, y0 to x1, y1. If color is 1 : black, 0 : white.

The function :

lcd.circle(x, y, r, color);

draw a circle with x,y center and radius r. If color is 1 : black, 0 : white.

The function :

lcd.fillcircle(x, y, r, color);

draw a filled circle with x,y center and radius r. If color is 1 : black, 0 : white.

The function :

lcd.pixel(x, y, color);

set a single pixel at x,y. If color is 1 : black, 0 : white. This function is not updating the lcd ! Even if the autoupdate is on. You have to call lcd.copy_to_lcd() after using this function - or to use a other function with autoupdate afterwards.

mbed rtos

To use the mbed rtos with the lib we have to make the lib thread save. What is the problem ? If different threads are writing to the lcd it can end in troubble. Thread1 is using the pintf("hello mbed") function to print this string to the actual position. After the chars "hel" are printed ,the scheduler is switching to thread2. This thread is writing at a different position on the screen. After that the scheduler is switch back to thread1 and the print function continue. Thread1 did not know that the internal cursor position has changed ....

To protect the access to the lcd we use a Mutex. If a thread has the mutex and a other thread also want it, the second thread has to wait.

Mutex lcd_mutex;  // define the mutex
    //...
lcd_mutex.lock(); // get the mutex or wait

//acccess to the lcd
 
lcd_mutex.unlock(); // free the mutex 

We use this framing to access the lcd.

Test program to show : http://mbed.org/users/dreschpe/code/lab1/

Revision:
2:bdc53502af17
Parent:
1:66dd8afbfd06
Child:
3:468cdccff7af
diff -r 66dd8afbfd06 -r bdc53502af17 C12832_lcd.cpp
--- a/C12832_lcd.cpp	Tue Oct 16 17:50:37 2012 +0000
+++ b/C12832_lcd.cpp	Thu Oct 18 20:54:09 2012 +0000
@@ -80,7 +80,7 @@
 {
     contrast = o;
     wr_cmd(0x81);      //  set volume
-    wr_cmd(o & 0x2F);
+    wr_cmd(o & 0x3F);
 }
 
 unsigned int C12832_LCD::get_contrast(void)
@@ -95,10 +95,14 @@
 {
     _A0 = 0;
     _CS = 0;
+#if defined TARGET_LPC1768     // fast without mbed lib
     LPC_SSP1->DR = cmd;
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
-    _CS = 1;
+#else
+    _spi.write(cmd);
+#endif
+_CS = 1;
 }
 
 // write data to lcd controller
@@ -107,9 +111,13 @@
 {
     _A0 = 1;
     _CS = 0;
+#if defined TARGET_LPC1768     // fast without mbed lib
     LPC_SSP1->DR = dat;
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // wait for SPI1 idle
+#else
+    _spi.write(dat);
+#endif
     _CS = 1;
 }
 
@@ -147,12 +155,14 @@
 
     wr_cmd(0xA6);     // display normal
 
-    //setup DMA channel 0
+    
+#if defined TARGET_LPC1768          //setup DMA channel 0       
     LPC_SC->PCONP |= (1UL << 29);   // Power up the GPDMA
-    LPC_GPDMA->DMACConfig = 1;          // enable DMA controller
+    LPC_GPDMA->DMACConfig = 1;      // enable DMA controller
     LPC_GPDMA->DMACIntTCClear = 0x1;
     LPC_GPDMA->DMACIntErrClr = 0x1;
     LPC_GPDMACH0->DMACCLLI = 0;
+#endif
     // clear and update LCD
     memset(buffer,0x00,512);  // clear display buffer
     copy_to_lcd();
@@ -180,12 +190,16 @@
 
 void C12832_LCD::copy_to_lcd(void)
 {
+#ifndef TARGET_LPC1768 
+    int i;
+#endif
     //page 0
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB0);      // set page address  0
+    _A0 = 1;
+#if defined TARGET_LPC1768     
     _CS = 0;
-    _A0 = 1;
     // start 128 byte DMA transfer to SPI1
     LPC_GPDMACH0->DMACCDestAddr = (uint32_t)&LPC_SSP1->DR; // we send to SSP1
     LPC_SSP1->DMACR = 0x2;  // Enable SSP1 for DMA.
@@ -200,13 +214,19 @@
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
     _CS = 1;
+#else  // no DMA
+    for(i=0;i<128;i++){
+        wr_dat(buffer[i]);
+        }
+#endif 
 
     // page 1
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB1);      // set page address  1
+    _A0 = 1;
+#if defined TARGET_LPC1768         
     _CS = 0;
-    _A0 = 1;
     // start 128 byte DMA transfer to SPI1
     LPC_GPDMA->DMACIntTCClear = 0x1;
     LPC_GPDMA->DMACIntErrClr = 0x1;
@@ -219,13 +239,19 @@
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
     _CS = 1;
+#else // no DMA
+    for(i=128;i<256;i++){
+        wr_dat(buffer[i]);
+        }
+#endif     
 
     //page 2
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB2);      // set page address  2
+    _A0 = 1;
+#if defined TARGET_LPC1768      
     _CS = 0;
-    _A0 = 1;
     // start 128 byte DMA transfer to SPI1
     LPC_GPDMA->DMACIntTCClear = 0x1;
     LPC_GPDMA->DMACIntErrClr = 0x1;
@@ -238,13 +264,20 @@
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
     _CS = 1;
-    
+#else // no DMA
+    for(i=256;i<384;i++){
+        wr_dat(buffer[i]);
+        }
+#endif         
+
     //page 3
     wr_cmd(0x00);      // set column low nibble 0
     wr_cmd(0x10);      // set column hi  nibble 0
     wr_cmd(0xB3);      // set page address  3
-    _CS = 0;
     _A0 = 1;
+    
+     _CS = 0;
+#if defined TARGET_LPC1768     
     // start 128 byte DMA transfer to SPI1
     LPC_GPDMA->DMACIntTCClear = 0x1;
     LPC_GPDMA->DMACIntErrClr = 0x1;
@@ -257,6 +290,11 @@
     do {
     } while ((LPC_SSP1->SR & 0x10) == 0x10); // SPI1 not idle
     _CS = 1;
+#else // no DMA
+    for(i=384;i<512;i++){
+        wr_dat(buffer[i]);
+        }
+#endif   
 }
 
 void C12832_LCD::cls(void)