STM32模拟开门狗中断,设置高低电压阈值
程序员文章站
2022-04-01 18:42:58
...
当被ADC 转换的模拟电压低于低阈值或者高于高阈值时,就会产生中断,前提是我们开启了模拟看门狗中断,其中低阈值和高阈值由ADC_LTR 和ADC_HTR 设置。例如我们设置高阈值是2.5V,那么模拟电压超过2.5V 的时候,就会产生模拟看门狗中断,反之低阈值也一样。
#include "bsp_adc.h"
__IO uint16_t ADC_ConvertedValue;
/**
* @brief ADC GPIO ³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
static void ADCx_GPIO_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// ´ò¿ª ADC IO¶Ë¿ÚʱÖÓ
ADC_GPIO_APBxClock_FUN ( ADC_GPIO_CLK, ENABLE );
// ÅäÖà ADC IO Òý½Åģʽ
// ±ØÐëΪģÄâÊäÈë
GPIO_InitStructure.GPIO_Pin = ADC_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
// ³õʼ»¯ ADC IO
GPIO_Init(ADC_PORT, &GPIO_InitStructure);
}
/**
* @brief ADC Watchdog ³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
static void ADC_WatchdogConfig(void)
{
ADC_AnalogWatchdogSingleChannelConfig(ADCx, ADC_CHANNEL);
ADC_AnalogWatchdogThresholdsConfig(ADCx, 0x0350, 0x0200);
ADC_AnalogWatchdogCmd(ADCx, ADC_AnalogWatchdog_SingleRegEnable);
}
/**
* @brief ÅäÖÃADC¹¤×÷ģʽ
* @param ÎÞ
* @retval ÎÞ
*/
static void ADCx_Mode_Config(void)
{
ADC_InitTypeDef ADC_InitStructure;
// ´ò¿ªADCʱÖÓ
ADC_APBxClock_FUN (ADC_CLK, ENABLE );
// ADC ģʽÅäÖÃ
// ֻʹÓÃÒ»¸öADC£¬ÊôÓÚ¶ÀÁ¢Ä£Ê½
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
// ½ûֹɨÃèģʽ£¬¶àͨµÀ²ÅÒª£¬µ¥Í¨µÀ²»ÐèÒª
ADC_InitStructure.ADC_ScanConvMode = DISABLE ;
// Á¬Ðøת»»Ä£Ê½
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
// ²»ÓÃÍⲿ´¥·¢×ª»»£¬Èí¼þ¿ªÆô¼´¿É
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
// ת»»½á¹ûÓÒ¶ÔÆë
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
// ת»»Í¨µÀ1¸ö
ADC_InitStructure.ADC_NbrOfChannel = 1;
// ³õʼ»¯ADC
ADC_Init(ADCx, &ADC_InitStructure);
// ÅäÖÃADCʱÖÓΪPCLK2µÄ8·ÖƵ£¬¼´9MHz
RCC_ADCCLKConfig(RCC_PCLK2_Div8);
// ÅäÖà ADC ͨµÀת»»Ë³ÐòºÍ²ÉÑùʱ¼ä
ADC_RegularChannelConfig(ADCx, ADC_CHANNEL, 1,
ADC_SampleTime_239Cycles5); //ADC_SampleTime_239Cycles5 ADC_SampleTime_55Cycles5
// ADC ת»»½áÊø²úÉúÖжϣ¬ÔÚÖжϷþÎñ³ÌÐòÖжÁȡת»»Öµ
//ADC_ITConfig(ADCx, ADC_IT_EOC, ENABLE);
ADC_WatchdogConfig();
// ¿ªÆôADC £¬²¢¿ªÊ¼×ª»»
ADC_Cmd(ADCx, ENABLE);
// ³õʼ»¯ADC У׼¼Ä´æÆ÷
ADC_ResetCalibration(ADCx);
// µÈ´ýУ׼¼Ä´æÆ÷³õʼ»¯Íê³É
while(ADC_GetResetCalibrationStatus(ADCx));
// ADC¿ªÊ¼Ð£×¼
ADC_StartCalibration(ADCx);
// µÈ´ýУ׼Íê³É
while(ADC_GetCalibrationStatus(ADCx));
//Ä£Äâ¿´ÃŹ·²úÉúÖжϣ¬ÔÚÖжϷþÎñ³ÌÐòÖжÁȡת»»Öµ
ADC_ITConfig(ADCx, ADC_IT_AWD, ENABLE);
// ÓÉÓÚûÓвÉÓÃÍⲿ´¥·¢£¬ËùÒÔʹÓÃÈí¼þ´¥·¢ADCת»»
ADC_SoftwareStartConvCmd(ADCx, ENABLE);
}
static void ADC_NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
// ÓÅÏȼ¶·Ö×é
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
// ÅäÖÃÖжÏÓÅÏȼ¶
NVIC_InitStructure.NVIC_IRQChannel = ADC_IRQ;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**
* @brief ADC³õʼ»¯
* @param ÎÞ
* @retval ÎÞ
*/
void ADCx_Init(void)
{
ADCx_GPIO_Config();
ADCx_Mode_Config();
ADC_NVIC_Config();
}
/*********************************************END OF FILE**********************/
void ADC_IRQHandler(void)
{
printf("ADC AWD is happened 1.\r\n");
/***
EOCÖжϷ½Ê½
printf("\r\n ----ADC_IRQHandler----\r\n");
if (ADC_GetITStatus(ADCx,ADC_IT_EOC)==SET)
{
// ¶ÁÈ¡ADCµÄת»»Öµ
ADC_ConvertedValue = ADC_GetConversionValue(ADCx);
}
ADC_ClearITPendingBit(ADCx,ADC_IT_EOC);
***/
ADC_ITConfig(ADCx, ADC_IT_AWD, DISABLE);
if(SET == ADC_GetFlagStatus(ADCx, ADC_FLAG_AWD))
{
ADC_ClearFlag(ADCx, ADC_FLAG_AWD);
ADC_ClearITPendingBit(ADCx, ADC_IT_AWD);
printf("ADC AWD is happened 2.\r\n");
ADC_ConvertedValue = ADC_GetConversionValue(ADCx);
}
}
int main(void)
{
// ÅäÖô®¿Ú
USART_Config();
// ADC ³õʼ»¯
ADCx_Init();
printf("\r\n ----ÕâÊÇÒ»¸öADCµ¥Í¨µÀÖж϶ÁȡʵÑé----\r\n");
while (1)
{
ADC_ConvertedValueLocal =(float) ADC_ConvertedValue/4096*3.3;
printf("\r\n The current AD value = 0x%04X \r\n",
ADC_ConvertedValue);
printf("\r\n The current AD value = %f V \r\n",
ADC_ConvertedValueLocal);
printf("\r\n\r\n");
ADC_ITConfig(ADCx, ADC_IT_AWD, ENABLE);
Delay(0xffffee);
}
}
ADC_AnalogWatchdogThresholdsConfig(ADCx, 0x0350, 0x0200);
设置电压高低电平阈值,0xFFF对应3.3V,最高模拟电压值。这里高电平是(0x350/0xFFF)*3.3V,低电平是(0x200/0xFFF)*3.3V。
当采样模拟电压高于高电平阈值,低于低电平的时候阈值,就会产生中断。
上一篇: CANOpen 同步报文使用