1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Problem with PIC24F LCD code

Discussion in 'Code Repository' started by natmag, Aug 19, 2013.

  1. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    I am trying my to implement a simple program such that my PIC24F displays a string of characters on my LCD screen. Unfortunately, nothing is being written on screen and on debugging it seems that PORTA and PORTB pins never change from their initial 0 state. Cannot understand why this is happening :S

    Am using the Microstick to program my PIC24FV16KM202 together with the following code:
    Code (text):

     /* Delay Function being used throughout the program */
    void delay(int num)
    {
        int i =0;
        for (i=0; i<num; i++)
        {
            ;
        }
    }

    /* LCD_command function to send a command to LCD */
    void LCD_command(unsigned char c)
    {
        LCD_rs = 0; /* Disable register select */
        LCD_rw = 0; /* Disable Read/Write */
        LCD_en = 1; /* Enable */
        delay(200);
        LCD_data = c; /* Write Command on LCD pin */
        delay(200);
        LCD_en = 0; /* Disable */
    }

    /* Function to Initialize LCD */
    void Init_LCD(void)
    {
        LCD_rs = 0;
        LCD_rw = 0;
        delay(800);
        LCD_en = 1;
        delay(500);
        LCD_en = 0;
        delay(500);
        LCD_en = 1;
        delay(500);
        LCD_en = 0;
        LCD_command(0x38); /*Function Set: 2 Line, 8-bits, 5x7 dots*/
        LCD_command(0x08); /*Display Off*/
        LCD_command(0x0F); /*Display On, Cursor On*/
        LCD_command(0x06); /*Entry Mode*/
        LCD_command(0x80); /*Entry Mode DDRAM address*/
    }

    /* Function to clear the LCD Screen */
    void Clear_LCD(void)
    {
        LCD_rs = 0;
        LCD_command(0x1); /* Clear Command */
        delay(800);
    }
    /* Function to write a character onto LCD */
    void Write_char_LCD(unsigned char c)
    {
        LCD_rw = 0;
        LCD_rs = 1;
        LCD_en = 1;
        delay(200);
        LCD_data = c; /* c is the character to be written */
        delay(200);
        LCD_en = 0;
    }

    /* Function to write a string of characters on LCD */
    void Write_string_LCD(const char *s)
    {
        LCD_rs = 1;
        while(*s)
        {
            Write_char_LCD(*s++); /* Write each character in string, one at a time */
        }
    }



    int main(void)
    {
        unsigned int i;
       
        // Set up output pin for LED
       
        TRISB = 0x0000;
        TRISAbits.TRISA0 = 0;
        TRISAbits.TRISA1 = 0;
        TRISAbits.TRISA2 = 0;
        TRISAbits.TRISA3 = 0;
       
        PORTAbits.RA1 = 0;
        PORTAbits.RA2 = 0;
        PORTAbits.RA3 = 0;

        OSCCONbits.COSC = 0x7;
        OSCCONbits.NOSC = 0x7;
        CLKDIVbits.RCDIV = 0x6;

        Init_LCD();
        Clear_LCD();

        while(1)
        {

            Write_string_LCD("***pH Meter***");
            delay(100);


            // To make the LED blink visibly, we have to wait a while between toggling

            // the LED pin.
            for(i = 0; i < 65535; i++)
            {
                Nop();
                Nop();
                Nop();
                Nop();
            }
           

            // Toggle the LED output pin to alternate between the LED being on and off
            LATAbits.LATA0 ^= 1;
        }
    }
     

    Please help :S :)
     
  2. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Hi,
    Actually you can also cut the chase and use libraries and the command will be like:

    Lcd_Disp(0,1,"Hello!");

    I think your compiler should also have a library for this. I presume you're using the Hitachi 16x2 LCD display, are you?
     
  3. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    Yes i am using the Hitachi 16x2 LCD display.
    Ill try using that command Thnx.

    What is baffling me is why on debugging nothing is being written in the registers. For instance if I write
    Code (text):


    TRISBbits.RB1 = 0;

    PORTBbits.RB1 = 1;
     
    The value of RB1 doesnt change :S
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia

    Well you can toggle the pins via LED can you? I'd test if the pins respond to a simple program first if I were you.
     
  6. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    Are you able to blink the LED in the real hardware? Maybe there is a bug in the simulator.

    EDIT: Just like Vizier said.. make a simple LED blinkin program to test the hardware.
     
  7. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    Im using the MPLAB C16 compiler and it doesnt have such a library to allow the use of the command Lcd_Disp(0,1,"Hello!");:(
     
  8. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    The LED blinks in hardware but the LCD doesnot initialize and display the string of characters
     
  9. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    RB1 has analogue input capability.... AD1CHS register needs to be set / unset..
     
  10. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    Thanks that solved my problem :)
     
  11. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I think it would be strange if the analog input is enabled by default on reset. All IO pins on a microcontroller should be digital inputs (tristate) by default.. that is my opinion.
     
  12. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Pics always are.... You have to turn them off....
     
  13. misterT

    misterT Well-Known Member Most Helpful Member

    Joined:
    Apr 19, 2010
    Messages:
    2,697
    Likes:
    368
    Location:
    Finland
    I haven't worked with pics for a long time and that was unexpected to me.. just shows that you really need to read the datasheet.
     
  14. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Yup but as I used MikroC before this, initialization on a pin basis is rare for me.
     
  15. Vizier87

    Vizier87 Active Member

    Joined:
    May 12, 2009
    Messages:
    1,495
    Likes:
    12
    Location:
    Johor Bahru, Malaysia
    Ouch... no libraries... are you sure? writing your own function for display takes a bit more time then. Oh well you did manage to display the basics though, did you?
     
  16. natmag

    natmag New Member

    Joined:
    Jul 5, 2013
    Messages:
    6
    Likes:
    0
    Location:
    malta
    Not as yet, it seems that when i try to initalise the LCD using the command function

    Code (text):

    void LCD_command(unsigned char c)
    {
        LCD_rs = 0; /* Disable register select */
        LCD_rw = 0; /* Disable Read/Write */
        LCD_en = 1; /* Enable */
        delay(200);
        PORTB = c; /* Write Command on LCD pin */
        delay(200);
        LCD_en = 0; /* Disable */
    }
     
    and the character input c has a value of 0x38,the value 0x8 is being written on PORTB eventhough the whole port is set as a digital output
     
  17. Ian Rogers

    Ian Rogers Super Moderator Most Helpful Member

    Joined:
    Mar 28, 2011
    Messages:
    9,164
    Likes:
    910
    Location:
    Rochdale UK
    ONLINE
    Are you sure!!! The C30 compiler is for 16 bit chips.... The old C16 compiler hasn't been around for ages...

    The C30 has oodles of libraries.....
     

Share This Page