xin chào cả nhà. em dang tập viết lập trình. em sử dụng atmega8 viết trên CodeVisionAVR. em muốn lưu chuỗi vào bộ nhớ eeprom. vấn đề của em là lưu nhiều chuỗi vào eeprom với dịa chỉ xác định. em đang gặp chục chặc là vị trí lưu ko đúng. chuôi lưu sau lại chở về vị trí ban đâu mà ko tiếp tục lưu ở vị trí dịnh sẵn. phần đọc eeprom chỉ dọc được ở vị trí lưu chuỗi đầu các chuỗi sau ở vị trí khác ko odcjd được.
đây là code của em . mong ae mổ sẻ giùm.
#include <mega8.h>
#include <alcd.h>
#include <delay.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
unsigned char *a="01634709835";
unsigned char *b="09179472770";
unsigned char *c="09741413050";
unsigned char *d="abcdefnhyk0";
unsigned char a1[12],a2[12],a3[12],a4[12];
/////////xu ly luu eeprom\\\\\\\\\\\\
unsigned char Data[12];
unsigned int uuu=0;
//////ghi vao eeprom\\\\\\\\\\
void ghi_eeprom(unsigned int Address, unsigned char Data[12])
{
unsigned int nnn=0;
nnn=Address;
while(nnn<=(Address+12))
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address and data registers */
EEAR = nnn;
EEDR = Data[nnn];
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
nnn++;
}
while(uuu<=12)
{
Data[uuu]=' ';
uuu++;
}
}
//////////doc tu eeprom\\\\\\\\\\\\\\\\
unsigned char doc_eeprom(unsigned int Address)
{
unsigned int aaa=0;
aaa=Address;
while(aaa<=(Address+12))
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address register */
EEAR = aaa;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
Data[aaa]=EEDR;
aaa++;
}
return aaa;
}
void main(void)
{
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
lcd_init(16);
#asm("sei")
ghi_eeprom(0,a);
doc_eeprom(0);
strcpy(a1,Data);
ghi_eeprom(12,b);
doc_eeprom(12);
strcpy(a2,Data);
ghi_eeprom(24,c);
doc_eeprom(24);
strcpy(a3,Data);
ghi_eeprom(36,d);
doc_eeprom(36);
strcpy(a4,Data);
while (1)
{
printf("%s",a1);
printf("\r\n");
delay_ms(1000);
printf("%s",a2);
printf("\r\n");
delay_ms(1000);
printf("%s",a3);
printf("\r\n");
delay_ms(1000);
printf("%s",a4);
printf("\r\n");
delay_ms(1000);
}
}
đây là code của em . mong ae mổ sẻ giùm.
#include <mega8.h>
#include <alcd.h>
#include <delay.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
unsigned char *a="01634709835";
unsigned char *b="09179472770";
unsigned char *c="09741413050";
unsigned char *d="abcdefnhyk0";
unsigned char a1[12],a2[12],a3[12],a4[12];
/////////xu ly luu eeprom\\\\\\\\\\\\
unsigned char Data[12];
unsigned int uuu=0;
//////ghi vao eeprom\\\\\\\\\\
void ghi_eeprom(unsigned int Address, unsigned char Data[12])
{
unsigned int nnn=0;
nnn=Address;
while(nnn<=(Address+12))
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address and data registers */
EEAR = nnn;
EEDR = Data[nnn];
/* Write logical one to EEMWE */
EECR |= (1<<EEMWE);
/* Start eeprom write by setting EEWE */
EECR |= (1<<EEWE);
nnn++;
}
while(uuu<=12)
{
Data[uuu]=' ';
uuu++;
}
}
//////////doc tu eeprom\\\\\\\\\\\\\\\\
unsigned char doc_eeprom(unsigned int Address)
{
unsigned int aaa=0;
aaa=Address;
while(aaa<=(Address+12))
{
/* Wait for completion of previous write */
while(EECR & (1<<EEWE))
;
/* Set up address register */
EEAR = aaa;
/* Start eeprom read by writing EERE */
EECR |= (1<<EERE);
/* Return data from data register */
Data[aaa]=EEDR;
aaa++;
}
return aaa;
}
void main(void)
{
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
UCSRA=0x00;
UCSRB=0x98;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;
lcd_init(16);
#asm("sei")
ghi_eeprom(0,a);
doc_eeprom(0);
strcpy(a1,Data);
ghi_eeprom(12,b);
doc_eeprom(12);
strcpy(a2,Data);
ghi_eeprom(24,c);
doc_eeprom(24);
strcpy(a3,Data);
ghi_eeprom(36,d);
doc_eeprom(36);
strcpy(a4,Data);
while (1)
{
printf("%s",a1);
printf("\r\n");
delay_ms(1000);
printf("%s",a2);
printf("\r\n");
delay_ms(1000);
printf("%s",a3);
printf("\r\n");
delay_ms(1000);
printf("%s",a4);
printf("\r\n");
delay_ms(1000);
}
}
Comment