527 lines
8.7 KiB
C
527 lines
8.7 KiB
C
/*
|
|
* my_code.c
|
|
*
|
|
* Created on: Oct 2, 2021
|
|
* Author: wuwenfeng
|
|
*/
|
|
#include "my_code.h"
|
|
#include "button.h"
|
|
#include "ht1621.h"
|
|
|
|
|
|
extern ADC_HandleTypeDef hadc;
|
|
|
|
struct button key1,key2,key3,key4,overload;
|
|
|
|
|
|
#define set_filtering_times 50
|
|
struct
|
|
{
|
|
//ADC_HandleTypeDef *device;
|
|
int filtering_times;
|
|
unsigned long adc_filtering[2];
|
|
int adc_value[2];
|
|
}ADCC;
|
|
|
|
|
|
struct
|
|
{
|
|
uint32_t moto_run;
|
|
int pwm_run;
|
|
|
|
|
|
char moto1a;
|
|
char moto1b;
|
|
char moto2a;
|
|
char moto2b;
|
|
|
|
char moto1a_;
|
|
char moto1b_;
|
|
char moto2a_;
|
|
char moto2b_;
|
|
|
|
}moto;
|
|
|
|
void ds_in_or_out(char a)//change the io function
|
|
{
|
|
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
|
GPIO_InitStruct.Pin = HC595_DLK_Pin;
|
|
|
|
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
|
if(a==0)
|
|
{
|
|
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
|
}
|
|
if(a==1)
|
|
{
|
|
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
|
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
|
}
|
|
HAL_GPIO_Init(HC595_DLK_GPIO_Port, &GPIO_InitStruct);
|
|
}
|
|
uint8_t Read_Ds()
|
|
{
|
|
ds_in_or_out(0);
|
|
if(READ_HC595_DCK){return 0;}else{return 1;}
|
|
//return READ_HC595_DCK;
|
|
}
|
|
|
|
void Sand_Byte_to_595_2(uint8_t h)
|
|
{
|
|
ds_in_or_out(1);
|
|
HC595_DCK(0);
|
|
HC595_SCK2(0);
|
|
HC595_RCK(0);
|
|
for(char a=0;a<8;a++)
|
|
{
|
|
if((h<<a)&0x80)
|
|
{
|
|
HC595_DCK(1);
|
|
}else
|
|
{
|
|
HC595_DCK(0);
|
|
}
|
|
HC595_SCK2(1);
|
|
HC595_SCK2(0);
|
|
}
|
|
HC595_RCK(1);
|
|
HC595_RCK(0);
|
|
}
|
|
|
|
|
|
void hc2_sever()
|
|
{
|
|
char h=0;
|
|
if(dis_buff.led_run==1)
|
|
{
|
|
h|=0x01;
|
|
}
|
|
if(dis_buff.moto1a==1)
|
|
{
|
|
h|=0x02;
|
|
}
|
|
if(dis_buff.moto1b==1)
|
|
{
|
|
h|=0x04;
|
|
}
|
|
if(dis_buff.moto2a==1)
|
|
{
|
|
h|=0x08;
|
|
}
|
|
if(dis_buff.moto2b==1)
|
|
{
|
|
h|=0x10;
|
|
}
|
|
Sand_Byte_to_595_2(h);
|
|
}
|
|
|
|
void moto_server()
|
|
{
|
|
if(HAL_GetTick()>moto.moto_run)
|
|
{
|
|
moto.moto_run=HAL_GetTick()+10;
|
|
if(moto.moto1a!=moto.moto1a_)
|
|
{
|
|
if(moto.moto1a>moto.moto1a_)
|
|
{
|
|
moto.moto1a_++;
|
|
}
|
|
if(moto.moto1a<moto.moto1a_)
|
|
{
|
|
moto.moto1a_--;
|
|
}
|
|
}
|
|
if(moto.moto1b!=moto.moto1b_)
|
|
{
|
|
if(moto.moto1b>moto.moto1b_)
|
|
{
|
|
moto.moto1b_++;
|
|
}
|
|
if(moto.moto1b<moto.moto1b_)
|
|
{
|
|
moto.moto1b_--;
|
|
}
|
|
}
|
|
if(moto.moto2a!=moto.moto2a_)
|
|
{
|
|
if(moto.moto2a>moto.moto2a_)
|
|
{
|
|
moto.moto2a_++;
|
|
}
|
|
if(moto.moto2a<moto.moto2a_)
|
|
{
|
|
moto.moto2a_--;
|
|
}
|
|
}
|
|
if(moto.moto2b!=moto.moto2b_)
|
|
{
|
|
if(moto.moto2b>moto.moto2b_)
|
|
{
|
|
moto.moto2b_++;
|
|
}
|
|
if(moto.moto2b<moto.moto2b_)
|
|
{
|
|
moto.moto2b_--;
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
moto.pwm_run++;
|
|
if(moto.pwm_run==10)
|
|
{
|
|
moto.pwm_run=0;
|
|
}
|
|
|
|
if(moto.pwm_run<moto.moto1a_)
|
|
{
|
|
dis_buff.moto1a=1;
|
|
}else
|
|
{
|
|
dis_buff.moto1a=0;
|
|
}
|
|
if(moto.pwm_run<moto.moto1b_)
|
|
{
|
|
dis_buff.moto1b=1;
|
|
}else
|
|
{
|
|
dis_buff.moto1b=0;
|
|
}
|
|
if(moto.pwm_run<moto.moto2a_)
|
|
{
|
|
dis_buff.moto2a=1;
|
|
}else
|
|
{
|
|
dis_buff.moto2a=0;
|
|
}
|
|
if(moto.pwm_run<moto.moto2b_)
|
|
{
|
|
dis_buff.moto2b=1;
|
|
}else
|
|
{
|
|
dis_buff.moto2b=0;
|
|
}
|
|
|
|
}
|
|
|
|
void my_code()
|
|
{
|
|
uint32_t runtime=0,move=0;
|
|
uint8_t mode=0,overload_mode=0;
|
|
|
|
uint16_t overload_times=0;
|
|
long countdown=0;
|
|
long countdown_set=15000;
|
|
dis_buff.d_num[0]=8;
|
|
dis_buff.d_num[1]=8;
|
|
dis_buff.d_num[2]=8;
|
|
dis_buff.d_num[3]=8;
|
|
|
|
dis_buff.moto1a=0;
|
|
dis_buff.moto1b=0;
|
|
dis_buff.moto2a=0;
|
|
dis_buff.moto2b=0;
|
|
|
|
moto.moto_run=0;
|
|
moto.pwm_run=0;
|
|
moto.moto1a=0;
|
|
moto.moto1b=0;
|
|
moto.moto2a=0;
|
|
moto.moto2b=0;
|
|
|
|
moto.moto1a_=0;
|
|
moto.moto1b_=0;
|
|
moto.moto2a_=0;
|
|
moto.moto2b_=0;
|
|
|
|
HT1621_Init();
|
|
while(1)
|
|
{
|
|
//*adc读取 并计滤波 并计算温度*/
|
|
///*获取两个通道*/
|
|
for(char a=0;a<2;a++)
|
|
{
|
|
HAL_ADC_Start(&hadc);
|
|
while(HAL_ADC_PollForConversion(&hadc,0xffff)!=HAL_OK);
|
|
ADCC.adc_filtering[a]+=HAL_ADC_GetValue(&hadc); //把读到的值加到滤波缓存
|
|
}
|
|
HAL_ADC_Stop(&hadc);
|
|
///*开始滤波*/
|
|
ADCC.filtering_times+=1; //每采样加一次记一次
|
|
if(ADCC.filtering_times==set_filtering_times) //当达到设定的滤波采样次数
|
|
{
|
|
ADCC.filtering_times=0;
|
|
ADCC.adc_value[0]=ADCC.adc_filtering[0]/set_filtering_times; //就除于采样次数
|
|
ADCC.adc_filtering[0]=0;
|
|
ADCC.adc_value[1]=ADCC.adc_filtering[1]/set_filtering_times;
|
|
ADCC.adc_filtering[1]=0;
|
|
}
|
|
|
|
|
|
switch(mode)
|
|
{
|
|
case 0:
|
|
//Startup
|
|
if(HAL_GetTick()>move)
|
|
{
|
|
move=HAL_GetTick()+100;
|
|
countdown-=100;
|
|
if(countdown<0)
|
|
{
|
|
mode=1;
|
|
}
|
|
|
|
|
|
|
|
dis_buff.d_num[0]=((countdown/100)%10);
|
|
dis_buff.d_num[1]=((countdown/100)%10);
|
|
dis_buff.d_num[2]=((countdown/100)%10);
|
|
dis_buff.d_num[3]=((countdown/100)%10);
|
|
|
|
|
|
dis_buff.dot1=countdown>>3;
|
|
dis_buff.dot2=countdown>>4;
|
|
dis_buff.dot3=countdown>>5;
|
|
dis_buff.dot4=countdown>>6;
|
|
|
|
}
|
|
break;
|
|
case 1:
|
|
//standby
|
|
moto.moto1a=0;
|
|
moto.moto1b=0;
|
|
moto.moto2a=0;
|
|
moto.moto2b=0;
|
|
|
|
if(HAL_GetTick()>runtime)
|
|
{
|
|
runtime+=1000;
|
|
|
|
if(dis_buff.led_run==1)
|
|
{
|
|
dis_buff.led_run=0;
|
|
}else
|
|
{
|
|
dis_buff.led_run=1;
|
|
}
|
|
}
|
|
dis_buff.d_num[0]=0xff;
|
|
dis_buff.d_num[1]=0xff;
|
|
dis_buff.d_num[2]=0xff;
|
|
dis_buff.d_num[3]=0xff;
|
|
|
|
dis_buff.dot1=0;
|
|
dis_buff.dot2=0;
|
|
dis_buff.dot3=0;
|
|
dis_buff.dot4=0;
|
|
overload_times=0;
|
|
if(key2.code!=0)
|
|
{
|
|
mode=2;
|
|
countdown=countdown_set;
|
|
}
|
|
if(key3.code!=0)
|
|
{
|
|
mode=3;
|
|
countdown=countdown_set;
|
|
}
|
|
if(key1.code!=0)
|
|
{
|
|
mode=4;
|
|
countdown=10000;
|
|
}
|
|
break;
|
|
case 2:
|
|
moto.moto1a=10;
|
|
moto.moto1b=0;
|
|
moto.moto2a=10;
|
|
moto.moto2b=0;
|
|
if(HAL_GetTick()>move)
|
|
{
|
|
move=HAL_GetTick()+100;
|
|
if(dis_buff.led_run==1)
|
|
{
|
|
dis_buff.led_run=0;
|
|
}else
|
|
{
|
|
dis_buff.led_run=1;
|
|
}
|
|
countdown-=100;
|
|
if(countdown<0)
|
|
{
|
|
mode=1;
|
|
}
|
|
}
|
|
dis_buff.d_num[3]=(countdown/100)%10;
|
|
dis_buff.d_num[2]=(countdown/1000)%10;
|
|
dis_buff.d_num[1]=((countdown/10000)%10);
|
|
dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
|
|
dis_buff.dot3=1;
|
|
|
|
if(key3.code!=0)
|
|
{
|
|
mode=3;
|
|
countdown=countdown_set-countdown;
|
|
}
|
|
if(key4.code!=0)
|
|
{
|
|
mode=1;
|
|
}
|
|
if(overload.code!=0)
|
|
{
|
|
overload_times+=1;
|
|
}
|
|
if(overload_times>2)
|
|
{
|
|
overload_mode=2;
|
|
mode=5;
|
|
}
|
|
break;
|
|
case 3:
|
|
moto.moto1a=0;
|
|
moto.moto1b=10;
|
|
moto.moto2a=0;
|
|
moto.moto2b=10;
|
|
if(HAL_GetTick()>move)
|
|
{
|
|
move=HAL_GetTick()+100;
|
|
if(dis_buff.led_run==1)
|
|
{
|
|
dis_buff.led_run=0;
|
|
}else
|
|
{
|
|
dis_buff.led_run=1;
|
|
}
|
|
countdown-=100;
|
|
if(countdown<0)
|
|
{
|
|
mode=1;
|
|
}
|
|
}
|
|
dis_buff.d_num[3]=(countdown/100)%10;
|
|
dis_buff.d_num[2]=(countdown/1000)%10;
|
|
dis_buff.d_num[1]=((countdown/10000)%10);
|
|
dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
|
|
dis_buff.dot3=1;
|
|
if(key2.code!=0)
|
|
{
|
|
mode=2;
|
|
countdown=countdown_set-countdown;
|
|
}
|
|
if(key4.code!=0)
|
|
{
|
|
mode=1;
|
|
}
|
|
if(overload.code!=0)
|
|
{
|
|
overload_times+=1;
|
|
}
|
|
if(overload_times>2)
|
|
{
|
|
overload_mode=3;
|
|
mode=5;
|
|
}
|
|
break;
|
|
case 4:
|
|
//setting mode
|
|
dis_buff.led_run=0;
|
|
if(HAL_GetTick()>move)
|
|
{
|
|
move=HAL_GetTick()+100;
|
|
if(dis_buff.dot1==1)
|
|
{
|
|
dis_buff.dot1=0;
|
|
}else
|
|
{
|
|
dis_buff.dot1=1;
|
|
}
|
|
countdown-=100;
|
|
if(countdown<0)
|
|
{
|
|
mode=1;
|
|
}
|
|
|
|
if(key2.code<0){countdown_set+=1000;countdown=10000;}
|
|
if(key3.code<0){countdown_set-=1000;countdown=10000;}
|
|
|
|
}
|
|
|
|
if(key2.code>0){countdown_set+=100;countdown=10000;}
|
|
if(key3.code>0){countdown_set-=100;countdown=10000;}
|
|
if(countdown_set<100){countdown_set=100;}
|
|
if(countdown_set>60000){countdown_set=60000;}
|
|
|
|
if(key1.code!=0){mode=1;}
|
|
dis_buff.d_num[3]=(countdown_set/100)%10;
|
|
dis_buff.d_num[2]=(countdown_set/1000)%10;
|
|
dis_buff.d_num[1]=((countdown_set/10000)%10);
|
|
dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
|
|
dis_buff.dot3=1;
|
|
|
|
break;
|
|
case 5:
|
|
//overload
|
|
moto.moto1a=0;
|
|
moto.moto1b=0;
|
|
moto.moto2a=0;
|
|
moto.moto2b=0;
|
|
dis_buff.led_run=1;
|
|
overload_times=0;
|
|
dis_buff.d_num[3]=(countdown/100)%10;
|
|
dis_buff.d_num[2]=(countdown/1000)%10;
|
|
dis_buff.d_num[1]=((countdown/10000)%10);
|
|
dis_buff.d_num[1]=dis_buff.d_num[1]==0?255:dis_buff.d_num[1];
|
|
dis_buff.dot3=1;
|
|
if(key4.code!=0){mode=1;}
|
|
|
|
if(key2.code!=0)
|
|
{
|
|
mode=2;
|
|
if(overload_mode==2)
|
|
{
|
|
|
|
}else
|
|
{
|
|
countdown=countdown_set-countdown;
|
|
}
|
|
|
|
}
|
|
if(key3.code!=0)
|
|
{
|
|
mode=3;
|
|
if(overload_mode==3)
|
|
{
|
|
|
|
}else
|
|
{
|
|
countdown=countdown_set-countdown;
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
}
|
|
|
|
if(ADCC.adc_value[0]>600||ADCC.adc_value[1]>600)
|
|
{
|
|
GEI_BUTTON_CODE(&overload,1);
|
|
}else
|
|
{
|
|
GEI_BUTTON_CODE(&overload,0);
|
|
}
|
|
|
|
GEI_BUTTON_CODE(&key1,dis_buff.button_flag[0]);
|
|
GEI_BUTTON_CODE(&key2,dis_buff.button_flag[1]);
|
|
GEI_BUTTON_CODE(&key3,dis_buff.button_flag[2]);
|
|
GEI_BUTTON_CODE(&key4,dis_buff.button_flag[3]);
|
|
//display_and_button_loop();
|
|
HT1621_Display();
|
|
hc2_sever();
|
|
moto_server();
|
|
}
|
|
}
|