Yahoo Groups archive

AVR-Chat

Index last updated: 2026-04-28 22:41 UTC

Thread

serial to EEPROM on Atmega8?

serial to EEPROM on Atmega8?

2006-06-04 by Ryan

Hi folks,

I'm working on a project using the atmega8, and would like to
implement more features into it.  Basically, there are 3 different
flavors for this project to control, and (now) the end user has to
select one before I send it to them.  

I'd much rather have them program it themselves.  Now, the problem is,
I didn't design it with this in mind.  <grin>

The good news, is I don't think I need to make any hardware changes. 
PD0 and PD1 (RXD and TXD) go directly to the outside world via a
connector.  A max232 interface is needed there, but no big deal.

Now, the question is, can I use these two pins to have a PC/laptop
write to the internal EEPROM?  I thought about uploading new firmware
each time, but then I have to worry about encryption, etc, etc.  So
instead, just write to eeprom a few bytes about how to control the
output pins correctly for whatever flavor the user wants.  Have the
eeprom read at startup or what have you.

Can this be done?  What will it take?  Thanks!

Ryan

Re: [AVR-Chat] serial to EEPROM on Atmega8?

2006-06-04 by Ned Konz

Ryan wrote:
> Hi folks,
> 
> I'm working on a project using the atmega8, and would like to
> implement more features into it.  Basically, there are 3 different
> flavors for this project to control, and (now) the end user has to
> select one before I send it to them.  
> 
> I'd much rather have them program it themselves.  Now, the problem is,
> I didn't design it with this in mind.  <grin>
> 
> The good news, is I don't think I need to make any hardware changes. 
> PD0 and PD1 (RXD and TXD) go directly to the outside world via a
> connector.  A max232 interface is needed there, but no big deal.
> 
> Now, the question is, can I use these two pins to have a PC/laptop
> write to the internal EEPROM?

Not directly, but your program can receive characters and program the 
EEPROM. One trick from the bootloaders is to only receive these 
configuration commands within (say) the first 2 seconds, and to also 
have some way to uniquely identify these commands.

The datasheet describes how to program the EEPROM; if you're using a 
language that has libraries there's probably a library for doing the 
EEPROM stuff already.

For instance, in the avr-libc there is:


#include <avr/io.h>
#include <avr/eeprom.h>
#include <stdint.h>


uint8_t EEMEM myModeSaved = 0;	// byte in EEPROM, defaults to 0

// to read it...

uint8_t myMode = eeprom_read_byte(&myModeSaved);

// to write it...

eeprom_write_byte(&myModeSaved, 3);


 > I thought about uploading new firmware
> each time, but then I have to worry about encryption, etc, etc.  So
> instead, just write to eeprom a few bytes about how to control the
> output pins correctly for whatever flavor the user wants.  Have the
> eeprom read at startup or what have you.
> 
> Can this be done? 

Sure.

> What will it take?  Thanks!

See the above.

Enjoy,
-- 
Ned Konz
ned@bike-nomad.com
http://bike-nomad.com

Re: serial to EEPROM on Atmega8?

2006-06-04 by Ryan

Thanks for the reply.  So it sounds like what I would want to do is
create a startup loop that checks the RS232 for characters, and when
it receives them, have it stuff the needed ones into EEPROM.  Do I
need to worry about things like checksums for the data?  I really am a
newbie when it comes to RS232.  I don't want the EEPROM getting
garbage and blowing things up.

Secondly, after everything gets updated, can I reconfigure the PD0 and
PD1 (rxd and txd) back into outputs?  I'm not sure if they can be
changed on-the-fly in the middle of a program.

I'm currently working with bascom for my sanity...  Grew up with
basic, so it's almost like a comfort food.  :)

Ryan

--- In AVR-Chat@yahoogroups.com, Ned Konz <ned@...> wrote:
Show quoted textHide quoted text
> Not directly, but your program can receive characters and program the 
> EEPROM. One trick from the bootloaders is to only receive these 
> configuration commands within (say) the first 2 seconds, and to also 
> have some way to uniquely identify these commands.

Serial to EEPROM on Atmega8, reconfigure back into outputs

2006-06-04 by Erich Ludwig

Hi Ryan,
no problem to change data direction of ports during run, easy with BASCOM. You can use the CONFIG PORT statement not only in the header of a program. I already had to switch data direction during run because of many lines talking to one single input where always only one sender was set to OUTPUT. That was easy with BASCOM.
Erich
Show quoted textHide quoted text
----- Original Message -----
From: Ryan
Sent: Sunday, June 04, 2006 4:58 PM
Subject: [AVR-Chat] Re: serial to EEPROM on Atmega8?

Thanks for the reply. So it sounds like what I would want to do is
create a startup loop that checks the RS232 for characters, and when
it receives them, have it stuff the needed ones into EEPROM. ; Do I
need to worry about things like checksums for the data? I really am a
newbie when it comes to RS232. I don't want the EEPROM getting
garbage and blowing things up.

Secondly, after everything gets updated, can I reconfigure the PD0 and
PD1 (rxd and txd) back into outputs? I'm not sure if they can be
changed on-the-fly in the middle of a program.

I'm currently working with bascom for my sanity... Grew up with
basic, so it's almost like a comfort food. :)

Ryan

--- In AVR-Chat@yahoogroups.com, Ned Konz wrote:
> Not directly, but your program can receive characters and program the
> EEPROM. One trick from the bootloaders is to only receive these
> configuration commands within (say) the first 2 seconds, and to also
> have some way to uniquely identify these commands.




Re: [AVR-Chat] Re: serial to EEPROM on Atmega8?

2006-06-05 by Ned Konz

