Newhaven 320x240 LCD

Dependencies:   mbed

Revision:
2:2058e2f79157
Parent:
0:c8893901ef8a
Child:
3:1cf3ec6c70d7
--- a/newhaven.cpp	Sun Feb 27 21:14:19 2011 +0000
+++ b/newhaven.cpp	Sun Feb 27 21:40:59 2011 +0000
@@ -1,3 +1,7 @@
+/* mbed Newhaven LCD Library
+ * Copywrite (c) 2011, Paul Evans
+ */
+
 #include "mbed.h"
 #include "newhaven.h"
 
@@ -8,17 +12,17 @@
 
 void delay(unsigned int n)
 {
-	unsigned int i,j;
-	for (i=0;i<n;i++)
-  		for (j=0;j<350;j++)
-  			{;}
+    unsigned int i,j;
+    for (i=0;i<n;i++)
+          for (j=0;j<350;j++)
+              {;}
 }
 void delay1(unsigned int i)
 {
-	while(i--);
+    while(i--);
 }
 
-
+// send commands to the LCD
 void NHLCD::comm_out(unsigned char j){
     LCD_PORT->output();  
     A0 = 1;
@@ -32,6 +36,7 @@
     CS = 1; 
 }
 
+// send data to the LCD
 void NHLCD::data_out(unsigned char j){
     LCD_PORT->output();
     A0 = 0;
@@ -45,101 +50,107 @@
     CS = 1;
 }
 
