Last active
September 1, 2015 20:06
-
-
Save pingswept/38463537e1a8f6ecaf73 to your computer and use it in GitHub Desktop.
Code for the Rascal Micro Precision Voltage Shield on an Arduino Due
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Works, but very slow. */ | |
/* Licensed under the GNU General Public License, version 3 */ | |
/* Copyright Brandon Stafford, September 1, 2015 */ | |
#define SCALE_FACTOR 0.000152587890625 | |
// 10/(2^16) = 0.000152587890625 | |
#define BUSY 3 | |
#define RESET 4 | |
#define START_CONVERSION 5 | |
#define CHIP_SELECT 10 | |
#define SOFTSPI_MISO 12 | |
#define SOFTSPI_SCK 13 | |
#define TOTAL_RAW_BYTES 16 | |
#define SOFTSPI_MISO_BIT 0x00000100 // pin 12 is PD8, Port D, bit 8 | |
#define SOFTSPI_SCK_BIT 0x08000000 // pin 13 is PB27, Port B, bit 27 | |
// Arduino Due schematic: https://www.arduino.cc/en/uploads/Main/arduino-Due-schematic.pdf | |
int bytesToRead = TOTAL_RAW_BYTES; | |
byte raw[TOTAL_RAW_BYTES]; | |
signed long parsed[8]; | |
void setup() { | |
pinMode(BUSY, INPUT); | |
pinMode(RESET, OUTPUT); | |
pinMode(START_CONVERSION, OUTPUT); | |
pinMode(CHIP_SELECT, OUTPUT); | |
pinMode(SOFTSPI_MISO, INPUT); | |
pinMode(SOFTSPI_SCK, OUTPUT); | |
Serial.begin(115200); | |
digitalWrite(START_CONVERSION, HIGH); | |
digitalWrite(CHIP_SELECT, HIGH); | |
digitalWrite(RESET, HIGH); | |
delay(1); | |
digitalWrite(RESET, LOW); | |
} | |
void loop() { | |
int i; | |
int j; | |
boolean input_data; | |
byte currentByte = 0; | |
digitalWrite(START_CONVERSION, LOW); | |
delayMicroseconds(10); | |
digitalWrite(START_CONVERSION, HIGH); | |
while (digitalRead(BUSY) == HIGH) { | |
// wait for conversion to complete | |
} | |
digitalWrite(CHIP_SELECT, LOW); | |
while (bytesToRead > 0) { | |
currentByte = TOTAL_RAW_BYTES - bytesToRead; | |
for(j=0; j<8; j++) { | |
REG_PIOB_CODR = SOFTSPI_SCK_BIT; // set clock line low | |
raw[currentByte] <<= 1; | |
if((PIOD->PIO_PDSR & SOFTSPI_MISO_BIT)) { | |
raw[currentByte] |= 0x01; | |
} else { | |
raw[currentByte] &= 0xFE; | |
} | |
REG_PIOB_SODR = SOFTSPI_SCK_BIT; // set clock line high | |
} | |
bytesToRead--; | |
} | |
digitalWrite(CHIP_SELECT, HIGH); | |
bytesToRead = TOTAL_RAW_BYTES; | |
parseRawBytes(); | |
//Serial.write(raw, 16); | |
for(i=0; i<8; i++) { | |
Serial.print((float)parsed[i] * SCALE_FACTOR, 5); | |
Serial.print(","); | |
} | |
Serial.print("\r\n"); | |
for(i=0; i<8; i++) { | |
Serial.print(parsed[i]); | |
Serial.print(","); | |
} | |
Serial.print("\r\n"); | |
delay(1000); | |
} | |
void parseRawBytes() { | |
parsed[0] = (raw[0] << 8) + raw[1]; | |
parsed[1] = (raw[2] << 8) + raw[3]; | |
parsed[2] = (raw[4] << 8) + raw[5]; | |
parsed[3] = (raw[6] << 8) + raw[7]; | |
parsed[4] = (raw[8] << 8) + raw[9]; | |
parsed[5] = (raw[10] << 8) + raw[11]; | |
parsed[6] = (raw[12] << 8) + raw[13]; | |
parsed[7] = (raw[14] << 8) + raw[15]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment