// -------------------------------------- // MCP9804 (I2C temp sensor) test // // June 2018 by Mej // ///////////////////////////////////////////////////////////////////////////////////////////////////////// const byte En_3V3 = 7; //pin1 PE6-AIN0-Int6 const byte LED0 = A0; //pin36 (led D3 White) const byte LED1 = A1; //pin37 (led D2 Orange) const byte PWM_A = 9; //pin29* PB5-OC1A-ADC12 const byte PWM_B = 10; //pin30* PB3-OC1B-ADC13 const byte PWM_C = 5; //pin31* PC6-OC3A const byte PWM_D = 6; //pin27* PD7-OC4D-ADC10-T0 const byte IO_A = 12; //pin26 PD6-/OC4D-ADC9-T1 const byte IO_B = 4; //pin25 PD4-ICP1-ADC8 const byte IO_C = A3; //pin39 PF4-TCK-ADC4 const byte IO_D = A2; //pin38 PF5-TMS*ADC5 // timing monitoring variables unsigned long t_0 = millis(); // gives time since startup in ms. Overflow after 50 days. unsigned long t = millis(); unsigned long loop_cnt = 0; //loop count const int D = 1000; //delay between loops in ms // nRF24, video tuto and guide through here: https://howtomechatronics.com/tutorials/arduino/arduino-wireless-communication-nrf24l01-tutorial/ #include #include #include RF24 radio(13, 11); // CE on pin 32, CSN on pin 12 const byte address_RFA[6] = "00001"; // Wire and I²C functions #include void set_I2C_register(byte ADDRESS, byte REGISTER, byte VALUE) { Wire.beginTransmission(ADDRESS); Wire.write(REGISTER); Wire.write(VALUE); Wire.endTransmission(); } byte get_I2C_register(byte ADDRESS, byte REGISTER) { Wire.beginTransmission(ADDRESS); Wire.write(REGISTER); Wire.endTransmission(); Wire.requestFrom(ADDRESS, 1); //read 1 byte byte x = Wire.read(); return x; } ///////////////////////////////////////////////////////////////////////////////////////////////////////// //## MCP9804 (I²C thermometer) address //Address const byte MCP9904_1 = B1001100; // 4C const byte MCP9904_22k = B0011100; // 1C 22kOhm //const byte MCP9904_33k = B0111100; // 3C 33kOhm or no resistance //const byte MCP9904s[] = {MCP9904_1, MCP9904_22k, MCP9904_33k}; const byte MCP9904s[] = {MCP9904_1, MCP9904_22k}; const byte MCP9904_cnt =2; byte MCP9904 = MCP9904_22k; byte data1, data2; //Used to store reading from MCP9804 registers float Ts[] = {66.6, 66.6, 66.6, 66.6}; //Used to store the temperature of respectivelly: Internal, Q1, Q2, Q3 void setup() { //MCU pins set up pinMode(En_3V3, OUTPUT); pinMode(LED0, OUTPUT); pinMode(LED1, OUTPUT); pinMode(PWM_A, OUTPUT); pinMode(PWM_B, OUTPUT); pinMode(PWM_C, OUTPUT); pinMode(PWM_D, OUTPUT); pinMode(IO_A, OUTPUT); pinMode(IO_B, OUTPUT); pinMode(IO_C, OUTPUT); pinMode(IO_D, OUTPUT); digitalWrite(En_3V3, 0); digitalWrite(LED0, 0); digitalWrite(LED1, 0); digitalWrite(PWM_A, 0); digitalWrite(PWM_B, 0); digitalWrite(PWM_C, 0); digitalWrite(PWM_D, 0); digitalWrite(IO_A, 0); digitalWrite(IO_B, 0); digitalWrite(IO_C, 0); digitalWrite(IO_D, 0); //Serial set up Serial.begin(115200); while (!Serial); // wait for serial monitor to be monitoring ^^ delay(1000); Serial.println("\nSerial is up at 115200 baud"); //Set and scan I²C bus Wire.begin(); Serial.println("\nI2C Scanning..."); byte error, address; int nDevices =0; for(address = 16; address < 100; address++ ) { // The i2c_scanner uses the return value of // the Write.endTransmisstion to see if // a device did acknowledge to the address. // Copy/Pasted/Tweeked from https://playground.arduino.cc/Main/I2cScanner delay(1); Wire.beginTransmission(address); error = Wire.endTransmission(); if (error == 0) { Serial.print("I2C device found at address 0x"); if (address<16) Serial.print("0"); Serial.print(address,HEX); Serial.println(" !"); nDevices++; } else if (error==4) { Serial.print("Unknown error at address 0x"); if (address<16) Serial.print("0"); Serial.println(address,HEX); } } if (nDevices == 0) Serial.println("No I2C devices found\n"); else Serial.println("done\n"); //Set config of MCP9804s, cf datasheet p19/51 (Register Description) for(int i=0; i