Skip to content

Commit

Permalink
updated ADC SPI code
Browse files Browse the repository at this point in the history
  • Loading branch information
molloyd committed Feb 7, 2015
1 parent f1cb584 commit a179e5d
Show file tree
Hide file tree
Showing 10 changed files with 20,083 additions and 0 deletions.
Binary file modified chp08/spi/adc_example/ADCExample
Binary file not shown.
Binary file added chp08/spi/adc_example/ADCmulti
Binary file not shown.
42 changes: 42 additions & 0 deletions chp08/spi/adc_example/ADCmulti.cpp
@@ -0,0 +1,42 @@
/* Using an SPI ADC (e.g., the MCP3008)
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014
* ISBN 9781118935125. Please see the file README.md in the repository root
* directory for copyright and GNU GPLv3 license information. */

#include <iostream>
#include <sstream>
#include "bus/SPIDevice.h"
#define SAMPLES 200

using namespace std;
using namespace exploringBB;

short combineValues(unsigned char upper, unsigned char lower){
return ((short)upper<<8)|(short)lower;
}

int main(){
// cout << "Starting EBB SPI ADC Multi Example" << endl;
SPIDevice *busDevice = new SPIDevice(1,0); //Using second SPI bus (both loaded)
busDevice->setSpeed(4000000); // Have access to SPI Device object
busDevice->setMode(SPIDevice::MODE0);

unsigned char send[3], receive[3];
int samples[SAMPLES];

send[0] = 0b00000001; // The Start Bit followed
// Set the SGL/Diff and D mode -- e.g., 1000 means single ended CH0 value
send[1] = 0b10000000; // The MSB is the Single/Diff bit and it is followed by 000 for CH0
send[2] = 0; // This byte doesn't need to be set, just for a clear display

for(int i=0; i<SAMPLES; i++){
busDevice->transfer(send, receive, 3);
samples[i]=combineValues(receive[1]&0b00000011, receive[2]);
}
// cout << "The samples that were captured are:" << endl;
for(int i=0; i<SAMPLES; i++){
cout << i << " " << samples[i] << endl;
}
// cout << "\nEnd of EBB SPI ADC Example" << endl;
}
Binary file added chp08/spi/adc_example/ADCmulti2
Binary file not shown.
39 changes: 39 additions & 0 deletions chp08/spi/adc_example/ADCmulti2.cpp
@@ -0,0 +1,39 @@
/* Using an SPI ADC (e.g., the MCP3008)
* Written by Derek Molloy for the book "Exploring BeagleBone: Tools and
* Techniques for Building with Embedded Linux" by John Wiley & Sons, 2014
* ISBN 9781118935125. Please see the file README.md in the repository root
* directory for copyright and GNU GPLv3 license information. */

#include <iostream>
#include <sstream>
#include "bus/SPIDevice.h"
#define SAMPLES 20000

using namespace std;
using namespace exploringBB;

short combineValues(unsigned char upper, unsigned char lower){
return ((short)upper<<8)|(short)lower;
}

int main(){
SPIDevice *busDevice = new SPIDevice(1,0); //Using second SPI bus (both loaded)
busDevice->setSpeed(4000000); // Have access to SPI Device object
busDevice->setMode(SPIDevice::MODE0);

unsigned char send[3], receive[3];
int samples[SAMPLES];

send[0] = 0b00000001; // The Start Bit followed
// Set the SGL/Diff and D mode -- e.g., 1000 means single ended CH0 value
send[1] = 0b10000000; // The MSB is the Single/Diff bit and it is followed by 000 for CH0
send[2] = 0; // This byte doesn't need to be set, just for a clear display

for(int i=0; i<SAMPLES; i++){
busDevice->transfer(send, receive, 3);
samples[i] = (short((receive[1]&0b00000011))<<8)|(short(receive[2]));
}
for(int i=0; i<SAMPLES; i++){
cout << i << " " << samples[i] << endl;
}
}
2 changes: 2 additions & 0 deletions chp08/spi/adc_example/build
@@ -1,2 +1,4 @@
#!/bin/bash
g++ -o ADCmulti ADCmulti.cpp bus/SPIDevice.cpp bus/BusDevice.cpp
g++ -O3 -o ADCmulti2 ADCmulti2.cpp bus/SPIDevice.cpp bus/BusDevice.cpp
g++ -o ADCExample ADCExample.cpp bus/SPIDevice.cpp bus/BusDevice.cpp

0 comments on commit a179e5d

Please sign in to comment.