I'm quite a beginner in embedded systems. I was trying to create a simple system that consists of two ATMega32 micro controllers.
One of them stores a value in an EEPROM and the others reads it from the EEPROM and lights a led depending on this value.
I've done the writing part successfully. But the reading part (the other micro controller) is not working and I don't know exactly whats wrong.
Unfortunately I don't know what's exactly wrong so I'll post the schema and errors. (I've included the files too, if you wanted to test it yourself).
Here's my circuit in Proteus (system is not 100% complete, only writing a value and reading is written in code):
Here's my code: (MCU 1 - writing)
(MCU 2 - reading) :
One of them stores a value in an EEPROM and the others reads it from the EEPROM and lights a led depending on this value.
I've done the writing part successfully. But the reading part (the other micro controller) is not working and I don't know exactly whats wrong.
Unfortunately I don't know what's exactly wrong so I'll post the schema and errors. (I've included the files too, if you wanted to test it yourself).
Here's my circuit in Proteus (system is not 100% complete, only writing a value and reading is written in code):


Here's my code: (MCU 1 - writing)
Code:
/*
* basic.c
*
* Created: 12/23/2015 1:43:21 PM
* Author : Rafael
*/
#include <avr/io.h>
char i2c_send_start();
char i2c_send_addr(char);
char i2c_send_data(char);
char i2c_send_stop();
char i2c_check_status(char);
int main(void)
{
DDRA &= (~(1 << PA0) & ~(1 << PA5));
while (1)
{
if(PINA & (1 << PA0))
{
while(PINA & (1 << PA0)){}
if(i2c_send_start())
{
if(i2c_send_addr(0xa0))
{
i2c_send_data(0x00);
if(i2c_send_data(0x01))
{
i2c_send_stop();
}
}
}
}
}
}
char i2c_send_start()
{
TWCR |= (1 << TWSTA) | (1 << TWEN) | (1 << TWINT);
while(!(TWCR & (1 << TWINT))){}
return i2c_check_status(0x08);
}
char i2c_send_addr(char addr)
{
TWDR = addr;
TWCR &= ~(1 << TWSTA);
TWCR |= (1 << TWINT) | (1 << TWEN);
while(!(TWCR & (1 << TWINT))){}
return i2c_check_status(0x18);
}
char i2c_send_data(char data)
{
TWDR = data;
TWCR |= (1 << TWINT) | (1 << TWEN);
while(!(TWCR & (1 << TWINT))){}
return i2c_check_status(0x28);
}
char i2c_send_stop()
{
TWCR |= (1 << TWSTO) | (1 << TWEN);
//start
TWCR |= (1 << TWINT);
while(!(TWCR & (1 << TWINT))){}
return 1;
}
char i2c_check_status(char status)
{
if((TWSR & 0xf8) == status) {
return 1;
} else {
return 0;
}
}
Code:
/*
* receiver.c
*
* Created: 12/23/2015 1:43:21 PM
* Author : Rafael
*/
#include <avr/io.h>
char i2c_send_start();
char i2c_send_addr(char);
void i2c_send_data(char);
char i2c_receive_data();
char i2c_send_stop();
char i2c_check_status(char);
int main(void)
{
DDRA &= ~(1 << PA0);
DDRD = (1 << PD0) | (1 << PD3);
char status = '\0';
while (1)
{
if(PINA & (1 << PA0))
{
while(PINA & (1 << PA0)){}
if(i2c_send_start())
{
if(i2c_send_addr(0xa1))
{
i2c_send_data(0x00);
status = i2c_receive_data();
i2c_send_stop();
}
}
if(0x01 == status) {
PORTD = 1 << PD0;
} else if(0x02 == status) {
PORTD = 1 << PD3;
}
}
}
}
char i2c_send_start()
{
TWCR |= (1 << TWSTA) | (1 << TWEN) | (1 << TWINT);
while(!(TWCR & (1 << TWINT))){}
return i2c_check_status(0x08);
}
char i2c_send_addr(char addr)
{
TWDR = addr;
TWCR &= ~(1 << TWSTA);
TWCR |= (1 << TWINT) | (1 << TWEN);
while(!(TWCR & (1 << TWINT))){}
return i2c_check_status(0x40);
}
void i2c_send_data(char data)
{
TWDR = data;
TWCR |= (1 << TWINT) | (1 << TWEN);
while(!(TWCR & (1 << TWINT))){}
}
char i2c_receive_data()
{
TWCR |= (1 << TWINT) | (1 << TWEN);
while(!(TWCR & (1 << TWINT))){}
return TWDR;
}
char i2c_send_stop()
{
TWCR |= (1 << TWSTO) | (1 << TWEN);
//start
TWCR |= (1 << TWINT);
while(!(TWCR & (1 << TWINT))){}
return 1;
}
char i2c_check_status(char status)
{
if((TWSR & 0xf8) == status) {
return 1;
} else {
return 0;
}
}
Attachments
-
56.4 KB Views: 21