家用買的凈水器有時放水給忘了水灌的到處都是,于是想出做一個水流量計,要多水就出多少容量水,給大家分享一下;
中斷處理
- #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)
2024-4-2 19:29 上傳
點擊文件名下載附件
主程序
|