Advertisement
michael_s

Untitled

May 15th, 2025
501
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * ADPD188BI Integrated Optical Module for smoke detection from Analog Devices
  3.  * Data Sheet --> https://www.analog.com/media/en/technical-documentation/data-sheets/adpd188bi.pdf
  4.  * Using I2C Configuration on custom-made PCB
  5.  *
  6.  * Author: Arnau Donate Duch
  7.  *
  8.  * Inspired by work from Edward Mallon and Patricia Beddows (The Cave Pearl Project)
  9.  * https://thecavepearlproject.org/2017/11/03/configuring-i2c-sensors-with-arduino/
  10. */
  11.  
  12. #include <Wire.h>
  13.  
  14. #define ADPD188BI         0x64
  15. #define MODE              0x10
  16. #define SAMPLE_CLK        0x4B
  17.  
  18. #define PROGRAM_MODE_MSB          0b00000000
  19. #define PROGRAM_MODE_LSB          0b00000001
  20. #define NORMAL_MODE_MSB           0b00000000
  21. #define NORMAL_MODE_LSB           0b00000010
  22. #define SAMPLE_CLK_ENABLED_MSB    0b01001100
  23. #define SAMPLE_CLK_ENABLED_LSB    0b10010010
  24.  
  25.  
  26. //SETUP------------------------------------------------------------------------
  27.  
  28. void setup() {
  29.   Serial.begin(115200);
  30.   Wire.begin(6,7);
  31.   ADPD188BI_StartSampling();
  32. }
  33.  
  34.  
  35. //LOOP-------------------------------------------------------------------------
  36.  
  37. void loop() {
  38.   //i2c_readRegisterByte (ADPD188BI,0x60, 2); //read fata from FIFO
  39.   i2c_readRegisterByte (ADPD188BI,0x64, 2); //read fata from FIFO
  40.   //i2c_readRegisterByte (ADPD188BI,0x0, 2); //number of buts registered on FIFO
  41. }
  42.  
  43.  
  44. //FUNCTIONS--------------------------------------------------------------------
  45.  
  46. void ADPD188BI_StartSampling () {
  47.   //Set CLK32K_EN bit to 1 amd force device into programme mode
  48.   i2c_writeRegisterByte (ADPD188BI, SAMPLE_CLK, SAMPLE_CLK_ENABLED_MSB, SAMPLE_CLK_ENABLED_LSB);
  49.   i2c_writeRegisterByte (ADPD188BI, MODE, PROGRAM_MODE_MSB, PROGRAM_MODE_LSB);
  50.  
  51.   //Recomended settings ADPD188BI (Table 26 from Data Sheet)
  52.     i2c_writeRegisterByte (ADPD188BI, 0x11, 0b00110000, 0b01101000);
  53.   //i2c_writeRegisterByte (ADPD188BI, 0x11, 0b00110000, 0b10101001);
  54.   i2c_writeRegisterByte (ADPD188BI, 0x12, 0b00000010, 0b00000000);
  55.   i2c_writeRegisterByte (ADPD188BI, 0x14, 0b00000001, 0b00011101);
  56.   i2c_writeRegisterByte (ADPD188BI, 0x15, 0b00000000, 0b00000000);
  57.   i2c_writeRegisterByte (ADPD188BI, 0x17, 0b00000000, 0b00001001);
  58.   i2c_writeRegisterByte (ADPD188BI, 0x18, 0b00000000, 0b00000000);
  59.   i2c_writeRegisterByte (ADPD188BI, 0x19, 0b00111111, 0b11111111);
  60.   i2c_writeRegisterByte (ADPD188BI, 0x1A, 0b00111111, 0b11111111);
  61.   i2c_writeRegisterByte (ADPD188BI, 0x1B, 0b00111111, 0b11111111);
  62.   i2c_writeRegisterByte (ADPD188BI, 0x1D, 0b00000000, 0b00001001);
  63.   i2c_writeRegisterByte (ADPD188BI, 0x1E, 0b00000000, 0b00000000);
  64.   i2c_writeRegisterByte (ADPD188BI, 0x1F, 0b00111111, 0b11111111);
  65.   i2c_writeRegisterByte (ADPD188BI, 0x20, 0b00111111, 0b11111111);
  66.   i2c_writeRegisterByte (ADPD188BI, 0x21, 0b00111111, 0b11111111);
  67.   i2c_writeRegisterByte (ADPD188BI, 0x22, 0b00110101, 0b00111001);
  68.   i2c_writeRegisterByte (ADPD188BI, 0x23, 0b00110101, 0b00110110);
  69.   i2c_writeRegisterByte (ADPD188BI, 0x24, 0b00010101, 0b00110000);
  70.   i2c_writeRegisterByte (ADPD188BI, 0x25, 0b01100011, 0b00001100);
  71.   i2c_writeRegisterByte (ADPD188BI, 0x30, 0b00000011, 0b00100000);
  72.   i2c_writeRegisterByte (ADPD188BI, 0x31, 0b00000100, 0b00001110);
  73.   i2c_writeRegisterByte (ADPD188BI, 0x35, 0b00000011, 0b00100000);
  74.   i2c_writeRegisterByte (ADPD188BI, 0x36, 0b00000100, 0b00001110);
  75.   i2c_writeRegisterByte (ADPD188BI, 0x39, 0b00100010, 0b11110000);
  76.   i2c_writeRegisterByte (ADPD188BI, 0x3B, 0b00100010, 0b11110000);
  77.   i2c_writeRegisterByte (ADPD188BI, 0x3C, 0b00110001, 0b11000110);
  78.   i2c_writeRegisterByte (ADPD188BI, 0x42, 0b00011100, 0b00110100);
  79.   i2c_writeRegisterByte (ADPD188BI, 0x43, 0b10101101, 0b10100101);
  80.   i2c_writeRegisterByte (ADPD188BI, 0x44, 0b00011100, 0b00110100);
  81.   i2c_writeRegisterByte (ADPD188BI, 0x45, 0b10101101, 0b10100101);
  82.   i2c_writeRegisterByte (ADPD188BI, 0x58, 0b00000101, 0b01000100);
  83.   i2c_writeRegisterByte (ADPD188BI, 0x11, 0b00110000, 0b10101001);
  84.  
  85.   //Start normal Sampling operation
  86.   i2c_writeRegisterByte (ADPD188BI, MODE, NORMAL_MODE_MSB, NORMAL_MODE_LSB);
  87. }
  88.  
  89.  
  90. byte i2c_readRegisterByte (uint8_t deviceAddress, uint8_t registerAddress, int bytes) {
  91.   byte MSB;
  92.   byte LSB;
  93.   int fullByte;
  94.   Wire.beginTransmission(deviceAddress);
  95.   Wire.write(registerAddress);
  96.   Wire.endTransmission();
  97.   Wire.requestFrom(deviceAddress, bytes);
  98.   if (bytes==2){
  99.     MSB = Wire.read();
  100.     LSB = Wire.read();
  101.     fullByte = (((int)MSB) << 8) | LSB;
  102.     //Serial.print("0x");
  103.     //Serial.print(registerAddress,HEX);
  104.     //Serial.print(" = 0x");
  105.     //Serial.print(" = ");
  106.     Serial.println(fullByte,DEC);
  107.   } else if (bytes==1){
  108.     MSB = Wire.read();
  109.     Serial.print("0x");
  110.     Serial.print(registerAddress,HEX);
  111.     Serial.print(" = 0x");
  112.     Serial.println(MSB,HEX);
  113.   } else {
  114.     Serial.println("Number of bytes retrieved not supported.");
  115.   }
  116.   return fullByte;
  117. }
  118.  
  119. byte i2c_writeRegisterByte (uint8_t deviceAddress, uint8_t registerAddress, uint8_t MSB_RegisterByte, uint8_t LSB_RegisterByte){
  120.   byte result;
  121.   Wire.beginTransmission(deviceAddress);
  122.   Wire.write(registerAddress);
  123.   Wire.write(MSB_RegisterByte);
  124.   Wire.write(LSB_RegisterByte);
  125.   result = Wire.endTransmission(); //returns 0 if write operation was successful
  126.   if(result > 0){
  127.     Serial.print(F("FAIL in I2C register write! Error code: "));Serial.println(result);
  128.   }
  129.   return result;    //check the return from Wire.endTransmission() the first time you write to a sensor. if result is 0, then I2C sensor coms are working
  130. }
  131.  
  132. byte i2c_setRegisterBit (uint8_t deviceAddress, uint8_t registerAddress, uint8_t bitPosition, bool state){
  133.   byte registerByte;
  134.   byte result;
  135.   registerByte = i2c_readRegisterByte (deviceAddress, registerAddress,1); // load the current register byte
  136.   if (state) {   // when state = 1
  137.     registerByte |= (1 << bitPosition);   //bitPosition of registerByte now = 1
  138.     //or use bitSet(registerByte, bitPosition);
  139.   }else{
  140.     registerByte &= ~(1 << bitPosition);   // bitPosition now = 0
  141.     //or use bitClear(registerByte, bitPosition);
  142.   }
  143.   // now we load that altered byte back into the register we got it from:
  144.   result = i2c_writeRegisterByte ( deviceAddress, registerAddress, bitPosition, state);
  145.   return result;   // result =0 if the byte was successfully written to the register
  146. }
  147.  
  148. bool i2c_getRegisterBit (uint8_t deviceAddress, uint8_t registerAddress, uint8_t bitPosition){
  149.   byte registerByte;
  150.   registerByte = i2c_readRegisterByte(deviceAddress, registerAddress,1);
  151.   return ((registerByte >> bitPosition) & 0b00000001);  // or use (bitRead(registerByte, bitPosition))
  152. }
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement