[MCU] 【RISC-V MCU CH32V103测评】+ EXTI中断输入开关OLED

eew_VHNtMb   2021-2-21 22:56 楼主

       

 

        上次驱动OLED屏之后一天晚上在想,这屏要是晚上也亮着不就小夜灯了(晚上有光睡不着). 所以这次加个光敏电阻模块让他白天显示,晚上就关掉屏.光敏电阻模块经LM393后输出高低电平,触发中断输入,然后读取中断脚PA0引脚电平后置变量exti0_status  0或1.主要处理在while里处理.

/*******************************************************************************
* Function Name  : EXTI0_INT_INIT
* Description    : Initializes EXTI0 collection.
* Input          : None
* Return         : None
*******************************************************************************/
void EXTI0_INT_INIT(void)
{
  GPIO_InitTypeDef  GPIO_InitStructure;
  EXTI_InitTypeDef EXTI_InitStructure;
  NVIC_InitTypeDef NVIC_InitStructure;

  RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOA,ENABLE);//开启时钟
  //设定引脚
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;         //设为浮动输入
  GPIO_Init(GPIOA, &GPIO_InitStructure);
  //中断参数设置
   /* GPIOA ----> EXTI_Line0 */
  GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
  EXTI_InitStructure.EXTI_Line=EXTI_Line0;
  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;           //中断模式
  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;//上升下降沿触发
  EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  EXTI_Init(&EXTI_InitStructure);
  //优先级设置
  NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  NVIC_Init(&NVIC_InitStructure);
}

        先初始化一下引脚,中断模式,中断优先级.

        然后是中断响应

/*******************************************************************************
* Function Name  : EXTI0_IRQHandler
* Description    : This function handles EXTI0 Handler.
* Input          : None
* Return         : None
*******************************************************************************/
void EXTI0_IRQHandler(void)
{
   
  if(EXTI_GetITStatus(EXTI_Line0)!=RESET)
  {
    //根据引脚电平设置exti0_status变量值
    if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == Bit_SET)
    {
        exti0_status = 1;   
    }else
    {
        exti0_status = 0;   
    }
    EXTI_ClearITPendingBit(EXTI_Line0);     /* 清除标志 */
  }
}

main中编写代码如下

	while(1)
  {

		
        
	    if(exti0_status == 0)
	    {
	        OLED_DisPlay_On();    //开启OLED
	        uint16_t Vrl = 3.3f * ADC_Value / 4095.f;
	        uint16_t Vrl_t = ((3.3f * ADC_Value / 4095.f)*1000-Vrl);
	        OLED_ShowNum(48,16,Vrl,1,24);
	        OLED_ShowChar(62,16,'.',24);//.
	        OLED_ShowNum(74,16,Vrl_t,3,24);
	        OLED_Refresh();
	        Delay_Ms(50);
	    }else
	    {
	        OLED_DisPlay_Off();   //关闭OLED
	    }


  }

        烧录进去后可以看到图一开灯OLED亮起,图二关灯OLED关闭.

   

夜深了,追剧去了.....

 

  • 1.jpg
  • 2.jpg
  • b90033310151ba4309bc32d886ec363.jpg

回复评论

暂无评论,赶紧抢沙发吧
电子工程世界版权所有 京B2-20211791 京ICP备10001474号-1 京公网安备 11010802033920号
    写回复