PIC microcontroller and LCD

Discussion in 'Embedded Systems and Microcontrollers' started by Macabra, Sep 9, 2008.

  1. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    Hi, I've been working on this issue for days now and I'm not a programmer but I've been trying to make my pic (PIC24HJ12GP201) communicate with my LCD (Matrix Orbital LK204-25). When I connect to output on hyperterm the code works great it shows what it is suppose to show, however I cannot seem to make it work on LCD which is using the serial RS-232 interface. Can anyone please give me an idea as to how to make it work?

    Here is the code that I have:

    #define FCY 25000000
    #define BAUDRATE 19200
    #define BRGVAL ((FCY/BAUDRATE)/16)-1

    void initUART(void)
    {
    PORTBbits.RB4 = 1;

    ///***************************
    // Assign U1Rx To Pin RP0
    //***************************
    RPINR18bits.U1RXR = 0;
    //***************************
    // Assign U1CTS To Pin RP1
    //***************************
    RPINR18bits.U1CTSR = 1;
    //***************************
    // Configure Output Functions
    // (See Table 9-2)
    //***************************
    //***************************
    // Assign U1Tx To Pin 11
    //***************************
    RPOR4bits.RP8R = 3;
    //***************************
    // Assign U1RTS To Pin 12
    //***************************
    RPOR4bits.RP9R = 4;
    //UART Initialization//

    PLLFBD=38; // M=40
    CLKDIVbits.PLLPOST=0; // N1=2
    CLKDIVbits.PLLPRE=0; // N2=2
    RCONbits.SWDTEN=0; // Disable Watch Dog Timer
    while(OSCCONbits.LOCK!=1) {}; // Wait for PLL to lock
    U1MODEbits.STSEL = 0; // 1-stop bit
    U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
    U1MODEbits.ABAUD = 0; // Autobaud Disabled
    U1MODEbits.BRGH = 0; // Low Speed mode
    U1BRG = BRGVAL; // BAUD Rate Setting for 19200
    U1STAbits.UTXISEL1 = 0;
    U1MODEbits.UARTEN = 1; // Enable UART
    U1STAbits.UTXEN = 1; // Enable UART Tx


    }

    ///////////////////////////////////////////////////////////////////
    int main ()
    {


    unsigned int place1,place2,place3;
    int STX,ETX,sign,unit_num;
    int temperature;

    initADC(); // initialize the ADC
    initUART(); // initalize the UART
    LCDinit();
    OutStr(">");



    The LCDinit(); Is a function from a header. I didn't included it here, but what the LCD requires to perform tasks is to have decimal values sent to it. For instance in order to clear the screen which I've been trying to do, the following must be done (this is excerpt from LCD manual);

    Clear Screen
    Syntax Hexadecimal 0xFE 0x58
    Decimal 254 88
    ASCII 254 “X”
    Description This command will immediately clear all of the contents of the display.

    so what I've done is make the following commands

    WriteUART1(254);
    WriteUART1(88);

    I do not know why it does not work by sending this, I have the LCD to it's default setting to 19200 baudrate and that's how i set software too. Please, any help with be greatly appreciated :(
     
  2. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    I did more research and it seems that PICs function as TTL, so one of the things I needed to do is change the LCD protocol to TTL. Unfortunately nothing works and I don't see any communication with it :(

    Someone who knows how to resolve this..it really is boggling me :confused:
     
  3. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    A quick scan of the LCD manual indicates that there are two modes of communicating with it. RS-232 and I2C. The RS-232 interface signal levels are defined for input voltages in the range where a low is less than -3V and a high is greater than +3V.

    The I2C interfaces is indeed a 0V to 5V signal interface but the serial data definition is different from the RS-232 serial data definition.

    hgmjr
     
  4. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You could get your hands on a MAX232 device. This device runs on 5V but is capable of translating from the 0 to 5V logic levels used by the microcontroller to the typical -10V to +10V signal levels required by RS-232.

    hgmjr
     
  5. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    thank you for replying to my troubles hgmjr! :)

    And as a matter of fact, I do have that chip!! I connected it and like said before it works for hyperterminal but it does not work for my LCD. This is my first time ever programming a PIC, I'm sure something about my code is weird. Any other suggestions? :confused:
     
  6. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Just to make sure that I am clear on the details, you have added a MAX232 device to your PIC design so you clearly have the correct RS-232 level being generated by your PIC board. You are feeding these RS-232 signal levels to your LCD but the LCD is not responding to any of the commands or data.

    You also indicated (I believe) that the RS-232 output of your PIC design generates the correct characters on the computer monitor (running hyperterminal) in response to the characters you send it.

    All of the above understanding being true then a few things to check are that the LCD is set to the correct bit-per-second data rate and that the LCD is set to RS-232 mode and not I2C data mode. Do you have the correct pinout so that you are sending the data to the LCD on its receive input pin and not accidentally on its transmit output pin?

    Can you post a copy of the schematic of your PIC board so that we can have a look at the hardware connections you are using.

    hgmjr
     
  7. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    yes, I have it so it is on RS232 as opposed to TTL and I2C because it is on it's defualt setting. So, that being said the baud rate is set to 19200 as well. Everything you stated on your last post is absolutely correct :D

    Attached is the schematic of the pic..there's some opamps and temperature sensor you can ignore that :) The meat of this is the bottom circuit with the pic and Max3232.

    Thanks again hgmjr, this has been killing me for like 24 hrs now :mad:
     
  8. nanovate

    Distinguished Member

    May 7, 2007
    665
    1
    Here is a PDF of the schematic. The connections around the MAX3232 look OK.
     
  9. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Macabra,

    Based on the information in the datasheet section 2.1.2 RS-232 Communications in the manual for PCB Version 1.23 it appears that you need to connect pin 2 of your PIC design (9 pin connector) to pin 3 of the LCD (9 pin connector) and pin 3 of your PIC design (9 pin connector) to pin 2 of the LCD (9 pin connector). Is this the way you have the cable pinned? If not then I would suggest you give this wiring order a try.

    The LCD maker provides a straight through cable to connect from the PC to the LCD. That infers that pin 2 is an input on one end and an output on the other. Now you say that you connected your PIC design to the PC and all worked fine. If you used straight through cable between your PIC design and the PC then that means that PIC design and the LCD have their 9 pin connectors wired identical in that pin 2 on each is an input and pin 3 on each is an output (or visa-versa). That makes me think that the cable between the PIC design and the LCD will need to have pins 2 and 3 cross-wired as I indicated earlier.

    Hope I haven't added to your confusion.

    hgmjr
     
  10. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    hmm I understood what you meant, and that is how I have it wired with the TX of LCD to RX of PIC and RX of LCD to TX of PIC. I've also put pin 5 to ground for both. Unfortunately, I get nothing still. The LCD does not clear nor print out a single char. :confused:

    The manual that your referenced is from previous LCD model too, not that it is any different, but the 2.0 version one is the one that shows the LCD I have.

    I just tried connecting the RX/TX signal from the pic (before going to the MAX232 chip to the LCD in TTL protocol and it didn't do anything either. Changed it back to RS-232
     
    Last edited: Sep 10, 2008
  11. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    So you are already connecting pin 3 to 2 and pin 2 to pin 3.

    Are you certain that the power is connected correctly to your LCD?

    hgmjr
     
  12. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    Oh yes, absolutely!

    I mean the LCD actually does lit and displays the text "Matrix Orbital and the model number" however from there it does not clear the screen nor prints out any characters. When I had it connected to hyperterminal, the characters would write over the text.

    Noticeable things that occur is that when I run the code, the text of the LCD seems to dim and then darken. I don't know what this means. It means that it is communicating but not receiving things well?? I don't understand. Like said before the baud rate is the same for LCD to what I defined it on code.
     
  13. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    So the LCD works with the PC and the PIC design works with the PC but the PIC does not work with the LCD.

    Have you considered an exorcist:)?

    hgmjr
     
  14. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    I've considered throwing it out my window hehe :D
     
  15. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    hgmjr!!

    I got it to print out something!! Reason was that I didn't turned off flow control! FLOW CONTROL! >_<!!

    now the only problem that I am having is that everytime I turn off the LCD and turn it back on it prints out this weird character nonstop until a debug my code. Phew another thing to work on but I am happy to get something printed nevertheless.

    Now I can tell it what to print (can only print one character), but it does not clear the screen still. I read through the manual and it says that I have to send the commands in bytes..you have a clue as to what it means?

    To clear display the manual states to send the following instruction:

    Clear Screen:
    Hexadecimal 0xFE 0x58
    Decimal 254 88
    ASCII 254 “X”
     
  16. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    Aahhhh!!! A sense of humor in the midst of frustration. You will go far.

    hgmjr
     
  17. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    I would interpret the instructions to mean that you send the 0xfe and then you send the character 0x58. The 0xfe character basically acts like a shift key that sets the LCD up to respond to the next character as a command.

    hgmjr
     
  18. Macabra

    Thread Starter Active Member

    May 31, 2008
    49
    0
    yeah my same thought. Thanks a bunch for your help hgmjr! I really appreciate what you've done. :)
     
  19. hgmjr

    Moderator

    Jan 28, 2005
    9,030
    214
    You are very welcome.

    I am well acquainted with ATMEL's AVR family of microcontrollers. My familiarity with the PIC series is inadequate to give you assistance with your PIC porgramming issues.

    hgmjr
     
Loading...