256 lines
No EOL
7.3 KiB
C++
256 lines
No EOL
7.3 KiB
C++
#include <SPI.h>
|
|
#include <Wire.h>
|
|
//updated to include a whole bunch of extra functions 120818
|
|
/* 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
|
|
|
|
13 READ ADC CH1
|
|
14 READ ADC CH2
|
|
|
|
15 WRITE PWM PERIOD MSB
|
|
16 WRITE PWM PERIOD LSB
|
|
17 READ PWM PERIOD MSB
|
|
18 READ PWM PERIOD LSB
|
|
|
|
*/
|
|
|
|
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);
|
|
Serial.setTimeout(65535);
|
|
|
|
}
|
|
|
|
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, 6= generic 2 part command, 7 = generic read]");
|
|
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:
|
|
{
|
|
//MSB
|
|
Serial.println("Input a voltage value Ch1 msb[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(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;
|
|
}
|
|
case 4:
|
|
{
|
|
//MSB
|
|
Serial.println("Input a voltage value Ch1 msb[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(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
|
|
spi_exchange(4);
|
|
spi_exchange(sendValue);
|
|
break;
|
|
}
|
|
case 5:
|
|
{
|
|
Serial.println("disabling HV");
|
|
spi_exchange(11);
|
|
spi_exchange(0);
|
|
|
|
Serial.println("enabling HV");
|
|
spi_exchange(11);
|
|
spi_exchange(3);
|
|
break;
|
|
}
|
|
//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;
|
|
}
|
|
}
|
|
|
|
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...
|
|
Serial.println(Wire.endTransmission());
|
|
Serial.println("I2C send command:" + String(address) + " data:" + String(packet1));
|
|
|
|
|
|
} |