Files
checker_m4/source/main/mymain.c

395 lines
7.6 KiB
C
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "mymain.h"
#include "main.h"
#include "board.h"
#include "debug.h"
#include "core_delay.h"
#include "string.h"
#include "stdio.h"
typedef struct{
pwm_def *pwm;
int key_old;
int key;
int key1_old;
int key1;
int sen;
char str_buff[200];
}mymain_def;
static mymain_def g_m={0};
void *mymain_init(void)
{
delay_init();
// m.pwm=dev_get("pwm");
// m.pwm->init(m.pwm);
//DBG_LOG("pwm start.");
// m.pwm->start(m.pwm,1);
GPIO_InitTypeDef init={0};
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
__HAL_RCC_GPIOI_CLK_ENABLE();
// 输出脚
init.Mode = GPIO_MODE_OUTPUT_PP;
init.Pull = GPIO_PULLUP;
init.Pin = GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9;
PERIPH_LOCK(GPIOF);
HAL_GPIO_Init(GPIOF, &init);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_7|GPIO_PIN_8|GPIO_PIN_9,GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
// 指示灯PF1(绿) PG15(红)
init.Pin = GPIO_PIN_1;
PERIPH_LOCK(GPIOF);
HAL_GPIO_Init(GPIOF, &init);
HAL_GPIO_WritePin(GPIOF,GPIO_PIN_1,GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
init.Pin = GPIO_PIN_15;
PERIPH_LOCK(GPIOG);
HAL_GPIO_Init(GPIOG, &init);
HAL_GPIO_WritePin(GPIOG,GPIO_PIN_15,GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOG);
// 到位传感器
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_6;
PERIPH_LOCK(GPIOI);
HAL_GPIO_Init(GPIOI, &init);
PERIPH_UNLOCK(GPIOI);
// 按键
// PG3 key2 批检仪使用
// PH7 key1 赋码仪批检仪使用
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_3;
PERIPH_LOCK(GPIOG);
HAL_GPIO_Init(GPIOG, &init);
PERIPH_UNLOCK(GPIOG);
init.Pin = GPIO_PIN_7;
PERIPH_LOCK(GPIOH);
HAL_GPIO_Init(GPIOH, &init);
PERIPH_UNLOCK(GPIOH);
// 拨码
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_1|GPIO_PIN_6;
PERIPH_LOCK(GPIOE);
HAL_GPIO_Init(GPIOE, &init);
PERIPH_UNLOCK(GPIOE);
init.Mode = GPIO_MODE_INPUT;
init.Pull = GPIO_NOPULL;
init.Pin = GPIO_PIN_8|GPIO_PIN_9;
PERIPH_LOCK(GPIOH);
HAL_GPIO_Init(GPIOH, &init);
PERIPH_UNLOCK(GPIOH);
return &g_m;
}
void send_str_to_a7(const char *str);
// 控制输出脚1=PF9;2=PF8;3=PF7
void out_set(int index,int power)
{
mymain_def *m=&g_m;
PERIPH_LOCK(GPIOF);
HAL_GPIO_WritePin(GPIOF,1<<(10-index),power?GPIO_PIN_SET:GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
sprintf(m->str_buff,"out_set:%d,%d",index,power);
send_str_to_a7(m->str_buff);
}
// 控制ledr
void ledr_set(int power)
{
PERIPH_LOCK(GPIOG);
HAL_GPIO_WritePin(GPIOG,1<<15,power?GPIO_PIN_SET:GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOG);
send_str_to_a7("ledr seted");
}
// 控制ledg
void ledg_set(int power)
{
PERIPH_LOCK(GPIOF);
HAL_GPIO_WritePin(GPIOF,1<<1,power?GPIO_PIN_SET:GPIO_PIN_RESET);
PERIPH_UNLOCK(GPIOF);
send_str_to_a7("ledg seted");
}
// 发送一个脉冲
void moter_plused(int us)
{
// out3
//HAL_GPIO_TogglePin(GPIOF,1<<7);
// out1
HAL_GPIO_TogglePin(GPIOF,1<<9);
delay_us(us);
}
// PF7-PWM PF8-DST PI6_STOP
// 低电平向下,向下为正
void moter_run(int pwm,int count)
{
int use_sen=0;
if(count>0){
HAL_GPIO_WritePin(GPIOF,1<<8,GPIO_PIN_RESET);
}else{
HAL_GPIO_WritePin(GPIOF,1<<8,GPIO_PIN_SET);
count=-count;
use_sen=1;
}
int max_=500;
int min_=35;
int temp=max_;
int add_count=0;
int step_sub=0;
for(int i=0;i<count;i++)
{
//已到位则退出
if((HAL_GPIO_ReadPin(GPIOI,1<<6)==RESET)&&(use_sen!=0)) return;
moter_plused(temp);
if(pwm==0){
// pwm==0时 自动加减速
if(i<count/2){
if(temp>min_) {step_sub+=temp; if(step_sub>=1000) {temp--; step_sub=0;}}
else add_count++;
}else{
if(add_count>0) add_count--;
else {step_sub+=temp; if(step_sub>=1000) {temp++; step_sub=0;}}
}
}else{
// pwm!=0时 匀速
temp=1000000/pwm;
}
}
// out3
//HAL_GPIO_WritePin(GPIOF,1<<7,GPIO_PIN_SET);
// out1
HAL_GPIO_WritePin(GPIOF,1<<9,GPIO_PIN_SET);
}
static int str_ainttoi(const char *s)
{
int ret=0;
int sig=1;
if(*s=='-'){
s++;
sig=-1;
}
while(*s)
{
if(*s>='0'&&*s<='9')
{
ret*=10;
ret+=*s-'0';
}
else return ret*sig;
s++;
}
return ret*sig;
}
int read_key(void *mymain)
{
mymain_def *m=mymain;
int key=0,key2=0;
key=HAL_GPIO_ReadPin(GPIOG,1<<3);
if(key!=m->key_old)
{
//连续采集5次都为相反电平时才输出此电平
for(int i=0;i<2;i++)
{
delay_ms(20);
key2=HAL_GPIO_ReadPin(GPIOG,1<<3);
if(key!=key2) return key2;
}
return key;
}
return m->key_old;
}
int read_key1(void *mymain)
{
mymain_def *m=mymain;
int key=0,key2=0;
key=HAL_GPIO_ReadPin(GPIOH,1<<7);
if(key!=m->key1_old)
{
//连续采集5次都为相反电平时才输出此电平
for(int i=0;i<2;i++)
{
delay_ms(20);
key2=HAL_GPIO_ReadPin(GPIOH,1<<7);
if(key!=key2) return key2;
}
return key;
}
return m->key1_old;
}
void mymain_scan(void *mymain)
{
mymain_def *m=mymain;
m->key=read_key(mymain);
m->key1=read_key1(mymain);
m->sen=HAL_GPIO_ReadPin(GPIOI,1<<6);
if(m->key_old!=m->key){
if(m->key==RESET)
{
send_str_to_a7("key press");
}else
{
send_str_to_a7("key up");
}
m->key_old=m->key;
}
if(m->key1_old!=m->key1){
if(m->key1==RESET)
{
send_str_to_a7("ke1 press");
}else
{
send_str_to_a7("ke1 up");
}
m->key1_old=m->key1;
}
}
int read_sw(void *mymain)
{
int ret=0;
ret|=HAL_GPIO_ReadPin(GPIOE,1<<6);
ret|=HAL_GPIO_ReadPin(GPIOH,1<<9)<<1;
ret|=HAL_GPIO_ReadPin(GPIOE,1<<1)<<2;
ret|=HAL_GPIO_ReadPin(GPIOH,1<<8)<<3;
return ret;
}
void mymain_loop(void *mymain,char *cmd_str)
{
mymain_def *m=mymain;
if(strncmp(cmd_str,"moter ",6)==0)
{
// 上升下降
int count=str_ainttoi(cmd_str+6);
if(count!=0){
moter_run(0,count);
if(count>0){
send_str_to_a7("moter down");
}else{
send_str_to_a7("moter up");
}
}
}else if(strncmp(cmd_str,"moterinit",9)==0)
{
// 初始化
moter_run(2000,-25000);
send_str_to_a7("moterinited");
}
else if(strncmp(cmd_str,"motertest ",10)==0)
{
// 初始化
int count=str_ainttoi(cmd_str+10);
moter_run(2000,count);
send_str_to_a7("motertest end");
}
else if(strncmp(cmd_str,"readsw",6)==0)
{
int sw=read_sw(mymain);
sprintf(m->str_buff,"readsw %d",sw);
send_str_to_a7(m->str_buff);
}
else if(strncmp(cmd_str,"readkey",7)==0){
if(m->key)
send_str_to_a7("key up");
else
send_str_to_a7("key press");
}
else if(strncmp(cmd_str,"readke1",7)==0){
if(m->key1)
send_str_to_a7("ke1 up");
else
send_str_to_a7("ke1 press");
}
else if(strncmp(cmd_str,"readsen",7)==0){
// 光感
if(m->sen)
send_str_to_a7("sen high");
else
send_str_to_a7("sen low");
}
else if(strncmp(cmd_str,"out",3)==0){
if((cmd_str[3]>='1')&&(cmd_str[3]<='3')){
if(strncmp(cmd_str+5,"on",2)==0){
out_set(cmd_str[3]-'0',1);
}else if(strncmp(cmd_str+5,"off",3)==0){
out_set(cmd_str[3]-'0',0);
}
}
}
else if(strncmp(cmd_str,"ledr",4)==0){
if(strncmp(cmd_str+5,"on",2)==0){
ledr_set(1);
}else{
ledr_set(0);
}
}
else if(strncmp(cmd_str,"ledg",4)==0){
if(strncmp(cmd_str+5,"on",2)==0){
ledg_set(1);
}else{
ledg_set(0);
}
}
else if(strncmp(cmd_str,"build",5)==0){
send_str_to_a7("buildtime: " __DATE__ " " __TIME__);
}else{
send_str_to_a7("unknown cmd");
}
}
void mymain_loop__(void *mymain,char *cmd_str)
{
while(1){
HAL_GPIO_TogglePin(GPIOF,1<<7);
HAL_GPIO_TogglePin(GPIOF,1<<8);
HAL_GPIO_TogglePin(GPIOF,1<<9);
HAL_Delay(2000);
}
}