ADC.h文件
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
/* 采用ADC1的通道11 引脚为PC^1 模式必须是模拟输入*/
#define ADC_GPIO_RCC RCC_APB2Periph_GPIOC
#define ADC_GPIO_PORT GPIOC
#define ADC_GPIO_PIN1 GPIO_Pin_0
#define ADC_GPIO_PIN2 GPIO_Pin_1
#define ADC_GPIO_MODE GPIO_Mode_AIN
/* 配置与中断有关的信息 */
#define ADC_IRQn ADC1_2_IRQn
#define ADC_RCC RCC_APB2Periph_ADC1
/* 配置ADC初始化结构体的宏定义 */
#define ADCx ADC1
#define ADCx_ContinuousConvMode ENABLE //连续转换模式
#define ADCx_DataAlign ADC_DataAlign_Right //转换结果右对齐
#define ADCx_ExternalTrigConv ADC_ExternalTrigConv_None //不使用外部触发转换,采用软件触发
#define ADCx_Mode ADC_Mode_Independent //只使用一个ADC,独立模式
#define ADCx_NbrOfChannel 1 //一个转换通道
#define ADCx_ScanConvMode DISABLE //禁止扫描模式,多通道时使用
/* 通道信息和采样周期 */
#define ADC_Channel ADC_Channel_11
#define ADC_SampleTime ADC_SampleTime_55Cycles5
/* 函数声明 */
void AdcInit_DMA(void);
void ADC_NVIC_Configuration(void);
#endif
ADC.c文件
#include "stm32f10x_adc.h"
#include "stm32f10x.h"
#include "sys_conf.h"
#include "timer.h"
#include "Flash.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "ADC.h"
//void ADC_GPIO_Config(void) // adc 初始化 IO 模拟输入
//{
// GPIO_InitTypeDef GPIO_InitStruct;
// RCC_APB2PeriphClockCmd(ADC_GPIO_RCC, ENABLE);
//
// GPIO_InitStruct.GPIO_Pin = ADC_GPIO_PIN1|ADC_GPIO_PIN2 ;
// GPIO_InitStruct.GPIO_Mode = ADC_GPIO_MODE ;
//
// GPIO_Init(ADC_GPIO_PORT , &GPIO_InitStruct);
//}
#define ADC1_DR_Address ((uint32_t)0x4001244C)
u16 resurt[2];
/**
* @brief Configures Vector Table base location.
* @param None
* @retval None
*/
void ADC_NVIC_Configuration(void) // adc 中断设置
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure and enable ADC interrupt */
NVIC_InitStructure.NVIC_IRQChannel = ADC1_2_IRQn;
//串口和timer5为最高0级,adc为次高1级,tim2(PID)为最低2级,高级中断可以中断低级中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//DMA方式ADC转换
void AdcInit_DMA(void)
{
//ADC相关管脚初始化
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
// DMA_InitTypeDef DMA_InitStructure;
// RCC_APB2PeriphClockCmd(ADC_GPIO_RCC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC |RCC_APB2Periph_ADC1|RCC_APB2Periph_ADC2 , ENABLE );
/* Configure PC.00-05 as analog input -------------------------*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOC, &GPIO_InitStructure);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 1, ADC_SampleTime_71Cycles5); //CPU温度
//ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_71Cycles5); //比例阀1反馈
/* Enable ADC1 DMA */
// ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 开启 ADC 进行转换*/
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 reset calibaration register 重置 ADC*/
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register 等待初始化完成 */
while(ADC_GetResetCalibrationStatus(ADC1));
/* Start ADC1 calibaration 开始校准*/
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration 等待校准完成*/
while(ADC_GetCalibrationStatus(ADC1));
/* Start ADC1 Software Conversion 软件触发 ADC转换*/
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
/* Enable ADC1 EOC interupt */
ADC_ITConfig(ADC1, ADC_IT_EOC, ENABLE);
/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC2, &ADC_InitStructure);
/* ADC1 regular channel14 configuration */
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5); //CPU温度
//ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 2, ADC_SampleTime_71Cycles5); //比例阀1反馈
/* Enable ADC1 DMA */
// ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 开启 ADC 进行转换*/
ADC_Cmd(ADC2, ENABLE);
/* Enable ADC1 reset calibaration register 重置 ADC*/
ADC_ResetCalibration(ADC2);
/* Check the end of ADC1 reset calibration register 等待初始化完成 */
while(ADC_GetResetCalibrationStatus(ADC2));
/* Start ADC1 calibaration 开始校准*/
ADC_StartCalibration(ADC2);
/* Check the end of ADC1 calibration 等待校准完成*/
while(ADC_GetCalibrationStatus(ADC2));
/* Start ADC1 Software Conversion 软件触发 ADC转换*/
ADC_SoftwareStartConvCmd(ADC2, ENABLE);
/* Enable ADC2 */
ADC_Cmd(ADC2, ENABLE);
/* Enable ADC1 EOC interupt */
ADC_ITConfig(ADC2, ADC_IT_EOC, ENABLE);
}
/**
* @brief This function handles ADC1 and ADC2 global interrupts requests.
ADC_SampleTime_239Cycles5时,16通道8.929K,每112uS转换一次,每次ADC中断占用5.2uS,1ms转换
ADC_SampleTime_71Cycles5(温度为239.5)时,16通道23.81K,每42.3uS转换一次,每次ADC中断占用5.2uS,1ms转换
*/
void ADC1_2_IRQHandler(void)
{
//判断AD1扫描转换是否完成,读取规则通道数据后自动清除EOC标志
if(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)==SET){ //启用ADC DMA后自动清除ADC_FLAG_EOC,
resurt[0] =ADC_GetConversionValue(ADC1);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
}
if(ADC_GetFlagStatus(ADC2,ADC_FLAG_EOC)==SET){ //启用ADC DMA后自动清除ADC_FLAG_EOC,
resurt[1] =ADC_GetConversionValue(ADC2);
ADC_ClearFlag(ADC2,ADC_FLAG_EOC);
}
}
void DMA2_Channel3_IRQHandler(void)
{
}
main.c文件
#include "stm32f10x.h"
#include "sys_conf.h"
#include "TickS.h"
#include "timer.h"
#include "Flash.h"
#include "GPIO.h"
#include "ADC.h"
u16 Usart_BaudRate;
int main(void)
{
/*配置系统文件*/
SystemInit();
delay_init(SYSCLK);
Timer3_Init(); // 定时器初始化
PROCGPIO_Init(); // IO 初始化
Usart_PROC_INT(); // 读取flash
ADC_NVIC_Configuration();
AdcInit_DMA();
bsp_InitUart1(9600); //串口1 初始化
Uart2_Configuration(9600); // 串口2 初始化
WDG_Init(); // 看门狗初始化
while(1)
{
WatchDog(); // 喂狗
PROC_Time(); // 处理 定时变量
ModBusPackPro(); // 处理串口1 程序
Usart2_Check_Data(); // 处理 串口2 发送
Rev_UART2_Pro(); // 处理 串口2 接收
PROC_DC();
}
}