123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #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"
- #include "HX711.h"
- #define HwInput PBin(7) //红外传感器输入
- #define KEY1 PAin(0) // 手动控制加热
- #define JR_OUT_PIN PBout(12) //控制加热输出
- #define CS_OUT_PIN PBout(13) //控制出水 电磁阀
- void Hw_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //使能端口时钟
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; // 端口配置
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //上拉输入
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOC13
- }
- #define Trig PBout(10) // 超声波测距
- #define CW_ECHO1 PBin(11)
- u16 Distance=0;
- //超声波端口初始化
- void Wave_IO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2 , ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB , ENABLE);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PB0接TRIG
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设为推挽输出模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化外设GPIO
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //接ECH0
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //设为输入
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- TIM_DeInit(TIM2);
- TIM_TimeBaseStructure.TIM_Period=65535; /* 自动重装载寄存器周期的值(计数值) */
- /* 累计 TIM_Period个频率后产生一个更新或者中断 */
- TIM_TimeBaseStructure.TIM_Prescaler= (360 - 1); /* 时钟预分频数 72M/360 */
- TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /* 采样分频 */
- TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /* 向上计数模式 */
- TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
- TIM_ClearFlag(TIM2, TIM_FLAG_Update); /* 清除溢出中断标志 */
-
-
- // NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- //TIM2_NVIC_Configuration();//定时器初始化
- //TIM2_Configuration();
- }
- //void TIM2_IRQHandler()
- //{
- // static u8 st;
- // st=TIM_GetFlagStatus(TIM2, TIM_IT_Update);
- // if(st!=0)
- // {
- // TIM_ClearFlag(TIM2, TIM_IT_Update);
- // }
- //}
- //测距
- //开始测距,发送一个>10us的脉冲,然后测量返回的高电平时间
- u16 Wave_Start(void)
- {
- u32 Distance;
- u32 timeout=0;
- Trig=1;
- delay_us(20); //输入一个10us的高电平
- Trig=0;
- TIM_SetCounter(TIM2,0);
- timeout=0;
- while(!CW_ECHO1) //等待高电平
- {
- if(++timeout>60000)break;
- delay_us(1);
- }
- TIM_Cmd(TIM2, ENABLE); //开启时钟
- timeout=0;
- while(CW_ECHO1) //等待低电平
- {
- if(++timeout>600000)break;
- delay_us(1);
- }
- TIM_Cmd(TIM2, DISABLE); //定时器2失能
- Distance=TIM_GetCounter(TIM2)*5*34/2000; //计算距离 厘米cm
- TIM_SetCounter(TIM2,0);
- if(Distance>200)Distance=200;
- return Distance;
- }
- u8 jr_flag=0;
- u8 get_cnt=0;
- u8 hw_cnt=0;
- u8 cs_out_flag=0; //出水标志
- u16 sw_val=0;//水位值
- u8 cs_out_zs_flag=0;//出水自锁标志位
- char showbuff[16];
- short temp_val=0;
- u8 mode_flag=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();
- DS18B20_Init();
- Wave_IO_Init();
- while(1)
- {
-
- if(++get_cnt>100) //采集时间计数器
- {
- get_cnt=0;
- temp_val = DS18B20_Get_Temp();//采集温度值
- sprintf(showbuff,"temp:%2d.%1d",temp_val/10,temp_val%10); //打印成字符串
- OLED_ShowString(0,2,(u8*)showbuff,16); //显示
- }
- if(mode_flag) //自动模式
- {
- if(HwInput==0) //检测是否有杯子
- {
- if(++hw_cnt>20) //计数100ms
- {
- sw_val = Wave_Start();
- if(sw_val>4)
- {
- CS_OUT_PIN=0;//控制出水
- }else
- {
- CS_OUT_PIN=1;//停止输出
- }
- cs_out_flag=1;
- }
- }else
- {
- hw_cnt=0;
- cs_out_flag=0;
- CS_OUT_PIN=1;//停止输出
- }
-
- if(jr_flag) //加热开关flag
- {
- JR_OUT_PIN=0;
- OLED_ShowString(0,4,(u8*)"heater:on ",16); //显示
- }else
- {
- JR_OUT_PIN=1;
- OLED_ShowString(0,4,(u8*)"heater:off",16); //显示
- }
- }else //手动模式
- {
- if(KEY1==0) //按键检测
- {
- delay_ms(5);
- if(KEY1==0)
- {
- jr_flag^=1; //控制加热
- while(!KEY1); //等待松手
- }
- }
- }
- delay_ms(5);//延时
-
- }
- }
|