Copyright (C), 2012-2022, yin.
FileName: main.c
Author: 小枣年糕
Date: 2012\05\01
Description: ADC1 DMA tempersensor printf
Version: V3.0
IDE: MDK 4.22a
HardWare: stm32F407IG HSE = 25M PLL = 168M
History: V1.0
Function: 利用ADC读取芯片内部温度传感器的值
***********************************************************/
#include
/*定义ADC1的数据寄存器地址,DMA功能要用到外设的数据地址
*ADC1的数据地址为外设基地址+偏移地址,基地址在RM0090 Reference
*manual(参考手册)的地址映射表里,为0x40012000,ADC_DR
*偏移地址为0x4C,故实际地址为0x40012000+0x4C = 0x4001204C */
#define ADC1_DR_Addr ((uint32_t)0x4001204C)
__IO float Temper;
void ADC_Config(void);
void USART_Config(void);
void DMA_Config(void);
void NVIC_Config(void);
void Delay(uint32_t nCount);
/* printf函数重定向 */
int fputc(int ch, FILE *f);
main()
{
/*在主函数main之前通过调用启动代码运行了SystemInit函数,而这个函数位于system_stm32f4xx.c”。
程序运行起始于启动文件的第175行(LDR R0, =SystemInit)。sys时钟为HSE频率/PLL_M*PLL_N/PLL_P,
定义HSE为25M,则sys时钟频率为168M */
ADC_Config();
USART_Config();
DMA_Config();
NVIC_Config();
GPIO_SetBits(GPIOG, GPIO_Pin_6); //关闭LED
ADC_SoftwareStartConv(ADC1); //如果不是外部触发则必须软件开始转换
while (1)
{
Delay(0x0ffffff);
//printf("size of int is %d \n", sizeof(int)); //测试可知32位系统的int占4个字节
Temper = (ADCoverValue*3300/4096-760)/2.5 + 25;
printf("ADCoverVaule=%04X Temper=%.2f C\n", ADCoverValue, Temper); //串口输出温度值
}
}
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初始化函数
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);
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_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_ADC1, ENABLE); //开ADC1时钟
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(ADC1, &ADC_InitStructure);
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4; //分频为4,f(ADC)=21M
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; //失能DMA_MODE
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles;//两次采样间隔20个周期
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_TempSensorVrefintCmd(ENABLE);//使能温度传感器的基准电源
ADC_Cmd(ADC1, ENABLE); //使能ADC1
ADC_DMACmd(ADC1, ENABLE); //使能ADC1的DMA
ADC_DMARequestAfterLastTransferCmd(ADC1, 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时钟
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_Cmd(USART6, ENABLE); //使能 USART6
}
{
/* 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);
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);
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 DMA_Config(void)
Description: DMA配置函数
Input: 延时的时间
Output:无
Return:无
*************************************************/
void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure;
/*首先开DMA2时钟,由407参考手册-RM0090-Reference manual
*165页可知,ADC与DMA2映射,而且DMA2挂载在AHB1时钟总线上*/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE);
DMA_StructInit( &DMA_InitStructure);
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //选择Channel_0
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Addr; //数据传输的外设首地址,详解见上
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCoverValue; //自己定义待发送数组的首地址,要强制转换为32位
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; //数据传输方向选择为内存->外设
DMA_InitStructure.DMA_BufferSize = 1; //传输数据大小为16,单位由以下确定,大小要配合定义的数组类型和外设数据类型
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设地址寄存器自动增加禁止,因为这里只用到了DR数据寄存器
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; //内存地址自增不允许,因为只存取一个数
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; //外设的数据大小,因为USART6_DR数据寄存器为8为,故选Byte
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; //这里也选Byte
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA传输模式为Normal,如果为Circular,将会循环传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //优先级为VeryHigh
DMA_Cmd(DMA2_Stream0, ENABLE); //使能DMA2_Stream0通道
/* DMA中断开 */
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);// USART6 可以换成 USART2 等
while (!(USART6->SR & USART_FLAG_TXE));
return (ch);
}
*作用:ADC3_DMA数据完全完成后产生中断
*/
void DMA2_Stream0_IRQHandler(void)
{
if (DMA_GetITStatus(DMA2_Stream0, DMA_IT_TCIF0) != RESET)
{
DMA_ClearITPendingBit(DMA2_Stream0, DMA_IT_TCIF0);
GPIO_ResetBits(GPIOG, GPIO_Pin_6); //点亮LED,起到中断指示作用
/*添加用户代码*/
}
}
上一篇:ARM中的软件中断
下一篇:stm32的ADC之规则组和注入组
推荐阅读最新更新时间:2024-03-16 15:03