123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222 |
- #include "led.h"
- #include "delay.h"
- #include "sys.h"
- #include "stm32f10x.h"
- #include "stdio.h"
- #include "usart.h"
- #include "adc.h"
- #include "oled.h"
- #include "ds18b20.h"
- #define HwInput PBin(7) //红外传感器输入
- #define KEY1 PAin(0) // 手动控制加热
- #define JR_OUT_PIN PCout(14) //控制输出
- #define CS_OUT_PIN PBout(13) //控制
- #define KEY_GPIO_PROT GPIOA
- #define KEY1_GPIO_PIN GPIO_Pin_0
- #define KEY2_GPIO_PIN GPIO_Pin_1
- #define KEY3_GPIO_PIN GPIO_Pin_2
- #define KEY4_GPIO_PIN GPIO_Pin_2
- #define KEY1_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY1_GPIO_PIN)
- #define KEY2_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY2_GPIO_PIN)
- #define KEY3_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY3_GPIO_PIN)
- #define KEY4_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY4_GPIO_PIN)
- //#define KEY5_IN GPIO_ReadInputDataBit(KEY_GPIO_PROT,KEY5_GPIO_PIN)
- u8 start_flag=0;
- void Hw_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC, ENABLE);
- GPIO_InitStructure.GPIO_Pin = KEY1_GPIO_PIN|KEY2_GPIO_PIN|KEY3_GPIO_PIN|KEY4_GPIO_PIN;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
- GPIO_Init(KEY_GPIO_PROT, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- u8 set_flag=0;
- u8 delay_off_time=30; //没人时关闭时间
- u8 time1_temp=0;
- u8 true_rt_time=60; //有人时延时关闭时间
- u8 time2_temp=0;
- u8 manual_mode_flag=0;//模式控制
- void key_Task(void)
- {
- static char key_flag=0;
- if(KEY1_IN==0||KEY2_IN==0||KEY3_IN==0||KEY4_IN==0)
- {
- if(key_flag==0)
- {
- key_flag=1;
- if(KEY1_IN==0)
- {
- if(++set_flag>1)
- {
- set_flag=0;
- }
- start_flag=0;
- }
- else if(KEY2_IN==0)
- {
- if(set_flag==0)
- {
- if(++delay_off_time>99)delay_off_time=0;
- }else if(set_flag==1)
- {
- if(++true_rt_time>99)true_rt_time=0;
- }
- }
- else if(KEY3_IN==0)
- {
- if(set_flag==0)
- {
- if(delay_off_time>0)delay_off_time--;
- }else if(set_flag==1)
- {
- if(true_rt_time>0)true_rt_time--;
- }
- }
- else if(KEY4_IN==0)
- {
- manual_mode_flag^=1;
- // start_flag=0;
- }
- }
- }else
- {
- key_flag=0;
- }
- }
- u8 get_time=0;
- u16 sec;
- void TIM2_IRQHandler()
- {
- u8 st;
- st=TIM_GetFlagStatus(TIM2, TIM_IT_Update);
- if(st!=0)
- {
- TIM_ClearFlag(TIM2, TIM_IT_Update);
- key_Task();
- get_time++;
-
- if(start_flag)
- {
- if(++sec>50*60)
- {
- sec=0;
- if(time1_temp>0)time1_temp--;
- if(time2_temp>0)time2_temp--;
-
- if(time1_temp==0||time2_temp==0)
- {
- start_flag=0;
- JR_OUT_PIN=0;//关闭电源
- manual_mode_flag=0;//手动模式下开关标志关闭
- }
- }
- }
- }
- }
- void Timer2_init()
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM2,ENABLE);
- TIM_TimeBaseStructure.TIM_Period = 20000-1;
- TIM_TimeBaseStructure.TIM_Prescaler =72-1;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0x0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_DeInit(TIM2);
- TIM_TimeBaseInit(TIM2, & TIM_TimeBaseStructure);
- TIM_Cmd(TIM2, ENABLE);
- TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
- NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init (&NVIC_InitStructure);
- }
- u8 check_sta=0;
- char showbuff[16]={0};
- int main(void)
- {
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置中断优先级分组为组2:2位抢占优先级,2位响应优先级
- delay_init(); //延时函数初始化
- LED_Init(); //初始化与LED连接的硬件接口
- uart_init(9600);
- ADC1_Init();
- OLED_Init();
- OLED_Clear();
- Hw_Init();
- Timer2_init();
- while(1)
- {
- if(get_time>100) //100ms刷新一次
- {
- get_time=0;
- sprintf(showbuff,"check_states:%d",check_sta); //识别状态
- OLED_ShowString(0,2,(u8*)showbuff,16);
- sprintf(showbuff,"time1:%3d %3d",delay_off_time,time1_temp); //没人延时关闭的时间
- OLED_ShowString(0,4,(u8*)showbuff,16);
-
- sprintf(showbuff,"time2:%3d %3d",true_rt_time,time2_temp); //使用者自行设置关闭时间
- OLED_ShowString(0,6,(u8*)showbuff,16);
-
- if(set_flag==0) //自动模式 通过摄像头控制
- {
- OLED_ShowString(0,0,(u8*)"mode:auto ",16);
- if(check_sta==1)
- {
- if(start_flag==0)
- {
- start_flag=1;
- time1_temp=delay_off_time;
- time2_temp=0;
- }
- }else
- {
- start_flag=0;//不倒计时
- JR_OUT_PIN=1;//开启电源
- time1_temp=0;
- time2_temp=0;
- }
- }else //手动模式 使用者自行设置延时断电
- {
- OLED_ShowString(0,0,(u8*)"mode:manual",16);
- if(manual_mode_flag==1)
- {
- if(start_flag==0)
- {
- start_flag=1;
- time2_temp=true_rt_time;
- time1_temp=0;
- JR_OUT_PIN=1;//开启电源
- }
- }else
- {
- manual_mode_flag=0;
- time1_temp=0;
- JR_OUT_PIN=1;//开启电源
- time2_temp=0;
- start_flag=0;
- }
- }
- }
- }
- }
|