SparkFun Forums 

Where electronics enthusiasts find answers.

Discussions on how to get your MSP JTAG programmer up and running.
By badcomxanatos
#71233
I am having problems interfacing my LCD with my msp430f149. The LCD has work before for some time and then it just stop working for some reason and I ain't able to get to initialize and show characters properly until a random time in the future. I am sending my initialization code:

#include "msp430x14x.h"
#include "stdlib.h"

//Ports and Function List
//MSP LCD Function
//P1.5 P4 RS 0I-1D
//P1.6 P5 R/W 0W-1R
//P1.7 P6 E

//P2.0 P7 DB0
//P2.1 P8 DB1
//P2.2 P9 DB2
//P2.3 P10 DB3
//P2.4 P11 DB4
//P2.5 P12 DB5
//P2.6 P13 DB6
//P2.7 P14 DB7

void wait(long milliseconds){
int ms=0;
int i=0;
int j=0;
int k=0;
for(ms=0; ms<milliseconds; ms++){
for(i=0; i<16; i++){
for(j=0; j<250; j++){
for(k=0; k<10; k++);
}
}
}
}

void clearLCD(){
P1OUT &= ~0xF0; //and.b 00001111b,P1OUT
P1OUT |= 0x80; //Enable or.b 10000000b,P1OUT
P2OUT = 0x01; //mov.b 00000001b,P2OUT
wait(5);
P1OUT &= ~0x80; //Disable and.b 01111111b,P1OUT
wait(5);
}

void initializeLCD()
{

//Clear pins
P1OUT &= ~0xF0;
P2OUT = 0x00;

//Set directions
P1DIR |= 0xF0;
P2DIR |= 0xFF;

//Clear pins
P1OUT &= ~0xF0;
P2OUT = 0x00;
wait(15);

//Initializes LCD to 2 lines.
P1OUT |= 0x80; //Enable
P2OUT = 0x3C;
wait(4);
P1OUT &= ~0x80; //Disable

wait(4);

//Initializes LCD to 2 lines.
P1OUT |= 0x80; //Enable
P2OUT = 0x3C;
wait(4);
P1OUT &= ~0x80; //Disable

wait(4);

//Initializes LCD to 2 lines.
P1OUT |= 0x80; //Enable
P2OUT = 0x3C;
wait(3);
P1OUT &= ~0x80; //Disable

wait(3);

//Initializes LCD to 2 lines.
P1OUT |= 0x80; //Enable
P2OUT = 0x3C;
wait(3);
P1OUT &= ~0x80; //Disable

wait(3);

//Display Off
P1OUT |= 0x80; //Enable
P2OUT = 0x08;
wait(3);
P1OUT &= ~0x80; //Disable

wait(3);

clearLCD();

//Entry Mode Set (Display on, Cursor blinking command)
P1OUT |= 0x80; //Enable
P2OUT = 0x06;
wait(3);
P1OUT &= ~0x80; //Disable

wait(3);

}

//Outputs this command to the LCD
void LCD_command(unsigned char var){
P1OUT &= 0x0F;
P1OUT |= 0x80; //Enable
P2OUT = var;
wait(3);
P1OUT &= 0x7F; //Disable
wait(3);
}

//Outputs the ASCII code from the var through P2
void LCD_senddata(unsigned char var){
P1OUT |= 0x20;
P1OUT |= 0x80; //Enable
P2OUT = var;
wait(3);
P1OUT &= 0x7F; //Disable
wait(3);

}

//This function displays the first 7 characters of a string in the specified position
void LCD_sendstring(unsigned char pos, char string[] ){
int x = 0;
LCD_command(pos);
while(string[x] != '\0'){
LCD_senddata(string[x]);
x++;
}
}

// This function displays all characters from String mess.
void LCD_message(char mess[]){
int x = 0;
while(mess[x] != '\0'){
LCD_senddata(mess[x]);
x++;
}
}

void writeSelections(int selection, char upLeft[], char upRight[]){
LCD_command(0x01); //LCD clear command, just in case.
if(selection == 1){ //Will display all strings with an arrow on the first position
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_senddata(0x7E); // Arrow ASCII code
LCD_sendstring(0x81, upLeft); // First String on first position
LCD_sendstring(0xC1, upRight);
}else if(selection == 2){
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_sendstring(0x81, upLeft);
LCD_senddata(0x7F);
LCD_sendstring(0xC1, upRight);
}else if(selection == 3){
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_sendstring(0x81, upLeft);
LCD_command(0xC0);
LCD_senddata(0x7E);
LCD_sendstring(0xC1, upRight);
}else if(selection == 4){
LCD_command(0x81); // Set Ram location to first character in LCD
LCD_sendstring(0x81, upLeft);
LCD_sendstring(0xC1, upRight);
LCD_senddata(0x7F);
}
}

void write(int selection, char message[], char option1[]){ //Works exactly like writeSelection.
LCD_command(0x01); // Clear LCD just in case
if(selection == 1){
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_message(message);
LCD_command(0xC0);
LCD_senddata(0x7E);
LCD_sendstring(0xC1, option1);
}else if(selection == 2){
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_message(message);
LCD_command(0xC0);
LCD_sendstring(0xC1, option1);
LCD_senddata(0x7F);
}else if(selection == 0){
LCD_command(0x80); // Set Ram location to first character in LCD
LCD_message(message);
LCD_command(0xC0);
LCD_sendstring(0xC1, option1);
}
}

int main(void){

char test[] = "This is a test";
char option1[] = "Ok Cancel";

WDTCTL = WDTPW + WDTHOLD;

initializeLCD();

write(2,test,option1);
}


Any response will be appreciated. Dont hesitate to ask me for additional information.
By JonBroadwell
#71252
Are you sure that your delay code is working? A good compiler might optimize out all of your 'for' loops because they don't appear to do any useful work (since they're only working on local automatic variables which will be discarded on exit). Try using global variables marked "volatile" instead of locals in your loops and see if that helps.

-Jon
By JonBroadwell
#71253
Also, it's typical to have a while(1) loop in your main function somewhere. Typically in embedded systems you don't want to return from main(), as there's nowhere to return TO. We don't have no stinkin' command line...

int main(void){

char test[] = "This is a test";
char option1[] = "Ok Cancel";

WDTCTL = WDTPW + WDTHOLD;

initializeLCD();

write(2,test,option1);

while(1); // STOP.
}
By badcomxanatos
#71439
Thank you all for your prompt response. It turned out it was not a program related problem. The target board i was using was damaged XD.