zockerlein

TinyWire Example

Feb 9th, 2021
869
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /* ATtiny85 as an I2C Master   Ex2        BroHogan                           1/21/11
  2.  * I2C master reading DS1621 temperature sensor. Display to I2C GPIO LED.
  3.  * SETUP:
  4.  * ATtiny Pin 1 = (RESET) N/U                      ATtiny Pin 2 = (D3) N/U
  5.  * ATtiny Pin 3 = (D4) to LED1                     ATtiny Pin 4 = GND
  6.  * ATtiny Pin 5 = SDA on DS1621  & GPIO            ATtiny Pin 6 = (D1) to LED2
  7.  * ATtiny Pin 7 = SCK on DS1621  & GPIO            ATtiny Pin 8 = VCC (2.7-5.5V)
  8.  * NOTE! - It's very important to use pullups on the SDA & SCL lines!
  9.  * DS1621 wired per data sheet. This ex assumes A0-A2 are set LOW for an addeess of 0x48
  10.  * PCA8574A GPIO was used wired per instructions in "info" folder in the LiquidCrystal_I2C lib.
  11.  * This ex assumes A0-A2 are set HIGH for an addeess of 0x3F
  12.  * LiquidCrystal_I2C lib was modified for ATtiny - on Playground with TinyWireM lib.
  13.  * TinyWireM USAGE & CREDITS: - see TinyWireM.h
  14.  */
  15.  
  16. //#define DEBUG
  17. #include <TinyWireM.h>                  // I2C Master lib for ATTinys which use USI
  18. #include <LiquidCrystal_I2C.h>          // for LCD w/ GPIO MODIFIED for the ATtiny85
  19.  
  20. #define GPIO_ADDR     0x3F              // (PCA8574A A0-A2 @5V) typ. A0-A3 Gnd 0x20 / 0x38 for A
  21. #define DS1621_ADDR   0x48              // 7 bit I2C address for DS1621 temperature sensor
  22. #define LED1_PIN         4              // ATtiny Pin 3
  23. #define LED2_PIN         1              // ATtiny Pin 6
  24.  
  25. int tempC = 0;                          // holds temp in C
  26. int tempF = 0;                          // holds temp in F
  27.  
  28. LiquidCrystal_I2C lcd(GPIO_ADDR,16,2);  // set address & 16 chars / 2 lines
  29.  
  30.  
  31. void setup(){
  32. #ifdef DEBUG
  33.   pinMode(LED1_PIN,OUTPUT);
  34.   pinMode(LED2_PIN,OUTPUT);
  35.   Blink(LED1_PIN,2);                    // show it's alive
  36. #endif
  37.   TinyWireM.begin();                    // initialize I2C lib
  38.   Init_Temp();                          // Setup DS1621
  39.   lcd.begin();                           // initialize the lcd
  40.   lcd.backlight();                      // Print a message to the LCD.
  41.   lcd.print("Hello, Temp!");
  42.   delay (2000);
  43. }
  44.  
  45.  
  46. void loop(){
  47.   Get_Temp();                           // read current temperature
  48.   lcd.clear();                          // display it
  49.   lcd.print("C: ");
  50.   lcd.print(tempC,DEC);
  51.   lcd.setCursor(7,0);
  52.   lcd.print("F: ");
  53.   lcd.print(tempF,DEC);
  54. #ifdef DEBUG
  55.   Blink(LED1_PIN,tempC/10);             // blink 10's of temperature on LED 1
  56.   delay (1000);
  57.   Blink(LED2_PIN,tempC%10);             // blink 1's of temperature on LED 2
  58. #endif
  59.   delay (4000);                         // wait a few sec before next reading
  60. }
  61.  
  62.  
  63. void Init_Temp(){ // Setup the DS1621 for one-shot mode
  64.   TinyWireM.beginTransmission(DS1621_ADDR);
  65.   TinyWireM.send(0xAC);                 // Access Command Register
  66.   TinyWireM.send(B00000001);            // Using one-shot mode for battery savings
  67.   //TinyWireM.send(B00000000);          // if setting continious mode for fast reads
  68.   TinyWireM.endTransmission();          // Send to the slave
  69. }
  70.  
  71.  
  72. void Get_Temp(){  // Get the temperature from a DS1621
  73.   TinyWireM.beginTransmission(DS1621_ADDR);
  74.   TinyWireM.send(0xEE);                 // if one-shot, start conversions now
  75.   TinyWireM.endTransmission();          // Send 1 byte to the slave
  76.   delay(750);                           // if one-shot, must wait ~750 ms for conversion
  77.   TinyWireM.beginTransmission(DS1621_ADDR);
  78.   TinyWireM.send(0xAA);                 // read temperature (for either mode)
  79.   TinyWireM.endTransmission();          // Send 1 byte to the slave
  80.   TinyWireM.requestFrom(DS1621_ADDR,1); // Request 1 byte from slave
  81.   tempC = TinyWireM.receive();          // get the temperature
  82.   tempF = tempC * 9 / 5 + 32;           // convert to Fahrenheit
  83. }
  84.  
  85.  
  86. #ifdef DEBUG
  87. void Blink(byte led, byte times){ // poor man's GUI
  88.   for (byte i=0; i< times; i++){
  89.     digitalWrite(led,HIGH);
  90.     delay (400);
  91.     digitalWrite(led,LOW);
  92.     delay (175);
  93.   }
  94. }
  95. #endif
  96.  
Advertisement
Add Comment
Please, Sign In to add comment