void Fc_init(u32 add)
{
GPIO_InitTypeDef GPIO_InitStruct;
ADC_CommonInitTypeDef ADC_CommonInitStruct;
ADC_InitTypeDef ADC_InitStruct;
DMA_InitTypeDef DMA_InitStruct;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
RCC_AHB1PeriphClockLPModeCmd(RCC_AHB1Periph_DMA1,ENABLE);
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AN;
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_NOPULL;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStruct);
ADC_CommonInitStruct.ADC_DMAAccessMode=ADC_DMAAccessMode_1;
ADC_CommonInitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_CommonInitStruct.ADC_Prescaler=ADC_Prescaler_Div2;
ADC_CommonInitStruct.ADC_TwoSamplingDelay=ADC_TwoSamplingDelay_20Cycles;
ADC_CommonInit(&ADC_CommonInitStruct);
ADC_InitStruct.ADC_ContinuousConvMode=ENABLE;//连续转换
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right;
ADC_InitStruct.ADC_ExternalTrigConvEdge=ADC_ExternalTrigConvEdge_None;//禁止外部触发检测,使用软件触发
ADC_InitStruct.ADC_NbrOfConversion=1;
ADC_InitStruct.ADC_Resolution=ADC_Resolution_12b;
ADC_InitStruct.ADC_ScanConvMode=DISABLE;
ADC_Init(ADC1,&ADC_InitStruct);
DMA_InitStruct.DMA_PeripheralBaseAddr=ADC1_BASE+0X4c; //ADC1的数据寄存器
DMA_InitStruct.DMA_Channel=DMA_Channel_0; //查找映射关系
DMA_InitStruct.DMA_Memory0BaseAddr=add; //存储器地址
DMA_InitStruct.DMA_DIR=DMA_DIR_PeripheralToMemory; //传输方向从内存到外设
DMA_InitStruct.DMA_Mode=DMA_Mode_Circular; //循环模式
DMA_InitStruct.DMA_BufferSize=1;
DMA_InitStruct.DMA_MemoryDataSize=DMA_MemoryDataSize_Byte;
DMA_InitStruct.DMA_FIFOMode=DMA_FIFOMode_Disable;
DMA_InitStruct.DMA_FIFOThreshold=DMA_FIFOThreshold_Full;
DMA_InitStruct.DMA_MemoryBurst=DMA_PeripheralBurst_Single;
DMA_InitStruct.DMA_PeripheralBurst=DMA_PeripheralBurst_Single;
DMA_InitStruct.DMA_MemoryInc=DMA_MemoryInc_Disable;
DMA_InitStruct.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
DMA_InitStruct.DMA_Priority=DMA_Priority_Medium;
DMA_InitStruct.DMA_MemoryDataSize=DMA_PeripheralDataSize_HalfWord;
DMA_InitStruct.DMA_PeripheralDataSize =DMA_PeripheralDataSize_HalfWord;
DMA_Init(DMA1_Stream0,&DMA_InitStruct);
DMA_Cmd(DMA1_Stream0,ENABLE);
ADC_Cmd(ADC1,ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,ENABLE);//ADC1复位
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1,DISABLE);//ADC复位结束
ADC_DMACmd(ADC1,ENABLE);//使能ADC转换,通过DMA
ADC_SoftwareStartConv(ADC1);
}
#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "fc.h"
u16 convers_data;
float result_data;
int main()
{
delay_init(168);
uart_init(115200);
Fc_init(&convers_data); //这里为什么会报错???STM32F4的地址不是32位的吗
while(1)
{
result_data=(float)convers_data/4096*5;
printf("the result is %.4f V
",result_data);
delay_ms(100);
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>