Tarkoituksena liittää SCA3100 kiihtyvyysanturi EM430F6137RF900 kehitysalustaan SPI-väylää hyväksi käyttäen ja tehdä myös ohjelmisto anturin lukemista varten.
SCA3100-D04 kiihtyvyysanturi:
- mittausakseleita 3 (x,y ja z)
- mittausalue +-2g
- mittaustarkuus +-30mg
- lämpötilan mittausalue -40...+125 C
- SPI väylä
Liitännät:
AVSS/DVSS = CON9 pin 1
AVDD/DVDD = CON9 pin 5
CSB = PORT2 pin 8
MISO = PORT1 pin 6
MOSI = PORT1 pin 7
SCK = PORT1 pin 8
INT pinniä ei tarvitse liittää
Koodi:
Code Block |
---|
#define XTAL 16000000L
#define TICKSPERMS (XTAL / 1000 / 5 - 1)
#define TICKSPERUS (TICKSPERMS / 1000)
#define SPICLOCK 500 // SPI clock = 500kHz
#define SPIFRAMEDELAY ((1000 / SPICLOCK) * 6) // SPI interframe delay [us] = 6 * Tsck
#define SPICSBDELAY ((1000 / SPICLOCK) / 2) // SCK -> CSB delay [us] = 0.5 * Tsck
// LIBRARIES
#include "cc430x613x.h"
#include "TI_CC_spi.h"
#include "math.h"
// PORT DEFINITIONS
#define PORT_CSB_OUT P2OUT
#define PORT_CSB_DIR P2DIR
#define TX_BUFFER UCA0TXBUF
#define RX_BUFFER UCA0RXBUF
// PIN DEFINITIONS
#define PIN_CSB BIT7
// FUNCTION PROTOTYPES
unsigned char ReadRegister(unsigned char Address);
unsigned char WriteRegister(unsigned char Address, unsigned char Data);
unsigned short realValues(unsigned short values);
void wait_ms(unsigned short ms);
void wait_us(unsigned short us);
unsigned char Data;
unsigned char Data_MSB;
unsigned char Data_LSB;
unsigned char RevID;
unsigned short Xdata;
unsigned short Ydata;
unsigned short Zdata;
unsigned short testi;
unsigned short x_msb;
unsigned short x_lsb;
short x_value;
short x_final;
unsigned short y_msb;
unsigned short y_lsb;
short y_value;
unsigned short z_msb;
unsigned short z_lsb;
short z_value;
void main(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
PMAPPWD = 0x02D52; // Get write-access to port mapping regs
P1MAP6 = PM_UCA0SIMO; // Map UCA0SIMO output to P2.0
P1MAP5 = PM_UCA0SOMI; // Map UCA0SOMI output to P2.2
P1MAP7 = PM_UCA0CLK; // Map UCA0CLK output to P2.4
PMAPPWD = 0; // Lock port mapping registers
P1OUT |= BIT2; // Set P1.0 for LED
// Set P1.2 for slave reset
P1OUT |= 0x01;
PORT_CSB_DIR |= PIN_CSB;
PORT_CSB_OUT |= PIN_CSB; // Unselect gyro sensor
P1DIR |= BIT2 + BIT0; // Set P1.0, P1.2 to output direction
P1DIR |= BIT5 + BIT6 + BIT7; // ACLK, MCLK, SMCLK set out to pins
P1SEL |= BIT5 + BIT6 + BIT7; // P2.0,2,4 for debugging purposes.
UCA0CTL1 |= UCSWRST; // **Put state machine in reset**
UCA0CTL0 |= UCMST+UCSYNC+UCCKPH+UCMSB; // 3-pin, 8-bit SPI master
// Clock polarity high, MSB
UCA0CTL1 |= UCSSEL0; // SMCLK
UCA0BR0 = 0x02; // /2
UCA0BR1 = 0; //
UCA0MCTL = 0; // No modulation
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
UCA0IE |= UCRXIE; // Enable USCI_A0 RX interrupt
while(1)
{
x_lsb = ReadRegister(0x04);
x_msb = ReadRegister(0x05);
x_value = (x_msb<<8) | x_lsb;
x_value = realValues(x_value);
y_lsb = ReadRegister(0x06);
y_msb = ReadRegister(0x07);
y_value = (y_msb<<8) | y_lsb;
y_value=realValues(y_value);
z_lsb = ReadRegister(0x08);
z_msb = ReadRegister(0x09);
z_value = (z_msb<<8) | z_lsb;
z_value=realValues(z_value);
RevID = ReadRegister(0x00);
wait_ms(10);
}
}
// Read a byte from the gyro sensor
unsigned char ReadRegister(unsigned char Address)
{
unsigned char Result;
Address <<= 2; // Address to be shifted left by 2 and RW bit to be reset
PORT_CSB_OUT &= ~PIN_CSB; // Select gyro sensor
Result = RX_BUFFER; // Read RX buffer just to clear interrupt flag
TX_BUFFER = Address; // Write address to TX buffer
while (!(UCA0IFG&UCRXIFG)); // Wait until new data was written into RX buffer
Result = RX_BUFFER; // Read RX buffer just to clear interrupt flag
TX_BUFFER = 0; // Write dummy data to TX buffer
while (!(UCA0IFG&UCRXIFG)); // Wait until new data was written into RX buffer
Result = RX_BUFFER; // Read RX buffer
wait_us(SPICSBDELAY); // Delay to satisfy 1/2 clk period delay from
// falling edge of clk to CSB
PORT_CSB_OUT |= PIN_CSB; // Deselect gyro sensor
wait_us(SPIFRAMEDELAY); // Delay to satisfy 6x clk period for CS high state
return Result; // Return new data from RX buffer
}
// Write a byte to the gyro sensor
unsigned char WriteRegister(unsigned char Address, unsigned char Data)
{
unsigned char Result;
Address <<= 2; // Address to be shifted left by 2
Address |= 2; // RW bit to be set
PORT_CSB_OUT &= ~PIN_CSB; // Select gyro sensor
Result = RX_BUFFER; // Read RX buffer just to clear interrupt flag
TX_BUFFER = Address; // Write address to TX buffer
while (!(UCA0IFG&UCRXIFG)); // Wait until new data was written into RX buffer
Result = RX_BUFFER; // Read RX buffer just to clear interrupt flag
TX_BUFFER = Data; // Write data to TX buffer
while (!(UCA0IFG&UCRXIFG)); // Wait until new data was written into RX buffer
Result = RX_BUFFER; // Read RX buffer
wait_us(SPICSBDELAY); // Delay to satisfy 1/2 clk period delay from
// falling edge of clk to CSB
PORT_CSB_OUT |= PIN_CSB; // Deselect gyro sensor
wait_us(SPIFRAMEDELAY); // Delay to satisfy 6x clk period for CS high state
return Result;
}
// Read all 6 rate registers using the decrement reading feature
// and convert to 16-bit unsigned values
void wait_ms(unsigned short ms)
{
unsigned short a, b;
for (a = ms; a > 0; a--) // outer loop takes 5 ck per round
for (b = TICKSPERMS; b > 0; b--) // inner loop takes 5 ck per round
asm(" nop");
}
// wait us
void wait_us(unsigned short us)
{
unsigned short a;
us *= TICKSPERUS;
for (a = us; a > 0; a--) // loop takes 5 ck per round
asm(" nop");
}
unsigned int shift(unsigned short thisData, int thisBit)
{
unsigned short data;
data=thisData>>(thisBit-1);
data=data&0x01;
return data;
}
unsigned short realValues(unsigned short values)
{
short taulukko[14];
int i;
int bitti=3;
unsigned short valmis;
for(i=0;i<14;i++)
{
taulukko[i]= shift(values,bitti);
bitti++;
}
valmis=(10/9)*(-taulukko[13]*pow(2,13)+taulukko[12]*pow(2,12)+taulukko[11]*pow(2,11)+taulukko[10]*pow(2,10)+taulukko[9]*pow(2,9)+taulukko[8]*pow(2,8)+taulukko[7]*pow(2,7)+taulukko[6]*pow(2,6)+taulukko[5]*pow(2,5)+taulukko[4]*pow(2,4)+taulukko[3]*pow(2,3)+taulukko[2]*pow(2,2)+taulukko[1]*2+taulukko[0]);
return valmis;
}
|