Wire.begin();// join i2c bus (address optional for master)
//setup spi
pinMode(SS,OUTPUT);
SPI.begin();
SPI.setBitOrder(MSBFIRST);
//SPI.setClockDivider(SPI_CLOCK_DIV128 );
//begin serial
Serial.begin(115200);
Serial.setTimeout(65535);
// wait for the serial connection
while(!Serial){}
// set threshs to the default value
if(useDAC){
analogWrite(DAC0,thresh1_default);
analogWrite(DAC1,thresh2_default);
Serial.println("Set threshold DAC CH1 to "+String(thresh2_default)+" AND CH2 to: "+String(thresh2_default));
}else{
DacUpd(MCP4725_ADD0,thresh1_default);
DacUpd(MCP4725_ADD1,thresh2_default);
}
// directly setup the interrupts
toggle_coinc_interrupts(coinc_interruts_enabled);
}
voidloop(){
Serial.println("Input a command!");
Serial.println("[1= set thresh1 & thresh2, 2= set thrshold1, 3= set threshold2, 4= set HV ch1, 5 = set HV ch2, 6= set HV CH1 and CH2, 7 = set HV enables,\n 8= generic 2 part command, 9 = generic read, 10 = toggle coincidence interrupt, 11 = toggle single interrupts]");
intcmd=readIntFromSerial();
switch(cmd){
case1:
{
Serial.println("Set a threshold value CH1 AND CH2 [1,4096]: ");
intvalue=readIntFromSerial();
//setThreshold(3, value);
if(useDAC){
analogWrite(DAC0,value);
analogWrite(DAC1,value);
Serial.println("Set threshold DAC CH1 AND CH2 to: "+String(value));
}else{
DacUpd(MCP4725_ADD0,value);
DacUpd(MCP4725_ADD1,value);
}
break;
}
case2:
{
Serial.println("Set a threshold value CH1[1,4096]: ");
intvalue=readIntFromSerial();
//setThreshold(3, value);
if(useDAC){
analogWrite(DAC0,value);
Serial.println("Set threshold DAC CH1 to: "+String(value));
}else{
DacUpd(MCP4725_ADD0,value);
}
break;
}
case3:
{
Serial.println("Set a threshold value CH2[1,4096]: ");
intvalue=readIntFromSerial();
//setThreshold(3, value);
if(useDAC){
analogWrite(DAC1,value);
Serial.println("Set threshold DAC CH2 to: "+String(value));
}else{
DacUpd(MCP4725_ADD1,value);
}
break;
}
case4:
{
//MSB
Serial.println("Setting CH1 MSB to 0");
intsendValue=0;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV CH2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(1);
spi_exchange(sendValue);
//LSB
Serial.println("Input a voltage value CH1 LSB[1=highest,255=lowest,56=nominal]");
sendValue=readIntFromSerial();
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(2);
spi_exchange(sendValue);
break;
}
case5:
{
//MSB
Serial.println("Setting CH2 MSB to 0");
intsendValue=0;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(3);
spi_exchange(sendValue);
//LSB
Serial.println("Input a voltage value CH2 LSB[1=highest,255=lowest,56=nominal]");
sendValue=readIntFromSerial();
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(4);
spi_exchange(sendValue);
break;
}
case6:
{
Serial.println("Input a voltage value CH1 AND CH2 LSB[1=highest,255=lowest,56=nominal]");
intboth_sendValue=readIntFromSerial();
Serial.println("Setting CH1 MSB to 0");
intsendValue=0;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV CH2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(1);
spi_exchange(sendValue);
//LSB
Serial.println("Setting CH1 LSB to "+String(both_sendValue));
sendValue=both_sendValue;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(2);
spi_exchange(sendValue);
Serial.println("Setting CH2 MSB to 0");
sendValue=0;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(3);
spi_exchange(sendValue);
//LSB
Serial.println("Setting CH2 LSB to "+String(both_sendValue));
sendValue=both_sendValue;
//if (sendValue < HV_MAX_VAL) {
// Serial.print("HV Value is too high! Setting HV ch2 to:");
// Serial.println(HV_MAX_VAL);
//send the command code
spi_exchange(4);
spi_exchange(sendValue);
break;
}
case7:
{
Serial.println("disabling HV");
spi_exchange(11);
spi_exchange(0);
Serial.println("enabling HV");
spi_exchange(11);
spi_exchange(3);
break;
}
//generic command sender
case8:
{
Serial.println("Input a command to send");
Serial.println(" 01 WRITE MSB(16 high bits) HV1_DUTY_CYCLE");