Mega8内部带有256个字节的E2PROM,省去了用外部E2PROM扩/展的麻烦,读写较为简单,程序如下:
/************************************************
eeprom.h如下:
*********************************************/
#ifndef _eeprom_
#define _eeprom_
void EE_Write(unsigned char writeaddr,unsigned char writedata);
unsigned char EE_Read(unsigned char readaddr);
#endif;
/*************************************************
eeprom.c
*************************************************/
#include
#include "eeprom.h"
void EE_Write(unsigned char writeaddr,unsigned char writedata)
{
while(EECR&(1<EEAR=writeaddr;
EEDR=writedata;
EECR|=(1<EECR|=(1<}
unsigned char EE_Read(unsigned char readaddr)
{
while(EECR&(1EEAR=readaddr;
EECR=(1<return EEDR;
}
/*********************************************************
main.c
*****************************************************/
#include
#include "eeprom.h"
#include "uart.h"
unsigned char table[16]={"0123456789abcdef"};
void DelayXms(unsigned int x)//延迟xms
{
unsigned int m;
while(x--)
{
m=1275;
while(m--);
}
}
void main(void)
{
unsigned int i;
unsigned char j;
Uart_init();//串口初始化。
EE_Write(0x01,0x11);//内部E2PROM地址0X01,写入数据0X11;
DelayXms(5);
Prints("Put 0x11 to addr 0x01...",1);
EE_Write(0x0f,0x06);//内部E2PROM地址0X23,写入数据0X06;
Prints("Put 0x06 to addr 0x23...",1);
DelayXms(5);
for(i=0x10;i<0x50;i++)//写0x55到地址0x10~0x4f。
{
EE_Write(i,0x55);
DelayXms(5);
}
Prints("Read all the data of each addr as:",1);
for(i=1;i<257;i++)//读取整个E2PROM的值。
{
j=EE_Read(i-1);
Send_abyte(table[j/16]);
Send_abyte(table[j%16]);
Send_abyte(' ');
if(i%16==0)//每行显示16个数。
{
Send_abyte(10);
Send_abyte(13);
}
}
while(1);
}
结果用了串口调试,结果截图如下:
不知什么原因,显示不出来了,要的下载工程附件,到里面看看,呵呵!
补上图:
工程附件如下:
文章评论(0条评论)
登录后参与讨论