已回答假定已回答

ADIS16405与STM32F103CBT6通信

ZiyuanWang 在 2013-5-29 詢問的問題
最後回覆由xiaozhang於2016-10-8提供

     我最近在使用stm32与ADIS16405进行SPI通信,遇到了一些困难,已经卡住两周了,希望有经验的工程师帮帮我。

     我想先读取设备号和Z轴的加速度,但是读出的结果都是0XFFFF。我用了两种办法进行的编程:一种是使用ST的V3.5的固件库,另外一个是用模拟SPI的方式,但是都不成功。

     我从官方得到了ADuC7026与ADIS16405进行模拟SPI通信的程序。我不明白为什么不直接用SPI的硬件资源,而是用软件方式模拟。

     下面是我使用ST的V3.5的固件库具体的实现代码,使用的是SPI1,引脚已经定义

void RCC_Config(void)

{

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | \

                                                                                                                        RCC_APB2Periph_USART1 | RCC_APB1Periph_USART2, ENABLE);

          RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);          // APB2 max 72MHz

          RCC_ClearFlag();

}

 

void SPI1_Init(void)

{

          /* Private variables ---------------------------------------------------------*/

          GPIO_InitTypeDef GPIO_InitStructure;

          SPI_InitTypeDef SPI_InitStructure;

 

          /* GPIO configuration */

          GPIO_InitStructure.GPIO_Pin = SPI1_MISO | SPI1_MOSI | SPI1_SCLK;

          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;

          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_Init(GPIOA, &GPIO_InitStructure);

 

          GPIO_InitStructure.GPIO_Pin = SPI1_NSS;

          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;                    // NSS

          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_Init(GPIOA, &GPIO_InitStructure);

          GPIO_SetBits(GPIOA, SPI1_NSS);                                                                                                              // Not select

 

          GPIO_InitStructure.GPIO_Pin = SPI1_RST;

          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;          // General output

          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

          GPIO_Init(GPIOA, &GPIO_InitStructure);

 

          GPIO_SetBits(GPIOA, SPI1_RST);

          /* SPI1 configuration */

 

          SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;          // 2Lines_FullDuplex

          SPI_InitStructure.SPI_Mode = SPI_Mode_Master;

          SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;

          SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;

          SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;

          SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;

          SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;          // APB2 max 72MHz, SCLK_rate<2MHz

          SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;

          SPI_InitStructure.SPI_CRCPolynomial = 7;

          SPI_Init(SPI1, &SPI_InitStructure);

  SPI_Cmd(SPI1, ENABLE);

}

 

void ADIS16405_Write(int16_t RegAddress)

{

          GPIO_ResetBits(GPIOA, SPI1_NSS);

          while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

          SPI_I2S_SendData(SPI1, RegAddress);

          while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

          GPIO_SetBits(GPIOA, SPI1_NSS);

}

 

 

int16_t ADIS16405_Read(int16_t RegAddress)

{

          GPIO_ResetBits(GPIOA, SPI1_NSS);

          while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);

          SPI_I2S_SendData(SPI1, RegAddress);

 

          while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET);

          GPIO_SetBits(GPIOA, SPI1_NSS);

          return SPI_I2S_ReceiveData(SPI1);

}

結果