This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

plz,require suggestion in LCD 4 bit mode.

i have written the following code for LCD interfacing in 4 bit mode to ez430-rf2500

#include<msp430x22x4.h>

#define lcd_data P2OUT;

 

#define set_rs  P4OUT|=BIT4;

#define clear_rs  P4OUT&=~BIT4;

 

#define set_en P4OUT|=BIT5;

#define clear_en P4OUT&=~BIT5;

 

void lcdcmd(unsigned char);

void lcddata(unsigned char);

void delay(unsigned int);

void lcdcmdi(unsigned char);

unsigned char h_byte,l_byte;

unsigned int mesg;

void main()

{  

P2DIR|= BIT0+BIT1+BIT2+BIT3;

P4DIR|=BIT4+BIT5; 

unsigned char i=0;

unsigned char command[] = {0x28,0x0E,0x01,0x06,0x80,0};

unsigned int temp= 32;

unsigned char p,q;

delay(20);

lcdcmdi(0x30);

delay(10);

lcdcmdi(0x30);

delay(1);

lcdcmdi(0x30);

delay(1);

lcdcmdi(0x20);

delay(1);

   while(1)

   {                             

         mesg=temp;

      p=(mesg/10)+48;

q=(mesg%10)+48;

 

for (i=0;command[i]!=0;i++)

              {

      lcdcmd(command[i]);

          } 

 

lcddata(p);

lcddata(q);

     }

}

void lcdcmdi(unsigned char value)

{

 value >>=4;

 lcd_data=value;

 clear_rs;

 set_en;

 delay(10);

 clear_en;

 return;

 }

 

void delay(unsigned int itime)

{

unsigned int i,j;

for(i=0;i<itime;i++)

for(j=0;j<1275;j++);

}

 

void lcdcmd(unsigned char value)

{

    l_byte = value & 0x0f;

    h_byte = (value>>4) & 0x0f;

    lcd_data=h_byte;

    clear_rs;

    set_en ;

    delay(10);

    clear_en = 0;

    lcd_data=l_byte;

    set_en;

    delay(10);

    clear_en=0;

    return;

}

 

void lcddata(unsigned char value)

{

    l_byte = value & 0x0f;

    h_byte = (value>>4) & 0x0f;

    lcd_data=h_byte;

    set_rs ;

    set_en ;

    delay(10);

    clear_en;

    lcd_data=l_byte;

    set_en;

    delay(10);

    clear_en;

    return;

}

************************************************************************************************************************************************

 

but i found that p2.6 ,p2.7 are used for communication with antenna.so, i can't disturb them.

is there any way i can only 4 bits in lcd_data.because p2out refers to all 8 bits.

when i send a nibble the upperbits of p2out will be 0 .this disturbs antenna apllication.

 how can i configure only 4 bits to be in output mode in lcd_data? 

plz help!

  • Instead of setting the register you can resort to a bitwise or and a bitwise and operation:

    So if you have 4 bits like 1011 (=0x0B) and want them to be set to the lower 4 bits of P2OUT you can do something like this:

    P2OUT |=  0x0B; // set 1s
    P2OUT &= (0xF0 | 0x0B); // set 0s

    Also note that you might want to check your code entirely I only glanced over it and (value & 0x0F) will not give a low byte but a low nibble.

    I don't know if it is a problem for your application if you have a second state on the lines as you won't be able to set 4 bits directly this way but there is always an unwanted transition.

    So I'd write it like this:

     void lcddata(unsigned char value)
    {
    unsigned char lowNibble, highNibble;
    lowNibble = value & 0x0F;
    highNibble = (value>>4) & 0x0F;
    LCD_DATA |= highNibble;
    LCD_DATA &= (0xF0 | highNibble);
    SET_RS;
    SET_EN;
    delay(10);
    CLEAR_EN;
    LCD_DATA |= lowNibble;
    LCD_DATA &= (0xF0 | lowNibble);
    SET_EN;
    delay(10);
    CLEAR_EN;
    }

     I hope you won't feel to offended but there are a lot of issues with your code in my eyes, and also with your style of asking for advice.

    First an advice about delay-loops: They will get optimized away by the compiler if you turn on any optimization without actually declaring the counting variables as volatile. There is also an intrinsic to delay a set amount of cycles: __delay_cycles() in Code Composer Studio.

    Second, something on defines: you do not use a semicolon on the end of a define. This can lead to quite horrible problems. Also defines are usually written in uppercase to separate them from normal variable declarations and function names.

    Third: Don't use ambiguous names. lcd_data and lcddata this is the same. Name them like TransferLCDData() and LCD_DATA_PORT, you will find this makes your code a lot more maintainable and understandable for other people.

    Last: Write your questions in a more readable manner, do not use abbreviations like plz, thx and other stuff found on your average bulletin board. If you keep your standard of writing on a high level you'll most likely get more and faster answers. Attach code at the bottom, so that someone who wants to answer your question can read it first, decide if he can answer it and analyse your code after this step.
    Forcing someone to scroll down a mighty wall of code before you even know what the question is about will lead to a reflex-like click on the back button.
    And for wall of codes in general: Reduce your problem to the only relevant things. For the solution I posted the lcddata-function together with the defines would have been sufficient. If someone needs more information he will ask for it.

**Attention** This is a public forum