Jump to content
43oh

Recommended Posts

Hi! First of all I want to say that I'm a beginner with ucontrollers. I recently got hold of a launchpad and I had a LCD at home and I managed to write on it, using the code and the pin definitions from TI.

 

However I want to build a shield and it would be easier to use other definitions. I've attached the code and wrote in the comments where i changed the code.

 

Could somebody please help me and explain why I'm only getting strange characters on my LCM?

Thanks!

 

#include 

#define     LCM_DIR               P1DIR
#define     LCM_OUT               P1OUT

#define     LCM_PIN_RS            BIT0          
#define     LCM_PIN_EN            BIT1          
#define     LCM_PIN_D7            BIT3          //instead of BIT7  
#define     LCM_PIN_D6            BIT4          //instead of BIT6
#define     LCM_PIN_D5            BIT2          //instead of BIT5
#define     LCM_PIN_D4            BIT5          //instead of BIT4


#define     LCM_PIN_MASK  ((LCM_PIN_RS | LCM_PIN_EN | LCM_PIN_D7 | LCM_PIN_D6 | LCM_PIN_D5 | LCM_PIN_D4))

#define     FALSE                 0
#define     TRUE                  1

//
// Routine Desc:
//
// This is the function that must be called
// whenever the LCM needs to be told to
// scan it's data bus.
//
// Parameters:
//
//     void.
//
// Return
//
//     void.
//
void PulseLcm()
{
   //
   // pull EN bit low
   //
   LCM_OUT &= ~LCM_PIN_EN;
   __delay_cycles(200);

   //
   // pull EN bit high
   //
   LCM_OUT |= LCM_PIN_EN;
   __delay_cycles(200);

   //
   // pull EN bit low again
   //
   LCM_OUT &= (~LCM_PIN_EN);
   __delay_cycles(200);
}



//
// Routine Desc:
//
// Send a byte on the data bus in the 4 bit mode
// This requires sending the data in two chunks.
// The high nibble first and then the low nible
//
// Parameters:
//
//    ByteToSend - the single byte to send
//
//    IsData - set to TRUE if the byte is character data
//                  FALSE if its a command
//
// Return
//
//     void.
//
void SendByte(char ByteToSend, int IsData)
{
   //
   // clear out all pins
   //
   LCM_OUT &= (~LCM_PIN_MASK);
   //
   // set High Nibble (HN) -

   LCM_OUT |= (2 >> (ByteToSend & 0xF0)); //instead of LCM_OUT |= (ByteToSend & 0xF0); 

   if (IsData == TRUE)
   {
       LCM_OUT |= LCM_PIN_RS;
   }
   else
   {
       LCM_OUT &= ~LCM_PIN_RS;
   }

   //
   // we've set up the input voltages to the LCM.
   // Now tell it to read them.
   //
   PulseLcm();
    //
   // set Low Nibble (LN) -

   LCM_OUT &= (~LCM_PIN_MASK);
   LCM_OUT |= ((ByteToSend & 0x0F) << 2);//instead of LCM_OUT |= ((ByteToSend & 0xF0)<<4); 

   if (IsData == TRUE)
   {
       LCM_OUT |= LCM_PIN_RS;
   }
   else
   {
       LCM_OUT &= ~LCM_PIN_RS;
   }

   //
   // we've set up the input voltages to the LCM.
   // Now tell it to read them.
   //
   PulseLcm();
}


//
// Routine Desc:
//
// Set the position of the cursor on the screen
//
// Parameters:
//
//     Row - zero based row number
//
//     Col - zero based col number
//
// Return
//
//     void.
//
void LcmSetCursorPosition(char Row, char Col)
{
   char address;

   //
   // construct address from (Row, Col) pair
   //
   if (Row == 0)
   {
       address = 0;
   }
   else
   {
       address = 0x40;
   }

   address |= Col;

   SendByte(0x80 | address, FALSE);
}


//
// Routine Desc:
//
// Clear the screen data and return the
// cursor to home position
//
// Parameters:
//
//    void.
//
// Return
//
//     void.
//
void ClearLcmScreen()
{
   //
   // Clear display, return home
   //
   SendByte(0x01, FALSE);
   SendByte(0x02, FALSE);
}


//
// Routine Desc:
//
// Initialize the LCM after power-up.
//
// Note: This routine must not be called twice on the
//           LCM. This is not so uncommon when the power
//           for the MCU and LCM are separate.
//
// Parameters:
//
//    void.
//
// Return
//
//     void.
//
void InitializeLcm(void)
{
   //
   // set the MSP pin configurations
   // and bring them to low
   //
   LCM_DIR |= LCM_PIN_MASK;
   LCM_OUT &= ~(LCM_PIN_MASK);


   //
   // wait for the LCM to warm up and reach
   // active regions. Remember MSPs can power
   // up much faster than the LCM.
   //
   __delay_cycles(100000);


   //
   // initialize the LCM module
   //
   // 1. Set 4-bit input
   //
   LCM_OUT &= ~LCM_PIN_RS;
   LCM_OUT &= ~LCM_PIN_EN;

   LCM_OUT = BIT2; //instead of LCM_OUT = 0x20; that means D5 is 1.
   PulseLcm();

   //
   // set 4-bit input - second time.
   // (as reqd by the spec.)
   //
   SendByte(0x28, FALSE);

   //
   // 2. Display on, cursor on, blink cursor
   //
   SendByte(0x0E, FALSE);

   //
   // 3. Cursor move auto-increment
   //
   SendByte(0x06, FALSE);
}


//
// Routine Desc
//
// Print a string of characters to the screen
//
// Parameters:
//
//    Text - null terminated string of chars
//
// Returns
//
//     void.
//
void PrintStr(char *Text)
{
   char *c;

   c = Text;

   while ((c != 0) && (*c != 0))
   {
       SendByte(*c, TRUE);
       c++;
   }
}


//
// Routine Desc
//
// main entry point to the sketch
//
// Parameters
//
//     void.
//
// Returns
//
//     void.
//
void main(void)
{
   WDTCTL = WDTPW + WDTHOLD;             // Stop watchdog timer

   InitializeLcm();

   ClearLcmScreen();

   PrintStr("Hello World!");

   while (1)
   {
       __delay_cycles(1000);
   }

}

Link to post
Share on other sites

Right off the bat, did you rewire it properly? You have the data pins as 3,4,2,5 instead of simply 2,3,4,5.

 

Second, which characters are you getting on the screen when you expect which other character? You could look up the code on the lcd datasheet, and figure out where it is going wrong there.

 

Finally, are the command codes working? Are you successfully getting the lcd into 4bit mode?

Link to post
Share on other sites
Right off the bat, did you rewire it properly? You have the data pins as 3,4,2,5 instead of simply 2,3,4,5.

 

Second, which characters are you getting on the screen when you expect which other character? You could look up the code on the lcd datasheet, and figure out where it is going wrong there.

 

Finally, are the command codes working? Are you successfully getting the lcd into 4bit mode?

 

 

Thanks for the reply!

 

I've just realized that, but it was simpler to wire the data pins as 3,4,2,5. I tried to build a void function that simply shifts the bits, but without too much luck.

I think I'll start the code from scratch and write it in this curious way.

 

Thanks again!

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...