CosmicPiV2I2CSPItest.ino-fd.../CosmicPiV2I2CSPItest.ino

256 lines
7.3 KiB
Arduino
Raw Normal View History

2018-07-30 20:52:02 +00:00
#include <SPI.h>
#include <Wire.h>
2018-08-12 13:06:38 +00:00
//updated to include a whole bunch of extra functions 120818
2018-07-30 20:52:02 +00:00
/* 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
2018-08-12 13:06:38 +00:00
13 READ ADC CH1
14 READ ADC CH2
2018-07-30 20:52:02 +00:00
2018-08-12 13:06:38 +00:00
15 WRITE PWM PERIOD MSB
16 WRITE PWM PERIOD LSB
17 READ PWM PERIOD MSB
18 READ PWM PERIOD LSB
2018-07-30 20:52:02 +00:00
*/
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);
2018-08-12 13:06:38 +00:00
Serial.setTimeout(65535);
2018-07-30 20:52:02 +00:00
}
void loop() {
Serial.println("Input a command!");
2018-08-12 13:06:38 +00:00
Serial.println("[1= set thrshold1; 2= set threshold2, 3= set HV ch1, 4 = set HV ch2, 5 = set HV enables, 6= generic 2 part command, 7 = generic read]");
2018-07-30 20:52:02 +00:00
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:
{
2018-08-12 13:06:38 +00:00
//MSB
Serial.println("Input a voltage value Ch1 msb[1=highest,255=lowest,56=nominal]");
2018-07-30 20:52:02 +00:00
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
2018-08-12 13:06:38 +00:00
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
2018-07-30 20:52:02 +00:00
spi_exchange(2);
spi_exchange(sendValue);
break;
}
case 4:
{
2018-08-12 13:06:38 +00:00
//MSB
Serial.println("Input a voltage value Ch1 msb[1=highest,255=lowest,56=nominal]");
2018-07-30 20:52:02 +00:00
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
2018-08-12 13:06:38 +00:00
spi_exchange(3);
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
2018-07-30 20:52:02 +00:00
spi_exchange(4);
spi_exchange(sendValue);
break;
}
case 5:
{
2018-08-01 11:47:39 +00:00
Serial.println("disabling HV");
spi_exchange(11);
spi_exchange(0);
2018-07-30 20:52:02 +00:00
Serial.println("enabling HV");
spi_exchange(11);
spi_exchange(3);
break;
}
2018-08-12 13:06:38 +00:00
//generic command sender
case 6:
{
Serial.println("Input a command to send");
Serial.println(" 01 WRITE MSB(16 high bits) HV1_DUTY_CYCLE");
Serial.println(" 02 WRITE LSB(16 low bits) HV1_DUTY_CYCLE");
Serial.println(" 03 WRITE MSB(16 high bits) HV2_DUTY_CYCLE");
Serial.println(" 04 WRITE LSB(16 low bits) HV2_DUTY_CYCLE");
Serial.println(" 05 WRITE 16 bits LED_DUTY_CYCLE");
Serial.println(" 11 WRITE STATUS");
Serial.println(" 15 WRITE PWM PERIOD MSB");
Serial.println(" 16 WRITE PWM PERIOD LSB");
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(sendValue);
Serial.println("Input a value to send");
sendValue = readIntFromSerial();
spi_exchange(sendValue);
break;
}
//generic command sender
case 7:
{
Serial.println("Input a value to read");
Serial.println(" 06 READ MSB(16 high bits) HV1_DUTY_CYCLE");
Serial.println(" 07 READ LSB(16 low bits) HV1_DUTY_CYCLE");
Serial.println(" 08 READ MSB(16 high bits) HV2_DUTY_CYCLE");
Serial.println(" 09 READ LSB(16 low bits) HV2_DUTY_CYCLE");
Serial.println(" 10 READ 16 bits LED_DUTY_CYCLE");
Serial.println(" 12 READ STATUS");
Serial.println(" 13 READ ADC CH1");
Serial.println(" 14 READ ADC CH2");
Serial.println(" 17 READ PWM PERIOD MSB");
Serial.println(" 18 READ PWM PERIOD LSB");
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(sendValue);
spi_exchange(0); //send a null to get the value we wanted back.
break;
}
2018-07-30 20:52:02 +00:00
}
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();
2018-08-12 13:06:38 +00:00
//while (val == 0){
//delay(100);
//val = Serial.parseInt();
//}
2018-07-30 20:52:02 +00:00
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...
2018-08-12 13:06:38 +00:00
Serial.println(Wire.endTransmission());
2018-07-30 20:52:02 +00:00
Serial.println("I2C send command:" + String(address) + " data:" + String(packet1));
}