嵌入式C语言查表法

转自:https://blog.csdn.net/morixinguan/article/details/51799668    作者:Engineer-Bruce_Yang

 

就像下面的这个表

之前写过上面这个标题的一篇文章,讲的是以位移的方式去遍历表中的数据,效率非常高,但是,如果要实现一个乱序的流水灯或者跑马灯的话,思考一个这样的算法是不可取的,很费时间,也很费脑力,于是,今天就说一说查表法,如果在程序中运用查表法,不论多么复杂的程序,只要符合一张表,那都可以实现,非常简单,体力活而已,接下来看看下面这个程序,对上面这个进行操作吧。       

#include <stdio.h>
#include <windows.h>
//这里的行可以自由写,这样就不受限制,想做出什么样的效果都可以。
int array[][10] =  {
 
    0x03,0x00,0x00,0x00,0x00,//第一列
    0x00,0x02,0x00,0x00,0x00,
    
    0x0C,0x00,0x00,0x00,0x00,//第二列
    0x00,0x04,0x00,0x00,0x00,
    
    0x10,0x00,0x00,0x00,0x00,//第三列
    0x00,0x08,0x00,0x00,0x00,
    
    0x60,0x00,0x00,0x00,0x00,//第四列
    0x00,0x10,0x00,0x00,0x00,
    
    0x80,0x01,0x00,0x00,0x00,//第五列
    0x00,0x20,0x00,0x00,0x00,
    
    0xAA,0x55,0x00,0x00,0xC0,//end
    0x00,0x00,0x00,0x00,0x00,
    
};
 
void to_Q112_cmd_designator_LED(int *array)
{
    int i;
    for(i = 0; i < 10; i++)
    {
        printf(" %3d ", *(array+i));    
    }
    printf("\n");
}
 
void delay_500ms(void)
{
    Sleep(500);
}
 
int main(void)
{
    int i,j;
    int tick;
    int count = 0;
    while(array[count][0] != 0xAA || array[count][1] != 0x55)//如果当数组第count行第0列等于0xAA,或者第count行第1列等于0x55时,那么就退出,否则就循环执行遍历数据 
    {    
        to_Q112_cmd_designator_LED((int *)(&array[0][0]+count*10) );//以首元素每次向后偏移10个字节
        delay_500ms();
        
        count++;
    }
    
    return 0;
}

 

运行结果:

 

   3    0    0    0    0    0    2    0    0    0
  12    0    0    0    0    0    4    0    0    0
  16    0    0    0    0    0    8    0    0    0
  96    0    0    0    0    0   16    0    0    0
 128    1    0    0    0    0   32    0    0    0

posted @ 2018-08-06 15:41  小刚的视界  阅读(8180)  评论(0编辑  收藏  举报