SPI Master.
Adds to interface:
<name>_SCLK : out std_logic
<name>_MOSI : out std_logic
<name>_MISO : in std_logic
<name>_SSn : out std_logic
where <name> is c++ object name.
Any number of instances are allowed.
#include <stdlib.h>
#include <math.h>
#include "fc_io.h"
#include "fc_system.h"
template<class T>
T& operator<<(T& os, int v)
{
char b[16];
itoa(v,b,10);
os << b;
return os;
}
{
U8 buff[4] = {0x0B, reg, 0x00, 0x00 };
spi.WriteRead( buff, buff, 4 );
return buff[3]<<8|buff[2];
}
int main(void)
{
U8 spi_command[3] = { 0x0A, 0x2D, 0x02 };
accel.StartWrite( spi_command, 3 );
accel.WaitReady();
for(;;) {
timer.Sleep( 100, TU_ms );
float x = GetAccel(accel,0x0E);
float y = GetAccel(accel,0x10);
float z = GetAccel(accel,0x12);
int roll = atanf( x/sqrtf(y*y+z*z) ) * 573.0f;
int pitch = atanf( y/sqrtf(x*x+z*z) ) * 573.0f;
uart_tx << "Roll: " << roll/10 << "." << abs(roll%10);
uart_tx << " \tPitch: " << pitch/10 << "." << abs(pitch%10) << "\r\n";
}
}