打印

DSP2812串口状态机解码之C++程序

[复制链接]
2556|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
hotpower|  楼主 | 2008-5-6 00:12 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
void UartObj::Decode(void)
{
unsigned char ch;
unsigned int *Ptr;
    while (ReceiveWritePtr != ReceiveReadPtr)    
    {
        ch = ReceiveBuffer[ReceiveReadPtr++];
        ReceiveReadPtr &= 0xff;
        switch (State)
        {
            case 0://'$'搜索
                if (ch == '$')
                {
                    State = 1;
                    ReceiveCount = 0;
                }
                ReceivePosition = ReceiveReadPtr;
                break;
            case 1://命令
                if (ch == ':')
                {
                    if (ReceiveCount == 5 && 
                        ReceiveBuffer[ReceivePosition] == 'W' &&
                        ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' &&
                        ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'i' &&
                        ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 't' &&
                        ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e')
                    {
                        State = 2;//Write命令地址
                    }  
                    else if (ReceiveCount == 4 && 
                        ReceiveBuffer[ReceivePosition] == 'R' &&
                        ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' &&
                        ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' &&
                        ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'd')
                    {
                        State = 3;//Read命令地址
                    }  
                    else if (ReceiveCount == 5 && 
                        ReceiveBuffer[ReceivePosition] == 'E' &&
                        ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'r' &&
                        ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 'a' &&
                        ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 's' &&
                        ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 'e')
                    {
                        State = 4;//Erase命令地址
                    }
                    else
                    {
                        State = 0;
                    }
                    ReceiveCount = 0;
                    ReceivePosition = ReceiveReadPtr;
                    Address = 0;
                    Data = 0;
                }
                else if (ch == ';')
                {
                    if (ReceiveCount == 5 && 
                        ReceiveBuffer[ReceivePosition] == 'R' &&
                        ReceiveBuffer[(ReceivePosition + 1) & 0xff] == 'e' &&
                        ReceiveBuffer[(ReceivePosition + 2) & 0xff] == 's' &&
                        ReceiveBuffer[(ReceivePosition + 3) & 0xff] == 'e' &&
                        ReceiveBuffer[(ReceivePosition + 4) & 0xff] == 't')
                    {
                        State = 0;
                        for(;;);
                    }
                }
                else//命令字符个数
                {
                    ReceiveCount ++;
                }
                break;
            case 2://Write命令地址
            case 3://Read命令地址
            case 4://Erase命令地址
                if ((State == 2) && (ch == ','))
                {
                    State = 5;//Write命令数据
                }
                else if ((State == 3) && (ch == ';'))
                {
                    Ptr = (unsigned int*)Address;
                    Data = *Ptr;
                    if ((Address >= 0x80000) && (Address <= 0x8ffff))
                    {
                        putstr("FLASH数据读出:[");
                    }
                    else
                    {
                        putstr("RAM数据读出:][");
                    }
                    puthex(Address);
                    putstr("]=");
                    puthex(Data);
                    puts("");
                    State = 0;
                }
                else if ((State == 4) && (ch == ';'))
                {
                    if ((Address >= 0x80000) && (Address <= 0x8ffff))
                    {
                        Flash.SectorErase((Address & 0xffff) >> 11);
                        putstr("FLASE扇区擦除:[");
                        puthex(Address & 0xfffff800);
                        putstr("-");
                        puthex(Address | 0x7ff);
                        putstr("]");
                        puts("");
                    }
                    State = 0;
                }
                else if ((ch >= '0') && (ch <= '9'))
                {
                    Address <<= 4;
                    Address |= ch - '0';
                }
                else if ((ch >= 'A') && (ch <= 'F'))
                {
                    Address <<= 4;
                    Address |= ch - 'A' + 10;
                }
                else if ((ch >= 'a') && (ch <= 'f'))
                {
                    Address <<= 4;
                    Address |= ch - 'a' + 10;
                }
                break;
            case 5://Write命令数据
                if (ch == ';')//命令结束字符
                {
                    if ((Address >= 0x80000) && (Address <= 0x8ffff))
                    {
                        Flash.Write(FLASH[Address & 0xffff], Data);
                        putstr("FLASH数据写入:[");
                    }
                    else
                    {
                        Ptr = (unsigned int*)Address;
                        *Ptr = Data;
                        putstr("RAM数据写入:][");
                    }
                    puthex(Address);
                    putstr("]=");
                    puthex(Data);
                    puts("");
                    State = 0;
                }
                else if ((ch >= '0') && (ch <= '9'))
                {
                    Data <<= 4;
                    Data |= ch - '0';                    
                }
                else if ((ch >= 'A') && (ch <= 'F'))
                {
                    Data <<= 4;
                    Data |= ch - 'A' + 10;                    
                }
                else if ((ch >= 'a') && (ch <= 'f'))
                {
                    Data <<= 4;
                    Data |= ch - 'a' + 10;                    
                }
                break;
            default:
                State = 0;
                break;
        }
    }
}

相关帖子

沙发
yangjx1984| | 2008-6-24 15:30 | 只看该作者

我的毕业设计就是DSP做的

顶!好

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1460

主题

21619

帖子

506

粉丝