微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > stm32f407之ADC与DMA

stm32f407之ADC与DMA

时间:11-10 来源:互联网 点击:

ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; //独立模式
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//分频为4,f(ADC3)=21M
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //失能DMA_MODE
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;//两次采样间隔5个周期
ADC_CommonInit(&ADC_CommonInitStructure);

ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 1, ADC_SampleTime_15Cycles); //规则通道配置,1表示规则组采样顺序
//ADC_ITConfig(ADC3, ADC_IT_EOC, ENABLE);//使能ADC转换结束中断
ADC_Cmd(ADC3, ENABLE);//使能ADC3

/*********************ADC看门狗配置***************************/
ADC_AnalogWatchdogCmd(ADC3, ADC_AnalogWatchdog_SingleRegEnable);
ADC_AnalogWatchdogThresholdsConfig(ADC3, 0x0E8B, 0x0555); //阈值设置。高:3V 低:1V
ADC_AnalogWatchdogSingleChannelConfig(ADC3, ADC_Channel_7);
ADC_ITConfig(ADC3, ADC_IT_AWD, ENABLE);

ADC_DMACmd(ADC3, ENABLE); //使能ADC3的DMA
ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); //单通道模式下上次转换完成后DMA请求允许,也就是持续DMA
}
/*************************************************
Function: void USART_Config(void)
Description: USART配置函数
Input: 无
Output:无
Return:无
*************************************************/
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟

/* 配置USART6 */
USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate =115200;
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_Rx | USART_Mode_Tx;
USART_Init(USART6, &USART_InitStructure);
USART_ClockStructInit(&USART_ClockInitStruct); //之前没有填入缺省值,是不行的
USART_ClockInit(USART6, &USART_ClockInitStruct);

USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART6中断
USART_Cmd(USART6, ENABLE); //使能 USART6
}

void NVIC_Config()
{
/* USART6中断配置 */
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn; //嵌套通道为USART6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应优先级为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);

/* DMA中断配置 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn; //嵌套通道为DMA2_Stream0_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //响应优先级为 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);

/* ADC中断配置 */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQn; //嵌套通道为ADC_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //抢占优先级为 1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; //响应优先级为 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中断使能
NVIC_Init(&NVIC_InitStructure);
}

/*************************************************
Function: void USART6_Puts(char * str)
Description: USART6发送数据
Input: 待发送数据指针
Output:无
Return:无
*************************************************/
void USART6_Puts(char * str)
{
while (*str)
{
USART_SendData(USART6, *str++);
/* Loop until the end of transm

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top