(1)很多时候我们使用一个软件调试工具时,里面都约定有特定的命令格式来方便使用。此时,程序一般要先对特定格式的命令进行解析(parse),再分解成组合命令来执行。假设一个单步I2C调试工具中,在命令行中输入可用命令,有如下约定:
>>command for START:
start
s
[
>>command for STOP:
stop
st
]
>>command for WRITE:
write data
w data
> data
>>command for READ:
read
r
<
那么,解析命令行中的字符串就显得比较关键。如下函数cmdParser就是解析字符串函数。
void cmdParser(char *cmdLine)
{
char cmd[100];
int data = 0;
if(strlen(cmdLine)<=0){showMSG("/n/r");return ;} //如果命令为空
sscanf(cmdLine,"%s %x",cmd,&data); //将字符串解析成命令和数据。比如写命令+写数据
strcpy(cmd,::strlwr(cmd)); //转小写
if(strcmp(cmd,"start")==0||strcmp(cmd,"[")==0||strcmp(cmd,"s")==0)
{
IICStart();
showMSG("IIC Bus start"); //显示工作状态
clearCMD(); //清除命令输入框内容,一般设置""即可
}
else if(strcmp(cmd,"stop")==0||strcmp(cmd,"]")==0||strcmp(cmd,"st")==0)
{
IICStop();
showMSG("IIC Bus stop");
clearCMD();
}
else if(strcmp(cmd,"write")==0||strcmp(cmd,"w")==0||strcmp(cmd,">")==0)
{
if(IICWriteByte(data)) //写data
{
sprintf(cmd,"IIC write 0x%x",data);
showMSG(cmd);
}
else
{
sprintf(cmd,"err: write 0x%x err",data);
showMSG(cmd);
}
clearCMD();
}
else if(strcmp(cmd,"read")==0||strcmp(cmd,"r")==0||strcmp(cmd,"<")==0)
{
data = IICReadByte();
sprintf(cmd,"IIC read 0x%x",data);
showMSG(cmd);
clearCMD();
}
else if(strcmp(cmd,"ack")==0||strcmp(cmd,"a")==0)
{
IICSendAck(1);
showMSG("Send ACK");
clearCMD();
}
else if(strcmp(cmd,"noack")==0||strcmp(cmd,"na")==0)
{
IICSendAck(0);
showMSG("Send NO ACK");
clearCMD();
}
else
{
showMSG("err: unknown command");
clearCMD();
}
}
(2)利用基础之三的IIC的基础语句,组合成特定芯片的控制处理函数
void IICWriteSN3199OneCommand(BYTE subaddr,BYTE data)
{
IICStart();
IICWriteByte(SN3199_IIC_WRITE_ADDRESS);
IICWriteByte(subaddr);
IICWriteByte(data);
IICStop();
}