buuuuu
buuuuu

Reputation: 9

import string by serial port and print on LCD display

//================= konfigure LCD
// porta za podatoci e PORTB
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;
char name;
char txt[16];


void init(){
    PORTB = 0xFF;
    TRISB = 0x00;
    ANSEL = 0x00;
    ANSELH = 0x00;
    C1ON_bit = 0;
    C2ON_bit = 0;
    UART1_Init(9600);
    Delay_ms(100);
    Lcd_Init();
    Lcd_Cmd(_LCD_CLEAR);
    Lcd_Cmd(_LCD_CURSOR_OFF);
}
//=========================================================



void main()
{
    init();
    UART1_Write_Text("Start");
    UART1_Write(10);
    UART1_Write(13);
    do{
    }
    while(!UART1_Data_Ready());

    name = UART1_Read();
    if (name!='.')
    {
        strcpy(txt,"name");
        strcat(txt,name);
        Lcd_Out(1,1, txt);
        Delay_ms(1);
    }
    else
        Lcd_Out(2,1, "error");
}

I want to display string="MYNAME" on LCD. I import the string on serial port, but it was not display on LCD. What is the error? Could someone help me? I use pic16f887 and pic simulator IDE. Is there some function or something else?

Upvotes: 0

Views: 2103

Answers (1)

tcrosley
tcrosley

Reputation: 789

You need to check the UART1_Data_Ready for each character, and put the receiving of characters into a loop which ends when you press the '.'. Also, you need to move the strcpy out of the loop so you don't write over the output string on every character.

EDITED: replaced strcat, since it requires both variables to be strings.

I also added an echo to make sure the program is receiving the character correctly from the UART.

strcpy(txt,"name ");
while (1)
{
    while(!UART1_Data_Ready());

    name = UART1_Read();
    if (name!='.')
    {
        char i;

        UART1_Write(name);
        i = strlen(txt);
        txt[i] = name;      // add char to end of string
        txt[i+1] = '\0';    // add zero to terminate new string
        Lcd_Out(1,1, txt);
        Delay_ms(1);
    }
    else
    {
        UART1_Write(13);     // cr
        UART1_Write(10);     // lf
        break;
}

Upvotes: 0

Related Questions