Newhaven 320x240 LCD

Dependencies:   mbed

newhaven.cpp

Committer:
pbevans89
Date:
2011-02-27
Revision:
3:1cf3ec6c70d7
Parent:
2:2058e2f79157
Child:
4:aa6dc362b462

File content as of revision 3:1cf3ec6c70d7:

/* mbed Newhaven LCD Library
 * Copywrite (c) 2011, Paul Evans
 */

#include "mbed.h"
#include "newhaven.h"

NHLCD::NHLCD(PinName PIN_E,PinName PIN_RW,PinName PIN_A0,PinName PIN_CS,PinName PIN_RST, BusInOut *BUSLCD)
    : E(PIN_E),RW(PIN_RW),A0(PIN_A0),CS(PIN_CS),RST(PIN_RST){
    LCD_PORT = BUSLCD;   
}

void delay(unsigned int n){
    unsigned int i,j;
    for (i=0;i<n;i++)
          for (j=0;j<350;j++)
              {;}
}

void delay1(unsigned int i){
    while(i--);
}

// send commands to the LCD
void NHLCD::comm_out(unsigned char j){
    LCD_PORT->output();     // sets the buffer to output data
    A0 = 1;                 // set for a command output
    LCD_PORT->write(j);     // send the data in the buffer
    CS = 0;                 // active LOW chip select
    RW = 0;                 // set to 0 for write operation
    E = 1;                  // operation enable
    delay(1);               // wait for operation to complete
    E = 0;                  // reset values
    RW = 1;
    CS = 1; 
}

// send data to the LCD
void NHLCD::data_out(unsigned char j){
    LCD_PORT->output();     // sets the buffer to output data
    A0 = 0;                 // set for a data output
    LCD_PORT->write(j);     // send the data in the buffer
    CS = 0;                 // active LOW chip select
    RW = 0;                 // set to 0 for write operation
    E = 1;                  // operation enable
    delay(1);               // wait for operation to complete
    E = 0;                  // reset values           
    RW = 1;
    CS = 1;
}

// clears the entire screen
void NHLCD::clearScreen(){
    int n;
    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); // 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);   // bytes per line
    data_out(0xEF); // 240 displace lines
    data_out(0x28); // virtual address 1
    data_out(0x00); // virtual address 2
    
    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); // hdot scr
    data_out(0x00); // horizontal pixel shift = 0
    
    comm_out(0x5B); // overlay
    data_out(0x00); // OR
    
    comm_out(0x58); // set display
    data_out(0x56); 
    
    comm_out(0x5D); // cursor form
    data_out(0x04); // 5 pixels wide
    data_out(0x86); // 7 pixels tall
    
    comm_out(0x4C); // cursor direction = right
    
    comm_out(0x59); // disp on/off
    data_out(0x16); // on
    wait_ms(5);
}