Go Down

Topic: Problem in bit reading with pgm_read_byte() (Read 764 times) previous topic - next topic

m4tteo

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

Why this code is not working as expected?
Code: [Select]
#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


dhunt

You are missing the PROGMEM declaration:

Code: [Select]

const prog_uint8_t PROGMEM wcode[9] = {

m4tteo

Thank you. I'm sorry  :smiley-red:

Another question i have some function have a PROGMEM const like this
Code: [Select]

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?

m4tteo

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

Code: [Select]
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");
            }
        }               
    }
}

Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy