STM32F103控制AD7606采集模拟信号
1、配置STM32F103的SPI口
void AD7606_Port_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDefSPI_InitStructure;
RCC_APB2PeriphClockCmd(AD_SPI_CS_GPIO_CLK | AD_SPI_MISO_GPIO_CLK | AD_SPI_SCK_GPIO_CLK, ENABLE);
RCC_APB1PeriphClockCmd(AD_SPI_CLK, ENABLE);
/////////////////////////SPI_CLK////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_SCK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(AD_SPI_SCK_GPIO_PORT, &GPIO_InitStructure);
/////////////////////////SPI_MISO////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_MISO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(AD_SPI_MISO_GPIO_PORT, &GPIO_InitStructure);
/////////////////////////SPI_CS////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_SPI_CS_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_SPI_CS_GPIO_PORT, &GPIO_InitStructure);
/////////////////////////SPI_3?ê??ˉ////////////////////////
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_High;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge;
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(AD_SPI, &SPI_InitStructure);
SPI_Cmd(AD_SPI, ENABLE);
/////////////////////////AD7606_RESET////////////////////////
RCC_APB2PeriphClockCmd(AD_RESET_GPIO_CLK | AD_CONVT_GPIO_CLK , ENABLE);
GPIO_InitStructure.GPIO_Pin = AD_RESET_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_RESET_GPIO_PORT, &GPIO_InitStructure);
/////////////////////////AD7606_CONVT////////////////////////
GPIO_InitStructure.GPIO_Pin = AD_CONVT_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(AD_CONVT_GPIO_PORT, &GPIO_InitStructure);
}
2、AD7606复位信号
void AD7606_Reset(void)
{
AD_RESET_LOW();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_HIGH();
AD_RESET_LOW();
}
3、AD7606开始转换信号
void AD7606_StartConvt(void)
{
AD_CONVT_LOW();
AD_CONVT_LOW();
AD_CONVT_LOW();
AD_CONVT_HIGH();
}
4、STM32F103读取AD7606数据
short AD7606_ReadBytes(void)
{
short AD7606_Data = http://www.dg8.com.cn/tool/0;
while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_TXE) == RESET)
{
}
SPI_I2S_SendData(AD_SPI, 0xFFFF);
while (SPI_I2S_GetFlagStatus(AD_SPI, SPI_I2S_FLAG_RXNE) == RESET)
{
}
AD7606_Data = http://www.dg8.com.cn/tool/SPI_I2S_ReceiveData(AD_SPI);
return AD7606_Data;
}
5、STM32F103读取AD7606前CS拉低,拉高,重新开始转换
void samp_return(void)
{
int j=0;
AD_SPI_CS_LOW();
for(j=0;j<6;j++)
{
AD7606_Return[j]=AD7606_ReadBytes();
}
AD_SPI_CS_HIGH();
//ads7606_Delay(100);
AD7606_StartConvt();
ads7606_Delay(200);
}
6、数据量转化为模拟量公式
【STM32F103控制AD7606采集模拟信号】电压=samp_sum_jiaoshudu_return[0]*10.0/32768/(2.5/2.5);//采用正负10电压模拟输入,参考电压2.5V
7、主函数main中初始化AD7606
AD7606_Port_Init();
Adc_Init();
AD7606_Reset();
AD_SPI_CS_HIGH();
AD_CONVT_HIGH();
AD7606_StartConvt();
推荐阅读
- 基于stm32f103zet6的定时器的学习3_定时器产生4路PWM
- 基于stm32f103zet6之nor flash的学习
- STM32F103学习笔记 _九 输入捕获实验
- 液压水位控制阀如何安装?先选对,后安装
- 单片机控制洗衣机C程序设计
- 利用89C51单片机的P1口控制8个发光二极管LED编程
- 将TNY279 电源芯片作为开关电源的控制芯片,效果如何?
- 上位机控制单片机LED
- STM32f103 双串口配置和中断
- 51单片机_STC89C52RC光电耦合控制继电器实验