Newhaven 320x240 LCD

Dependencies:   mbed

Committer:
pbevans89
Date:
Sun Feb 27 21:40:59 2011 +0000
Revision:
2:2058e2f79157
Parent:
0:c8893901ef8a
Child:
3:1cf3ec6c70d7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pbevans89 2:2058e2f79157 1 /* mbed Newhaven LCD Library
pbevans89 2:2058e2f79157 2 * Copywrite (c) 2011, Paul Evans
pbevans89 2:2058e2f79157 3 */
pbevans89 2:2058e2f79157 4
pbevans89 0:c8893901ef8a 5 #include "mbed.h"
pbevans89 0:c8893901ef8a 6 #include "newhaven.h"
pbevans89 0:c8893901ef8a 7
pbevans89 0:c8893901ef8a 8 NHLCD::NHLCD(PinName PIN_E,PinName PIN_RW,PinName PIN_A0,PinName PIN_CS,PinName PIN_RST, BusInOut *BUSLCD)
pbevans89 0:c8893901ef8a 9 : E(PIN_E),RW(PIN_RW),A0(PIN_A0),CS(PIN_CS),RST(PIN_RST){
pbevans89 0:c8893901ef8a 10 LCD_PORT = BUSLCD;
pbevans89 0:c8893901ef8a 11 }
pbevans89 0:c8893901ef8a 12
pbevans89 0:c8893901ef8a 13 void delay(unsigned int n)
pbevans89 0:c8893901ef8a 14 {
pbevans89 2:2058e2f79157 15 unsigned int i,j;
pbevans89 2:2058e2f79157 16 for (i=0;i<n;i++)
pbevans89 2:2058e2f79157 17 for (j=0;j<350;j++)
pbevans89 2:2058e2f79157 18 {;}
pbevans89 0:c8893901ef8a 19 }
pbevans89 0:c8893901ef8a 20 void delay1(unsigned int i)
pbevans89 0:c8893901ef8a 21 {
pbevans89 2:2058e2f79157 22 while(i--);
pbevans89 0:c8893901ef8a 23 }
pbevans89 0:c8893901ef8a 24
pbevans89 2:2058e2f79157 25 // send commands to the LCD
pbevans89 0:c8893901ef8a 26 void NHLCD::comm_out(unsigned char j){
pbevans89 0:c8893901ef8a 27 LCD_PORT->output();
pbevans89 0:c8893901ef8a 28 A0 = 1;
pbevans89 0:c8893901ef8a 29 LCD_PORT->write(j);
pbevans89 0:c8893901ef8a 30 CS = 0;
pbevans89 0:c8893901ef8a 31 RW = 0;
pbevans89 0:c8893901ef8a 32 E = 1;
pbevans89 0:c8893901ef8a 33 delay(1);
pbevans89 0:c8893901ef8a 34 E = 0;
pbevans89 0:c8893901ef8a 35 RW = 1;
pbevans89 0:c8893901ef8a 36 CS = 1;
pbevans89 0:c8893901ef8a 37 }
pbevans89 0:c8893901ef8a 38
pbevans89 2:2058e2f79157 39 // send data to the LCD
pbevans89 0:c8893901ef8a 40 void NHLCD::data_out(unsigned char j){
pbevans89 0:c8893901ef8a 41 LCD_PORT->output();
pbevans89 0:c8893901ef8a 42 A0 = 0;
pbevans89 0:c8893901ef8a 43 LCD_PORT->write(j);
pbevans89 0:c8893901ef8a 44 CS = 0;
pbevans89 0:c8893901ef8a 45 RW = 0;
pbevans89 0:c8893901ef8a 46 E = 1;
pbevans89 0:c8893901ef8a 47 delay(1);
pbevans89 0:c8893901ef8a 48 E = 0;
pbevans89 0:c8893901ef8a 49 RW = 1;
pbevans89 0:c8893901ef8a 50 CS = 1;
pbevans89 0:c8893901ef8a 51 }
pbevans89 0:c8893901ef8a 52
pbevans89 2:2058e2f79157 53 // clears the entire screen
pbevans89 0:c8893901ef8a 54 void NHLCD::clearScreen(){
pbevans89 0:c8893901ef8a 55 int n;
pbevans89 2:2058e2f79157 56 comm_out(0x46); // command to set cursor location
pbevans89 2:2058e2f79157 57 data_out(0x00); // 0x00 is the start of text screen
pbevans89 2:2058e2f79157 58 data_out(0x00);
pbevans89 2:2058e2f79157 59 comm_out(0x42); // command to write data
pbevans89 2:2058e2f79157 60 for(n=0;n<1200;n++){ // 1200 locations on the screen
pbevans89 2:2058e2f79157 61 data_out(0x20); // fill each with a blank
pbevans89 2:2058e2f79157 62 }
pbevans89 2:2058e2f79157 63 comm_out(0x46); // command to set cursor location
pbevans89 2:2058e2f79157 64 data_out(0xB0); // 0x4B0 is the start of drawing screen
pbevans89 2:2058e2f79157 65 data_out(0x04);
pbevans89 2:2058e2f79157 66 comm_out(0x42); // command to write data
pbevans89 2:2058e2f79157 67 for(n=0;n<9600;n++){ // 9600 total byte locations
pbevans89 2:2058e2f79157 68 data_out(0x00); // set each to 0
pbevans89 0:c8893901ef8a 69 }
pbevans89 0:c8893901ef8a 70 }
pbevans89 0:c8893901ef8a 71
pbevans89 2:2058e2f79157 72 // write text on the screen
pbevans89 2:2058e2f79157 73 void NHLCD::text(char *text, char row, char col){
pbevans89 2:2058e2f79157 74 int c = row*40+col; // gets the correct address for the cursor
pbevans89 2:2058e2f79157 75 comm_out(0x46); // command to set cursor location
pbevans89 2:2058e2f79157 76 data_out((unsigned char)(c&0xFF)); // lower 8 bits of address
pbevans89 2:2058e2f79157 77 data_out((unsigned char)((c&0xFF00)>>8)); // upper 8 bits of address
pbevans89 2:2058e2f79157 78 comm_out(0x42); // command to write data to screen
pbevans89 2:2058e2f79157 79 while(*text != 0) { // write until you hit a null terminator
pbevans89 2:2058e2f79157 80 data_out(*text); // write the current character to the screen
pbevans89 2:2058e2f79157 81 text++; // move to the next character
pbevans89 2:2058e2f79157 82 }
pbevans89 2:2058e2f79157 83 }
pbevans89 0:c8893901ef8a 84
pbevans89 2:2058e2f79157 85 /* set an individual pixel on the screen.
pbevans89 2:2058e2f79157 86 * pixels are grouped in bytes, so you must isolate a particular pixel.
pbevans89 2:2058e2f79157 87 */
pbevans89 2:2058e2f79157 88 void NHLCD::setPixel(int row, int col){
pbevans89 2:2058e2f79157 89 int loc = (0x04<<8)|(0xB0); //sets location to the top left corner of drawing screen
pbevans89 2:2058e2f79157 90 int c = loc+row*40+(col/8); // gets address of the correct byte
pbevans89 2:2058e2f79157 91 comm_out(0x46); // command to set cursor location
pbevans89 2:2058e2f79157 92 data_out((unsigned char)(c&0xFF)); // lower 8 bits of address
pbevans89 2:2058e2f79157 93 data_out((unsigned char)((c&0xFF00)>>8)); // upper 8 bits of address
pbevans89 2:2058e2f79157 94 comm_out(0x43); // command to read the byte
pbevans89 2:2058e2f79157 95 LCD_PORT->input(); // sets the buffer to input data
pbevans89 2:2058e2f79157 96 unsigned char buffer = LCD_PORT->read(); // stores byte in buffer
pbevans89 2:2058e2f79157 97 buffer = buffer|(1<<(7-((row*320+col)%8))); // sets the particular pixel on the byte
pbevans89 2:2058e2f79157 98 LCD_PORT->output(); // sets the buffer to output data
pbevans89 2:2058e2f79157 99
pbevans89 2:2058e2f79157 100 comm_out(0x46); //command to set cursor location
pbevans89 2:2058e2f79157 101 data_out((unsigned char)(c&0xFF)); // lower 8 bits of address
pbevans89 2:2058e2f79157 102 data_out((unsigned char)((c&0xFF00)>>8)); // upper 8 bits of address
pbevans89 2:2058e2f79157 103 comm_out(0x42); // command to write to the screen
pbevans89 2:2058e2f79157 104 data_out(buffer); // write buffer to the screen
pbevans89 2:2058e2f79157 105 }
pbevans89 2:2058e2f79157 106
pbevans89 2:2058e2f79157 107 // initialize the LCD
pbevans89 0:c8893901ef8a 108 void NHLCD::Init(void){
pbevans89 2:2058e2f79157 109 /* reset the device */
pbevans89 0:c8893901ef8a 110 RST = 0;
pbevans89 0:c8893901ef8a 111 delay(5);
pbevans89 0:c8893901ef8a 112 RST = 1;
pbevans89 0:c8893901ef8a 113 delay(10);
pbevans89 0:c8893901ef8a 114
pbevans89 2:2058e2f79157 115 comm_out(0x40); // system set command
pbevans89 0:c8893901ef8a 116 delay(5);
pbevans89 2:2058e2f79157 117 data_out(0x30); // parameters
pbevans89 2:2058e2f79157 118 data_out(0x87); // horizontal character size (0x80 = 1) MUST BE MULTIPLE OF 320
pbevans89 2:2058e2f79157 119 data_out(0x07); // vertical character size (0x00 = 1) MUST BE MULTIPLE OF 240
pbevans89 2:2058e2f79157 120 data_out(40); // addresses per line
pbevans89 2:2058e2f79157 121 data_out(80); // bytes per line
pbevans89 2:2058e2f79157 122 data_out(0xEF); // 240 displace lines
pbevans89 2:2058e2f79157 123 data_out(0x28); // virtual address 1
pbevans89 2:2058e2f79157 124 data_out(0x00); // virtual address 2
pbevans89 0:c8893901ef8a 125
pbevans89 2:2058e2f79157 126 comm_out(0x44); // scroll
pbevans89 2:2058e2f79157 127 data_out(0x00); // start address 1
pbevans89 2:2058e2f79157 128 data_out(0x00); // start address 2
pbevans89 2:2058e2f79157 129 data_out(0xEF); // 240 lines
pbevans89 2:2058e2f79157 130 data_out(0xB0); // 2nd screen start1
pbevans89 2:2058e2f79157 131 data_out(0x04); // 2nd screen start2
pbevans89 2:2058e2f79157 132 data_out(0xEF); // 2nd screen 240 lines
pbevans89 2:2058e2f79157 133 data_out(0x00); // 3rd screen address1
pbevans89 2:2058e2f79157 134 data_out(0x00); // 3rd screen address2
pbevans89 2:2058e2f79157 135 data_out(0x00); // 4th screen address1
pbevans89 2:2058e2f79157 136 data_out(0x00); // 4th screen address2
pbevans89 0:c8893901ef8a 137
pbevans89 2:2058e2f79157 138 comm_out(0x5A); // hdot scr
pbevans89 2:2058e2f79157 139 data_out(0x00); // horizontal pixel shift = 0
pbevans89 0:c8893901ef8a 140
pbevans89 2:2058e2f79157 141 comm_out(0x5B); // overlay
pbevans89 2:2058e2f79157 142 data_out(0x00); // OR
pbevans89 0:c8893901ef8a 143
pbevans89 2:2058e2f79157 144 comm_out(0x58); // set display
pbevans89 2:2058e2f79157 145 data_out(0x56);
pbevans89 0:c8893901ef8a 146
pbevans89 2:2058e2f79157 147 comm_out(0x5D); // cursor form
pbevans89 2:2058e2f79157 148 data_out(0x04); // 5 pixels wide
pbevans89 2:2058e2f79157 149 data_out(0x86); // 7 pixels tall
pbevans89 0:c8893901ef8a 150
pbevans89 2:2058e2f79157 151 comm_out(0x4C); // cursor direction = right
pbevans89 0:c8893901ef8a 152
pbevans89 2:2058e2f79157 153 comm_out(0x59); // disp on/off
pbevans89 2:2058e2f79157 154 data_out(0x16); // on
pbevans89 0:c8893901ef8a 155 wait_ms(5);
pbevans89 0:c8893901ef8a 156 }