Commit 9b4c942c authored by Dave Griffiths's avatar Dave Griffiths

fixed reading -> pi

parent ebcaa3b5
......@@ -18,8 +18,6 @@ void I2C_stop(void){
ISR(TWI_vect){
// DDRB |= _BV(PB0);
// temporary stores the received data
uint8_t data;
......@@ -44,7 +42,7 @@ ISR(TWI_vect){
else{ // if a databyte has already been received
// store the data at the current address
rxbuffer[buffer_address] = data;
i2cbuffer[buffer_address] = data;
// increment the buffer address
buffer_address++;
......@@ -60,10 +58,9 @@ ISR(TWI_vect){
}
}
}
else if( (TWSR & 0xF8) == TW_ST_DATA_ACK ){ // device has been addressed to be a transmitter
else if( (TWSR & 0xF8) == TW_ST_DATA_ACK)
{ // device has been addressed to be a transmitter
PORTD=0xaa;
// copy data from TWDR to the temporary memory
data = TWDR;
......@@ -72,11 +69,11 @@ ISR(TWI_vect){
buffer_address = data;
}
PORTD=buffer_address;
// copy the specified buffer address into the TWDR register for transmission
TWDR = txbuffer[buffer_address];
TWDR = i2cbuffer[buffer_address];
// increment buffer read address
buffer_address++;
......@@ -91,6 +88,27 @@ ISR(TWI_vect){
}
}
else if((TWSR & 0xF8) == TW_ST_DATA_NACK)
{ // device has been addressed to be a transmitter
// copy data from TWDR to the temporary memory
data = TWDR;
// if no buffer read address has been sent yet
if( buffer_address == 0xFF ){
buffer_address = data;
}
//PORTD=buffer_address;
// copy the specified buffer address into the TWDR register for transmission
TWDR = i2cbuffer[buffer_address];
// increment buffer read address
buffer_address++;
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN) |
(0<<TWSTA) | (0<<TWSTO) | (0<<TWWC);
}
else{
// if none of the above apply prepare TWI to be addressed again
......
......@@ -2,8 +2,7 @@
#define I2C_SLAVE_H
volatile uint8_t buffer_address;
volatile uint8_t txbuffer[0xFF];
volatile uint8_t rxbuffer[0xFF];
volatile uint8_t i2cbuffer[0xFF];
void I2C_init(uint8_t address);
void I2C_stop(void);
......
......@@ -147,6 +147,7 @@ AVRDUDE_PORT = COM9 # programmer connected to serial device
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex
#AVRDUDE_WRITE_EEPROM = -U eeprom:w:$(TARGET).eep
AVRDUDE_WRITE_FUSE = -U lfuse:w:0xe2:m
# Uncomment the following if you want avrdude's erase cycle counter.
......@@ -282,7 +283,7 @@ gccversion :
# Program the device.
program: $(TARGET).hex $(TARGET).eep
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM)
$(AVRDUDE) $(AVRDUDE_FLAGS) $(AVRDUDE_WRITE_FLASH) $(AVRDUDE_WRITE_EEPROM) $(AVRDUDE_WRITE_FUSE)
......
......@@ -28,8 +28,7 @@ int main(void){
PORTD = 0xff;
for (int i=0; i<0xFF; i++) {
txbuffer[i]=0x55;
rxbuffer[i]=0x55;
i2cbuffer[i]=0;
}
int led_counter = 0;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment