سلام من قصد دارم تا یک عدد از نوع unsigned long int رو در یک EEPROM از طریق SPI بنویسم بعد همون عدد رو بخونم و روی یک LCD2*16 نشون بدم. در واقع این برنامه بخشی از یک پروژه ی دیگر هست. کد نوشته شده در زیر قرار داده شد لطفا اشکال این کد رو بفرمایید.
#include <mega8.h>
#asm
.equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>
#include <spi.h>
void write_eeprom(unsigned long int add , unsigned long int data);
unsigned long int read_eeprom (unsigned long int add);
char buf[100];
void main(void)
{
int i=0, data, get_data;
PORTB=0x00;
DDRB=0x3C;
DDRC=0x01;
SPCR=0x50;
SPSR=0x00;
lcd_init(16);
while (1)
{
i++;
data=55;
write_eeprom(i,data);
delay_ms(100);
data=read_eeprom(i);
sprintf(buf,"value=%d",data);
lcd_puts(buf);
}
}
void write_eeprom(unsigned long int add , unsigned long int data)
{
PORTC.0=0; //Pin CS=0
spi(6); //Write command 06h for set Write enable
PORTC.0=1; //Pin CS=1
PORTC.0=0; //Pin CS=0
spi(2); //Write command 02h for write data to memory
spi((add& 0xff00)>>8); //send high address
spi(add & 0x00ff); //send low address
spi(data); //send data
PORTC.0=1; //Pin CS=1
}
unsigned long int read_eeprom(unsigned long int add)
{
unsigned long int data;
PORTC.0=0; // pin CS=0
spi(3); // Write command 03h for read data from memory
spi((add& 0xff00)>>8); //send high address
spi(add & 0x00ff); // send low address
data=spi(255); //read data
PORTC.0=1; //Pin CS=1
return data;
}