操作系统 C 中未执行的 for 循环
a for loop not executed in an operating system C
我在执行 for 循环时遇到问题,我创建了一个包含定义值的静态 table,然后我将我的 table 作为参数传递给要处理的函数。
基本上我的代码如下所示:
#define ID_01 0x0000
#define ID_02 0x0001
#define ID_03 0x0002
#define ID_04 0x0003
#define ID_05 0x0004
#define ID_06 0x0005
#define ID_07 0x0006
#define ID_08 0x0007
#define ID_09 0x0008
/*...
*/
#define ID_LAST 0xFFFF
static char table[]={
ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
void process( char *table){
int LastId=0;
char *Command;
for ( Command=table; LastId==0 ; Command++){
switch(Command)
{
case ID_01:
do_stuff01();
break;
case ID_02:
do_stuff02();
break;
...
case ID_LAST:
LastId=1;
break;
default:
break;
}
}
}
我尝试打印一些消息进行调试,但程序不执行任何打印的消息,即使是循环之前和之后的消息。
但是当我将 for 循环更改为:
for(i=0;i<10;i++)
所有消息都已打印。但我必须像一开始那样处理。
PS:这部分代码是在操作系统任务运行中执行到微控制器中的,我只是一个初学者。
现在您正在使用 switch (Command)
,其中 Command
保存地址 od table
变量。
将switch
改为
switch (*Command) { //Use value at pointed Command.
}
请注意,在执行 *Command
时,您会取消引用 char
,即 1 个字节。您的 ID 有 2 个字节,因此您丢失了数据。
变化:
static char table[] = {ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
缩短为 16 位值
static unsigned short table[]={ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
稍后,修改您的 process
函数以接受 unsigned short
void process( const unsigned short *table) { //Unsigned short
int LastId = 0;
unsigned short *Command; //Unsigned short
for ( Command=table; LastId==0 ; Command++){
switch(*Command) { //Added star
//...
}
}
//...
我会将您的 process
代码重写为:
void process(const unsigned short *table, size_t tableLen) {
while (tableLen--) {
switch (*table) {
case ID_1: /* Do stuff */ break;
}
table++; //Increase pointer to next ID element
}
}
//Usage then like this:
static unsigned short table[] = {ID_1, ID_2, ID_3, ..., ID_n};
//Put pointer and length of table
process(table, sizeof(table)/sizeof(table[0]));
一般来说,它会像下面这样构造和映射 ID/FUNC。
#include <stdio.h>
#define ID_01 0x0000
#define ID_02 0x0001
/* ... */
#define ID_LAST 0xFFFF
typedef void (*func)();
typedef struct {
char n;
func f;
} fmap;
void do_something01() { }
void do_something02() { }
/* ... */
static fmap fmaps[] = {
{ID_01, do_something01},
{ID_02, do_something02},
/* ... */
{ID_LAST, NULL},
};
我在执行 for 循环时遇到问题,我创建了一个包含定义值的静态 table,然后我将我的 table 作为参数传递给要处理的函数。 基本上我的代码如下所示:
#define ID_01 0x0000
#define ID_02 0x0001
#define ID_03 0x0002
#define ID_04 0x0003
#define ID_05 0x0004
#define ID_06 0x0005
#define ID_07 0x0006
#define ID_08 0x0007
#define ID_09 0x0008
/*...
*/
#define ID_LAST 0xFFFF
static char table[]={
ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
void process( char *table){
int LastId=0;
char *Command;
for ( Command=table; LastId==0 ; Command++){
switch(Command)
{
case ID_01:
do_stuff01();
break;
case ID_02:
do_stuff02();
break;
...
case ID_LAST:
LastId=1;
break;
default:
break;
}
}
}
我尝试打印一些消息进行调试,但程序不执行任何打印的消息,即使是循环之前和之后的消息。
但是当我将 for 循环更改为:
for(i=0;i<10;i++)
所有消息都已打印。但我必须像一开始那样处理。
PS:这部分代码是在操作系统任务运行中执行到微控制器中的,我只是一个初学者。
现在您正在使用 switch (Command)
,其中 Command
保存地址 od table
变量。
将switch
改为
switch (*Command) { //Use value at pointed Command.
}
请注意,在执行 *Command
时,您会取消引用 char
,即 1 个字节。您的 ID 有 2 个字节,因此您丢失了数据。
变化:
static char table[] = {ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
缩短为 16 位值
static unsigned short table[]={ID_01, ID_02 ,ID_03, ID_04, .... , ID_LAST}
稍后,修改您的 process
函数以接受 unsigned short
void process( const unsigned short *table) { //Unsigned short
int LastId = 0;
unsigned short *Command; //Unsigned short
for ( Command=table; LastId==0 ; Command++){
switch(*Command) { //Added star
//...
}
}
//...
我会将您的 process
代码重写为:
void process(const unsigned short *table, size_t tableLen) {
while (tableLen--) {
switch (*table) {
case ID_1: /* Do stuff */ break;
}
table++; //Increase pointer to next ID element
}
}
//Usage then like this:
static unsigned short table[] = {ID_1, ID_2, ID_3, ..., ID_n};
//Put pointer and length of table
process(table, sizeof(table)/sizeof(table[0]));
一般来说,它会像下面这样构造和映射 ID/FUNC。
#include <stdio.h>
#define ID_01 0x0000
#define ID_02 0x0001
/* ... */
#define ID_LAST 0xFFFF
typedef void (*func)();
typedef struct {
char n;
func f;
} fmap;
void do_something01() { }
void do_something02() { }
/* ... */
static fmap fmaps[] = {
{ID_01, do_something01},
{ID_02, do_something02},
/* ... */
{ID_LAST, NULL},
};