8051 interfacing LCD in C

Discussion in 'Embedded Systems and Microcontrollers' started by Dritech, Apr 17, 2015.

  1. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Hi all,

    I am sending a string to be displayed on an LCD. To detect end of string, I am using a while loop as follows:

    Code (Text):
    1. void uart_string(unsigned char string[])
    2. {
    3.    unsigned char index;
    4.  
    5.    while (string[index] != '\0')
    6.    {
    7.         uart_send(string[index]);
    8.         index++;
    9.    }
    10. }
    Why is the program not exiting the while loop please?
     
  2. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    What is the initial value of index?
    How do we know the string is terminated with a '\0'?
     
    Dritech likes this.
  3. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    mmm that was the problem.
    I changed the code as follows:

    Code (Text):
    1. unsigned char index = 0;
    Thanks.
     
    Papabravo likes this.
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Strings in C terminate with a zero by definition and tradition. Make sure the strings you create follow this.

    You do need to initialize the index variable. I did this below, as well as a few tweaks that may or may not result in a bit smaller generated code:

    Code (Text):
    1. void uart_string(unsigned char string[])
    2. {
    3.    unsigned char index = 0;
    4.  
    5.    while (string[index])
    6.    {
    7.         uart_send(string[index++]);
    8.    }
    9. }
     
    Dritech likes this.
  5. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Bravo for figuring that out on your own with just a small hint.
     
  6. Dritech

    Thread Starter Well-Known Member

    Sep 21, 2011
    756
    5
    Another quick question.
    I am using a switch to trigger external interrupt. Do I have to use a pull-up resistor? There are examples with pull-ups and others with the switch connected to the ground only.
    Also, do I have to declare the interrupt pin as input?
     
  7. Papabravo

    Expert

    Feb 24, 2006
    10,135
    1,786
    Port 0 is the only port on the standard 8051 with open drain outputs. This means an external resistor must be used for the port pins to be inputs AND you need to write 1's to the output data latch to turn the sinking drivers off. This lets the external pullup hold the pin high.
     
Loading...