SHARE
TWEET

Untitled

a guest May 27th, 2019 78 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3.  
  4. #define TWI_FREQ   100000
  5. #define SLA_ADDRES 104
  6.  
  7. #ifndef F_CPU
  8. #define F_CPU        16000000UL
  9. #endif // !F_CPU
  10.  
  11. #ifndef TWI_FREQ
  12. #define TWI_FREQ     100000
  13. #endif // !TWI_FREQ
  14.  
  15. #ifndef I2C
  16. #define I2C
  17.  
  18. void i2c_initialise(void)
  19. {
  20.   // Set pre-scalers in TWPS1 TWPS0 (no pre-scaling)
  21.   TWSR = 0B00000000;
  22.   // Set bit rate
  23.   TWBR = ((F_CPU / TWI_FREQ) - 16) / 2;
  24.   // 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
  25.   PRR &= (0 << PRTWI);
  26.   // Enable TWI and interrupt
  27.   TWCR = (1 << TWIE) | (1 << TWEN);
  28. }
  29.  
  30. void i2c_start(void)
  31. {
  32.   // Send start condition
  33.   TWCR = (1 << TWINT) | (1 << TWSTA) | (1 << TWEN);
  34.   // Wait until the TWI is ready for the next operation
  35.   while (!(TWCR & (1 << TWINT)));
  36. }
  37.  
  38. void i2c_send_sla_adress(uint8_t addres)
  39. {
  40.   // Write SLA+R/W in TWI data register
  41.   TWDR = addres;
  42.   // Enable TWI and clear interrupt flag
  43.   TWCR = (1 << TWINT) | (1 << TWEN);
  44.   // Wait until TWI finish its current job
  45.   while (!(TWCR & (1 << TWINT)));
  46. }
  47.  
  48. void i2c_send_data(uint8_t data)
  49. {
  50.   // Copy data in TWI data register
  51.   TWDR = data;
  52.   TWCR = (1 << TWINT) | (1 << TWEN);
  53.   // Wait until TWI finish its current job
  54.   while (!(TWCR & (1 << TWINT)));
  55. }
  56.  
  57. void ic2_stop(void)
  58. {
  59.   // Send start condition
  60.   TWCR = (1 << TWINT) | (1 << TWEN) | (1 << TWSTO);
  61.   // Wait until the TWI is ready for the next operation
  62.   while (!(TWCR & (1 << TWSTO)));
  63. }
  64.  
  65. void i2c_Ack(void)
  66. {
  67.   // Enable TWI and clear interrupt flag
  68.   TWCR = (1 << TWEN) | (1 << TWINT) | (1 << TWEA);
  69.   // Wait until TWI finish its current job
  70.   while (!(TWCR&(1 << TWINT)));
  71. }
  72.  
  73. void i2c_Nack(void)
  74. {
  75.   // Enable TWI and clear interrupt flag
  76.   TWCR = (1 << TWEN) | (1 << TWINT);
  77.   // Wait until TWI finish its current job
  78.   while (!(TWCR&(1 << TWINT)));
  79. }
  80.  
  81. uint8_t i2c_get_data(uint8_t sla_address, uint8_t register_address)
  82. {
  83.   i2c_start();
  84.   // Write mode.
  85.   i2c_send_sla_adress(sla_address << 1);
  86.   i2c_send_data(register_address);
  87.   i2c_start();
  88.   // Read mode.
  89.   i2c_send_sla_adress((sla_address << 1) | 0B00000001);
  90.   i2c_Nack();
  91.   ic2_stop();
  92.   return TWDR;
  93. }
  94.  
  95. #endif // !I2C
  96.  
  97. float temp;
  98.  
  99. void setup() {
  100.   // put your setup code here, to run once:
  101.   Serial.begin(9600);
  102.   i2c_initialise();
  103.   i2c_start();
  104.   i2c_send_sla_adress(SLA_ADDRES << 1);
  105.   i2c_send_data(58);
  106.   i2c_start();
  107.   i2c_send_sla_adress((SLA_ADDRES << 1) | 0B00000001);
  108.   i2c_Ack(); // Address 0.
  109.   Serial.println(TWDR, DEC);
  110.  
  111.   i2c_Ack(); // Address 1.
  112.   Serial.println(TWDR, DEC);
  113.  
  114.   i2c_Ack(); // Address 2.
  115.   Serial.println(TWDR, DEC);
  116.  
  117.   i2c_Ack(); // Address 3.
  118.   Serial.println(TWDR, DEC);
  119.  
  120.   ic2_stop(); // Address 7.
  121.  
  122. }
  123.  
  124. void loop() {
  125.   // put your main code here, to run repeatedly:
  126.   temp = i2c_get_data(SLA_ADDRES, 8) + (i2c_get_data(SLA_ADDRES, 9) / 10 );
  127.   Serial.print(temp, DEC);
  128.   Serial.print("°C\n");
  129.  
  130.  
  131. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top