From 0e3aef1cc3f173e02fa86567b3872ac705e6eebe Mon Sep 17 00:00:00 2001 From: James Devine Date: Sat, 21 Jul 2018 19:25:15 +0200 Subject: [PATCH] --- CosmicPiV15dualpsu.ino | 298 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 298 insertions(+) create mode 100644 CosmicPiV15dualpsu.ino diff --git a/CosmicPiV15dualpsu.ino b/CosmicPiV15dualpsu.ino new file mode 100644 index 0000000..9233e9b --- /dev/null +++ b/CosmicPiV15dualpsu.ino @@ -0,0 +1,298 @@ +/* + +Features: + Working: + Set HV + Set Threshold + Print out events + ToDo: + Print precise GPS timestamps for events + HV calibration? + Threshold calibration? + + +Bonus: + A)Temperature + B) Pressure + C) XYZ acceleration + + +*/ + +#include + +#define LED1_pin 12 // green and lower one +#define LED2_pin 11 // red and upper one + +#define TRIGOUT 5 +#define STRIGBOUT A0 +#define STRIGAOUT A5 + +#define MAX5387_PA0_pin A9 +#define MAX5387_PA1_pin A10 +#define MAX5387_PA2_pin A11 +#define ref1_pin A1 +#define ref2_pin A2 + +const int HV_MAX_VAL = 2; +const int HV_MIN_VAL = 255; + +//set up the pins to remap SPI by hand +const int num_devices = 2; +const int SS_pin[num_devices] = {14, 15}; +const int SCK_pin = 17; +//const int MISO_pin = 22; +const int MOSI_pin = 16; + +byte thresh1; +byte thresh2; + +unsigned long timemeasure; +unsigned long timeoffset; + +float instarate; + +unsigned long event_count = 0; + +extern volatile unsigned long timer0_millis; +unsigned long new_value=0; + +void setup() { + //setup analogue writemode + analogWriteResolution(12); + // setup pins + setPinModes(); + setConstantPins(); + attachInterrupt(digitalPinToInterrupt(TRIGOUT), [=] () {printTimeAndPin(TRIGOUT, "SiPM_c");}, RISING); + // setup serial comm + Serial.begin(115200); + // setup initial thresholds + //setThreshold(1, 100); + //setThreshold(2, 100); + analogWrite(DAC0, 1374); + analogWrite(DAC1, 1374); + // set HV to minimum + byte HV_val = 255; + Serial.print("Setting HV to: "); + Serial.println(HV_val); + bitBangCh1(HV_val); + bitBangCh2(HV_val); + timeoffset = 0; +} + +void loop() { + Serial.println("Input a command!"); + Serial.println("[1= set both thrsholds; 2= set HV ch1, 3= set HV ch2]"); + int cmd = readIntFromSerial(); + switch(cmd){ + case 1: + { + Serial.println("Set a threshold value [1,4096]: "); + int value = readIntFromSerial(); + //setThreshold(3, value); + analogWrite(DAC0, value); + analogWrite(DAC1, value); + break; + } + case 2: + { + Serial.println("Input a voltage value [1=highest,255=lowest]"); + byte sendValue = (byte) readIntFromSerial(); + if (sendValue < HV_MAX_VAL){ + Serial.print("HV Value is too high! Setting HV ch1 to:"); + Serial.println(HV_MAX_VAL); + bitBangCh1(HV_MAX_VAL); // Transmit data + } else{ + Serial.print("Setting HV to:"); + Serial.println(sendValue); + bitBangCh1(sendValue); // Transmit data + } + break; + } + case 3: + { + Serial.println("Input a voltage value [1=highest,255=lowest]"); + byte sendValue = (byte) readIntFromSerial(); + if (sendValue < HV_MAX_VAL){ + Serial.print("HV Value is too high! Setting HV ch2 to:"); + Serial.println(HV_MAX_VAL); + bitBangCh2(HV_MAX_VAL); // Transmit data + } else{ + Serial.print("Setting HV to:"); + Serial.println(sendValue); + bitBangCh2(sendValue); // Transmit data + } + break; + } + } + + +} + + + +void setPinModes(){ + // I2C adress pins for the MAX5387 + pinMode(MAX5387_PA0_pin, OUTPUT); + pinMode(MAX5387_PA1_pin, OUTPUT); + pinMode(MAX5387_PA2_pin, OUTPUT); + // Analoge input pins form the threshold + pinMode(ref1_pin, INPUT); + pinMode(ref2_pin, INPUT); + // status LEDs + pinMode(LED1_pin, OUTPUT); + pinMode(LED2_pin, OUTPUT); + // trigger pins + pinMode(TRIGOUT, INPUT); + // HV pins + digitalWrite(SS, HIGH); // Start with SS high + for (int i=0; i 255 || value < 1){ + return; + } else { + value = byte(value); + } + + Wire.begin(); + Wire.beginTransmission(byte(0x28)); // transmit to device #112 + switch(pot_channel){ + case 1: + Serial.print("Setting channel 1 to: "); + Serial.println(value); + Wire.write(byte(B00010001)); //sets value to the first channel + Wire.write(value); + thresh1 = value; + break; + case 2: + Serial.print("Setting channel 2 to: "); + Serial.println(value); + Wire.write(byte(B00010010)); //sets value to the second channel + Wire.write(value); + thresh2 = value; + break; + case 3: + Serial.print("Setting both channels channel to: "); + Serial.println(value); + Wire.write(byte(B00010011)); //sets value to both channels + Wire.write(value); + thresh1 = value; + thresh2 = value; + break; + } + + Wire.endTransmission(); +} + + +byte bitBangCh1(byte _send) // This function is what bitbangs the data +{ + + //reception isn't implemented in this version. + //byte _receive = 0; + //for(int j=0; j