Main Menu
Home
AVR News
Atmel AVR
AVR Development Tools
Valuable Tools
WinAVR toolset
Makefile for WinAVR
AVR Virtual Simulators
Hardware for prototyping
GCC and AVR-GCC
Short introduction to C
AVR-GCC Tutorial
Example AVR Projects
AVR-GCC articles
WinAVR Site Map
Scienceprog FORUM
ScienceProg BLOG
Disclaimer
WinARM Tutorial
Latest comments
Measuring motor speed and...
Dear Administrator The code above...
More...

Programming AVR ADC module...
hi please send me example usart with...
More...

Measuring motor speed and...
main.c: In function...
More...

Running TX433 and RX433 RF...
(Hopefully this is not a re-post). I am...
More...

Running TX433 and RX433 RF...
I just bnought a pair of the RX/TX 433...
More...

Friendly sites

Related Items:

Recommended sites


More ways to use AVR Flash memory using WinAVR PDF Print E-mail
Written by Administrator   
Tuesday, 31 October 2006
Last Updated ( Tuesday, 31 October 2006 )

We've been talking about controlling AVR Flash memory in previous post. We talked how ease we can write and read from flash memory using PROGMEM atribute. But lets see more ways of usage Flash memory in your applications.

Sometimes when you write a code and in some places you are just sending strings to LCD or USART using simple expression like:

SendSTR(“This is a string”);

This seems very simple but not good way to use strings, because any way this string is being hardcoded in to flash memory, but compiler is forced to load this string to SRAM during microcontroller initialization before main() routine starts. And this string stays in RAM. This is ok for one or two strings but when you use significant amounts of strings this way then you really waste your RAM by holding all static unchanging data.

We know that working with flash requires library:

#include <avr/pgmspace.h>

One way is to use PROGMEM atribute that holds your data in flash and doesn't load it to RAM during startup:

const char FlashString[] PROGMEM = "This is a string ";

Using this declaration sending string function could look like:


void SendSTR_P(const char *FlashSTR)  
{
uint8_t i;  
for (i = 0; pgm_read_byte(&pFlashSTR[i]); i++)  
	{       
		sendChar(pgm_read_byte(&pFlashSTR[i]));  
	} 

 


Call this procedure in your program like this:

SendSTR_P(FlashString);

This is the way you have to write your own procedure to send strings stored in Flash memory. But there is another way if you want to use in-line strings – use PSTR macro instead PROGMEM declaration. So you can use:

SendSTR_P(PSTR(“FlashString”));

This macro doesn't allow to work with strings like they are in ram, because pointer points to flash memory not to RAM. But there are few functions that help to work with strings stored in flash memory. They are all stored in pgmspace.h library. Those functions are similar to string functions like memcpy, strncmp but with _P prefix, for instance:

strncmp_P("RAM string", PSTR("Flash string"));

This is a good habit of naming functions that work with flash strings.

In conclusion of this we can say that we have two ways declaring flash strings: using PROGMEM atribute when declaring values or using PSTR macro function and use in-line strings:

const char FlashString[] PROGMEM = "This is a string in flash";

SendSTR_P(FlashString);

or simply use:

SendSTR_P(PSTR(“This is a string in flash”));

First way is more practical when same string is used multiple times and second way with inline flash strings is suitable when string is used only one time.


Related Items:

   

Users' Comments  
 

Average user rating

 


Add your comment
Only registered users can comment an article. Please login or register.

No comment posted



mXcomment 1.0.6 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
< Prev   Next >

© 2008 WinAVR AVR-GCC Tutorial
Joomla! is Free Software released under the GNU/GPL License.