1.bk 3435 作为配件开发模式 时, 开启 ADC_DRIVER 宏定义
这里 我们需要使用 adc 判断端口 变化的电压,从而 使电机 正反转
2. 定义 ADC 采用次数 LOCK_ADC_SAMPLE_TIME
开始 编写:
头文件:
bk_lock_adc.h
#ifndef __BK_LOCK_ADC_H__
#define __BK_LOCK_ADC_H__
#ifdef __cplusplus
extern "C"
{
#endif
/*********************************************************************
* INCLUDES
*/
#include "bk_common.h"
typedef enum _MOTOR_STATUS{
MOTOR_STOP=0x00,
MOTOR_POSITIVE=0x01,
MOTOR_REVERSE=0x02,
MOTOR_DOWN=0x03
}MOTOR_STATUS;
typedef enum _MOTOR_PROCESS_STATUS{
MOTOR_UNKOWN=0x00,
MOTOR_SURE=0x01,
MOTOR_WORK=0x02
}MOTOR_PROCESS_STATUS;
void lock_adc_sample_start(uint8_t status);
void Set_motor_lock(uint8_t status);
void Set_motor_processing(MOTOR_PROCESS_STATUS status);
void turn_it(MOTOR_STATUS status);
#ifdef __cplusplus
}
#endif
#endif
实现文件
bk_lock_adc.c
#include "lock_dp_report.h"
#include "bk_lock_adc.h"
#include "bk_common.h"
#define LOCK_ADC_SAMPLE_TIME 20
static uint32_t s_sample_idx = 0;
static uint32_t s_adc_value[LOCK_ADC_SAMPLE_TIME] = {0};
static uint32_t s_adc_value_sum = 0;
volatile uint8_t motor_processing_status;
uint8_t MOTOR_LOCK_STATUS = 0xff;//默认 上 锁
#define ADC_GPIO_CTL_PIN0 GPIOB_2 //P12
#define ADC_GPIO_CTL_PIN1 GPIOB_1 //P13 -> P11
//电机引脚配置
void motor_gpio_config(){
gpio_config(ADC_GPIO_CTL_PIN0, OUTPUT, PULL_NONE);
gpio_set(ADC_GPIO_CTL_PIN0,LEVEL_LOW);
gpio_config(ADC_GPIO_CTL_PIN1, OUTPUT, PULL_NONE);
gpio_set(ADC_GPIO_CTL_PIN1,LEVEL_LOW);
}
//自定义增加adc
void lock_adc_init(){
adc_init(0x03, 0);//channel 3 GPIO33
motor_gpio_config();
//Set_motor_processing(MOTOR_UNKOWN);
Set_motor_lock(0);//FALSE->关
}
void clear_adc_sample_data(){
s_sample_idx = 0;
memset(s_adc_value, 0x00, LOCK_ADC_SAMPLE_TIME);
s_adc_value_sum = 0;
}
void lock_adc_sample_start(uint8_t status){
clear_adc_sample_data();
bk_timer_start_0(SUBLE_TIMER105, 100, BK_TIMER_COUNT_ENDLESS);
BK_PRINTF("lock_adc_sample_start");
Set_motor_lock(status);//FALSE->关
}
void lock_adc_get_value_outtime_handler(){
if(s_sample_idx < LOCK_ADC_SAMPLE_TIME) {
s_adc_value[s_sample_idx] = adc_get_value();
}else{
suble_util_shell_sort((void*)s_adc_value, LOCK_ADC_SAMPLE_TIME);
for(uint8_t idx=5; idx<=14; idx++) {
//BK_PRINTF("idx:%d,s_adc_value: %d", idx,s_adc_value[idx]);
s_adc_value_sum += s_adc_value[idx];
}
uint32_t adc_value = s_adc_value_sum/10;
if(adc_value > 700){
if(!MOTOR_LOCK_STATUS){
turn_it(MOTOR_REVERSE);
}else{
turn_it(MOTOR_POSITIVE);
}
}
if(adc_value > 600 && adc_value < 650){
turn_it(MOTOR_DOWN);
bk_timer_stop_0(SUBLE_TIMER105);
MOTOR_LOCK_STATUS == 0 ? 1:0;
Set_motor_lock(MOTOR_LOCK_STATUS);
}
BK_PRINTF("adc_value: %d", adc_value);
clear_adc_sample_data();
//bk_timer_stop_0(SUBLE_TIMER105);
}
s_sample_idx++;
}
void Set_motor_processing(MOTOR_PROCESS_STATUS status){
motor_processing_status = status;
}
void Set_motor_lock(uint8_t status){
MOTOR_LOCK_STATUS = status;
}
void turn_it(MOTOR_STATUS status){
switch(status)
{
case MOTOR_STOP:
gpio_set(ADC_GPIO_CTL_PIN0,LEVEL_HIGH);
gpio_set(ADC_GPIO_CTL_PIN1,LEVEL_HIGH);
//BK_PRINTF("MOTOR_STOP \r\n");
break;
case MOTOR_POSITIVE://当前接线转向,关门
gpio_set(ADC_GPIO_CTL_PIN0,LEVEL_HIGH);
gpio_set(ADC_GPIO_CTL_PIN1,LEVEL_LOW);
//BK_PRINTF("MOTOR_POSITIVE \r\n");
break;
case MOTOR_REVERSE://当前接线转向,开门
gpio_set(ADC_GPIO_CTL_PIN0,LEVEL_LOW);
gpio_set(ADC_GPIO_CTL_PIN1,LEVEL_HIGH);
//BK_PRINTF("MOTOR_REVERSE \r\n");
break;
case MOTOR_DOWN:
gpio_set(ADC_GPIO_CTL_PIN0,LEVEL_LOW);
gpio_set(ADC_GPIO_CTL_PIN1,LEVEL_LOW);
//BK_PRINTF("MOTOR_DOWN \r\n");
break;
default:
break;
}
}
那么,在哪里调用呢?