Question on program memory and structure

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
#include 

typedef struct {
	unsigned int a;
	unsigned char b;
}dataStruct;

const dataStruct data PROGMEM  = {0xfe02, 0x01};

int main()
{
	unsigned int readData;

	readData = pgm_read_word(data.a);

	while(1){
	;
	}

	return 0;
}

I'm trying to read data from structure in program memory. Can I do like above code (not work after I test) ? Or I need to use memcpy_P copy to RAM before use it?

Thank.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I think you might need

pgm_read_word(&data.a)

because (I assume that) pgm_read_word takes an address as a parameter.

Nigel Batten
www.batsocks.co.uk

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

OK it work, thank you.

Last Edited: Wed. Nov 19, 2008 - 04:45 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

More question, if I read by pass a program memory pointer.

const dataStruct *data_P PROGMEM  = &data;
readData = pgm_read_word( pgm_read_word(&data_P->a) );

What is the correct way ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Try this:

typedef struct {
   unsigned int a;
   unsigned char b;
}dataStruct;

const dataStruct data PROGMEM  = {0xfe02, 0x01}; 
const dataStruct *data_P PROGMEM = &data;

dataStruct* tmp_ptr = (dataStruct*)pgm_read_word(&data_P);
readData = pgm_read_word( &tmp_ptr->a );