STM32通過SPI採集DT50 ADS8320數據

具體目的

    使用STM32的SPI通道,採集ADS8320對DT50(激光測距傳感器)進行了AD轉換之的數據。不要求進行濾波,但採集到的數據要與距離呈正相關。

**

具體代碼

**

main.h

#ifndef _MAIN_H
#define _MAIN_H
#include "SPI_DMA_Config.h"
#include "stm32f10x.h"
#include "delay_ms.h"

int SPI_Word(u16 word);
#endif

SPI_Config.h
#ifndef __SPI_CONFIG
#define __SPI_CONFIG
#include "stm32f10x.h"

void SPI_Config();
int Read_ADC_Value(void);
#endif
main.c

#include"main.h"
uint16_t Data[10];
uint16_t data=0;
int main()
{
    SystemInit();
    SPI_DMA_Config();
    while(1)
        {

        }

}

SPI_Config.c

#include "SPI_DMA_Config.h"
extern uint16_t Data[10];
extern uint16_t data;
void SPI_DMA_Config()
{
    GPIO_InitTypeDef GPIO_InitStructure;
    SPI_InitTypeDef SPI_InitStructure;
    TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
    NVIC_InitTypeDef NVIC_InitStructure;

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA , ENABLE );
    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2  | RCC_APB1Periph_TIM2, ENABLE );

    //ADS8320引腳配置
    /*----SPI_SCLK----PB13----*/         //時鐘信號
    /*----SPI_SDO----PB14----*/          //串行輸出
    /*----CS/SHDN----PB12----*/          //使能信號

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
    GPIO_Init(GPIOB, &GPIO_InitStructure);


    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; 
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; 
    GPIO_Init(GPIOB, &GPIO_InitStructure);

    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   
    GPIO_Init(GPIOA, &GPIO_InitStructure);

    //SPI配置
    SPI_Cmd(SPI2, DISABLE);
    SPI_InitStructure.SPI_Direction =SPI_Direction_2Lines_FullDuplex;
    SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
    SPI_InitStructure.SPI_DataSize = SPI_DataSize_16b;
    SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
    SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
    SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
    SPI_InitStructure.SPI_BaudRatePrescaler =SPI_BaudRatePrescaler_32;
    SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
    SPI_InitStructure.SPI_CRCPolynomial = 7;
    SPI_Init(SPI2, &SPI_InitStructure);
    SPI_Cmd(SPI2, ENABLE);

    NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;
    NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ; 
    NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
    NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; 
    NVIC_Init(&NVIC_InitStructure);

    TIM_TimeBaseStructure.TIM_Period = 1250-1;
    TIM_TimeBaseStructure.TIM_Prescaler = 144-1;
    TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
    TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
    TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

    TIM_Cmd(TIM2, ENABLE);   
    TIM_ITConfig(TIM2, TIM_IT_Update,ENABLE);            //打開TIM2中斷
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
}

//SPI接收採集數據,全雙工模式下,發送一次,採集一次
int SPI_Word(u16 word)      
{
    while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);
    SPI_I2S_SendData(SPI2,word);
    while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);
    return SPI_I2S_ReceiveData(SPI2);
}

int Read_ADC_Value(void)
{
    int ADC_value=0;

  GPIO_ResetBits(GPIOB,GPIO_Pin_12);//片選信號,低電平觸發,ADS8320進行一次數據採集
    ADC_value=SPI_Word(0x0000);
    GPIO_SetBits(GPIOB,GPIO_Pin_12);//關閉片選,屏蔽從低到高位數據,只採集,從高位到地位數據
    return ADC_value;   //返回採樣值
}


//利用定時器中斷進行數據採集,確定每次採集的間隔時間
void TIM2_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM2,TIM_IT_Update)!= RESET)         
    {

    TIM_ClearFlag(TIM2, TIM_FLAG_Update);   
                Data[data]=Read_ADC_Value();
                data++;
            if(data>9)
                data=0;

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