普中开源电子分享网

 找回密码
 立即注册
搜索
查看: 4044|回复: 2

51单片机与esp8266-01通信LCD1602显示

[复制链接]

2

主题

4

帖子

38

积分

新手上路

Rank: 1

积分
38
发表于 2020-1-11 22:01:18 | 显示全部楼层 |阅读模式
1金钱
单片机与esp8266-01连接,通过电脑网络调试助手TCP server 给esp8266-01发送消息,然后通过LCD1602显示,
单片机接收esp的数据显示就不正常了,下面是源码,用串口助手调试esp可以正常接收数据,用串口助手发送数据到单片机lcd1602也能正常显示;
就是esp通过串口发送给单片机后lcd不能正常显示了,不知道这是什么原因,之前帖子没及时回复被关闭了;现在重新发一遍




#include <string.h>
#include <lcd.h>
#define uint unsigned int
#define uchar unsigned char


uchar Recive_table[30];
uchar esp_status,dat_index,lcd_index;
uchar default_connent [] = "AT+CIPSTART=1,\"TCP\",\"192.168.0.100\",8000" ;
int is_in(char *s, char *c);

sbit LED = P2^0;


void ms_delay(uint t)
{
    uint i,j;
    for(i=t; i>0; i--)
        for(j=110; j>0; j--);
}

void us_delay(uchar t)
{
    while(t--);
}

void Uart_Init()                                
{
    TMOD = 0x20;
    SCON = 0x50;                                 //设置串行方式
    TH1 = 0xFD;                                         //波特率9600
    TL1 = 0xFD;
    PCON = 0x00;
    EA = 1;                                                //总中断打开
    ES = 1;                                                //开串口中断
    TR1 = 1;                                        //启动定时器1
}

/********************************************************************
名称:串口发送函数  功能:MCU向无线WIFI模块ESP8266发送数据
********************************************************************/
void Send_Uart(uchar value)
{
    ES=0;                                                  //关闭串口中断
    TI=0;                                           //清发送完毕中断请求标志位
    SBUF=value;                                 //发送
    while(TI==0);                                 //等待发送完毕
    TI=0;                                           //清发送完毕中断请求标志位
    ES=1;                                                  //允许串口中断
}



void ESP8266_Set(uchar *puf)
{
    while(*puf!='\0')                    
    {
        Send_Uart(*puf);                  //向WIFI模块发送控制指令。
        us_delay(5);
        puf++;
    }
    us_delay(5);
    Send_Uart('\r');                        
    us_delay(5);
    Send_Uart('\n');                 
}

void init_esp8266() {
    ESP8266_Set("AT+CIPMUX=1");
    ms_delay(100);
    ESP8266_Set(default_connent);
    ms_delay(100);
}

/**
* esp给服务器发送消息
**/
void sendToServer(uchar *puf) {
    ESP8266_Set("AT+CIPSEND=1,1");
    ms_delay(1);
    ESP8266_Set(puf);
}

void main() {
    LcdInit();
    Uart_Init();                                                                                    
    ms_delay(5000);
    init_esp8266();
   // ES=1;
         LcdWriteData('A');
    while(1)
    {


    }
}

void Uart_Interrupt() interrupt 4
{
    static uchar i=0;
    if(RI==1)
    {     
                          
        RI=0;
        Recive_table=SBUF;
                          if(Recive_table=='\n'){
                                  i=0;
                               
                                        LcdWriteCom(0x01);  
                                        //if(Recive_table[0]=='+'&&Recive_table[1]=='I'&&Recive_table[2]=='P'&&Recive_table[3]=='D')
                                        if(is_in(Recive_table,"+IPD")==1)
                                        {
                                               
                                                 for(lcd_index=0;lcd_index<16;lcd_index++){
                                                         if(Recive_table[lcd_index+9]=='\0'){//去掉+IPD,1,1这段
                                                           break;
                                                         }
                                                          if( Recive_table[lcd_index+9]==':'){
                                                           continue;
                                                         }
                                                       
                                                         LcdWriteData((char)Recive_table[lcd_index+9]);
                                                 }
                                        }
                                        else
                                 {   
                                                 LED=~LED;
                                         for(dat_index=0;dat_index<30;dat_index++){
                                                         if(Recive_table[dat_index] !='\0'&&lcd_index<16){
                                                                 LcdWriteData(Recive_table[dat_index]);
                                                          }
                                         }
        }
                                        //if(strcmp(Recive_table, "buys") == 0){ }
                                  memset(Recive_table, 0, 40);
                                }else{
                                  i++;
                                }
    }
    else
                {                       
                        TI=0;  
                }
}

int is_in(char *s, char *c)
{
    uint i=0,j=0,flag=0;
    while(i<strlen(s) && j<strlen(c)){
        if(s==c[j]){//如果字符相同则两个字符都增加
            i++;
            j++;
        }else{
            i=i-j+1; //主串字符回到比较最开始比较的后一个字符
            j=0;     //字串字符重新开始
        }
        if(j==strlen(c)){ //如果匹配成功
            flag=1;  //字串出现
            break;
        }
    }
    return flag;
}


我知道答案 本帖寻求最佳答案回答被采纳后将获得系统奖励10 金钱 , 目前已有2人回答

附件: 您需要 登录 才可以下载或查看,没有帐号?立即注册
回复

使用道具 举报

2

主题

4

帖子

38

积分

新手上路

Rank: 1

积分
38
 楼主| 发表于 2020-1-11 22:05:06 | 显示全部楼层
占楼,忽略代码中没有用到的参数和变量,本人Java语言,所以忽悠代码中的命名规范,
回复

使用道具 举报

0

主题

20

帖子

74

积分

注册会员

Rank: 2

积分
74
发表于 2020-1-18 21:32:44 | 显示全部楼层
mingtian 发表于 2020-1-11 22:05
占楼,忽略代码中没有用到的参数和变量,本人Java语言,所以忽悠代码中的命名规范,

这个问题在开发攻略里有讲,1602显示的是 ASCII码,必须把你得到的字符转换成ASCII码,方法是后面X+“ ”或者X+0x30或者 X+48,这是单字节
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关闭

站长推荐上一条 /1 下一条

Archiver|手机版|小黑屋|普中开源电子分享网 粤ICP备16123577号-2

GMT+8, 2024-4-19 18:33 , Processed in 0.094808 second(s), 32 queries .

Powered by 论坛搭建 X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表