Ryan wrote:
> Thanks for the reply.  So it sounds like what I would want to do is
> create a startup loop that checks the RS232 for characters, and when
> it receives them, have it stuff the needed ones into EEPROM.  Do I
> need to worry about things like checksums for the data?  I really am a
> newbie when it comes to RS232.  I don't want the EEPROM getting
> garbage and blowing things up.

Well, it sounds like you only have a few "mode bytes"; if you make sure 
that the data is sane before writing it to EEPROM it would help.

Because it's very common to have initial garbage characters on 
connection or power-up, you should have a recognizable character 
*sequence* that introduces your mode bytes. Ideally you have a handshake 
too.

Assuming that you're going to write a BASIC program for the sending end, 
it would make sense to have a checksum on the whole packet.

Even the weak 8-bit checksum (as opposed to a 16-bit CRC) is typically 
enough, even though it can sometimes miss errors (after all, you have a 
1/256 chance of getting the wrong character for the checksum itself).

You can make the whole process stronger by receiving *and* sending 
characters to make sure that both ends are in sync.

For instance, look at the following hypothetical example. In it, SOH, 
ACK, NAK, STX, ETX are constant byte values; their values are 
unimportant, though ANSI X3.4 (often called ASCII) defined values for 
them back in the ancient days of teletypes. Also, DC2 introduces the 
"hello" message and DC4 introduces the "eeprom" message. (note that the 
ANSI DC3 and DC1 codes are sometimes used as XON/XOFF characters for 
serial line handshaking; this shouldn't matter to you, since you'll be 
turning that interpretation off at the PC end). Note that this 
particular protocol has quite a few unnecessary bytes for the purpose of 
error detection. For instance, framing the message content in 
<STX>..<ETX> bytes lets you make (relatively) sure that you're seeing a 
byte sequence that's intended as a message. The <cksum> here can be 
something as simple as the modulo-256 sum of the bytes (that is, add 
together their values and then use (sum mod 256) as the result. I often 
use the inverse or negation of the checksum to make it a little quicker 
to detect at the micro end.

PC sends "hello" message till it gets a proper acknowledgement:

PC: <STX><DC2><ETX><cksum>
AVR: garbage, maybe...
PC: <STX><DC2><ETX><cksum>

AVR also got garbage, maybe, so sends a NAK:

AVR: <STX><NAK><ETX><cksum>

PC tries yet again:

PC: <STX><DC2><ETX><cksum>

AVR responds with an ACK:

AVR: <STX><ACK><ETX><cksum>

PC sends bytes for eeprom:

PC: <STX><DC4><b1><b2><b3><ETX><cksum>

AVR acknowledges:

AVR: <STX><ACK><ETX><cksum>


I've designed lots of serial protocols; there are a number of tricks and 
ways you can make them better. It's an engineering tradeoff: you have to 
balance the cost of bad communications (trashed EEPROMS? warranty 
returns? injured/dead humans[1]?) vs. the cost of doing a good job (more 
code and development time; slower communications) vs. the probability of 
bad comms.

One thing that helps is to monitor the framing error detect bit (FE, bit 
4 of UCSRA) and the data overrun bit (DOR, bit 3) in the USART status 
register. Parity can also help, but if you use it, you  have the added 
complication of either dealing with 7 bits of data per character, or 
figuring out how to do 9-bit communications on both ends.

Read the datasheet section on the USART.

> Secondly, after everything gets updated, can I reconfigure the PD0 and
> PD1 (rxd and txd) back into outputs?  I'm not sure if they can be
> changed on-the-fly in the middle of a program.

Sure. You can initialize them as outputs (in the DDD register), then 
turn on the RXEN and TXEN bits in the UCSRB register. This will override 
the data direction settings in DDD1 (for TXD) and DDD0 (for RXD). Note 
that you can enable the internal pull-up for RXD by setting the PORTD0 bit.

> I'm currently working with bascom for my sanity...  Grew up with
> basic, so it's almost like a comfort food.  :)

I grew out of BASIC around 1976, though I occasionally have to read it. 
Since there never was a standard for it, each version is a different 
language.

Good luck!

[1] note: it is a bad idea indeed to use simple async communications in 
safety-critical applications because it's so hard to detect hardware 
problems until you need to communicate; also, never rely on software 
alone in a safety-critical system (always have redundant hardware 
monitoring things, or at least a separate, different software system 
using redundant sensors)!

-- 
Ned Konz
ned@bike-nomad.com
http://bike-nomad.com

Re: serial to EEPROM on Atmega8?

2006-06-06 by Ryan

Sounds good.  Now if only I could get VB6 and the AVR to talk.  Trying
to learn too many things at once, I think.

Re: [AVR-Chat] Re: serial to EEPROM on Atmega8?

2006-06-06 by John Samperi

At 02:03 PM 6/06/2006, you wrote:
>if only I could get VB6 and the AVR to talk.

If you get too many VBs (an Australian beer) you won't
be able to talk :-D

I have a VB application on my website but it needs .NET2
and it does talk to AVRs or anything else on the com ports,
it is written with VB 2005 which is a free download from
M$ (for now at least). I actually paid for mine in the form
of Visual Studio standard edition.


Regards

John Samperi

********************************************************
Ampertronics Pty. Ltd.
11 Brokenwood Place Baulkham Hills, NSW 2153 AUSTRALIA
Tel. (02) 9674-6495       Fax (02) 9674-8745
Email: john@ampertronics.com.au
Website  http://www.ampertronics.com.au
*Electronic Design * Custom Products * Contract Assembly
********************************************************

Move to quarantaine

This moves the raw source file on disk only. The archive index is not changed automatically, so you still need to run a manual refresh afterward.