COMP2的i/o连接一条指令就可以:
我们计划用PB7,查表
SYSCFG_RIIOSwitchConfig(RI_IOSwitch_12, ENABLE); //PB7--IOSWITCH 12
/* Init COMP2: VREFINT is used as COMP2 inverting input
COMP2 output is connected to TIM2 input capture 2 (default configuration)
COMP2 speed is fast */
COMP_Init(COMP_InvertingInput_VREFINT, COMP_OutputSelect_TIM2IC2, COMP_Speed_Fast);
/* COMP2 edge detection: rising edge */
COMP_EdgeConfig(COMP_Selection_COMP2, COMP_Edge_Rising);
完整的COMP配置函数:
static void COMP_Config(void)
{
/* Connect internal reference voltage (VREFINT) to COMP1 inverting input:
The higher threshold is set to VREFINT ~ 1.22 V */
COMP_VrefintToCOMP1Connect(ENABLE);
/* Close I/O Switch 22 to slect PD1 as COMP1 and COMP2 non inverting inputs:
* Input voltage should be connected to PB7 */
SYSCFG_RIIOSwitchConfig(RI_IOSwitch_12, ENABLE); //--IOSWITCH 12
/* Enable Window mode */
COMP_WindowCmd(ENABLE);
/* Init COMP2: - COMP2 inverting input is connected to 1/4 VREFINT:
* The lower threshold is set to VREFINT/4 ~ 1.22 / 4 ~ 0.305 V
* and can be changed to other available possibilities
* - COMP2 output is connected to TIM2 input capture 2 (default config)
* - COMP2 speed is slow */
COMP_Init(COMP_InvertingInput_1_4VREFINT, COMP_OutputSelect_TIM2IC2, COMP_Speed_Fast);
/* COMP2 edge detection: rising and falling edges */
COMP_EdgeConfig(COMP_Selection_COMP2, COMP_Edge_Rising_Falling);
/* COMP1 edge detection: rising and falling edges */
COMP_EdgeConfig(COMP_Selection_COMP1, COMP_Edge_Rising_Falling);
/* Enable COMP1 Interrupt */
COMP_ITConfig(COMP_Selection_COMP1, ENABLE);
/* Enable COMP2 Interrupt */
COMP_ITConfig(COMP_Selection_COMP2, ENABLE);
}
窗口比较器根据COMP1&COMP2输出配置状态:
void CheckState(void)
{
/* Check if COMP1 and COMP2 output levels are high */
if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_High)
&& (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_High))
{
/* The input voltage is higher than VREFINT */
State = STATE_OVER_THRESHOLD;
}
/* Ceck if COMP1 output level is low and COMP2 output is high */
else if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_Low)
&& (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_High))
{
/* The input voltage is lower than VREFINT and higher than VREFINT/4 */
State = STATE_WITHIN_THRESHOLD;
}
/* Check if COMP1 and COMP2 output levels are low */
else if ((COMP_GetOutputLevel(COMP_Selection_COMP1) == COMP_OutputLevel_Low)
&& (COMP_GetOutputLevel(COMP_Selection_COMP2) == COMP_OutputLevel_Low))
{
/* The input voltage is lower than VREFINT/4 */
State = STATE_UNDER_THRESHOLD;
}
}
中断函数:
INTERRUPT_HANDLER(ADC1_COMP_IRQHandler, 18)
{
/* In order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction.
*/
if ((COMP_GetFlagStatus(COMP_Selection_COMP1) != RESET)
|| (COMP_GetFlagStatus(COMP_Selection_COMP2) != RESET))
{
/* In this exsample, an adjustable is used so a software filter is required
Software filter is used if the input voltage changes very fast */
for (SoftwareFilter = 0x00; SoftwareFilter < 0x10; SoftwareFilter++);
/* check input voltage level: within the thresolds, above the upper threshold
or under the lower threshold */
CheckState();
/* Clear COMP1 interrupt pending bit */
COMP_ClearITPendingBit(COMP_Selection_COMP1);
/* Clear COMP2 interrupt pending bit */
COMP_ClearITPendingBit(COMP_Selection_COMP2);
}
}
开始用PB7,信号监测不到
后改为PD0,查表为23通道
SYSCFG_RIIOSwitchConfig(RI_IOSwitch_23, ENABLE); //--IOSWITCH 12
正常执行,原因不明?
在一的COMP1实验中是可以正常接入COMP的,不知道为什么这个实验无法正常通过。
难道是COMP2只接受GROUP8的IO输入?
继续查资料:
暂时认为051只有PD0能用于窗口比较模式,也经过验证。
主函数:
实验现象:电位器旋转电压由小变大的变化:全灭-> LED1亮->2个LED全亮,反向依次熄灭。
void main(void)
{
/*************** Initialize LEDs available on STM8L15X-EVAL board ***********/
LED_Init();
/* CLK configuration -------------------------------------------*/
CLK_Config();
/* COMP configuration -------------------------------------------*/
COMP_Config();
enableInterrupts();
CheckState();
while (1)
{
if (State == STATE_OVER_THRESHOLD)
{
/* Turn on LED1&LED2*/
LED1_ON;
LED2_ON;
while (State == STATE_OVER_THRESHOLD)
{
/* add your code here */
}
}
else if (State == STATE_WITHIN_THRESHOLD)
{
/* Input voltage is within the thresholds: higher and lower thresholds */
/* LED1 ON and LED2 off */
LED1_ON;
LED2_OFF;
/* Enter Halt mode */
// halt();
}
else /* (State == STATE_UNDER_THRESHOLD) */
{
/* Turn off the LEDs*/
LED1_OFF;
LED2_OFF;
while (State == STATE_UNDER_THRESHOLD)
{
/* add your code here */
}
}
}
}