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;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- #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;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- TIM_DeInit(TIM2);
- TIM_TimeBaseStructure.TIM_Period=65535;
-
- TIM_TimeBaseStructure.TIM_Prescaler= (360 - 1);
- 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_InitStructure.NVIC_IRQChannel = TIM2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
-
-
- }
- u16 Wave_Start(void)
- {
- u32 Distance;
- u32 timeout=0;
- Trig=1;
- delay_us(20);
- 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);
- Distance=TIM_GetCounter(TIM2)*5*34/2000;
- 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);
- delay_init();
- LED_Init();
- 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)
- {
- 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)
- {
- 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);
-
- }
- }
|