본문 바로가기

[MSP430]/MSP430F5x

[MSP430F5529 EVM] SPI테스트 - 3축 가속도 센서 출력

[MSP430F5529 EVM] SPI테스트 - 3축 가속도 센서 출력

[MSP430F5510 EVM] 및 [MSP430F5529 EVM] 의 SPI는 PORT4의 USB1에 할당 되어 있다.
핀맵을 보면 아래와 같다.

P4.3/PM_UCB1CLK/PM_UCA1STE
P4.2/PM_UCB1SOMI/PM_UCB1SCL
P4.1/PM_UCB1SIMO/PM_UCB1SDA
P4.0/PM_UCB1STE/PM_UCA1CLK




MSP430F5x의 SPI특징
SPI mode features include:
• 7-bit or 8-bit data length
• LSB-first or MSB-first data transmit and receive
• 3-pin and 4-pin SPI operation
• Master or slave modes
• Independent transmit and receive shift registers
• Separate transmit and receive buffer registers
• Continuous transmit and receive operation
• Selectable clock polarity and phase control
• Programmable clock frequency in master mode
• Independent interrupt capability for receive and transmit
• Slave operation in LPM4



MSP430F5x SPI블록도





MSP430F5x SPI 통신 초기화 함수
void SPI1_Init(void) // SPI 통신 초기화 함수
{
 // Select SPI functionality on DIO pins for MOSI, MISO, & CLK.
 P4SEL |= (BIT1|BIT2|BIT3);  
   
  UCB1CTL1 |= UCSWRST;                      // **Put state machine in reset**
  UCB1CTL0 |= UCMST+UCSYNC+UCMSB+UCCKPH;    // 3-pin, 8-bit SPI master
  UCB1CTL1 |= UCSSEL_2;                     // SMCLK
  UCB1CTL1 &= ~UCSWRST;                     // **Initialize USCI state machine**
//  UCB1IE |= UCRXIE;     

   SPI1_SetSpeed(SPI_SPEED_1MHZ);
}



MSP430F5x SPI 전송 함수
#define SPI1_WRITE_BYTE(x)                 UCB1TXBUF = (x);
#define SPI1_READ_BYTE()     UCB1RXBUF
#define SPI1_WAIT_FOR_EN()     while (!(UCTXIFG&UCB1IFG));
#define SPI1_WAIT_FOR_SEND()              while (UCB1STAT & UCBUSY);
#define SPI1_WAIT_FOR_RX()     while (!(UCRXIFG&UCB1IFG))


unsigned char SPI1_WriteReadByte(unsigned char Data)
{
 SPI1_WAIT_FOR_EN();
 SPI1_WRITE_BYTE(Data);
 SPI1_WAIT_FOR_SEND();
 return SPI1_READ_BYTE();
}



MSP430F5510 SPI Accel 드라이버 함수
#define _SPI1_ENABLE    1

//-----------------------------------------------------------------------------
// myAccel3LV02 HAL
#define MY_ACCEL3LV02_SPI_MODE   1
#define MY_ACCEL3LV02_I2C_MODE   0

//AccelCS A8-P3.7
#define ACCEL_CS_BIT     BIT6
#define ACCEL_CS_PORT     PORT4

#define ACCEL_CS_INIT()     Sbi(P4DIR, ACCEL_CS_BIT);

#define ACCEL_CS_ASSERT()    Cbi(ACCEL_CS_PORT, ACCEL_CS_BIT)
#define ACCEL_CS_DEASSERT()    Sbi(ACCEL_CS_PORT, ACCEL_CS_BIT)
#define ACCEL_SPI_INIT()    SPI1_Init()
#define ACCEL_Read      SPI1_WriteReadByte
#define ACCEL_Write      SPI1_WriteReadByte
//-----------------------------------------------------------------------------



MSP430F5529 3축 가속도 센서 출력 테스트 예제 소스코드
#include "system.h"
#include "serial.h"
#include "spi.h"
#include "myAccel.h"

void main(void)
{
   short data = 0;
    SystemInit();

    Led1Init();
    Led1On();
   
 DebugInit(BAUD_115200);
 DebugPrint("MSP430F5529 EVM SPI Accel Test\r\n");

 myAccel3lvInit();
    myAccel3lvWrite(CTRL_REG1, 0xC7);  //1000.0111 Power on, enable all axis, self test off
   
    while(1)
    {
        Led1Toggle();
       GetAccelValue(AXIS_X, &data);
       DebugPrint("%d\r\n", data); 
 
        Delay(100);
    }
}


반응형