Using PROGMEM for hexadecimal array

Hi guys, need a little bit of help and guidance here.

I’m trying to interface with a laser mouse sensor (the ADNS-9500 specifically) and it seems it requires a firmware upload on power-up. This is provided in a text file in hexadecimal format. Since the size of the firmware is rather large, I’m trying to use PROGMEM so that my Mega2560 doesn’t run out of memory. The upload however does not seem to be working (deduced via register value which does not match that it is supposed to hold), and so I tried printing out the values that were being read from the flash memory prior to sending to the sensor. The values didn’t seem to match that in the array, so I wrote a test program to check this out:

#include "Arduino.h"
#include <avr/pgmspace.h>

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

prog_uchar testdata PROGMEM = 0x01;
prog_uchar testdata2 = 0x01;
prog_uchar testdata3 = (unsigned char)pgm_read_byte(&testdata);
Serial.print(0x01);
Serial.print(" ");
Serial.print(testdata2);
Serial.print(" ");
Serial.println(testdata3);
}

The results I get are “1 1 15”.

I also tried using testdata instead of &testdata in the pgm_read_byte function and got the following result: “1 1 148”

Is this a sign that something is wrong with the flash memory on my arduino, or is this what we should expect to happen?
Got the same result on both the Mega2560 and Boarduino (328p).

The default format print/ln uses is decimal, so that's the output I would expect. I think what you want is to use write(), which doesn't parse the data, but sends it in it's raw format.

prog_uchar should only be used for variables that are going to be stored in PROGMEM (and it's depricated in avr-libc 1.8.0, so you should probably get rid of it completely). Specifically, testdata3 should be unsigned char, not prog_uchar

Thanks to WizenedEE for catching my mistake there. I’ve corrected it, getting the results 1 1 27 and 1 1 148 respectively.

Sorry for not being very clear, I should clarify my question further. I am trying to use PROGMEM to store my data, and this is a test program I wrote to see if the data was being transferred and subsequently read back correctly. The first two print commands certainly worked out as expected, returning a decimal value of 1 for the hexadecimal value of 0x01. The third print statement however returns 27 or 148 when depending on whether I used “&testdata” or “testdata” in the pgm_read_byte command.

My question is, does this show that the data is somehow being corrupted along the way? How can I know if the data I want written is actually being written correctly?

How can I know if the data I want written is actually being written correctly?

It is being written correctly, it is not being corrupted along the way. The trick is to understand exactly what you are asking the computer to do.

The third print statement however returns 27 or 148 when depending on whether I used "&testdata" or "testdata" i

That is because you are asking it to print out two totally different and mathematically unrelated numbers. One is the memory location and the other is the contents of that memory location.

I think you might find this thread helpful: http://arduino.cc/forum/index.php/topic,120603

Try moving the definitions of the arrays to outside any function.

Thank you very much Wizened, that thread was extremely helpful!