Hello,
I have a atmega328 which I want to test with a MLX90614 temperature sensor. Unfortunately the sensor is not available on simulide so I thought of measuring the SCL and SDA lines with an oscillosope.
In the following you can see my code.
But on the simulation nothing happens. Can anyone tell me where my mistake is?
I have a atmega328 which I want to test with a MLX90614 temperature sensor. Unfortunately the sensor is not available on simulide so I thought of measuring the SCL and SDA lines with an oscillosope.
In the following you can see my code.
- Code:
#define F_CPU 1000000UL
#include <avr/io.h>
#include <util/delay.h>
void BusWaitForFlagToBeSet()
{
while(!(TWCR & (1<<TWINT))); // Wait for Flag to be set
}
void BusClearFlagandEnable ()
{
TWCR=(1<<TWINT) | (1<<TWEN); //Clear Flag and Enable
}
void Bus_StartOrRestart ()
{
TWCR= (1<<TWEN) | (1<<TWINT) | (1<<TWSTA);
}
void BusClearFlagandEnablewithACK ()
{
TWCR= (1<<TWINT) | (1<<TWEN) | (1<<TWEA);
}
int main(void)
{
DDRC=0xFF;
//Initalizie Bus
PRR &= ~(1<<PRTWI); // Turn off power reduction so I2C is not disabled
TWCR &= ~(1<<TWIE); // Turn of Interrupt Enable
TWBR=2; // Set the Bit Rate Register at 2 for 1MHz CPU Clock and SCL Clock at 50kHz
TWSR &= ~(1<<TWPS1)| (1<<TWPS0); // No Prescaler
//Start Condition
Bus_StartOrRestart ();
BusWaitForFlagToBeSet();
//Send Slave-Adress+W,only one sensor: 0x00
TWDR=0x00;
BusClearFlagandEnable ();
BusWaitForFlagToBeSet();
// Write Data (Register Adress), Command
TWDR=0x07;
BusClearFlagandEnable ();
BusWaitForFlagToBeSet();
//Repeat Start
Bus_StartOrRestart ();
BusWaitForFlagToBeSet();
//Send Slave Adress and R, one sensor: 0x00
TWDR=0x00;
BusClearFlagandEnable ();
BusWaitForFlagToBeSet();
//Read Data Byte Low
uint8_t data1 =0;
data1=TWDR;
BusClearFlagandEnable ();
BusWaitForFlagToBeSet();
//Read Data Byte High
uint8_t data2 =0;
data2=TWDR;
BusClearFlagandEnable ();
BusWaitForFlagToBeSet();
uint8_t data=(data2<<8)+data1;
//Stop Condition
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
while (1)
{
}
}
But on the simulation nothing happens. Can anyone tell me where my mistake is?