Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Interfacing ATmega32 with FM24C256 Memor

Status
Not open for further replies.

mandriva

Newbie level 4
Joined
Mar 12, 2010
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Location
Iran
Activity points
1,337
Hi My Dear Friends,

I want to store an string on a location of FM24C256 FRAM and then retrieve it and display on a alphanumeric LCD. As this memory uses I2C, I have used CodevisionAVR's I2C example to implement my project. The problem is that it seems write operation is done correctly (I am not completely sure) but when I read the memory starting from the same location as write starting location, I get LCD full of unknown character. I have attached my code and circuit's schematic.

Any helpful idea will get rewards in paradise.

#include <mega32.h>
#include <stdio.h>

// I2C Bus functions
#asm
.equ __i2c_port=0x1B ;PORTA
.equ __sda_bit=0
.equ __scl_bit=1
#endasm
#include <i2c.h>

// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>
#include <delay.h>

#define BUS_ADDRESS 0xa0

unsigned char read_fram(unsigned int address)
{
unsigned char data;
i2c_start();
i2c_write(BUS_ADDRESS);
i2c_write(address);
i2c_start();
i2c_write(BUS_ADDRESS | 1);
data = i2c_read(1);
i2c_stop();
return data;
}

void write_fram(unsigned int address, unsigned char data)
{
i2c_start();
i2c_write(BUS_ADDRESS);
i2c_write(address);
i2c_write(data);
i2c_stop();
delay_ms(20);
}

char* fram_gets(unsigned int start_addr)
{
char *t;
char c;
char str[33];
char i = 0;
lcd_clear();
while ((c = read_fram(start_addr)) != '\n')
{
sprintf(t, "%c", c);
lcd_puts(t);
str[i++] = c;
start_addr++;
}

str = '\n';
return(str);
}

void fram_puts(unsigned int start_addr, char str[])
{
char i = 0;
while (str != '\n')
{
write_fram(start_addr, str);
start_addr++;
i++;
}
}

void main(void)
{
// Declare your local variables here
char *str = "I2C";

// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0xFF;

// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;

// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;

// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// I2C Bus initialization
i2c_init();

// LCD module initialization
lcd_init(16);
lcd_puts(str);

while (1)
{
sprintf(str, "Writing to FRAM.");
lcd_clear();
lcd_puts(str);
delay_ms(1000);

sprintf(str, "This is FRAM test.\n");
fram_puts(0, str);

sprintf(str, "Reading from FRAM.");
lcd_clear();
lcd_puts(str);
delay_ms(1000);

str = fram_gets(0);
lcd_clear();
//lcd_puts(str);
delay_ms(1000);
};
}



[/quote]
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top