Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define TWI_FREQ 100000
- #define SLA_ADDRES 104
- #ifndef F_CPU
- #define F_CPU 16000000UL
- #endif // !F_CPU
- #ifndef TWI_FREQ
- #define TWI_FREQ 100000
- #endif // !TWI_FREQ
- #ifndef I2C
- #define I2C
- void i2c_initialise(void)
- {
- // Set pre-scalers in TWPS1 TWPS0 (no pre-scaling)
- TWSR = 0B00000000;
- // Set bit rate
- TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
- // Writing a logic one to this bit shuts down the TWI by stopping the clock to the module. When waking up the TWI again, the TWI should be re initialized to ensure proper operation
- PRR &= (0 << PRTWI);
- // Enable TWI and interrupt
- TWCR = (1 << TWIE) | (1 << TWEN);
- }
- void i2c_start(void)
- {
- // Send start condition
- TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
- // Wait until the TWI is ready for the next operation
- while (!(TWCR & (1 << TWINT)));
- }
- void i2c_send_sla_adress(uint8_t addres)
- {
- // Write SLA+R/W in TWI data register
- TWDR = addres;
- // Enable TWI and clear interrupt flag
- TWCR = (1 << TWINT) | (1 << TWEN);
- // Wait until TWI finish its current job
- while (!(TWCR & (1 << TWINT)));
- }
- void i2c_send_data(uint8_t data)
- {
- // Copy data in TWI data register
- TWDR = data;
- TWCR = (1 << TWINT) | (1 << TWEN);
- // Wait until TWI finish its current job
- while (!(TWCR & (1 << TWINT)));
- }
- void ic2_stop(void)
- {
- // Send start condition
- TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
- // Wait until the TWI is ready for the next operation
- while (!(TWCR & (1 << TWSTO)));
- }
- void i2c_Ack(void)
- {
- // Enable TWI and clear interrupt flag
- TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA);
- // Wait until TWI finish its current job
- while (!(TWCR&(1 << TWINT)));
- }
- void i2c_Nack(void)
- {
- // Enable TWI and clear interrupt flag
- TWCR = (1 << TWEN) | (1 << TWINT);
- // Wait until TWI finish its current job
- while (!(TWCR&(1 << TWINT)));
- }
- uint8_t i2c_get_data(uint8_t sla_address, uint8_t register_address)
- {
- i2c_start();
- // Write mode.
- i2c_send_sla_adress(sla_address << 1);
- i2c_send_data(register_address);
- i2c_start();
- // Read mode.
- i2c_send_sla_adress((sla_address << 1) | 0B00000001);
- i2c_Nack();
- ic2_stop();
- return TWDR;
- }
- #endif // !I2C
- float temp;
- void setup() {
- // put your setup code here, to run once:
- Serial.begin(9600);
- i2c_initialise();
- i2c_start();
- i2c_send_sla_adress(SLA_ADDRES << 1);
- i2c_send_data(58);
- i2c_start();
- i2c_send_sla_adress((SLA_ADDRES << 1) | 0B00000001);
- i2c_Ack(); // Address 0.
- Serial.println(TWDR, DEC);
- i2c_Ack(); // Address 1.
- Serial.println(TWDR, DEC);
- i2c_Ack(); // Address 2.
- Serial.println(TWDR, DEC);
- i2c_Ack(); // Address 3.
- Serial.println(TWDR, DEC);
- ic2_stop(); // Address 7.
- }
- void loop() {
- // put your main code here, to run repeatedly:
- temp = i2c_get_data(SLA_ADDRES, 8) + (i2c_get_data(SLA_ADDRES, 9) / 10 );
- Serial.print(temp, DEC);
- Serial.print("°C\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement