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 人回答
我来回答