1

i'm using a dspic33fj128mc802 with mplab xc16. I want to use an LCD in 4 bit mode but cant seem to initialise it and dont know what I'm doing wrong. Any help would be appreciated in debugging. I have looked at other examples and my code looks similar to other but still wont work

    //LCD Control pins

    #define LCD_RS       _LATB11
    #define LCD_EN       _LATA4

    #define TRIS_EN      TRISAbits.TRISA4    /* TRIS for E */
    #define TRIS_RS      TRISBbits.TRISB11   /* TRIS for RS */

    #define D7           LATBbits.LATB15
    #define D6           LATBbits.LATB14
    #define D5           LATBbits.LATB13
    #define D4           LATBbits.LATB12

    #define D7_TRIS           TRISBbits.TRISB15
    #define D6_TRIS           TRISBbits.TRISB14
    #define D5_TRIS           TRISBbits.TRISB13
    #define D4_TRIS           TRISBbits.TRISB12

    //LCD Data pins
    #define lcdport     _LATB11,_LATA4,_LATB15,_LATB14,_LATB13,_LATB12


    void delay_ms();
    void delay_us();
    void LCD_Init();
    void LCD_Command(char cmd);
    void LCD_ClearDisp();
    void LCD_RetHome();
    void LCD_Display();
    void LCD_ShiftR();
    void LCD_ShiftL();
    void LCD_4BitMode();
    void LCD_Init();


    void delay_ms()
    {
        __delay32(40000);
    }

    void delay_us()
    {
        __delay32(40);
    }

    void LCD_Command(char cmd) //LCD Command routine
    { 
        lcdport = ((cmd >> 4) & 0x0F)|LCD_EN;
        delay_ms(20);
        lcdport = ((cmd >> 4) & 0x0F);
        delay_ms(20);
        lcdport = (cmd & 0x0F)|LCD_EN;
        delay_ms(20);
        lcdport = (cmd & 0x0F);
        delay_ms(20);
     }

    void LCD_ClearDisp()
    {
        delay_ms(20);
        LCD_Command(0b0000000001);
        delay_ms(20);
    }

    void LCD_RetHome() //set cursor to first digit
    {
        delay_ms(20);
        LCD_Command(0b0000000010);
        delay_ms(20);
    }

    void LCD_Display()//disp on, cursor on, blinking on
    {
        delay_ms(20);
        LCD_Command(0b0000001111);
        delay_ms(20);
    }

    void LCD_ShiftR()//shift right
    {
        delay_ms(20);
        LCD_Command(0b000001100);
        delay_ms(20);
    }

    void LCD_ShiftL()//shift right
    {
        delay_ms(20);
        LCD_Command(0b000001000);
        delay_ms(20);
   }

   void LCD_4BitMode()//4 bit mode, 2 line, 5x7 dots
   {
       delay_ms(20);
       LCD_Command(0b0000101000);
       delay_ms(20);
   }

   void LCD_EntrySet()
   {
       delay_ms(20);
       LCD_Command(0b0000000110);
       delay_ms(20);
   }

   void DDRAM_address()
   {
       delay_ms(20);
       LCD_Command(0b0010000000);
       delay_ms(20);
   }
   void LCD_Strobe(void)  //This function pulls the enable line high and then low
   {    
       LCD_EN = 1;
       delay_ms(20);
       LCD_EN =  0;
       delay_ms(20);
   }
  
   void write_lcd(char dat)
   {
       LCD_RS = 1; // RS = 1
       LCD_EN = 1; // E = 1
       lcdport = dat;
       delay_ms(20);
       LCD_EN = 0; // E = 0
       delay_ms(20);
   }

   void LCD_Write_String(char *ptr)
   {
       while(*ptr)
       {
           write_lcd(*ptr);
           delay_ms(1);
           ptr++;
       }
   }

   void LCD_Init()
   {
       delay_ms(20);
       LCD_RS = 0;
       LCD_4BitMode();
       delay_ms(1);
       LCD_Strobe();
       delay_ms(1);
       LCD_Strobe();
       delay_ms(1);
       LCD_Strobe();
       LCD_ShiftR();
       delay_ms(1);
       LCD_EntrySet();
       delay_ms(1);
       DDRAM_address();
       delay_ms(1);
       LCD_ClearDisp();
   }

   int main ()
   {
       while(1)
       {
           LCD_Init();                  //Intilize LCD in 4-Bit Mode
           delay_ms(20);
           LCD_Command(0X80);          // Start Cursor From First Line
           delay_ms(20);
           LCD_Write_String("Hello");  //Print HELLO on LCD
           delay_ms(20);
           LCD_Command(0XC0);         // Start Cursor From Second Line
           delay_ms(20);
           LCD_Write_String("World"); //Print HELLO on LCD
           delay_ms(20);
           LCD_Strobe();
           delay_ms(20);
       }
   }
