状态机除了在Verilog HDL等硬件描述语言中实现线性的操作流程,还可以在单片机中完成一个顺序的事件
通过状态机的流程,使软件实现流程更加清晰。
下图为使用PIC单片机实现SIM800 GPRS初始化配置和通过UDP协议,连接xxx.com域名,端口:5008的过程
/******************************************************************
SIM800配置 连接网络函数
入参: 无
出参: 无
******************************************************************/
void SIM800_Configute(void)
{
switch(step_configure)
{
case 0 :
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, " AT\r\n");
step_configure = 1;
DelayMs(1000);
}
break;
case 1 :
{
if(strstr(usart2Buf,"SMS Ready") != NULL)
step_configure = 2;
else
step_configure = 0;
}
break;
case 2:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CGATT?\r\n");
step_configure = 3;
DelayMs(2000);
}
break;
case 3:
{
if(strstr(usart2Buf,"+CGATT: 1") != NULL && strstr(usart2Buf,"OK") != NULL)
step_configure = 4;
else
step_configure = 2;
}
break;
case 4:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CSTT=\"CMNET\"\r\n");
step_configure = 5;
DelayMs(1000);
}
break;
case 5:
{
if(strstr(usart2Buf,"OK") != NULL)
step_configure = 6;
else
step_configure = 4;
}
break;
case 6:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CIICR\r\n");
step_configure = 7;
DelayMs(1000);
}
break;
case 7:
{
if(strstr(usart2Buf,"OK") != NULL)
step_configure = 8;
else
step_configure = 6;
}
break;
case 8:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CIFSR\r\n");
step_configure = 9;
DelayMs(1000);
}
break;
case 9:
{
//收到GPRS的IP地址
step_configure = 10;
}
break;
case 10:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CIPSTART=\"UDP\",\"server.xxx.com\",\"5008\"\r\n");
step_configure = 11;
DelayMs(1000);
}
break;
case 11:
{
if(strstr(usart2Buf,"CONNECT OK") != NULL || strstr(usart2Buf,"ALREADY"))
step_configure = 12;
else
step_configure = 10;
}
break;
case 12:
{
memset(usart2Buf, 0, sizeof(usart2Buf));
UsartPrintf(USART2, "AT+CIPSEND\r\n");
step_configure = 13;
}
break;
case 13:
{
if(strstr(usart2Buf,">") != NULL)
step_configure = 15;
else
;
}
break;
default:;
}
}