Tarkoituksena liittää SCA3100 kiihtyvyysanturi EM430F6137RF900 kehitysalustaan SPI-väylää hyväksi käyttäen ja tehdä myös ohjelmisto ohjelma 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ä
...
Ohjelma:
CCS4:lla tehdyllä ohjelmalla voidaan lukea SCA3100 anturin kaikki 3 akselia. Laiteiston puuttellisuuden vuoksi akselien arvojen tarkkailu ei tällä hetkellä onnistu realiaikaisesti, esim. tulostaminen suoraan sarjaväylälle. Akselien arvoja voidaan myös tarkkailla CCS4:n sisällä, mutta tällöinkin vain pysäyttämällä ohjelma (ohjeet ).
Koodi:
Code Block |
---|
/***************************************************************************
* EM430F6137 Demo USCI_A0 SPI Interface to SCA3100 Accelerometer
*
* Uses TI EM430F6137 Development Tool
*
* Reads all 3 (x,y,z) axes
*/
|
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);AXIS REG #define X_LSB 0x04 #define X_MSB 0x05 #define Y_LSB 0x06 #define Y_MSB 0x07 #define Z_LSB 0x08 #define Z_MSB 0x09 // FUNCTION PROTOTYPES unsigned char WriteRegisterReadRegister(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 P2P1.06 P1MAP5 = PM_UCA0SOMI; // Map UCA0SOMI output to P2P1.25 P1MAP7 = PM_UCA0CLK; // Map UCA0CLK output to P2P1.47 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; // P2P1.05,26,47 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(0x04X_LSB); x_msb = ReadRegister(0x05X_MSB); x_value = (x_msb<<8) | x_lsb; x_value = realValues(x_value); y_lsb = ReadRegister(0x06Y_LSB); y_msb = ReadRegister(0x07Y_MSB); y_value = (y_msb<<8) | y_lsb; y_value=realValues(y_value); z_lsb = ReadRegister(0x08Z_LSB); z_msb = ReadRegister(0x09Z_MSB); 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 // andReturn convertnew todata 16-bitfrom unsignedRX valuesbuffer } 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 taulukkotable[14]; int i; int bitti=3; unsigned short valmisrdy; for(i=0;i<14;i++) { taulukkotable[i]= shift(values,bitti); bitti++; } valmisrdy=(10/9)*(-taulukkotable[13]*pow(2,13)+taulukkotable[12]*pow(2,12)+taulukkotable[11]*pow(2,11)+taulukkotable[10]*pow(2,10)+taulukkotable[9]*pow(2,9)+taulukkotable[8]*pow(2,8)+taulukkotable[7]*pow(2,7)+taulukkotable[6]*pow(2,6)+taulukkotable[5]*pow(2,5)+taulukkotable[4]*pow(2,4)+taulukkotable[3]*pow(2,3)+taulukkotable[2]*pow(2,2)+taulukkotable[1]*2+taulukkotable[0]); return valmisrdy; } |