单步执行多维数组

时间:2014-02-06 21:31:19

标签: c arrays multidimensional-array serial-port uart

我有一段在嵌入式系统上运行的代码。它的工作是将一些ASCII字符转换为专有数据。数据存储在一个多维数组中,虽然我无法通过硬件调试器确认这一点,但是看起来是什么问题,因为位变量保持在值2.这段代码的前两次是有效的。运行,但在第三次运行时它会中断并返回开始通过UART接口发送错误的数据。我可能也许其他人分析这个可能会看到我所缺少的东西。这是C99,我不太熟悉。 Bleow是整个函数,但我认为问题在于for语句?任何帮助将不胜感激!

void simple_uart_putstring(uint8_t *str, uint16_t length)
{
    //send data bits
    uint_fast8_t index = 0;
    uint8_t ch = str[index++];
    uint_fast8_t bitCount = 0;
    int index2 = 0;
    int bit = 0;

    if (length > 1)
    {
        while (length >= index)
        {
            if (bitCount < 2)
            {
                if (length < 10)
                {
                    //send sync bits
                    simple_uart_put(254);
                    simple_uart_put(223);
                    bitCount = 2;
                } else {
                    //send sync bits and add scrolling
                    simple_uart_put(254);
                    simple_uart_put(222);
                    bitCount = 2;
                }
            }
            //send each bit for each letter in the string
            for (uint_fast8_t i = 0; i < 5; i++)
            {
                index2 = (int)ch;
                bit = (int)i;
                simple_uart_put(matrix[index2 - 32][bit]);
                bitCount++;
            }
            ch = str[index++];
        }
        //the main controller is expecting 150 bits total to continue to send bit until 150
        while (bitCount <= 150)
        {
            simple_uart_put(0);
            bitCount++;
            if(bitCount >= 150)
            {
                bitCount = 0;
                break;
            }
        }
        bitCount = 0;
    }
}

这是一个数组样本:

 const uint8_t matrix[59][5] = 
    {
        { 0, 0, 0, 0, 0}, //space  
        { 0, 125, 0, 0, 0}, //!  
        { 0, 112, 0, 112, 0}, //"
{127, 127, 127, 127, 127 }, //# 
                                           { 18, 42, 107, 36, 0}, //$
                                           { 50, 52, 22, 38, 0}, //%
                                           { 38, 89, 57, 6, 9}, //&
                                           { 64, 48, 0, 0, 0}, //'
                                           { 0, 0, 62, 65, 0}, //(
                                           { 0, 65, 62, 0, 0}, //)
                                           { 20, 8, 62, 8, 20}, //*
                                           { 0, 8, 28, 8, 0}, //+
                                           { 1, 6, 0, 0, 0}, //,
                                           { 0, 8, 8, 8, 0}, //-
                                           { 3, 3, 0, 0, 0}, //.
                                           { 2, 4, 8, 16, 32}, //
                                           { 62, 69, 73, 62, 0}, //0
                                           { 1, 33, 127, 1, 0}, //1
                                           { 35, 67, 69, 49, 0}, //2
                                           { 34, 73, 73, 54, 0}, //3
                                           { 12, 20, 36, 127, 0}, //4
                                           { 114, 81, 81, 78, 0}, //5
                                           { 30, 41, 73, 6, 0}, //6
                                           { 64, 71, 72, 112, 0}, //7
                                           { 54, 73, 73, 54, 0}, //8
                                           { 48, 73, 74, 60, 0}, //9
                                           { 0, 54, 54, 0, 0}, //:
                                           { 0, 1, 54, 0, 0}, //;
                                           { 0, 8, 20, 34, 0}, //<
                                           { 0, 20, 20, 20, 0}, //=
                                           { 0, 34, 20, 8, 0}, //>
                                           { 32, 64, 69, 72, 48}, //?
                                           { 62, 65, 93, 93, 112}, //@

                                           { 63, 72, 72, 63, 0 }, //a
                                           { 127, 73, 73, 54, 0 }, //b
                                           { 62, 65, 65, 34, 0}, //c
                                           { 127, 65, 34, 28, 0 }, //d
                                           { 127, 73, 73, 65, 0}, //e
                                           { 127, 72, 72, 64, 0}, //f
                                           { 62, 65, 73, 47, 0}, //g
                                           { 127, 8, 8, 127, 0}, //h
                                           { 0, 65, 127, 65, 0},//i
                                           { 6, 65, 126, 64, 0}, //j
                                           { 127, 8, 20, 99, 0}, //k
                                           { 127, 1, 1, 1, 0}, //l
                                           { 127, 32, 24, 32, 127}, //m
                                           { 127, 16, 8, 127, 0}, //n
                                           { 62, 65, 65, 62, 0}, //o
                                           { 127, 72, 72, 48, 0}, //p
                                           { 60, 70, 66, 61, 0}, //q
                                           { 127, 76, 74, 49, 0}, //r
                                           { 50, 73, 73, 38, 0}, //s
                                           { 0, 64, 127, 64, 0}, //t
                                           { 126, 1, 1, 126, 0},
                                           { 127, 1, 2, 124, 0},
                                           { 126, 1, 6, 1, 126},
                                           { 99, 28, 28, 99, 0},
                                           { 112, 8, 8, 127, 0},
                                           { 71, 73, 81, 97, 0}};

