ADC模拟看门狗

如果被ADC转换的模拟电压低于低阀值或高于高阀值,AWD模拟看门狗状态位被设置。阀值位 于ADC_HTR和ADC_LTR寄存器的最低12个有效位中。通过设置ADC_CR1寄存器的AWDIE位 以允许产生相应中断。通过以下函数可以进行配置

  • void ADC_AnalogWatchdogCmd(ADC_TypeDef* ADCx, uint32_t ADC_AnalogWatchdog);
  • void ADC_AnalogWatchdogThresholdsConfig(ADC_TypeDef* ADCx, uint16_t HighThreshold, uint16_t LowThreshold);
  • void ADC_AnalogWatchdogSingleChannelConfig(ADC_TypeDef* ADCx, uint8_t ADC_Channel);
     

根据框图修改ADC一节的代码。添加看门狗功能代码和NVIC中断代码

ADC模拟看门狗_第1张图片

 

#include "stm32f10x.h"                  // Device header

void AD_Init(void)
{
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE);
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
	
	GPIO_InitTypeDef GPIO_Init_Structure;
	//配置为模拟输入模式.在AIN模式下,GPIO是无效的,防止GPIO的输入输出对模拟电压的影响
	GPIO_Init_Structure.GPIO_Mode=GPIO_Mode_AIN;
	GPIO_Init_Structure.GPIO_Pin=GPIO_Pin_0;
	GPIO_Init_Structure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Init_Structure);
	
	GPIO_Init_Structure.GPIO_Mode=GPIO_Mode_Out_PP;
	GPIO_Init_Structure.GPIO_Pin=GPIO_Pin_1;
	GPIO_Init_Structure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOA,&GPIO_Init_Structure);

	RCC_ADCCLKConfig(RCC_PCLK2_Div6);  //12MHZ
	ADC_RegularChannelConfig(ADC1,ADC_Channel_0,1,ADC_SampleTime_55Cycles5);  //55.5+12.5=68

	ADC_InitTypeDef ADC_InitStruct;
	ADC_InitStruct.ADC_ContinuousConvMode=DISABLE;
	ADC_InitStruct.ADC_ScanConvMode=DISABLE;
	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_Init(ADC1,&ADC_InitStruct);
	
	//模拟看门狗
	ADC_AnalogWatchdogSingleChannelConfig(ADC1,ADC_Channel_0);  //配置通道
	ADC_AnalogWatchdogThresholdsConfig(ADC1,3000,0);            //阈值
	ADC_AnalogWatchdogCmd(ADC1,ADC_AnalogWatchdog_SingleRegEnable);
	//一定不要忘了使能看门狗中断
	ADC_ITConfig(ADC1,ADC_IT_AWD,ENABLE);
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	NVIC_InitTypeDef NVIC_InitStruct;
	NVIC_InitStruct.NVIC_IRQChannel=ADC1_2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	NVIC_Init(&NVIC_InitStruct);
	//使能
	ADC_Cmd(ADC1,ENABLE);
	
	//复位校准
	ADC_ResetCalibration(ADC1);      //ADCx->CR2 |= CR2_RSTCAL_Set;
	while(ADC_GetResetCalibrationStatus(ADC1) == SET); //该位由软件设置并由硬件清除。在校准寄存器被初始化后该位将被清除
	ADC_StartCalibration(ADC1);
	while(ADC_GetCalibrationStatus(ADC1) == SET);
	
}

uint16_t AD_GetValue(void)
{
	//软件触发转换
	ADC_SoftwareStartConvCmd(ADC1,ENABLE);
	//不能用这个函数:由软件设置该位以启动转换,转换开始后硬件马上清除此位。
	//ADC_GetSoftwareStartConvStatus()
	
	//该位由硬件在(规则或注入)通道组转换结束时设置,由软件清除或由读取ADC_DR时清除。0:转换未完成;1:转换完成。
	while(ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC) ==RESET);
	
	return ADC_GetConversionValue(ADC1);
}

使用光敏电阻模块,当模拟数值超过阈值,进入中断,即光线弱时led灯点亮。

#include "stm32f10x.h"                  // Device header
#include "Delay.h"
#include "LED.H"
#include "Key.h"
#include "OLED.H"
#include "AD.H"

uint16_t light,temp;
uint8_t flag=0;

int main(void)
{
	OLED_Init();
	AD_Init();
	GPIO_SetBits(GPIOA,GPIO_Pin_1);
	while(1)
	{
		
		light = AD_GetValue();
		OLED_ShowNum(1,1,light,4);
		if(flag==1)
		{
			GPIO_ResetBits(GPIOA,GPIO_Pin_1);
			flag=0;
		}
		else
		{
			GPIO_SetBits(GPIOA,GPIO_Pin_1);
		}
	}
}

void ADC1_2_IRQHandler(void)
{
	if(ADC_GetITStatus(ADC1,ADC_IT_AWD) == SET)
	{
		flag=1;
		ADC_ClearITPendingBit(ADC1,ADC_IT_AWD);
	}
}

你可能感兴趣的:(STM32,单片机,嵌入式硬件)