commit 0a3b5543e9e79ed74e5580d6acb83abfa9c28803 Author: James Devine Date: Mon Jul 30 22:52:02 2018 +0200 diff --git a/CosmicPiV2I2CSPItest.ino b/CosmicPiV2I2CSPItest.ino new file mode 100644 index 0000000..0aeb094 --- /dev/null +++ b/CosmicPiV2I2CSPItest.ino @@ -0,0 +1,176 @@ +#include +#include + +/* Cosmic Pi SPI test routines - Master version, slave is commented out below. Runs on an Arduino DUE, + Pinouts: + MOSI - MOSI + MISO - MISO + SCK - SCK + Pin 10 - Pin 10 (SS) + GND - GND + 5V - 5V + + The slave echoes the master byte in the next (16 bit) transmission. + + + + + SPI COMMUNICATION TRANSACTION LIST OF COMMANDS + + WRITE OPERATIONS (FOR EACH WRITE, SLAVE MUST SEND BACK THE VALUE IT RECEIVES) + + 01 WRITE MSB(16 high bits) HV1_DUTY_CYCLE + 02 WRITE LSB(16 low bits) HV1_DUTY_CYCLE + 03 WRITE MSB(16 high bits) HV2_DUTY_CYCLE + 04 WRITE LSB(16 low bits) HV2_DUTY_CYCLE + 05 WRITE 16 bits LED_DUTY_CYCLE + + READ OPERATIONS (FOR EACH READ, SLAVE MUST SEND BACK THE VALUE OF THE RELATED REGISTER) + + 06 READ MSB(16 high bits) HV1_DUTY_CYCLE + 07 READ LSB(16 low bits) HV1_DUTY_CYCLE + 08 READ MSB(16 high bits) HV2_DUTY_CYCLE + 09 READ LSB(16 low bits) HV2_DUTY_CYCLE + 10 READ 16 bits LED_DUTY_CYCLE + + 11 WRITE STATUS + 12 READ STATUS + + +*/ + +byte CMD = 1; + +unsigned int duty_cycle_LED = 81; +unsigned int duty_cycle_channel_A = 100000; +unsigned int duty_cycle_channel_B = 200000; +unsigned int MSB; +unsigned int LSB; +byte val = 0; + +//unsigned int response = 0; +unsigned int answer = 0; +byte PSU_parametrized = 0; + +#define MCP4725_ADD0 0x60 +#define MCP4725_ADD1 0x61 + +int thresh1 = 255;//12 bit +int thresh2 = 255;//12 bit integer +//int threshhigh = 0; + + +//setpoints for the HV +int HVset1 = 56; +int HVset2 = 56; + +void setup() { + //setup i2c and init dac's at a reasonable value + Wire.begin(); // join i2c bus (address optional for master) + DacUpd(MCP4725_ADD0, thresh1); + DacUpd(MCP4725_ADD1, thresh2); + + //setup spi + pinMode(SS, OUTPUT); + SPI.begin(); + SPI.setBitOrder(MSBFIRST); + //SPI.setClockDivider(SPI_CLOCK_DIV128 ); + + //begin serial + Serial.begin(9600); + + +} + +void loop() { + + Serial.println("Input a command!"); + Serial.println("[1= set thrshold1; 2= set threshold2, 3= set HV ch1, 4 = set HV ch2, 5 = set HV enables]"); + int cmd = readIntFromSerial(); + switch (cmd) { + case 1: + { + Serial.println("Set a threshold value Ch1[1,4096]: "); + int value = readIntFromSerial(); + //setThreshold(3, value); + DacUpd(MCP4725_ADD0, value); + break; + } + case 2: + { + Serial.println("Set a threshold value Ch2[1,4096]: "); + int value = readIntFromSerial(); + //setThreshold(3, value); + DacUpd(MCP4725_ADD1, value); + break; + } + case 3: + { + Serial.println("Input a voltage value Ch1[1=highest,255=lowest,56=nominal]"); + int 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; + } + case 4: + { + Serial.println("Input a voltage value Ch2[1=highest,255=lowest,56=nominal]"); + int 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; + } + case 5: + { + Serial.println("enabling HV"); + spi_exchange(11); + spi_exchange(3); + break; + } + } + + delay(500); + + +} + +int spi_exchange(int data) { + digitalWrite(SS, LOW); + int response = SPI.transfer16(data); + Serial.println("SPI Master send command:" + String(data) + " Slave reply:" + String(response)); + digitalWrite(SS, HIGH); + delay(20); + return response; +} + + + +int readIntFromSerial(){ + int val = Serial.parseInt(); + while (val == 0){ + delay(100); + val = Serial.parseInt(); + } + return val; +} + + +void DacUpd(int address, int packet1) +{ + Wire.beginTransmission(address); + Wire.write(64); // cmd to update the DAC + Wire.write(packet1 >> 4); // the 8 most significant bits... + Wire.write((packet1 & 15) << 4); // the 4 least significant bits... + Wire.endTransmission(); + Serial.println("I2C send command:" + String(address) + " data:" + String(packet1)); + + +} \ No newline at end of file