Problems using PROGMEM

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

[moderator note: this fault diagnosis was attached to the end of the PROGMEM tutorial - it's not appropriate for it to be there so this has been split into a new thread in GCC Forum instead]

Hi all...... :) :) :)

i am fresher to this memory programming ...i have a small question...it may be simple for you but i didn't understand.here it is:

	#include 
   #include 
   #include 

   const uint8_t numbr[] PROGMEM = {21,22,23,24,25,26,27}; 

   int main() 
   { 
      DDRC = 0xFF;        
      while(1) 
      { 
      for(uint8_t i=0;i<8;i++)    
        { 
         PORTC = numbr[i]; 
         _delay_ms(1000); 
        } 
      } 
    return 0; 
   } 

Why this is not working.... :(

~~~Decoder ~~~

Last Edited: Wed. Nov 10, 2010 - 09:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

i am fresher to this memory programming ...i have a small question...it may be simple for you but i didn't understand.here it is:

You'll need to read the tutorial text - just adding PROGMEM to the variable definition isn't enough.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

I agree with Dean. But the short term solution is:

   #include 
   #include 
   #include 

   const uint8_t numbr[] PROGMEM = {21,22,23,24,25,26,27}; 

   int main() 
   { 
      DDRC = 0xFF;       
      while(1) 
      { 
      for(uint8_t i=0;i<8;i++)    
        { 
         PORTC = pgm_read_byte(&numbr[i]); 
         _delay_ms(1000); 
        } 
      } 
    return 0; 
   }

By the way it seems to me that you attempt to output 8 values from array that only has 7 entries?

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

Quote:
You'll need to read the tutorial text - just adding PROGMEM to the variable definition isn't enough.

sorry, i mistakenly kept the wrong code.....
i changed and kept the correct code... just see that..
#include 
   #include 
   #include 

   struct Lookup 
   { 
     uint8_t Volt; 
     uint8_t rpm; 
   }; 

   struct Lookup samp[] PROGMEM = {{1,50}, 
                           {2,100}, 
                           {4,125}, 
                           {8,150}, 
                           {16,175}, 
                           {32,200}, 
                           {64,225}, 
                           {128,250}, 
                           }; 
   int main() 
   { 
      uint8_t Look;
      DDRB = 0x00;
      DDRC = 0xFF;
		
     while(1)
     {
      Look = PINB;      

      for(uint8_t i=0;i<8;i++)    
        if(Look == pgm_read_byte(&samp[i].Volt)) 
        { 
         PORTC = pgm_read_byte(&samp[i].rpm); 
         break; 
        } 
      } 
    return 0; 
   } 

~~~Decoder ~~~

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

hey.....wats wrong that code(above).......

~~~Decoder ~~~

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

Quote:

wats wrong that code(above)

Nothing that I can see. What do YOU think is wrong with it?

(but note that it only accounts for the situation where ONE of all 8 bits in PINB is set)

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

And what AVR are you using? (And take a look at FAQ#5 in Cliffs post.)

Regards,
Steve A.

The Board helps those that help themselves.

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

Quote:
Nothing that I can see. What do YOU think is wrong with it?
the "if" condition is not executing....that means its not going into if loop..Can you tell me why?
Quote:
And what AVR are you using?
I'm using AVR studio 4

~~~Decoder ~~~

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

You are reading in the bits from PORTB, but you don't set the pin pullups. Unless all of PORTB is going to an external device which pulls the pins to both rails, you'll end up with many of the pins floating to random values.

Secondly, your code is assuming one-hot encoding, that is only one PORTB pin can be high at one time. If more than one pin goes high at the same time, your code will do nothing.

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

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

Quote:
Unless all of PORTB is going to an external device which pulls the pins to both rails, you'll end up with many of the pins floating to random values.
K, i made pull-ups high and then also .....is there any wrong in that if loop...?

~~~Decoder ~~~

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

Quote:
Secondly, your code is assuming one-hot encoding, that is only one PORTB pin can be high at one time. If more than one pin goes high at the same time, your code will do nothing.
Ya, you are right, i have coded reversely....thanx

~~~Decoder ~~~

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

i have one more question.......can we load the data from a file or from memory location

~~~Decoder ~~~

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

Quote:

can we load the data from a file or from memory location

Huh? You are loading the data from a memory location. From a flash memory location, to be precise. To load the data from a file you would need code for some sort of file system, and a place for that file system to reside, eg a part of the flash memory, an external flash, a SD card etc.. Here's one file system implementation that gets mentioned quite often here: http://elm-chan.org/fsw/ff/00ind...

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

Quote:
you would need code for some sort of file system, and a place for that file system to reside, eg a part of the flash memory, an external flash, a SD card etc..
wat do u mean by that...
because can we load from external memory like SD card..
i want the info because i project is, it takes readings from the sensor...those readings should be saved in look-up table and from there we have read the data...can we do that?

~~~Decoder ~~~

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

Quote:

wat do u mean by that...

What I wrote. (If I had meant something else, I would have written something else.)

You want to write and read a SD card. You need some simple or not so simple way to organize "chunks of data" on that SD card. You need to keep track of how your data is organized into the physical structure. Software to do these kinds of things generally go under the name "file system".

And I bet that sooner or later you will want the AVR to write data to the SD card, and then you want to read that SD card in a PC. Or vice versa. Now the options are narrowing - you need a file system that your PC can understand, and the ubiquitous one (while perhaps technically inferior) id the FAT system (supported by ElmChan's stuff that I pointed to above). Most SD cards come with a FAT system preformatted.

Suggested readings:
http://en.wikipedia.org/wiki/Fil...
http://en.wikipedia.org/wiki/SD_...

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

I guess it is possible to use SD/MMC without a filing system - just using raw sectors and your own, homebrew, allocation strategy but you then lose the ability to simply plug the card into a PC and drag/drop files to/from it. As you probably need SOME kind of space allocator anyway then why not use the well known FAT system that was invented by Microsoft for floppy disks. A Johan says, the ready made AVR solution to that is Chan's FatFs or, for small resource AVRs, PetitFs.

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

Decoder wrote:
Quote:
Nothing that I can see. What do YOU think is wrong with it?
the "if" condition is not executing....that means its not going into if loop..Can you tell me why?

What is "if loop"?

How do you know that it is "not executing"? What are the expected behaviour and in what way are the actual symptoms different from it?

Meantime, zip up your whole project directory and post it here.

JW