单片机裸机环境下编写AT指令程序
typedef enum
{
ATCMD_START = 0x00U,
ATCMD_SEND = 0x01U,
ATCMD_WAIT_REV = 0x02U,
ATCMD_REVOK = 0x03U,
ATCMD_TIMEOUT = 0x04U
} ATCMD_StatusTypeDef;
//AT指令状态机 0准备发送确定尝试发送次数 1发送状态 2等待接收 3接收成功 4接收超时
//------------------发送AT指令-----------------
//参数1:huart 串口号
//参数2:cmd AT指令内容
//参数3:timeout 单次发送的超时时间
//参数4:res 要判断的返回结果
//参数5:count 尝试次数
//返回值:ATCMD_REVOK 发送且收到回复 ATCMD_TIMEOUT 超时
ATCMD_StatusTypeDef AT_CMD_ESP12(UART_HandleTypeDef *huart,uint8_t* cmd,uint16_t timeout,const char* res,uint8_t count)
{
static ATCMD_StatusTypeDef atcmd_status=ATCMD_START;
static uint8_t cnt=1;
uint16_t temp;
switch(atcmd_status)
{
case ATCMD_START:
cnt = count;//初始化发送次数
atcmd_status=ATCMD_SEND;
break;
case ATCMD_SEND:
Clr_RxBuf();//清除串口接收缓存
HAL_UART_Transmit_IT(huart,cmd,strlen((const char*)cmd));//发送数据
atcmd_status=ATCMD_WAIT_REV;
Esp12cmd_tick=0;
break;
case ATCMD_WAIT_REV:
if(Esp12cmd_tick < timeout)
{
if(Hand((char*)res,&temp,0))//
{
atcmd_status=ATCMD_REVOK;
}
}
else
{
if(cnt>0)//再次发送
atcmd_status=ATCMD_SEND;
else
atcmd_status=ATCMD_TIMEOUT;
cnt--;
}
break;
case ATCMD_REVOK:
case ATCMD_TIMEOUT:
atcmd_status=ATCMD_START;
default:
break;
}
return atcmd_status;
}
//判断串口接收缓存中是否包含substr
//参数1 *substr 被判断的字符串
//参数2 *index substr在串口缓存中的位置
//参数3 start_index串口缓存起始判断位置
//返回值 包含返回1 不包含返回0
uint8_t Hand(char* substr,uint16_t *index,uint16_t start_index)
{
uint16_t i,flag=0;
char *p;
if(Uart5.RxFlag != 1)return 0;//一帧数据未接收完成,直接返回
Uart5.RxFlag = 0;
for(i=start_index; i<RX_LEN; i )
{
if(Uart5.RxBuf[i]==*substr)
{
flag=1;
p=substr;
while(*p)
{
if(Uart5.RxBuf[i]==*p)
{
*p ;
i ;
}
else
{
flag=0;
break;
}
}
if(flag==1)
{
*index = i;
break;
}
}
}
return flag;
}
//-------------------设置为AP模式------------
//设置的SSID 密码 和本机IP
//成功返回0 返回1表示错误 返回2正在设置
uint8_t SetAPMode(UART_HandleTypeDef *huart)
{
static uint8_t status=0;//
static ATCMD_StatusTypeDef res;
switch(status)
{
case 0:
res = AT_CMD_ESP12(huart,(uint8_t*)'AT CWMODE_DEF=2\r\n',1000,'OK',3);//设置为AP模式
if(res==ATCMD_REVOK)
status=1;
else if(res==ATCMD_TIMEOUT)
{
status=0;
return 1;
}
break;
case 1:
res = AT_CMD_ESP12(huart,(uint8_t*)'AT CWSAP_DEF=\'ESP8266\',\'1234567890\',5,3\r\n',1000,'OK',3);//设置AP
if(res==ATCMD_REVOK)
status=2;
else if(res==ATCMD_TIMEOUT)
{
status=0;
return 1;
}
break;
case 2:
res = AT_CMD_ESP12(huart,(uint8_t*)'AT CIPAP_DEF=\'192.168.5.1\',\'192.168.5.1\',\'255.255.255.0\'\r\n',1000,'OK',3);//设置IP
if(res==ATCMD_REVOK)
status=3;
else if(res==ATCMD_TIMEOUT)
{
status=0;
return 1;
}
break;
case 3://设置UDP
res = AT_CMD_ESP12(huart,(uint8_t*)'AT CIPSTART=\'UDP\',\'192.168.5.255\',8899,8266,0\r\n',1000,'OK',3);
if(res==ATCMD_REVOK)
status=4;
else if(res==ATCMD_TIMEOUT)
{
status=0;
return 1;
}
break;
case 4://设置完成
status=0;
return 0;
default:
break;
}
return 2;
}
void ESP12_Task(UART_HandleTypeDef *huart)
{
switch(WIFI_Status)
{
case 0://设置为AP模式
if(SetAPMode(huart) == 0)//设置成功,转到等待接收状态
{
WIFI_Status=1;
}
break;
case 1://等待接收数据
if(Uart5.RxFlag == 1)
{
Uart5.RxFlag = 0;
ESP12_Rev(huart);
WIFI_Status = 2;
}
break;
case 2:
if(Send_Data(huart,Uart5.TxBuf,Uart5.TxNum)<2)//发送数据
{
Uart5.TxFlag = 0;
WIFI_Status = 1;
}
break;
default:
break;
}
}
赞 (0)