通过串口导入字符串并在LCD显示屏上打印

时间:2011-12-18 17:02:18

标签: microcontroller pic

//================= 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");
}

我想在液晶显示屏上显示string="MYNAME"。我在串口上导入字符串,但它没有显示在LCD上。错误是什么?有人能帮助我吗?我使用pic16f887和pic模拟器IDE。是否有某种功能或其他东西?

1 个答案:

答案 0 :(得分:0)

您需要检查每个字符的UART1_Data_Ready,并将字符接收放入循环中,当您按下'。'时结束。此外,您需要将strcpy移出循环,这样就不会在每个字符上都写出输出字符串。

EDITED:替换了strcat,因为它需要两个变量都是字符串。

我还添加了一个回声,以确保程序正确地从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;
}