新手上路
- 积分
- 40
- 金钱
- 40
- 注册时间
- 2020-11-3
- 在线时间
- 6 小时
|
2金钱
ADC.C
static void ADC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// 优先级分组
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// 配置中断优先级
NVIC_InitStructure.NVIC_IRQChannel = ADC_Channelx;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void ADC_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AIN;
GPIO_InitStruct.GPIO_Pin = ADC_GPIO_PIN ;
GPIO_Init(ADC_GPIO_PORT, &GPIO_InitStruct);
}
static void ADC_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStruct ;
ADC_ClkCmd(ADC_GPIO_CLK,ENABLE);
ADC_InitStruct.ADC_ContinuousConvMode=ENABLE;
ADC_InitStruct.ADC_DataAlign=ADC_DataAlign_Right ;
ADC_InitStruct.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None ;
ADC_InitStruct.ADC_Mode=ADC_Mode_Independent;
ADC_InitStruct.ADC_NbrOfChannel=1;
ADC_InitStruct.ADC_ScanConvMode=DISABLE;
ADC_Init (ADC_x,&ADC_InitStruct);
ADC_PCLK2_DivCmd(RCC_PCLK2_Div8);
ADC_RegularChannelConfig (ADC_x, ADC_Channelx, 1, ADC_SampleTime_55Cycles5);
ADC_ITConfig(ADC_x, ADC_IT_EOC, ENABLE);
ADC_Cmd(ADC_x, ENABLE);
ADC_ResetCalibration ( ADC_x ) ;
while(ADC_GetResetCalibrationStatus(ADC_x));
ADC_StartCalibration ( ADC_x ) ;
while(ADC_GetSoftwareStartConvStatus ( ADC_x ) == 1 );
ADC_SoftwareStartConvCmd(ADC_x,ENABLE);
ADC_NVIC_Config();
}
void ADC_Config(void)
{
ADC_GPIO_Config();
ADC_Mode_Config();
ADC_NVIC_Config();
}
adc。h
#ifndef __ADC_H
#define __ADC_H
#include "stm32f10x.h"
// ADC 编号选择
// 可以是 ADC1/2,如果使用ADC3,中断相关的要改成ADC3的
#define ADC_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADCx ADC2
#define ADC_CLK RCC_APB2Periph_ADC2
// ADC GPIO宏定义
// 注意:用作ADC采集的IO必须没有复用,否则采集电压会有影响
#define ADC_GPIO_APBxClock_FUN RCC_APB2PeriphClockCmd
#define ADC_GPIO_CLK RCC_APB2Periph_GPIOC
#define ADC_PORT GPIOC
#define ADC_PIN GPIO_Pin_1
// ADC 通道宏定义
#define ADC_CHANNEL ADC_Channel_11
// ADC 中断相关宏定义
#define ADC_IRQ ADC1_2_IRQn
#define ADC_IRQHandler ADC1_2_IRQHandler
//#define ADC_IRQ ADC3_IRQn
//#define ADC_IRQHandler ADC3_IRQHandler
void ADCx_Init(void);
#endif /* __ADC_H */
|
|