Pages: [1]   Go Down
Author Topic: Problem in bit reading with pgm_read_byte()  (Read 559 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I need to work with bit, and so i choice pgm style.

Why this code is not working as expected?
Code:
#include <avr/pgmspace.h> //for pgm_read_byte

const prog_uint8_t wcode[9] = {   // store in program memory to save RAM
B10010000,
B11100000,
B01000010,
B11010000,
B00000000,
B00000000,
B01100100,
B00000101,
B10100000  //last 4 bit is not used
};
void setup() {               
 Serial.begin(9600);
}
void loop() {
 for (byte x=0; x<9; ++x) {
    byte data = pgm_read_byte (&wcode[x]);   // fetch data from program memory
        for (byte y=0; y<8; ++y) {
            if (data & (1<<y)) {
                Serial.print("1");
            } else {
       Serial.print("0");
            }
        }
        Serial.println("");
               
    }
    Serial.println("");
   Serial.println("");
    delay(15*1000);
}

This code return this bit sequence
Quote
00100000
10000100
01101001
00010011
00001100
10001011
10100000
10000010
11101111
Logged

Wellington, New Zealand
Offline Offline
Sr. Member
****
Karma: 1
Posts: 404
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

You are missing the PROGMEM declaration:

Code:
const prog_uint8_t PROGMEM wcode[9] = {
Logged


Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thank you. I'm sorry  smiley-red

Another question i have some function have a PROGMEM const like this
Code:
void sendCode2() {
const prog_uint8_t PROGMEM wcode[9] = {   // store in program memory to save RAM
B10000000,
B11100000,
B01000010,
B11010000,
B00000000,
B00000000,
B01100100,
B00000101,
B10100000  //last 4 bit is not used
};
useCode(&wcode);
}
void useCode(){
for (byte x=0; x<9; ++x) {
    byte data = pgm_read_byte (&wcode[x]);   // fetch data from program memory
        for (byte y=0; y<8; ++y)
            //do some stuff with code
            if (data & (1<<y)) {
                Serial.print("1");
            } else {
       Serial.print("0");
            }
        }               
    }
}

As you can seeuseCode function hasn't any param. What the correct method for passing "wcode" variable from sendCode() to useCode() ? I need to use pointers? How?
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 9
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Sorry, is correct to use this code? (note the pointers)

Code:
void sendCode2() {
const prog_uint8_t PROGMEM wcode[9] = {   // store in program memory to save RAM
B10000000,
B11100000,
B01000010,
B11010000,
B00000000,
B00000000,
B01100100,
B00000101,
B10100000  //last 4 bit is not used
};
useCode(&wcode);
}
void useCode(uint8_t *wcode){
for (byte x=0; x<9; ++x) {
    byte data = pgm_read_byte (&wcode[x]);   // fetch data from program memory
        for (byte y=0; y<8; ++y)
            //do some stuff with code
            if (data & (1<<y)) {
                Serial.print("1");
            } else {
       Serial.print("0");
            }
        }               
    }
}
Logged

Pages: [1]   Go Up
Jump to: