AD7705在STM32F103RBT6上的移植[硬件SPI]

利用STM32硬件SPI 與TM7705 收發數據

/*****************************主程序**************************/
//PA2:CS
//PA5:SCK
//PA6:MISO
//PA7:MOSI

#include "stm32f10x.h"
#include "spi.h"
#include "stdio.h"


void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
u16 ReadTM7705(void);
int fputc(int ch, FILE *f)
{
      USART_SendData(USART1, (unsigned char) ch);// USART1 可以換成 USART2 等
      while (!(USART1->SR & USART_FLAG_TXE));
      return (ch);
}

int GetKey (void)  
{ 
      while (!(USART1->SR & USART_FLAG_RXNE));
      return ((int)(USART1->DR & 0x1FF));
}

void Delay(vu32 nCount)
{
        for(; nCount != 0; nCount--);
}

void USART_Configuration(void)
{
        USART_InitTypeDef USART_InitStructure;

        /* 配置USART1波特率:9600;字長:8bit;停止位:1;奇偶校驗:無;關閉硬件流控制模式;開啓發送、接收功能 */
        USART_InitStructure.USART_BaudRate = 9600; 
        USART_InitStructure.USART_WordLength = USART_WordLength_8b; 
        USART_InitStructure.USART_StopBits = USART_StopBits_1; 
        USART_InitStructure.USART_Parity = USART_Parity_No; 
        USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; 
        USART_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; 
        USART_Init(USART1, &USART_InitStructure); 

        USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);//使能串口1中斷功能
        USART_Cmd(USART1, ENABLE);                                         //使能USART1
}

void GPIO_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
                                
        /* Configure USART1 Tx (PA.09) as alternate function push-pull */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        /* Configure USART1 Rx (PA.10) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        /* Configure USART1 Rx (PB.8) as input floating */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
        GPIO_Init(GPIOB, &GPIO_InitStructure);
}

/*********系統中斷管理**********/
void NVIC_Configuration(void)
{ 
        /* Configure the NVIC Preemption Priority Bits */  
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);        //設置優先級分組:先佔優先級0位,從優先級4位
}

/********配置系統時鐘,使能各外設時鐘***********/
void RCC_Configuration(void)
{
        SystemInit();        
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA 
                |RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO 
                |RCC_APB2Periph_SPI1, ENABLE );
}
        
/********TM7705初始化函數***********/
void TM7705_Init(void)
{         
        unsigned char i ;
        for(i = 0; i < 100; i++)
    { 
        SPIx_ReadWriteByte(0xFF);      //持續DIN高電平寫操作,恢復AD7705接口
    }
        SPIx_ReadWriteByte(0x20) ;                         //通道1 ,下一個寫時鐘寄存器
        Delay(1000); 
        SPIx_ReadWriteByte(0x02) ;                         //寫時鐘寄存器設置更新速率爲200Hz
        Delay(1000);
        SPIx_ReadWriteByte(0x10) ;                         //通道1 ,下一個寫設置寄存器
        Delay(1000);
        SPIx_ReadWriteByte(0x44) ;                         //寫設置寄存器 ,設置成雙極性、無緩衝、增益爲0、濾波器工作、自校準
        Delay(1000000);
}

/********讀16位數據************/
u16 ReadTM7705_16BitValue(void)
{ 
        unsigned long DataL = 0;
        unsigned long DataH = 0;
        unsigned long Ret = 0;

        DataH = SPIx_ReadWriteByte(0xFF);
    DataH = DataH << 8; 
    DataL = SPIx_ReadWriteByte(0xFF); 
        Ret          = DataH | DataL; 

        return(Ret) ;                             
}

/********讀取AD7705第一通道數據************/
u16 ReadTM7705(void)
{
    unsigned long Ret = 0;
        SPIx_ReadWriteByte(0x38) ;       //設置讀當前通道數據
        while(GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_8 == 1))
        {
                ;
        }
    Ret = ReadTM7705_16BitValue();
         
    return Ret;
}

/********主函數************/
int main(void)
{         
        u16 b;
        float c;
        RCC_Configuration();        
        NVIC_Configuration();
        GPIO_Configuration();
        USART_Configuration();
        SPIx_Init();
        TM7705_Init();
        while(1)
        {
                TM7705_Init();
                b = ReadTM7705();
                c = b*3.3/65535;
                printf("%f\n",c);
        }
}
/****************************SPI初始化*************************/
#include "spi.h"                                  
//SPI口初始化
//這裏針是對SPI1的初始化
void SPIx_Init(void)
        {         
        SPI_InitTypeDef  SPI_InitStructure;
        GPIO_InitTypeDef GPIO_InitStructure;
  
        /* Configure SPI1 pins: SCK, MISO and MOSI */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //複用推輓輸出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        /* Configure I/O for Flash Chip select */
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;  //SPI CS
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;  //複用推輓輸出
        GPIO_Init(GPIOA, &GPIO_InitStructure);
        
        /* Deselect the FLASH: Chip Select high */
        GPIO_SetBits(GPIOA,GPIO_Pin_2);
        GPIO_SetBits(GPIOB,GPIO_Pin_8);
        /* SPI1 configuration */
        SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;  //設置SPI單向或者雙向的數據模式:SPI設置爲雙線雙向全雙工
        SPI_InitStructure.SPI_Mode = SPI_Mode_Master;                //設置SPI工作模式:設置爲主SPI
        SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;                //設置SPI的數據大小:SPI發送接收8位幀結構
        SPI_InitStructure.SPI_CPOL = SPI_CPOL_High;                //選擇了串行時鐘的穩態:時鐘懸空高
        SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;        //數據捕獲於第二個時鐘沿
        SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;                //NSS信號由硬件(NSS管腳)還是軟件(使用SSI位)管理:內部NSS信號有SSI位控制
        SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64;                //定義波特率預分頻的值:波特率預分頻值爲256
        SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;        //指定數據傳輸從MSB位還是LSB位開始:數據傳輸從MSB位開始
        SPI_InitStructure.SPI_CRCPolynomial = 7;            //CRC值計算的多項式
        SPI_Init(SPI1, &SPI_InitStructure);             //根據SPI_InitStruct中指定的參數初始化外設SPIx寄存器
        
        //SPI1->CR1|=1<<6; //SPI設備使能
        /* Enable SPI1  */
        SPI_Cmd(SPI1, ENABLE); //使能SPI外設
        
        SPIx_ReadWriteByte(0xff);//啓動傳輸                 
}  
         

//SPIx 讀寫一個字節
//TxData:要寫入的字節
//返回值:讀取到的字節
u8 SPIx_ReadWriteByte(u8 TxData)
{                
        GPIO_ResetBits(GPIOA,GPIO_Pin_2);
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET); //檢查指定的SPI標誌位設置與否:發送緩存空標誌位
        /* Send byte through the SPI1 peripheral */
        SPI_I2S_SendData(SPI1, TxData); //通過外設SPIx發送一個數據
        /* Wait to receive a byte */
        while (SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE) == RESET); //檢查指定的SPI標誌位設置與否:接受緩存非空標誌
        /* Return the byte read from the SPI bus */
        GPIO_SetBits(GPIOA,GPIO_Pin_2);
        return SPI_I2S_ReceiveData(SPI1); //返回通過SPIx最近接收的數據                                    
}


心得總結:
1.CS片選在硬件SPI中非常重要,CS在模擬SPI中可以省略,這也是前期一直不出結果的原因。
2.DRDY腳應該設置成上拉輸入,MOSI/MISO/SCLK/CE設置成浮空輸入
3.當模塊數據準備好後,DRDY腳會產生低電平,單片機檢測到後即可讀取數據。
4.該模塊的採樣速度200Hz
5.STM32的硬件中有一個NSS腳,這個腳應該設置成軟件管理
6.TM7705(國產天微公司)系AD7705的山寨版,性能略差

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章