Skip to content

Instantly share code, notes, and snippets.

@pingud98
Last active July 19, 2019 11:42
Show Gist options
  • Save pingud98/ecbc934f43c1ab0c31f59019a809afa9 to your computer and use it in GitHub Desktop.
Save pingud98/ecbc934f43c1ab0c31f59019a809afa9 to your computer and use it in GitHub Desktop.
Cosmic Pi V1.6 first cosmics
//drive the dac channels
/*
Crude code to get the Cosmic Pi up and running, spits out events (coincidence on both channels, after setting the HV to about 28V
and the DAC to 22F (whatever that is).
Makes the light blink (it's not very bright) and prints the event number.
Rate on the test unit seems reasonable, we'll test this against a proper setup over the next few weeks
To be re-written using interrupts for production code.
*/
// the setup function runs once when you press reset or power the board
byte smallpart;
byte bigpart;
int x = 0; //cosmic counter
#include <Wire.h>
int HVVal = 0xFE;
void setup() {
// initialize digital pin LED_BUILTIN as an output.
pinMode(PA6, OUTPUT);
pinMode(PA4, OUTPUT);
pinMode(PA5, OUTPUT);
Serial.begin(9600);
//config SPI pins
pinMode(PC7, OUTPUT);
pinMode(PC8, OUTPUT);
pinMode(PC3, OUTPUT);
pinMode(PB13, OUTPUT);
//set input pins
pinMode(PC12, INPUT);
pinMode(PC11, INPUT);
pinMode(PB10, INPUT);
digitalWrite(PC7, LOW);
setHV(0xAD);
digitalWrite(PC7, HIGH);
digitalWrite(PC8, LOW);
setHV(0xAD);
digitalWrite(PC8, HIGH);
HVVal= HVVal+10;
Wire.setSDA(PB7);
Wire.setSCL(PB8);
Wire.begin();
smallpart = 0x2F; //the lsb part
bigpart = 0x02; // the msb part of the dac value
Serial.write("Hello ");
// Serial.write(HVVal);
//digitalWrite(PA5, HIGH); // turn the LED on (HIGH is the voltage level)
delay(1000); // wait for a second
digitalWrite(PA5, LOW); // turn the LED off by making the voltage LOW
//delay(10000);
int address = 0x60;
Wire.beginTransmission(address);
Wire.write(B00001000); // sends five bytes
Wire.write(bigpart); // sends one byte
Wire.write(smallpart);
Wire.endTransmission();
Wire.beginTransmission(address);
Wire.write(B00000000); // sends five bytes
Wire.write(bigpart); // sends one byte
Wire.write(smallpart);
Wire.endTransmission();
Serial.write("transmitted ");
Serial.write(" looping ");
}
// the loop function runs over and over again forever
void loop() {
//debounce loop
if (digitalRead(PB10) == 0) {
//Serial.write("0");
if (digitalRead(PB10) == 1) {
//low to high print x
//Serial.write("x");
Serial.println(x);
x++;
digitalWrite(PA5, HIGH);
delay(200);
digitalWrite(PA5, LOW);
}
}
/*if PC11 == LOW {
Serial.write("1");
if PC11 == HIGH {
//low to high print x
Serial.write("y");
}
}
*/
//smallpart--;
}
// set the two HV supplies
byte setHV(byte _send) // This function is what bitbangs the data
{
for(int i=0; i<8; i++) // There are 8 bits in a byte
{
digitalWrite(PC3, bitRead(_send, 7-i)); // Set MOSI
//delay(1);
digitalWrite(PB13, HIGH); // SCK high
//bitWrite(_receive, i, digitalRead(MISO_pin)); // Capture MISO
digitalWrite(PB13, LOW); // SCK low
//digitalWrite(MOSI_pin, LOW); // Set MOSI
}
//digitalWrite(SS_pin[j], HIGH); // SS high again
}
//return _receive; // Return the received data
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment