Go Down

Topic: ProgMem store Unsigned Long data (Read 919 times) previous topic - next topic

slinbody

Dec 31, 2013, 04:35 am Last Edit: Dec 31, 2013, 07:05 am by slinbody Reason: 1
Hi, all
I try to store Unsigned Long data with ProgMem.
The follwoing looks good:
Code: [Select]

#include <avr/pgmspace.h>

unsigned long mydata[] PROGMEM =
{
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
};


unsigned long byte1;


void setup()
{
Serial.begin(9600);
Serial.println("Setup Done");
}

void loop()
{
  for(int i=0;i<10;i++)
      Serial.println(pgm_read_byte(&mydata[i]));
  Serial.print("--------");
  for( ; ; ) ;
}

The result show: 0,1,2,3,4,5,6,7,8,9

But if I replace with bigger numbers, it goes wrong:
Quote

unsigned long mydata[] PROGMEM =
{
0,
11111,
2,
3,
4,
5,
6,
7,
8,
9
};

The result show: 0,103,2,3,4,5,6,7,8,9

Is there any better way to deal with bigger numbers??
Thank you.

pYro_65

#1
Dec 31, 2013, 05:29 am Last Edit: Dec 31, 2013, 05:31 am by pYro_65 Reason: 1
make the integer literal ( the number ) an unsigned long using a type suffix.
Quote
unsigned long mydata[] PROGMEM =
{
0,
11111UL,
2,
3,
4,
5,
6,
7,
8,
9
};


You can do the same with floats and others:
Quote
float f = 123.456F;
int i = 0xAABB; //Not needed
unsigned long long u = 0x1A7EBADF00DULL;


u requires the 'ULL' as the integer literal will be taken as a 32-bit or long.
Forum Mod anyone?
https://arduino.land/Moduino/

Coding Badly

The result show: 0,1,2,3,4,5,6,7,8,9


Not even close...

Code: [Select]
Setup Done
0
0
0
0
0
0
0
0
0
0
--------


If you want help post your >>> actual <<< code.  Please use [code] [/code] tags.

slinbody

Hi, pYro_65
I try the way you said.
Code: [Select]

#include <avr/pgmspace.h>

unsigned long mydata[] PROGMEM =
{
0,
11111UL,
2,
3,
4,
5,
6,
7,
8,
9
};


unsigned long byte1;


void setup()
{
Serial.begin(9600);
Serial.println("Setup Done");
}

void loop()
{
  for(int i=0;i<10;i++)
      Serial.println(pgm_read_byte(&mydata[i]));
//  byte1 = pgm_read_byte(&mydata[6]);
//  Serial.println(byte1);
  Serial.print("--------");
  for(;;);
}


The result:
Quote

Setup Done
0
103
2
3
4
5
6
7
8
9
--------

seems not to work correctly.

pYro_65

Didn't see your setup(), unsigned longs are 4 bytes, you read only one.

DEC: 11111
BIN: 10101101100111

the highlighted part as a single byte is 103

use pgm_read_dword()
http://www.nongnu.org/avr-libc/user-manual/group__avr__pgmspace.html
Forum Mod anyone?
https://arduino.land/Moduino/

slinbody

Hi, pYro_65
I thank you very much.
The following code works perfectly as I wish:
Code: [Select]

#include <avr/pgmspace.h>

const unsigned long mydata[] PROGMEM =
{
0,
756297739,
207540906,
207540794,
207541034,
4104581197,
302518077,
7,
8,
9
};


unsigned long byte1;


void setup()
{
Serial.begin(9600);
Serial.println("Setup Done");
}

void loop()
{
  for(int i=0;i<10;i++)
      Serial.println(pgm_read_dword(&mydata[i]));
//  byte1 = pgm_read_byte(&mydata[6]);
//  Serial.println(byte1);
  Serial.print("--------");
  for( ; ; ) ;
}



Go Up