和uart发送方法:

void simple_uart_put(uint8_t cr)
{
  NRF_UART0->TXD = (uint8_t)cr;

  while (NRF_UART0->EVENTS_TXDRDY!=1)
  {
    // Wait for TXD data to be sent
  }

  NRF_UART0->EVENTS_TXDRDY=0;
}

这种工作的一个例子是输入字符串是“AB”并且长度= 2; 它应该通过UART发送以下字节:

{254, 223, 63, 72, 72, 63, 0, 127, 73, 73, 54, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, etc....} 

前两个字节是同步字节,接下来的五个字节来自数组matrix[33][0 to 4],因为ASCII'A'= 65和65-32 = 33.然后接下来的五个来自ASCII'B'= 66并且66 - 32 = 34所以它们是从matrix[34][0 to 4]发送的。然后下一个n = 150-bitNumber被发送为0,因为主控制器总是期望150个字节。

1 个答案:

答案 0 :(得分:1)

请参阅底部的修改

如果没有所有功能的定义,我无法完全分析这一点,但有一些可疑的事情:

1) 此声明为奇数:

  uint_fast8_t index = 0;
  uint8_t ch = str[index++]; //always sets ch to first character of input "str" then increments index.  

注意: 更正了上一行的评论。

2) 虽然评论表明“字符串中每个字母的每个位”但它只处理5:

        for (uint_fast8_t i = 0; i < 5; i++){...} //what if lenth of input is less than 5? 

建议改为:

        for (uint_fast8_t i = 0; i < length; i++){...}  //used second argument "length"  

3) 最后,似乎null终止字符串应该遵循for循环,但是:(见行中的注释)(另外,使用的输入参数是“ abc“,4)

    for (uint_fast8_t i = 0; i < 5; i++)
    {
        index2 = (int)ch; //ch inits to first char in str, and indexes through
        bit = (int)i;
        //simple_uart_put(matrix[index2 - 32][bit]);
        bitCount++;
    }
    ch = str[index++]; //terminates in second character of input "b' in this case  
    //I think this should null terminate with '\0' if it is to be treated as a C string,
    //but because, as you say, this is "proprietary", I am not sure.  

编辑

我认为问题可能是你已经使用看起来足够的空间来声明变量矩阵,但只用三行数据初始化它:

const uint8_t matrix[59][5] = 
{
    { 0, 0, 0, 0, 0}, //space  
    { 0, 125, 0, 0, 0}, //!  
    { 0, 112, 0, 112, 0}, //"
}; //have only initialized matrix[0], matrix[1] and matrix[2]  

剩余的53行数据虽然归您所有,但我尚未初步确定。所以,当你说:

接下来的五个来自数组矩阵[33] [0到4],因为ASCII'A'= 65和65-32 = 33.然后接下来的五个来自ASCII'B'= 66和66 - 32 = 34因此它们从矩阵[34] [0到4]

发送

这表明随机数正好占据了那些未初始化的内存位置,这些内容正在写成:

simple_uart_put(matrix[index2 - 32][bit]);

编辑2 (见评论解释)

这是我的main(),并注释了simple_uart_putstring():

int     main()
{
    uint8_t *str;
    int len=3;

    str = malloc(3); //extra char for terminating null byte

    strcpy(str, "AB");
    simple_uart_putstring(str, 2);
    free(str);
}

void simple_uart_putstring(uint8_t *str, uint16_t length)
{
    //send data bits
  uint_fast8_t index = 0;
  uint8_t ch = str[index++]; //ch inits to first char in str, and indexes through
    uint_fast8_t bitCount = 0;
    int index2 = 0;
    int bit = 0;

    if (length > 1)
    {
        while (length >= index)
        {
            if (bitCount < 2)
            {
                    if (length < 10)
                    {
                        //send sync bits
                        //simple_uart_put(254);
                        //simple_uart_put(223);
                        bitCount = 2;
                    } else {
                        //send sync bits and add scrolling
                        //simple_uart_put(254);
                        //simple_uart_put(222);
                        bitCount = 2;
                    }   
            }
            //send each bit for each letter in the string
            for (uint_fast8_t i = 0; i < 5; i++)
            {
                index2 = (int)ch; //
                bit = (int)i;
                /*simple_uart_put(*/matrix[index2 - 32][bit];//);break here to view "matrix[index2 - 32][bit]"
                bitCount++;
            }
            ch = str[index++]; //
        }
        //the main controller is expecting 150 bits total to continue to send bit until 150
            while (bitCount <= 150)
        {
                //simple_uart_put(0);
                bitCount++;
                if(bitCount >= 150)
                {
                    bitCount = 0;
                    break;
                }
        }
        bitCount = 0;
    }
}
相关问题