具体目的
使用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
main.c
#include"main.h"
uint16_t Data[10];
uint16_t data=0;
int main()
{
SystemInit();
SPI_DMA_Config();
while(1)
{
}
}
SPI_Config.h
#ifndef __SPI_CONFIG
#define __SPI_CONFIG
#include "stm32f10x.h"
void SPI_Config();
int Read_ADC_Value(void);
#endif
SPI_Config.c
#include "SPI_Config.h"
extern uint16_t Data[10];
extern uint16_t data;
void SPI_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;
}
}
评论(0)
您还未登录,请登录后发表或查看评论