+// clears the entire screen
 void NHLCD::clearScreen(){
     int n;
-	comm_out(0x46);
-	data_out(0x00);
-	data_out(0x00);
-	comm_out(0x42);
-	for(n=0;n<1200;n++){
-		data_out(0x20);
-	}
-	comm_out(0x46);
-	data_out(0xB0);
-	data_out(0x04);
-	comm_out(0x42);
-	for(n=0;n<9600;n++){
-		data_out(0x00);
-	}
-}
-
-void NHLCD::text(char *text, char row, char col){
-    int c = row*40+col;
-    comm_out(0x46);
-    data_out((unsigned char)(c&0xFF));
-    data_out((unsigned char)((c&0xFF00)>>8));
-    comm_out(0x42);
-    while(*text != 0) {
-        data_out(*text);
-        text++;
+    comm_out(0x46);         // command to set cursor location
+    data_out(0x00);         // 0x00 is the start of text screen
+    data_out(0x00);
+    comm_out(0x42);         // command to write data
+    for(n=0;n<1200;n++){    // 1200 locations on the screen
+        data_out(0x20);     // fill each with a blank
+    }
+    comm_out(0x46);         // command to set cursor location
+    data_out(0xB0);         // 0x4B0 is the start of drawing screen
+    data_out(0x04);
+    comm_out(0x42);         // command to write data
+    for(n=0;n<9600;n++){    // 9600 total byte locations
+        data_out(0x00);     // set each to 0
     }
 }
 
+// write text on the screen
+void NHLCD::text(char *text, char row, char col){
+    int c = row*40+col;                         // gets the correct address for the cursor
+    comm_out(0x46);                             // command to set cursor location
+    data_out((unsigned char)(c&0xFF));          // lower 8 bits of address
+    data_out((unsigned char)((c&0xFF00)>>8));   // upper 8 bits of address
+    comm_out(0x42);                             // command to write data to screen
+    while(*text != 0) {                         // write until you hit a null terminator
+        data_out(*text);                        // write the current character to the screen
+        text++;                                 // move to the next character
+    }
+}
 
+/* set an individual pixel on the screen.
+ * pixels are grouped in bytes, so you must isolate a particular pixel.
+ */
+void NHLCD::setPixel(int row, int col){
+    int loc = (0x04<<8)|(0xB0);                 //sets location to the top left corner of drawing screen
+    int c = loc+row*40+(col/8);                 // gets address of the correct byte
+    comm_out(0x46);                             // command to set cursor location
+    data_out((unsigned char)(c&0xFF));          // lower 8 bits of address
+    data_out((unsigned char)((c&0xFF00)>>8));   // upper 8 bits of address
+    comm_out(0x43);                             // command to read the byte
+    LCD_PORT->input();                          // sets the buffer to input data
+    unsigned char buffer = LCD_PORT->read();    // stores byte in buffer
+    buffer = buffer|(1<<(7-((row*320+col)%8))); // sets the particular pixel on the byte
+    LCD_PORT->output();                         // sets the buffer to output data
+    
+    comm_out(0x46);                             //command to set cursor location
+    data_out((unsigned char)(c&0xFF));          // lower 8 bits of address
+    data_out((unsigned char)((c&0xFF00)>>8));   // upper 8 bits of address
+    comm_out(0x42);                             // command to write to the screen
+    data_out(buffer);                           // write buffer to the screen
+}
+
+// initialize the LCD
 void NHLCD::Init(void){
+    /* reset the device */
     RST = 0;
     delay(5);
     RST = 1;
     delay(10);
     
-    comm_out(0x40);
+    comm_out(0x40); // system set command
     delay(5);
-    data_out(0x30); //parameters
-    data_out(0x87); //horizontal character size (0x80 = 1) MUST BE MULTIPLE OF 320
-    data_out(0x07); //vertical character size (0x00 = 1)  MUST BE MULTIPLE OF 240
-    data_out(40); //addresses per line
-    data_out(80);
-    data_out(0xEF);
-    data_out(0x28);
-    data_out(0x00);
+    data_out(0x30); // parameters
+    data_out(0x87); // horizontal character size (0x80 = 1) MUST BE MULTIPLE OF 320
+    data_out(0x07); // vertical character size (0x00 = 1)  MUST BE MULTIPLE OF 240
+    data_out(40);   // addresses per line
+    data_out(80);   // bytes per line
+    data_out(0xEF); // 240 displace lines
+    data_out(0x28); // virtual address 1
+    data_out(0x00); // virtual address 2
     
-    comm_out(0x44);
-    data_out(0x00);
-    data_out(0x00);
-    data_out(0xEF);
-    data_out(0xB0);
-    data_out(0x04);
-    data_out(0xEF);
-    data_out(0x00);
-    data_out(0x00);
-    data_out(0x00);
-    data_out(0x00);
+    comm_out(0x44); // scroll
+    data_out(0x00); // start address 1
+    data_out(0x00); // start address 2
+    data_out(0xEF); // 240 lines
+    data_out(0xB0); // 2nd screen start1
+    data_out(0x04); // 2nd screen start2
+    data_out(0xEF); // 2nd screen 240 lines
+    data_out(0x00); // 3rd screen address1
+    data_out(0x00); // 3rd screen address2
+    data_out(0x00); // 4th screen address1
+    data_out(0x00); // 4th screen address2
     
-    comm_out(0x5A);
-    data_out(0x00);
+    comm_out(0x5A); // hdot scr
+    data_out(0x00); // horizontal pixel shift = 0
     
-    comm_out(0x5B);
-    data_out(0x00);
+    comm_out(0x5B); // overlay
+    data_out(0x00); // OR
     
-    comm_out(0x58);
-    data_out(0x56);
+    comm_out(0x58); // set display
+    data_out(0x56); 
     
-    comm_out(0x5D);
-    data_out(0x04);
-    data_out(0x86);
+    comm_out(0x5D); // cursor form
+    data_out(0x04); // 5 pixels wide
+    data_out(0x86); // 7 pixels tall
     
-    comm_out(0x4C);
+    comm_out(0x4C); // cursor direction = right
     
-    comm_out(0x59);
-    data_out(0x16);
+    comm_out(0x59); // disp on/off
+    data_out(0x16); // on
     wait_ms(5);
 }
-
-void NHLCD::setPixel(int row, int col){
-    int loc = (0x04<<8)|(0xB0);
-    int c = loc+row*40+(col/8);
-    comm_out(0x46);
-    data_out((unsigned char)(c&0xFF));
-    data_out((unsigned char)((c&0xFF00)>>8));
-    comm_out(0x43);
-    LCD_PORT->input();
-    unsigned char buffer = LCD_PORT->read();
-    buffer = buffer|(1<<(7-((row*320+col)%8)));
-    LCD_PORT->output();
-    
-    comm_out(0x46);
-    data_out((unsigned char)(c&0xFF));
-    data_out((unsigned char)((c&0xFF00)>>8));
-    comm_out(0x42);
-    data_out(buffer);
-}