昨日调试了stm32f407的ADC功用,由于那个恼人的printf函数问题这篇ADC文章一向没有发表出来,今日5.1归来抽暇补上。功用为完结ADC的单通道电压收集,并完结DMA数据管理,一起加了ADC看门狗功用,并用printf函数经过串口回来收集值与换算后的实践电压值。代码如下:
/************************************************************
Copyright (C), 2012-2022, yin.
FileName: main.c
Author: 小枣年糕
Date:2012\05\01
Description: ADC3 DMA printf
Version: V3.0
IDE: MDK 4.22a
HardWare: stm32F407IG HSE = 25M PLL = 168M
History: V1.0
***********************************************************/
#include
#include
/*界说ADC3的数据寄存器地址,DMA功用要用到外设的数据地址
*ADC3的数据地址为外设基地址+偏移地址,基地址在RM0090 Reference
*manual(参阅手册)的地址映射表里,为0x40012200,ADC_DR
*偏移地址为0x4C,故实践地址为0x40012200+0x4C = 0x4001224C */
#define ADC3_DR_Addr ((uint32_t)0x4001224C)
__IO uint16_t ADCoverVaule;
//uint16_t Buffer2[] = {0x1122}; //这个是我进行内存测验用的,程序没用到
void GPIO_Config(void);
void ADC_Config(void);
void USART_Config(void);
void USART6_Puts(char * str);
void DMA_Config(void);
void NVIC_Config(void);
void Delay(uint32_t nCount);
/* printf函数重定向 */
int fputc(int ch, FILE *f);
int GetKey (void);
main()
{
/*在主函数main之前经过调用发动代码运转了SystemInit函数,而这个函数坐落system_stm32f4xx.c”。
程序运转起始于发动文件的第175行(LDR R0, =SystemInit)。sys时钟为HSE频率/PLL_M*PLL_N/PLL_P,
界说HSE为25M,则sys时钟频率为168M */
GPIO_Config();
ADC_Config();
USART_Config();
DMA_Config();
NVIC_Config();
GPIO_SetBits(GPIOG, GPIO_Pin_6); //封闭LED
ADC_SoftwareStartConv(ADC3); //假如不是外部触发则有必要软件开端转化
while (1)
{
Delay(0x0ffffff);
//printf(“size of int is %d \n”, sizeof(int)); //测验可知32位体系的int占4个字节
printf(“ADCoverVaule=%04X VolVaule=%d mV\n”, ADCoverVaule, ADCoverVaule*3300/4096); //串口输出电压值
/*由于DMA作业是独立于CPU之外的,所以在DMA作业的一起CPU能够做其他事*/
}
}
/*************************************************
Function: void GPIO_Config(void)
Description: GPIO装备函数
Input: 无
Output:无
Return:无
*************************************************/
void GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* 使能GPIOC\GPIOF\GPIOG时钟*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOG | RCC_AHB1Periph_GPIOF, ENABLE);
GPIO_StructInit(&GPIO_InitStructure);
/* 初始化GPIOG的Pin_6为LED输出 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //指定第六引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //形式为输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //频率为快速
GPIO_Init(GPIOG, &GPIO_InitStructure); //调用IO初始化函数
/* 初始化GPIOG的Pin_9为模拟量输入 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOF, &GPIO_InitStructure);
/* 装备GPIOC_Pin6为TX输出 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //设置为复用,有必要为AF,OUT不可
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/* 装备GPIOC_Pin7为RX输入 */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //这也有必要为复用,与M3不同!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/* GPIO引脚复用功用设置 */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//这相当于M3的敞开复用时钟,只装备复用的引脚,
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//
}
/*************************************************
Function: void ADC_Config(void)
Description: ADC装备函数
Input: 无
Output:无
Return:无
*************************************************/
void ADC_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE);//开ADC时钟
ADC_DeInit();
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //精度为12位
ADC_InitStructure.ADC_ScanConvMode = DISABLE;//扫描转化形式失能,单通道不必
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;//接连转化使能
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //不必外部触发,软件触发转化
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;//数据右对齐,低字节对齐
ADC_InitStructure.ADC_NbrOfConversion = 1;//规矩了次序进行规矩转化的ADC通道的数目
ADC_Init(ADC3, &ADC_InitStructure);
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 transmission */
while (USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); //详见英文参阅的521页,当TXE被置起时,一帧数据传输完结
}
}
/*************************************************
Function: void DMA_Config(void)
Description: DMA装备函数
Input: 延时的时刻
Output:无
Return:无
*************************************************/
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*首先开DMA2时钟,由407参阅手册-RM0090-Reference manual
*165页可知,UASRT6与DMA2映射,并且DMA2挂载在AHB1时钟总线上*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_DeInit(DMA2_Stream0);
DMA_StructInit( &DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_2; //挑选Channel_2
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_Addr; //数据传输的外设首地址,详解见上
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCoverVaule; //自己界说待发送数组的首地址,要强制转化为32位
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //数据传输方向挑选为外设到内存
DMA_InitStructure.DMA_BufferSize = 1; //传输数据巨细为1,单位由以下确认,巨细要合作界说的数组类型和外设数据类型
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器主动增加制止,由于这儿只用到了DR数据寄存器
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //内存地址自增不答应
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设的数据巨细,由于ADC6_DR数据寄存器为16为,故选HalfWord
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //这儿也选Byte
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA传输形式为Circular,将会循环传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级为High
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);//使能DMA2_Stream0通道
/* DMA中止开 */
DMA_ITConfig(DMA2_Stream6, DMA_IT_TC, ENABLE);
DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);
}
/*************************************************
Function: void Delay(uint32_t nCount)
Description: 延时函数
Input: 延时的时刻
Output:无
Return:无
*************************************************/
void Delay(uint32_t nCount)
{
while (nCount–);
}
/*************************************************
Function: int fputc(int ch, FILE *f)
Description: fputc重定向函数–发送
Input:
Output:无
Return:ch
*************************************************/
int fputc(int ch, FILE *f)
{
USART_SendData(USART6, (unsigned char) ch);// USART1 能够换成 USART2 等
while (!(USART6->SR & USART_FLAG_TXE));
return (ch);
}
/*************************************************
Function: int GetKey (void)
Description: fputc重定向函数–接纳
Input: 无
Output:无
Return:USART6->DR & 0x1FF
*************************************************/
int GetKey (void)
{
while (!(USART6->SR & USART_FLAG_RXNE));
return ((int)(USART6->DR & 0x1FF));
}
中止服务函数:
/**称号:DMA中止服务程序
*效果:ADC3_DMA数据彻底完结后发生中止
*/
void DMA2_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
/*增加用户代码*/
}
}
/**称号:ADC看门狗中止服务程序
*效果:ADC输入超越边界发生中止,并点亮LED
*/
void ADC_IRQHandler(void)
{
GPIO_ResetBits(GPIOG,GPIO_Pin_6);
if (ADC_GetITStatus(ADC3, ADC_IT_AWD) == SET)
{
ADC_ClearITPendingBit(ADC3, ADC_IT_AWD);
ADC_Cmd(ADC3, DISABLE);
printf(“Input is out of threrd!!”);
}
}