Commit a5a1d022 authored by Dave Griffiths's avatar Dave Griffiths

rpi -> atmega works!

parent 5ebb6ea2
......@@ -17,26 +17,27 @@ void I2C_stop(void){
}
ISR(TWI_vect){
// DDRB |= _BV(PB0);
// temporary stores the received data
uint8_t data;
// own address has been acknowledged
if( (TWSR & 0xF8) == TW_SR_SLA_ACK ){
buffer_address = 0xFF;
// clear TWI interrupt flag, prepare to receive next byte and acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
else if( (TWSR & 0xF8) == TW_SR_DATA_ACK ){ // data has been received in slave receiver mode
else if( (TWSR & 0xF8) == TW_SR_DATA_ACK ){ // data has been received in slave receiver mode
// save the received byte inside data
data = TWDR;
// check wether an address has already been transmitted or not
if(buffer_address == 0xFF){
if(buffer_address == 0xFF){
buffer_address = data;
// clear TWI interrupt flag, prepare to receive next byte and acknowledge
TWCR |= (1<<TWIE) | (1<<TWINT) | (1<<TWEA) | (1<<TWEN);
}
......@@ -44,7 +45,7 @@ ISR(TWI_vect){
// store the data at the current address
rxbuffer[buffer_address] = data;
// increment the buffer address
buffer_address++;
......@@ -61,6 +62,8 @@ ISR(TWI_vect){
}
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;
......@@ -68,6 +71,9 @@ ISR(TWI_vect){
if( buffer_address == 0xFF ){
buffer_address = data;
}
PORTD=buffer_address;
// copy the specified buffer address into the TWDR register for transmission
TWDR = txbuffer[buffer_address];
......@@ -87,6 +93,9 @@ ISR(TWI_vect){
}
else{
// if none of the above apply prepare TWI to be addressed again
//PORTD=(TWSR & 0xF8);
TWCR |= (1<<TWIE) | (1<<TWEA) | (1<<TWEN);
}
}
#ifndef F_CPU
#define F_CPU 16000000UL
#endif
#include <avr/io.h>
#include <util/delay.h>
#include <stdlib.h>
......@@ -12,51 +8,45 @@
// buffer used to convert integer to string
char buffer[3];
#define LED PB0
void set_led_state(u8 s)
void set_led_state(unsigned char s)
{
if (s) PORTB|=_BV(LED);
else PORTB&=~_BV(LED);
}
void init_uart(uint16_t baudrate){
uint16_t UBRR_val = (F_CPU/16)/(baudrate-1);
UBRR0H = UBRR_val >> 8;
UBRR0L = UBRR_val;
UCSR0B |= (1<<TXEN0) | (1<<RXEN0) | (1<<RXCIE0); // UART TX (Transmit - senden) einschalten
UCSR0C |= (1<<USBS0) | (3<<UCSZ00); //Modus Asynchron 8N1 (8 Datenbits, No Parity, 1 Stopbit)
else PORTB&=~_BV(LED);
}
void uart_putc(unsigned char c){
#define cbi(sfr, bit) (_SFR_BYTE(sfr) &= ~_BV(bit))
while(!(UCSR0A & (1<<UDRE0))); // wait until sending is possible
UDR0 = c; // output character saved in c
}
void uart_puts(char *s){
while(*s){
uart_putc(*s);
s++;
}
}
int main(void){
init_uart(57600);
I2C_init(0x32); // initalize as slave with address 0x32
int main(void){
I2C_init(0x32<<1); // initalize as slave with address 0x32
// allow interrupts
sei();
while(1){
// convert receiver buffer index 0 to character array and send it via UART
itoa(rxbuffer[0], buffer, 10);
uart_puts(buffer);
_delay_ms(1000);
DDRD |= 0xff;
PORTD = 0xff;
for (int i=0; i<0xFF; i++) {
txbuffer[i]=0x55;
rxbuffer[i]=0x55;
}
int led_counter = 0;
while(1)
{
// if (rxbuffer[0]!=0) {
// DDRB |= _BV( LED );
// set_led_state(1);
// }
_delay_ms(500);
// set_led_state(0);
// PORTD = 0x00;
_delay_ms(500);
// PORTD = 0xff;
// set_led_state(1);
}
return 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