Created
May 26, 2014 02:44
-
-
Save hadleyrich/10ce7659498d5cf21015 to your computer and use it in GitHub Desktop.
EEPROM eample
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Example of how to use the avr-libc EEPROM functions; | |
* http://www.nongnu.org/avr-libc/user-manual/group__avr__eeprom.html | |
* | |
*/ | |
#define F_CPU 32000000 | |
#include <avr/io.h> | |
#include <avr/interrupt.h> | |
#include <avr/eeprom.h> | |
#include <util/delay.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <string.h> | |
#include "usart.h" | |
#include "clock.h" | |
#define STORED_STRING_LEN 13 | |
uint8_t eeprom_stored_byte EEMEM = 0; | |
uint32_t eeprom_stored_long EEMEM = 0; | |
float eeprom_stored_float EEMEM = 0.0; | |
char eeprom_stored_string[STORED_STRING_LEN] EEMEM = ""; | |
uint8_t stored_byte EEMEM = 0; | |
uint32_t stored_long EEMEM = 0; | |
float stored_float EEMEM = 0.0; | |
char stored_string[STORED_STRING_LEN] EEMEM = ""; | |
int main(void) { | |
sysclk_init(); | |
sei(); | |
// configure the usart1 (attached to USB) for 9600,8,N,1 to | |
// show debugging info, see usart examples for explanation | |
usart_init(1, 128, 128); | |
usart_conf(1, 9600, 8, none, 1, 0, NULL); | |
usart_map_stdio(1); | |
usart_run(1); | |
// wait for EEPROM to be ready | |
eeprom_busy_wait(); | |
// read values from EEPROM | |
stored_byte = eeprom_read_byte(&eeprom_stored_byte); // single byte | |
stored_long = eeprom_read_dword(&eeprom_stored_long); // long integer | |
stored_float = eeprom_read_float(&eeprom_stored_float); // floating point | |
eeprom_read_block((void*)&stored_string, (void*)&eeprom_stored_string, STORED_STRING_LEN); // arbitrary block | |
_delay_ms(1000); | |
puts("Values read after power on;\r\n"); | |
printf("stored_byte = %u\r\n", stored_byte); | |
printf("stored_long = %lu\r\n", stored_long); | |
printf("stored_float = %f\r\n", stored_float); | |
_delay_ms(100); | |
printf("stored_string = %s\r\n", stored_string); | |
stored_byte = 21; | |
stored_long = 1234567890; | |
stored_float = 27.72; | |
strncpy(stored_string, "Hello World!", STORED_STRING_LEN); | |
_delay_ms(100); | |
puts("\r\nValues now set to;\r\n"); | |
printf("stored_byte = %u\r\n", stored_byte); | |
printf("stored_long = %lu\r\n", stored_long); | |
printf("stored_float = %f\r\n", stored_float); | |
_delay_ms(100); | |
printf("stored_string = %s\r\n", stored_string); | |
// update values to EEPROM | |
// the _update_ functions will test whether the existing EEPROM value is the same | |
// before writing to save flash wear, there are also _write_ functions which do not | |
eeprom_update_byte(&eeprom_stored_byte, stored_byte); | |
eeprom_update_dword(&eeprom_stored_long, stored_long); | |
eeprom_update_float(&eeprom_stored_float, stored_float); | |
eeprom_update_block((void*)&stored_string, (void*)&eeprom_stored_string, STORED_STRING_LEN); | |
_delay_ms(100); | |
printf("\r\nValues written to EEPROM\r\n"); | |
printf("========================\r\n\r\n"); | |
_delay_ms(100); // let the characters go out the usart | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Where is the toolchain unpacked? | |
TOOLBASE ?= /usr/local/share/avr8-gnu-toolchain-linux_x86_64 | |
# What MCU do we have on this board? | |
MCU ?= atxmega64d4 | |
# Port that Kakapo popped up on | |
PORT ?= /dev/ttyUSB1 | |
# Application name | |
APP = eeprom | |
# If using libkakapo, uncomment | |
LIBKAKAPO = -lkakapo | |
#Tools we'll need | |
CC = $(TOOLBASE)/bin/avr-gcc | |
AVRDUDE = /usr/bin/avrdude | |
OBJCOPY = $(TOOLBASE)/bin/avr-objcopy | |
CFLAGS = -Os --std=c99 -funroll-loops -funsigned-char -funsigned-bitfields -fpack-struct -fshort-enums -Wstrict-prototypes -Wall -mcall-prologues -I. -I../../ | |
# This is used specifically for printf in the EEPROM example | |
CFLAGS += -Wl,-u,vfprintf -lprintf_flt -lm | |
CFLAGS += -mmcu=$(MCU) | |
INCLUDE = -L../../ | |
OBJ = $(patsubst %.c,%.o,$(wildcard *.c)) | |
build: $(APP).hex | |
program: $(APP).hex | |
$(AVRDUDE) -p $(MCU) -c avr109 -P $(PORT) -b 115200 -U flash:w:$(APP).hex -e | |
$(APP).hex: $(APP).elf | |
$(OBJCOPY) -O ihex -j .text -j .data $< $@ | |
$(APP).elf: $(OBJ) | |
$(CC) $(CFLAGS) $(INCLUDE) $^ -o $@ $(LIBKAKAPO) | |
%.o: %.c %.h Makefile | |
$(CC) -c $(CFLAGS) $< -o $@ | |
clean: | |
rm -f $(OBJ) $(APP).hex $(APP).elf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment