指针内存错误
问题是,
当使用返回闪存中所需模式地址的函数时,我无法从闪存获取数据(在下面的示例中进行了简化,仅使用 1 个常量:PATTERN_P)。
解释之前的一些代码
类型 patternp
被定义为
typedef prog_uchar patternp[NUM_ROWS];
全局 PATTERN_P 变量是一个类型为patternp的数组,定义为
const patternp PATTERN_P PROGMEM = {
0b11110000 ,
0b10010000 ,
0b10010000 ,
0b10010000 ,
0b11110000 ,
0b10000000 ,
0b10000000 ,
0b10000000
};
getpattern():
const patternp * getPattern()
{
//...
return &PATTERN_P;
}
main():
const patternp *bufferPattern = getPattern();
uint8_t rowPatternData[NUMBER_ROW_PER_MATRIX];
const patternp *bufferPattern = getPattern(s[iLetter]);
for(int iRow = 0; iRow<NUMBER_ROW_PER_MATRIX; iRow++)
{
rowPatternData[iRow]=pgm_read_byte( &PATTERN_P[iRow] ); // <--- WORK!
rowPatternData[iRow]=pgm_read_byte( bufferPattern[iRow] ); // Not Working!
}
解释
正如你所看到的,代码得到了模式 (在这个例子中,它每次都会返回 PATTERN_P...而不是我使用 pgm_read_byte 从闪存获取数据。它需要一个地址并返回数据。我使用模板的直接访问:&PATTERN_P[iRow],但当我使用 bufferPattern[iRow] 或 &bufferPattern[iRow] 时不起作用任何想法:
pgm_read_byte 在 pgmspace
The problem
I can't get the data from the Flash memory when using the function that return the address of the pattern desired in the Flash (simplified in the example below with only 1 constant : PATTERN_P).
Some code before explication
The type patternp
is defined as
typedef prog_uchar patternp[NUM_ROWS];
The global PATTERN_P variable is an array of type patternp, defined as
const patternp PATTERN_P PROGMEM = {
0b11110000 ,
0b10010000 ,
0b10010000 ,
0b10010000 ,
0b11110000 ,
0b10000000 ,
0b10000000 ,
0b10000000
};
getpattern():
const patternp * getPattern()
{
//...
return &PATTERN_P;
}
main():
const patternp *bufferPattern = getPattern();
uint8_t rowPatternData[NUMBER_ROW_PER_MATRIX];
const patternp *bufferPattern = getPattern(s[iLetter]);
for(int iRow = 0; iRow<NUMBER_ROW_PER_MATRIX; iRow++)
{
rowPatternData[iRow]=pgm_read_byte( &PATTERN_P[iRow] ); // <--- WORK!
rowPatternData[iRow]=pgm_read_byte( bufferPattern[iRow] ); // Not Working!
}
Explications
As you can see, the code get the pattern (in this example, it will return PATTERN_P every time... than I use pgm_read_byte to get the data from the Flash memory. This use the AVR pgmspace (link below). It takes an address and return the data. The code above work when I use the direct access of a template : &PATTERN_P[iRow], but won't work when I use bufferPattern[iRow] or &bufferPattern[iRow]. Any idea?
Reference : pgm_read_byte is defined in pgmspace
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
bufferPattern 是一个指向数组的指针。 当您编写 bufferPattern[iRow] 时,这不会计算为指向 patternp 的条目 iRow 的指针; [] 操作作用于指针,而不是它指向的数组。 您似乎想写的是 &((*bufferPattern)[iRow])。
这将解决眼前的问题。 然而,代码有点混乱。 通过直接传递数组,您的代码可能会得到简化(C 不会按值传递数组;因此它不会复制数组 - 您不需要创建指向数组的指针来避免这种情况)。
bufferPattern is a pointer to an array. When you write bufferPattern[iRow], this does NOT evaluate to a pointer to entry iRow of patternp; the [] operation is acting on the pointer, not the array it points to. What you appear to want to write is &((*bufferPattern)[iRow]).
That will fix the immediate problem. However, the code is a bit confusing. It may be that your code would be simplified by passing the array directly (C does not pass arrays by value; so it won't copy the array - you don't need to make a pointer to the array to avoid this).
这
是
当你的工作线给出这个时:
this
is
when your working line gives this: