我的状态机中并发滴答功能出现故障

时间:2015-02-10 02:11:48

标签: c embedded state-machine atmelstudio

代码I的写作目的是当我的第0位PINA为1时,PORTB上显示的数字会递增。同样,如果PINA的第1位为1,则该数字递减。如果按下按钮,它将以每秒一次的速率递增/递减。如果按钮按下至少3秒钟,则速率每400毫秒增加一个。如果按下两个按钮,PORTB上显示的数字将重置为0.定时器周期设置为100 ms。

我将状态机分成两个运行"同时"的状态机。一个SM调整I'递增/递减的周期,另一个SM调整等待/加/减/重置状态之间的转换。

我遇到的问题是我修改期间的状态机根本没有做任何事情。我有一个全局变量来跟踪在递增/递减之前我必须等待多少滴答(初始化为10),并且当我的Incr / Decr状态中的滴答数保持超过30滴时,该机器应该修改它(3)秒)。但这台机器根本没有做任何事情。我进行了修改,以便在我的并发SM启动时立即永久地更改为4个滴答,但是仍然没有做任何事情;递增/递减仍然每1秒发生一次,而不是每400毫秒发生一次。

我的代码出了什么问题? 有问题的代码:

enum States {Init, ButPres, Incr, Wait, Decr, Reset} state;
enum other_States {Init1, change_tick_total} state1;
unsigned char button = 0x00;
unsigned char cnt = 0x00;
unsigned char tick_cnt = 0;
unsigned char tick_total = 0x0A;

void change_period_tick() {
    switch (state1) {
        case Init1:
            state = change_tick_total;
            break;
        case change_tick_total:         
            state = change_tick_total;
            break;
        default:
            state = Init1;
            break;
    }

    switch (state1) {
        case change_tick_total:
            if (tick_cnt > 30)
                tick_total = 0x04;
            else
                tick_total = 0x0A;
            break;
        default:
            break;
    }       

}


void tick(){
    switch(state){
        case Init:
            state = ButPres;
            break;
        case ButPres:
            if(button == 0x01)
                state = Incr;
            else if(button == 0x02)
                state = Decr;
            else if(button == 0x03)
                state = Reset;
            else
                state = ButPres;
        break;
        case Incr:
            if(button == 0x01 && cnt < 9)
                state = Incr;
            else
                state = Wait;
            break;
            case Decr:
                if(button == 0x02 && cnt > 0){
                state = Decr;
            }
            else
                state = Wait;
            break;
            case Wait:
                if(button == 0x03)
                    state = Reset;
                else if(button)
                    state = Wait;
                else
                    state = ButPres;
                break;
            case Reset:
                if(button == 0x03)
                    state = Reset;
                else
                    state = Init;
                break;
            default:
                state = Init;
        break;
    }

    switch(state){
        case Init:
            cnt = 0x00;
            PORTB = cnt;
            break;
        case Incr:
            if(cnt < 0x09){             
                ++tick_cnt;
                if (tick_cnt % tick_total == 1)
                    ++cnt;
                PORTB = cnt;                
            }
            break;
        case Decr:
            if(cnt > 0x00){         
                ++tick_cnt;
                if (tick_cnt % tick_total == 1)
                    --cnt;
                PORTB = cnt;
            }
            break;
        case Reset:
            cnt = 0;
            PORTB = cnt;
        default:
            tick_cnt = 0; //once we're out of INCR/DECR, the tick_cnt resets, as we're on a new cycle of counting ticks
            break;
    }
}

int main(void)
{
    state = Init;
    state1 = Init1;

    DDRA = 0x00; PORTA = 0xFF;
    DDRB = 0xFF; PORTB = 0x00;
    // Initializes the LCD display  
    TimerSet(100);
    TimerOn();
    tick_total = 0x0A;
    while(1) {      
        button = ~PINA;     
        change_period_tick(); //this does no seem to be running at all
        tick();             
        while(!TimerFlag){}
        TimerFlag = 0;
    }
}

0 个答案:

没有答案