2
  • Where do you get stuck? Does it compile? Can you provide more detail on how it doesn't work? Sep 1, 2015 at 14:23
  • At the moment it doesn't seem like it initializes the LCD. The code compiles and I'm able to step through it bit I can't find the error. The LCD is 16x2 and currently only the first line displays all black blocks
    – JBH
    Sep 2, 2015 at 14:49

1 Answer 1

0

So the code compiles, you can step through it (presumably with an in-circuit debugger), but it doesn't do what you expect. The description you've given of the problem - or rather the mismatch between actual and expected behaviour is that the LCD doesn't seem to be initialised, and it displays something different to what you expect.

So to debug this, decompose the problem. Check your assumptions and expectations. I'd suggest you step through the program, and check, on each instruction (at least initially), that the program does what you expect. If something else happens, figure out why, try a fix, and test again. Now, this will quickly become tedious, so use breakpoints or "run to cursor" in the debugger to skip known-good sections, and home in on sections under scrutiny.

Your main() contains an infinite loop, and the loop body starts with a call to LCD_Init(), so let's focus on that for now. Below, I've 'inlined' some of the calls, so we can better see the program flow:

void LCD_Init()
{
    delay_ms(20);
    LCD_RS = 0;
    //LCD_4BitMode();
        delay_ms(20);
        //LCD_Command(0b0000101000);
            lcdport = ((cmd >> 4) & 0x0F)|LCD_EN;
            delay_ms(20);
            lcdport = ((cmd >> 4) & 0x0F);
            delay_ms(20);
            lcdport = (cmd & 0x0F)|LCD_EN;
            delay_ms(20);
            lcdport = (cmd & 0x0F);
            delay_ms(20);
        // end of LCD_Command(0b0000101000);
        delay_ms(20);
    // end of LCD_4BitMode();
    /*...*/
}
  1. delay_ms(20); Have you checked that this actually does a 20ms delay? you can do this by setting a pin, calling delay_ms(20) and clearing the pin, then measuring the pulse duration with an oscilloscope or logic analyser or similar. (I am aware this may be a bit basic, but I picked up on this because delay_ms() is defined without an argument, which made me think the compiler might complain about it). There are multiple calls to delay_ms(20); later in the above snipped, and I'll skip over them

  2. LCD_RS = 0; Have you checked that the actual pin changes when you execute this? Again I ask because I can't see you setting the pin up, so it may well be incorrectly configured.

  3. We then step into LCD_Command( 0b0000101000 ) (by the way, you're passing a 10-bit binary literal to a char parameter, the top 2 bits will not be passed), and the first statement in there is lcdport = ((cmd >> 4) & 0x0F)|LCD_EN;. Let's do the preprocessor substitutions for that statement, and we end up with the following monstrocity: _LATB11,_LATA4,_LATB15,_LATB14,_LATB13,_LATB12 = ((cmd >> 4) & 0x0F)|_LATA4; Let's break that down a bit:

    3.1. Looking at the right-hand side of assignment operator, we have ((cmd >> 4) & 0x0F)|_LATA4;. cmd >> 4 is 0b0010 and this is "bitwise or'ed" with value of _LATA4, i.e. 0 or 1 (but let's call it Y), so you assign 0b001Y. Now, I think that what you intend is to set the data lines to the top nibble of cmd and the LCD_EN bit set. But in C, you can't do it in this way.

    3.2. Looking at the left-hand side, you assign this only to _LATB12 - the comma operator does not do what you apparently think it does. Without knowing how xc16 handles assignments of ints to a bit variable (bit is non-standard), I would guess that you either assign 1 because the value is non-zero, or you assign the LSB, which would be _LATA4

I'm going to stop there with the analysis of your code. Hopefully, this will get you on your way with debugging and fixing your code, and probably raise more questions.

Once you've got the code to the point that it matches your intent and expectations, you'll be ready for round two of debugging - does the code (and your intent/expectations) match what the hardware expects? That'll be solved with similar techniques - decompose the problem, check actual and expected behaviour, find out whether it's the actual or expected behaviour that needs fixing, fix it, and retest.

Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.