//================= 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模拟器集成开发环境。有没有什么函数或者别的什么?
发布于 2011-12-19 02:24:46
您需要检查每个字符的UART1_Data_Ready,并将字符的接收放入一个循环中,该循环在您按下'.‘时结束。此外,还需要将strcpy移出循环,这样就不会在每个字符上重写输出字符串。
编辑:替换了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;
}https://stackoverflow.com/questions/8553286
复制相似问题