Gast
#501839
hat dieses modul schon mal wer verwendet?
falls ja eventuell auch mit nem ATMEGA?
hätt da nämlich meinen code nur funzt der net so wie er sollte!
wär cool wenn mir da wer helfen könnte!
/*
Two wire serial Interface for the ATMEGA 16 microcontroller.
It is used to communicate with our own EEPROM, which purpose is it
to store the measured data from the amp.
programmed by Christoph Lex
*/
//**********************************************************************
*****
#include<io.h>
#include<iom32.h>
#include<interrupt.h>
#define START 0x08
#define SLA_ACK 0x18
#define DAT_ACK 0x28
#define R_START 0x10
#define R_SLA_ACK 0x40
#define DTA_REC 0x50
void TWI_send(unsigned char ucDatabyte, unsigned char ucSLAVE_W,
unsigned char ucByteaddress);
unsigned char TWI_rec(unsigned char ucSLAVE_R, unsigned char
ucByteaddress, unsigned char ucSLAVE_W);
void Timer_1ms(int Time);
void Timer_10us(int Time);
void main()
{
unsigned char ucByteaddress;
unsigned char ucSLAVE_W;
int i=0;
DDRB = 0xFF;
TWBR = 0x01;
ucSLAVE_W = 0xE0;
ucByteaddress = 0x00;
TWI_send(0x51,ucSLAVE_W,ucByteaddress);
Timer_1ms(50);
// PORTB = ~TWI_rec(0xE1,0x03,0xE0);
}
void TWI_send(unsigned char ucDatabyte, unsigned char ucSLAVE_W,
unsigned char ucByteaddress)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and
initiate START condition
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) != START) // check if the value from the ack
matches the START cond.
PORTB = ~0xFE;
TWDR = ucSLAVE_W; // Slave - address + Write command are
written in the TWI data register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) != SLA_ACK) // check if the value from the ack
matches the SLAVE_address cond.
PORTB = ~0xFD;
TWDR = ucByteaddress; // Byteaddress is written into the data
register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) != DAT_ACK)
PORTB = ~0xFC;
TWDR = ucDatabyte; // Databyte is written into the data
register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) != DAT_ACK) // check if the value from the ack
matches the DATA_send cond.
PORTB = ~0xFB;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO); // transmit STOP condition
PORTB = ~0xFA;
}
unsigned char TWI_rec(unsigned char ucSLAVE_R, unsigned char
ucByteaddress, unsigned char ucSLAVE_W)
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and
initiate START condition
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) == START) // check if the value from the ack
matches the START cond.
PORTB = ~0x0E;
TWDR = ucSLAVE_W; // Slave - address + Write command are
written in the TWI data register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) == SLA_ACK) // check if the value from the ack
matches the SLAVE_address cond.
PORTB = ~0x0D;
TWDR = ucByteaddress; // Byteaddress is written into the data
register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) == DAT_ACK)
PORTB = ~0x0A;
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); // clear interrupt flag and
initiate START condition
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) == R_START) // check if the value from the ack
matches the START cond.
PORTB = ~0x09;
TWDR = ucSLAVE_R; // Slave - address + READ command are written
in the TWI data register
TWCR = (1<<TWINT)|(1<<TWEN); // clear flag and enable TWI
while(!(TWCR & (1<<TWINT))) // wait for interrupt flag
;
if ((TWSR & 0xF8) == R_SLA_ACK) // check if the value from the ack
matches the SLAVE_address cond.
PORTB = ~0x08;
TWCR = (1<<TWINT)|(1<<TWEN);
while(!(TWCR & (1<<TWINT)))
;
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
return TWDR;
}
void Timer_1ms(int Time)
{
int i;
for(i=0;i<Time;i++)
Timer_10us(100);
}
void Timer_10us(int Time)
{
int i;
for(i=0;i<Time*3;i++)
;
}
thx a lot!