大家好,我一直在为我的 FPGA 开发一款游戏。我在这里使用消息队列,我的问题是,当我想打印数组中的值时,即使我在其中放入不同的值,我也总是得到相同的结果。可能是我打印错误,或者任务运行时它们被重置。

#define   MSG_QUEUE_SIZE  4
OS_EVENT  *msgqueue;
void      *msgqueue_tbl[MSG_QUEUE_SIZE];

int say_array[MSG_QUEUE_SIZE];
int idx,x;

// Interrupt Service Routine for KEY0-KEY3 IRQ
void isr_pio_key(void *context, alt_u32 id)
{
INT32U msg = IORD_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE);

// Post message in queue
OSQPost(msgqueue, (void *)msg);

// Reset EDGE CAP
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE, 0x01);
}

void key_pressed_task(void *pdata)
{
INT8U error_code = OS_NO_ERR;
INT32U msg;
OS_Q_DATA queue_data;
INT16U num_msgs;

int say_array[MSG_QUEUE_SIZE];

for(;;) {

    srand(time(NULL));
    int led = rand()%4;

    if(led == 3){
        IOWR_32DIRECT(PIO_LEDG_BASE,0,0x08);
        say_array[3] = led;
        idx++;
    }
    if(led == 2){
        IOWR_32DIRECT(PIO_LEDG_BASE,0,0x04);
        say_array[2] = led;
        idx++;
    }
    if(led == 1){
        IOWR_32DIRECT(PIO_LEDG_BASE,0,0x02);
        say_array[1] = led;
        idx++;
    }
    if(led == 0){
        IOWR_32DIRECT(PIO_LEDG_BASE,0,0x01);
        say_array[0] = led;
        idx++;
    }

    printf("%d",led);

    if(idx==4)
    {
        printf("\nSimon said: ");
        for(x=0;x<4;x++)
            printf("%d",say_array[x]);
        printf("\n");

        idx=0;
        for(;;)
        {
            OSQQuery(msgqueue, &queue_data);
            num_msgs = queue_data.OSNMsgs;

            if(num_msgs > 0)
            {
                msg = (INT32U)OSQPend(msgqueue, 4, &error_code);
                printf("msg: %d\n", msg);

                if((!error_code) && (msg == 1))
                    printf("(KEY_PRESSED_TASK) KEY0 Pressed\n");break;

                if((!error_code) && (msg == 2))
                    printf("(KEY_PRESSED_TASK) KEY1 Pressed\n");break;

                if((!error_code) && (msg == 4))
                    printf("(KEY_PRESSED_TASK) KEY2 Pressed\n");break;

                if((!error_code) && (msg == 8))
                    printf("(KEY_PRESSED_TASK) KEY3 Pressed\n");break;

                msg = 0;
            }
        }
    }
    OSTimeDlyHMSM(0,0,1,0);
}
}

int main()
{
// Create message queue
msgqueue = OSQCreate(&msgqueue_tbl[0], MSG_QUEUE_SIZE);

// Set up interrupts (mask KEY0)
IOWR_ALTERA_AVALON_PIO_IRQ_MASK(PIO_KEY_BASE, 0x0F);
IOWR_ALTERA_AVALON_PIO_EDGE_CAP(PIO_KEY_BASE, 0x01);
alt_ic_isr_register(PIO_KEY_IRQ_INTERRUPT_CONTROLLER_ID, PIO_KEY_IRQ, (void *)isr_pio_key, NULL, NULL);

// Create task
OSTaskCreate(key_pressed_task, NULL, &KEY_PRESSED_TASK_STACK[STACKSIZE-1], KEY_PRESSED_TASK_PRIO);

// Start uC/OS-II
OSStart();

// Shouldn't reach this point
return -1;

}

我想知道的部分是:

printf("\nSimon said: ");
for(x=0;x<4;x++)
printf("%d",say_array[x]);
printf("\n");

上面的代码应该打印 LED 打开时设置的值。但它总是打印0123

有任何想法吗?

有帮助吗?

解决方案

在你的 for(;;) 你总是设置的循环 say_array[led] = led;. 。IE。什么时候 led==3 你设置 say_array[3] = 3. 。尝试使数组成为布尔值数组并切换元素,例如 say_array[led] = !say_array[led];还要删除 say_array 定义之一,因为一个在文件范围内,另一个在函数范围内。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top