首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >指针和PROGMEM的问题

指针和PROGMEM的问题
EN

Stack Overflow用户
提问于 2020-08-17 08:23:40
回答 1查看 603关注 0票数 1

我正在Arduino UNO上编程,遇到了一个非常烦人的路障。

我对PROGMEM有一个很奇怪的问题,我把它缩小到了一个指针数组的问题上。

当我用变量j索引PROGMEM数组时,它根据天气返回不同的结果,变量j是由一个静态数字设置的,或者是由另一个变量设置的。

程序号是存储在对象中的整数,并且只按以下方式设置,内存被调出,变量保存其值。我已经测试过了。

代码语言:javascript
复制
*(this->program) = 0;

将j设为0直接生成正确的地址。

代码语言:javascript
复制
uint16_t j = 0;
Serial.println(j);    
//printed 0 as expected

address = (uint16_t)*(pgmStringTable + (uint16_t)j); 
Serial.println(address);   
//gave address = 105, which is correct and PROGMEM reads the correct bytes

但是,使用下列方案变量设置j会产生不正确的地址

代码语言:javascript
复制
uint16_t j = *(this->program);
Serial.println(j);    
//printed 0 as expected

address = (uint16_t)*(pgmStringTable + (uint16_t)j); 
Serial.println(address);   
//gave address = 4, which isnt right and results in PROGMEM read errors

即使试图解决这个集合问题,比如在*(这个->程序)上做一些算术运算,或者把它发送给一个返回相同值的整数的函数,都于事无补。唯一有作用的是循环遍历所有整数,如下所示:

代码语言:javascript
复制
//loops through most unsigned integer values
for(uint16_t j = 0; j < 65000; j++){
  if(j == *(this->program)){
     address = (uint16_t)*(pgmStringTable + (uint16_t)j); 
     //address is correct and PROGMEM works correctly
  }
}

以上工作更有理由确认malloc是否正确工作,因为*(此->程序)在将malloc与j进行比较时返回正确的值。

这是一个非常糟糕的工作,因为它的效率非常低,将导致主要的周期浪费在我的程序,这是相当的时间敏感。

任何指向解决方案的指针或指针行为滑稽的原因都是非常有用的。

这个问题来自一个非常大的项目,因此为了帮助缩小错误,我在一个小得多的.ino文件中重新创建了这个问题。

整个代码是这样的。它在上传我的arduino UNO时也会产生同样的错误。

编辑:

  1. 在进一步研究后,将j设置为在我的主项目中不是常量的任何变量(因此该变量不能更改项目中的任何其他内容)似乎以相同的方式破坏了PGM。

代码语言:javascript
复制
const char pgmString1[] PROGMEM = "String 1__";
const char pgmString2[] PROGMEM = "2nd String";
const char *const pgmStringTable[] PROGMEM = {pgmString1, pgmString2};




class testObject{

    private:
        uint16_t* program;

    public:


        testObject(){
            program = (uint16_t*) malloc(sizeof(uint16_t));
            *(this->program) = 0;
        }

        void read(){
            if(*(this->program) == 0){
                //the console prints the below, meaning the value is definitly 0
                Serial.println("it was 0");
                *(this->program) = 0;
            }else{
                Serial.println("it wasnt 0");
                
                //if this is uncommeneted, this program outputs the correct values but the above comment will not be dispalyed
                //*(this->program) = 0;
            }

            char temp[10];
            uint16_t j = 0;
            uint16_t address = 0;   

////////////////////////////////////////////// indirectly setting j
            //sets the j value to be 0, which is what is stored in this->program
            j = *(this->program);

            //prints j to make sure its 0, then finds the address of the progmem string
            Serial.print(j);
            Serial.print("  1 Address: |"); 
            address = (uint16_t)*(pgmStringTable + (uint16_t)j);   
            Serial.print(address);     //the address printed is incorrect
            Serial.print("   ");

            //reads byte by byte from the address, it reads the wrong bytes
            for(int i = 0; i < 10; i++){    
                char myChar = pgm_read_byte( address + i );
                Serial.print(myChar);
            }
            Serial.println("\n");
                    
////////////////////////////////////////////// directly setting j
            //but just setting j = 0 works fine
            j=0;

            //so directly setting j as 0, same code as above but it produces the correct output
            //SAME AS ABOVE, COULDNT SEPERATE INTO ITS ONW FUNCTION
            Serial.print(j);
            Serial.print("  2 Address: |");
            address = (uint16_t)*(pgmStringTable + (uint16_t)j);
            Serial.print(address);
            Serial.print("   ");
            for(int i = 0; i < 10; i++){    
                char myChar = pgm_read_byte( address + i );
                Serial.print(myChar);
            }
            Serial.println();
            //

        }
};





testObject pgmReader;

void setup(){
    Serial.begin(9600);
}

void loop(){
    Serial.println("\n\n\n\n\n\n\n\n");
    delay(1000);
    pgmReader.read();

}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-08-17 14:04:45

不仅pgmString1pgmString2pgmStringTable也是PROGMEM。因此,您必须使用pgm_read_函数从pgmStringTable中读取。

代码语言:javascript
复制
address = pgm_read_ptr(pgmStringTable + (uint16_t)j);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63447172

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档