1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
|
#include "bcm2835.h"
#include <cmath>
#include <iostream>
using namespace std;
// COMMANDS
#define WAKEUP 0x02
#define SLEEP 0x04
#define RESET 0x06
#define START 0x09
#define STOP 0x0a
#define RDATAC 0x10
#define SDATAC 0x11
#define RDATA 0x12
#define OFSCAL 0x18
#define GANCAL 0x19
#define RREG1 0x20
#define WREG1 0x40
#define RREG2 0x08
#define WREG2 0x08
// REGISTERS
#define CONFIG0 0x85
#define CONFIG1 0x10 // checksum is kept off
#define CONFIG2 0x15 //10SPS data rate and Gate control mode
#define OFC0 0x00
#define OFC1 0x00
#define OFC2 0x00
#define FSC0 0x00
#define FSC1 0x00
#define FSC2 0x40
#define NUM 1024
int nobytes;
int S = 50;
int i,flag = 1;
int j, k, factor, converged, count = 0;
//int calib_comp = 1;
char status = LOW;
char txbuffer[11], rxbuffer[4], dummy;
float xhat, xhat_m, P_m, L , K, last_xhat_converged, xhat_converged = 0.0;
float P = 1.0;
float R = 0.01;
float Q, mean, variance = 0;
float current, lastreading, current_test[50];
double key, startkey;
float X1[4096];
float X2[4096];
float Xf1[4096];
float Xf2[4096];
float v[4096];
float xf[4096];
float c[65536];
float ys[65536];
spi_start();
initialise();
void spi_start()
{
bcm2835_init();
//cout << "The SPI mode is starting";
// INITIAL SETUP OF THE SPI DEVICE
bcm2835_spi_begin(); // Setup the SPI0 port on the RaspberryPi
bcm2835_spi_chipSelect(BCM2835_SPI_CS0); // Assert the chip select
bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_MSBFIRST); // Set the Bit order
bcm2835_spi_setChipSelectPolarity(BCM2835_SPI_CS0, LOW); // Set the the chip select to be active low
bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_64); // Set the clock divider, SPI speed is 3.90625MHz
bcm2835_spi_setDataMode(BCM2835_SPI_MODE1); // Set the Data mode
//cout << "The SPI mode has been started";
}
void initialise()
{
// INITIAL RESET OF THE CHIP
nobytes = 1;
txbuffer[0] = RESET;
bcm2835_spi_writenb(txbuffer, nobytes);
bcm2835_delay(100); //no accurate timing required
// WRITING OF THE CONTROL AND THE CALIBRATION REGISTERS
nobytes = 11;
txbuffer[0] = WREG1;
txbuffer[1] = WREG2;
txbuffer[2] = CONFIG0;
txbuffer[3] = CONFIG1;
txbuffer[4] = CONFIG2;
txbuffer[5] = OFC0;
txbuffer[6] = OFC1;
txbuffer[7] = OFC2;
txbuffer[8] = FSC0;
txbuffer[9] = FSC1;
txbuffer[10]= FSC2;
bcm2835_spi_writenb(txbuffer, nobytes);
bcm2835_delay(100); //no accurate timing required
}
|