Continue to Site

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.

  • 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.

Problem with PIC24F LCD code

Status
Not open for further replies.

natmag

New Member
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:
 /* 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 :)
 
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?
 
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:
TRISBbits.RB1 = 0;

PORTBbits.RB1 = 1;

The value of RB1 doesnt change :S
 
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.
 
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!");:(
 
RB1 has analogue input capability.... AD1CHS register needs to be set / unset..

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.
 
Pics always are.... You have to turn them off....

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.
 
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!");:(

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?
 
Not as yet, it seems that when i try to initalise the LCD using the command function

Code:
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
 
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!");:(

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.....
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top