/*LCD Test written by Derrick Huestis (Pi-Ro) * * This code will drive a 16x1 * lcd with memory set up in two * 8 caracter blocks. * * It is my first C program, so * let me know if it has problems! * * pinout: * lcdpin1----ground LCD Ground * lcdpin2----5v Supply Voltage * lcdpin3----ground Contrast Adjustment * lcdpin4----P1.7 Data/Instruction select * lcdpin5----ground Read/Write * lcdpin6----P1.6 Enable * lcdpin7----ground Unused pins for 8 bit mode * lcdpin8----ground * lcdpin9----ground * lcdpin10---ground * lcdpin11---P1.0 Data pins * lcdpin12---P1.3 * lcdpin13---P1.4 * lcdpin14---P1.5 */ #include //change this if you are using a different chip int main() { // message for the lcd char lcd[16] = "Hello There! "; //note: this message will end with black squares // if it doesn't have spaces for the last 4 characters WDTCTL = WDTPW + WDTHOLD; /* Stop watchdog timer */ P1DIR = BIT0 + BIT3 + BIT4 + BIT5 + BIT6 + BIT7; // output lcd pins P1OUT = 0; _delay_cycles(1000000); //pause to ensure the lcd is on // ----- initialize lcd ---- // This section would look a lot better if I could figure out how to use function // routines, but I just had errors when I tried so I will clean it up // when I learn how to do it properly //------------------------- P1OUT |= BIT3 + BIT6; // Function set routine _delay_cycles(1000); P1OUT &= ~BIT3 + BIT6; _delay_cycles(1500); P1OUT |= BIT3 + BIT6; // and repeat in 4-bit mode to declare 2 line mode _delay_cycles(500); P1OUT &= ~BIT3 + BIT6; _delay_cycles(500); P1OUT |= BIT5 +BIT6; // finish last 4 bits of function routine _delay_cycles(500); P1OUT = 0; _delay_cycles(350000); // delay for the lcd to start up P1OUT |= BIT6; // Display on, turn on cursor and blink _delay_cycles(500); //first 4 bits P1OUT = 0; _delay_cycles(500); P1OUT |= BIT5 + BIT3 + BIT4 + BIT0 + BIT6; // last 4 bits of display on routine _delay_cycles(500); P1OUT = 0; _delay_cycles(500); int i,d,e,p,q; //declare variables needed to run the lcd d = 0; //must be set to 0 before using bitwise, it will be the output for the pins e = 0; //this variable incruments to display different characters of the message for(p=0;p<2;p++) { //the code loops twice with a command after 8 characters to change memory address for(q=0;q<8;q++) { //this will loop 16 times for 16 characters d = 0; i = lcd[e]; //first 4 pinouts i >>= 4; //this uses bitwise to skip over P1.1 and P1.2 used for serial communication i &= 1; //it breaks the character into the first 4 bits d |= i; i = lcd[e]; i >>= 5; i &= 7; i <<= 3; d |= i; P1OUT = d; //send first 4 bits to lcd P1OUT |= BIT6 + BIT7; _delay_cycles(500); P1OUT = 0x80; _delay_cycles(500); d = 0; i = lcd[e]; //last 4 pinouts i &= 1; //same as above, but it breaks the pinouts into the last 4 bits d |= i; i = lcd[e]; i >>= 1; i &= 7; i <<= 3; d |= i; P1OUT = d; //send last 4 bits to lcd P1OUT |= BIT6 + BIT7; _delay_cycles(500); P1OUT = 0; _delay_cycles(500); e++; //get ready to send next character } if(p<1) { P1OUT |= BIT5 + BIT3 + BIT6; //this will change the address of the lcd _delay_cycles(500); //so additional text will flow across lcd P1OUT = 0; //instead of acting like an 8x2 lcd _delay_cycles(500); P1OUT |= BIT5 + BIT6; //last 4 bits _delay_cycles(500); P1OUT = 0; _delay_cycles(500); } } _BIS_SR(LPM4_bits); //program complete, enter low power mode }