Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
main.cpp
- Committer:
- justinbuckland
- Date:
- 2018-07-23
- Revision:
- 1:7f99e938ae2a
- Parent:
- 0:362148ad8f6f
- Child:
- 2:23f848b21b09
File content as of revision 1:7f99e938ae2a:
#include "mbed.h"
#define ALL_CH 15 //value of convst bus to read all chanels simultaneosly
#define IO_REF 10.17 //ref channel voltage mV (most accurate between 9-12V high supply voltage)
#define CHA0_REF 6217
#define CHA1_REF 6196
#define CHB0_REF 6231
#define CHC0_REF 6231
#define CHC1_REF 6223
#define CHD0_REF 6240
#define CHD1_REF 6228
Serial pc(USBTX, USBRX); // tx, rx
DigitalOut drive(p21);
DigitalOut yLED(p27);
DigitalOut gLED(p28);
DigitalOut rLED(p26);
AnalogIn battVolt(p19);
AnalogIn auxVolt(p20);
BusOut convt(p11, p12, p13, p14);
SPI spi(p5, p6, p7); // mosi, miso, sclk
DigitalOut cs(p8); //chip select
DigitalIn busy(p9);
DigitalOut reset(p10);
LocalFileSystem local("local");
FILE *fp = fopen("/local/TEST_LOG.csv", "w"); // Open "test_log" on the local file system for writing
char buffer16[16];
int val_array[8];
float voltages[8];
const char dummy = 0;
int i = 0;
float cur1 = 0;
float cur2 = 0;
float R1 = 0;
float R2 = 0;
int scale_factors[8];
int drivetime_ms;
float scale_factor = 0.0;
char outString[100];
int readChannels (char buffer[16], int values[8], int OSR){
//simultaneosly samples all channels and reads into buffer
short int val_array[8];
for (int i=0; i<OSR; i++) {
//send convert signal to all channels
convt = ALL_CH;
wait_us(1);
convt = 0;
//SPI(like) data transfer
cs = 0;
spi.write(&dummy, 1, buffer, 16);
cs=1;
//loop over bytes to add channel voltage values
for (int x=0; x<8; x++){
val_array[x] = buffer[2*x]<<8 | buffer16[(2*x) + 1];
values [x] = values[x] + val_array[x];
}
}
for (int y=0; y<8; y++){
values[y] = values[y]/OSR;
}
return 0;
}
int main() {
rLED = 0;
yLED = 0;
gLED = 0;
int OSR = 5;
drive = 0;
drivetime_ms = 1;
scale_factors[0] = CHA0_REF;
scale_factors[1] = CHA1_REF;
scale_factors[2] = CHB0_REF;
scale_factors[3] = 0;
scale_factors[4] = CHC0_REF;
scale_factors[5] = CHC1_REF;
scale_factors[6] = CHD0_REF;
scale_factors[7] = CHD1_REF;
pc.baud(115200);
pc.printf("Test start\n\r");
//Reset ADC sequence
reset = 1;
wait_ms(1);
reset = 0;
//set SPI serial to 2MHz, 16 bit data transfer, mode 2 (clock normally high, data preceeding clock cycle)
spi.format(8,2);
spi.frequency(2000000);
spi.set_default_write_value(0x00);
cs = 1;
//while(1) {
rLED = 1;
yLED = drive;
gLED = 1;
drive = 1;
wait_ms(drivetime_ms);
readChannels (buffer16, val_array, 1);
drive = 0;
rLED = 0;
pc.printf("Drive for %d ms\n", drivetime_ms);
for (int x=0; x<8; x++){
voltages[x] = (float)val_array[x]*scale_factors[x]/10000;
pc.printf("%f\n", voltages[x]);
}
cur1 = 2* (voltages[0] - voltages[1]);
cur2 = 2* (voltages[2] - voltages[1]);
R1 = (voltages[4]- voltages[5])/cur1;
R2 = (voltages[6]- voltages[7])/cur2;
pc.printf("V0 = %f\n", voltages[0]);
pc.printf("V1 = %f\n", voltages[1]);
pc.printf("V2 = %f\n", voltages[2]);
pc.printf("V4 = %f\n", voltages[4]);
pc.printf("V5 = %f\n", voltages[5]);
pc.printf("V6 = %f\n", voltages[6]);
pc.printf("V7 = %f\n", voltages[7]);
sprintf(outString, "R1, R2, Drive for %d ms, OSR %d\n", drivetime_ms, OSR);
fprintf(fp, outString);
for (int x=0; x<20; x++) {
drive = 1;
wait_ms(drivetime_ms);
readChannels (buffer16, val_array, OSR);
drive = 0;
for (int x=0; x<8; x++){
voltages[x] = val_array[x]*scale_factors[x]/10000;
}
cur1 = 2* (voltages[0] - voltages[1]);
cur2 = 2* (voltages[2] - voltages[1]);
// R1 = (voltages[4]- voltages[5])/cur1;
// R2 = (voltages[6]- voltages[7])/cur2;
R1 = cur1;
R2 = cur2;
sprintf(outString, "%f, %f\n", R1, R2);
fprintf(fp, outString);
wait_ms(100);
}
rLED = 0;
fclose(fp);
//
// pc.printf("Repeat Test?\n\rPress to increment wait time\n\r");
// while(1){
// if (pc.getc() != 0) {
// drivetime_ms++;
// break;
// }
// }
//}
}