byte ChecksumInput[44]; byte regAddr; byte regData; int c; int m; byte Numobjects = 0x02; byte P1_THR_0 = 0x88; byte P1_THR_1 = 0x88; byte P1_THR_2 = 0x88; byte P1_THR_3 = 0x88; byte P1_THR_4 = 0x88; byte P1_THR_5 = 0x88; byte P1_THR_6 = 0x42; byte P1_THR_7 = 0x10; byte P1_THR_8 = 0x84; byte P1_THR_9 = 0x21; byte P1_THR_10 = 0x08; byte P1_THR_11 = 0x40; byte P1_THR_12 = 0x40; byte P1_THR_13 = 0x40; byte P1_THR_14 = 0x40; byte P1_THR_15 = 0x00; byte P2_THR_0 = 0x88; byte P2_THR_1 = 0x88; byte P2_THR_2 = 0x88; byte P2_THR_3 = 0x88; byte P2_THR_4 = 0x88; byte P2_THR_5 = 0x88; byte P2_THR_6 = 0x42; byte P2_THR_7 = 0x10; byte P2_THR_8 = 0x84; byte P2_THR_9 = 0x21; byte P2_THR_10 = 0x08; byte P2_THR_11 = 0x40; byte P2_THR_12 = 0x40; byte P2_THR_13 = 0x40; byte P2_THR_14 = 0x40; byte P2_THR_15 = 0x00; void setup() { Serial.begin(19200, SERIAL_8N2); delay(10); c = 1; m = 1; } /*CHECKSUM FUNCTION*/ byte calcChecksum(byte cmd) { int checksumLoops = 0; cmd = cmd & 0x001F; if(cmd == 16) { ChecksumInput[0] = cmd; ChecksumInput[1] = P1_THR_0; ChecksumInput[2] = P1_THR_1; ChecksumInput[3] = P1_THR_2; ChecksumInput[4] = P1_THR_3; ChecksumInput[5] = P1_THR_4; ChecksumInput[6] = P1_THR_5; ChecksumInput[7] = P1_THR_6; ChecksumInput[8] = P1_THR_7; ChecksumInput[9] = P1_THR_8; ChecksumInput[10] = P1_THR_9; ChecksumInput[11] = P1_THR_10; ChecksumInput[12] = P1_THR_11; ChecksumInput[13] = P1_THR_12; ChecksumInput[14] = P1_THR_13; ChecksumInput[15] = P1_THR_14; ChecksumInput[16] = P1_THR_15; ChecksumInput[17] = P2_THR_0; ChecksumInput[18] = P2_THR_1; ChecksumInput[19] = P2_THR_2; ChecksumInput[20] = P2_THR_3; ChecksumInput[21] = P2_THR_4; ChecksumInput[22] = P2_THR_5; ChecksumInput[23] = P2_THR_6; ChecksumInput[24] = P2_THR_7; ChecksumInput[25] = P2_THR_8; ChecksumInput[26] = P2_THR_9; ChecksumInput[27] = P2_THR_10; ChecksumInput[28] = P2_THR_11; ChecksumInput[29] = P2_THR_12; ChecksumInput[30] = P2_THR_13; ChecksumInput[31] = P2_THR_14; ChecksumInput[32] = P2_THR_15; checksumLoops = 33; } if (cmd == 0) // Burst and listen (Preset1) command { ChecksumInput[0] = cmd; ChecksumInput[1] = Numobjects; checksumLoops = 2; } if( cmd == 9) // Register Read { ChecksumInput[0] = cmd; ChecksumInput[1] = regAddr; checksumLoops = 2; } if (cmd == 10) // Register write { ChecksumInput[0] = cmd; ChecksumInput[1] = regAddr; ChecksumInput[2] = regData; checksumLoops = 3; } uint16_t carry = 0; for (int i = 0; i < checksumLoops; i++) { if ((ChecksumInput[i] + carry) < carry) { carry = carry + ChecksumInput[i] + 1; } else { carry = carry + ChecksumInput[i]; } if (carry > 0xFF) { carry = carry - 255; } } carry = (~carry & 0x00FF); return carry; } /*MAIN LOOP*/ void loop() { Serial.flush(); if (c) { byte buf16[35] = {0x55, 0x10, P1_THR_0, P1_THR_1, P1_THR_2, P1_THR_3, P1_THR_4,P1_THR_5, P1_THR_6,P1_THR_7, P1_THR_8, P1_THR_9, P1_THR_10, P1_THR_11, P1_THR_12, P1_THR_13,P1_THR_14, P1_THR_15,P2_THR_0, P2_THR_1, P2_THR_2, P2_THR_3, P2_THR_4, P2_THR_5, P2_THR_6,P2_THR_7, P2_THR_8, P2_THR_9, P2_THR_10, P2_THR_11, P2_THR_12, P2_THR_13,P2_THR_14, P2_THR_15,calcChecksum(0x10)}; Serial.println("\nWrite Threshold Resistors\n"); Serial.write(buf16,sizeof(buf16));// No Response Operation (Threshold Bulk Write) regAddr = 0x1c; //Frequency register regData = 0x32;//50 byte buf[5] = {0x55, 0x0a, 0x1c, 0x32, calcChecksum(0x0a)};//85,10(cmd-),28,50 Serial.println("\nWrite Frequency Resistor\n"); Serial.write(buf,sizeof(buf));// No Response Operation (Register Write) delay(10); { regAddr = 0x1e; //PULSE_P1 Register regData = 0x30;//0011 0000 byte buf1[5] = {0x55, 0x0a, regAddr, regData, calcChecksum(0x0a)}; Serial.write(buf1,sizeof(buf1));// No Response Operation (Register Write) } { regAddr = 0x23;//FREQ_DIAG Register regData = 0x33;//0011 0011 byte buf1[5] = {0x55, 0x0a, regAddr, regData, calcChecksum(0x0a)}; Serial.write(buf1,sizeof(buf1));// No Response Operation (Register Write) } { regAddr = 0x25;//FVOLT_DEC Register regData = 0x1c;//0001 1100 byte buf1[5] = {0x55, 0x0a, regAddr, regData, calcChecksum(0x0a)}; Serial.write(buf1,sizeof(buf1)); // No Response Operation (Register Write) } { regAddr = 0x1f;//PULSE_P2 Register regData = 0x10;//0001 0000 byte buf1[5] = {0x55, 0x0a, regAddr, regData, calcChecksum(0x0a)}; Serial.write(buf1,sizeof(buf1)); // No Response Operation (Register Write) } c = 0; } // if (m) // { // for (int l=0;l<6;l++) // { Serial.flush(); delay(10); { byte buf2[4] = {0x55, 0x00, Numobjects, calcChecksum(0x00)}; Serial.println("\nSending Burst and Listen Command for Preset 1\n"); Serial.write(buf2,sizeof(buf2)); // No Response Operation (Burst and listen (Preset1)) } delay(10); Serial.flush(); if(m) { byte sensorValue; byte buf4[2] = {0x55, 0x08};//, calcChecksum(0x08)}; Serial.write(buf4,sizeof(buf4)); //Response Operation (All Except Register Read) delay(1); for(int n=0;n<4;n++) { delay(10); if (n == 0) sensorValue = Serial.read(); else Serial.read(); } Serial.println("\nReading the FIRST byte of response frame for SYSTEM DIAGNOSTICS command\n"); Serial.println(sensorValue); m=0; } byte sensorValue; regAddr = 0x40; //Enter any address of a REGMAP Register to read its value byte buf[4] = {0x55, 0x09, regAddr, calcChecksum(0x09)}; Serial.write(buf,sizeof(buf)); //Response Operation (Register Read) delay(1); for(int n=0;n<3;n++) { delay(10); if (n == 0) sensorValue = Serial.read(); else Serial.read(); } Serial.println("\nReading the FIRST byte of response frame for REGISTER READ command\n"); Serial.println(sensorValue); // } // m=0; // } }