家用买的净水器有时放水给忘了水灌的到处都是,于是想出做一个水流量计,要多水就出多少容量水,给大家分享一下;
中断处理
- #include "stm32f10x.h"
- #include "delay.h"
- #include "exti.h"
- #include "led.h"
- #include "display.h"
- #include "key.h"
- void EXIT_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- EXTI_InitTypeDef EXTI_InitStructure; //定义外部中断结构变
- NVIC_InitTypeDef NVIC_InitStructure; //定义向量中断结构变量
- RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB , ENABLE );
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);// 打开复用时钟
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource0); //连接中断管脚PB0
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; //B0-B1 为外部按键 作为3个独立外部输入中断
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // 输入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 最高输入速率50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); // 选择B端口
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//设置NVIC中断分组 0位抢占优先级,4位响应优先级
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;//选择中断通道0
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级0 只能为0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;//响应优先级2 共有16个
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
- NVIC_Init(&NVIC_InitStructure);//完成初始化
-
-
- NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;//选择中断通道0
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;//抢占优先级0 只能为0
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=2;//响应优先级2 共有16个
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//使能中断
- NVIC_Init(&NVIC_InitStructure);//完成初始化
- EXTI_InitStructure.EXTI_Line = EXTI_Line0 | EXTI_Line1;//选择中断线路0和1
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;//设置为中断请求,非事件请求
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//设置中断触发方式为下降沿触发
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;//外部中断使能
- EXTI_Init(&EXTI_InitStructure);
- }
- /*
- ********************************************************************************
- ** 函数名称 : EXTI0_IRQHandler(void)
- ** 函数功能 : 外部中断函数
- ** 输 入 : 无
- ** 输 出 : 无
- ** 返 回 : 无 RESET
- ********************************************************************************
- */
- void EXTI0_IRQHandler(void)
- {
- if(EXTI_GetITStatus(EXTI_Line0)!=RESET)//判断某个线上的中断是否发生
- {
-
- if((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)==0)) //按键真的被按下
- {
- mL=mL+sdmaichong_ml; //1脉冲为10ML水
-
- LjmL= LjmL+sdmaichong_ml;
- //100ML累计一次
-
- }
-
- // while(!GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0)); //等待松手 不能使用KEY_B0代替
- EXTI_ClearITPendingBit(EXTI_Line0); //清楚中断标志位
- }
- }
- /*
- ********************************************************************************
- ** 函数名称 : EXTI1_IRQHandler(void)
- ** 函数功能 : 外部中断函数
- ** 输 入 : 无
- ** 输 出 : 无
- ** 返 回 : 无 RESET
- ********************************************************************************
- */
复制代码
- /*IO使用情况
- A0 A1 A2 A3 A4 A5 A6 A7 键盘 B6 B7显示器 B0水流量感应器 C11 5脚 C12 6脚 24C模块
- C0电磁阀,C1报警信号
- */
- #include "stm32f10x_conf.h"
- #include "key.h"
- #include "display.h"
- #include "delay.h"
- #include "oled.h"
- #include "led.h"
- #include "exti.h"
- #include "24C02.h"
- #include "timer.h"
- int main()
- {
- SystemInit(); //将主频调整到72M
- LED_Init();
- OLED_Init(); //初始化OLED
- OLED_Clear() ;
- TIM1_Init(); //初始化定时器1
- EXIT_Init();
- EXTI0_IRQHandler();
- // EXTI1_IRQHandler();
- AT24CXX_Init(); //24CXX初始化
- // key_Init();
- Read_SmL_LJmL();
-
- while(1)
- {
- HL_key_Display();
- key_scan();
- Display();
-
- }
- }
复制代码
Keil代码下载:
水流量计 多菜单ML.7z
(226.49 KB, 下载次数: 33)
|