本帖最后由 Chenxinyes 于 2023-7-3 12:57 编辑
#include "zf_common_headfile.h"
#include "hal_device_registers.h"
#include "zf_driver_timer.h"
#include "isr.h"
#include "zf_driver_adc.h"
#include "zf_driver_gpio.h"
#include "hal_tim.h"
//#define IR_SENSOR_PIN GPIO_Pin_4 // 替换为实际连接到红外传感器的引脚
//#define BUZZER_PIN GPIO_Pin_9 // 替换为实际连接到蜂鸣器的引脚
#define BEEP (E9 )
#define sensor (B4 )
static ADC_Type *adc_module = ADC1;
static adc_channel_enum adc_channel = ADC1_CH0_A0;
void adc_module_init()
{
RCC_EnableAPB2Periphs(RCC_APB2_PERIPH_ADC1, true);
ADC_Init_Type adc_init;
adc_init.Resolution = ADC_8BIT;
adc_init.ConvMode = ADC_ConvMode_SingleSlot;
adc_init.Align = ADC_Align_Right;
ADC_Init(adc_module, &adc_init);
ADC_Enable(adc_module, true);
ADC_DoAutoCalib(adc_module);
}
uint16 adc_module_convert()
{
uint32_t data = 0;
ADC_EnableSeqSlot(adc_module, 0, 1);
ADC_DoSwTrigger(adc_module, true);
while ((ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE) !=
(ADC_GetStatus(adc_module) & (ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE)));
ADC_ClearStatus(adc_module, (ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE));
data = ADC_GetSlotConvResult(adc_module, 0, NULL);
return (data >> ADC_8BIT);
}
int main(void)
{
clock_init(SYSTEM_CLOCK_120M);
debug_init();
adc_init(ADC1_CH0_A0,ADC_8BIT);
gpio_init(B4, GPI, GPIO_LOW, GPI_FLOATING_IN);
gpio_init(E9, GPO, GPIO_HIGH, GPO_PUSH_PULL);
adc_module_init();
timer_init(TIM_1, TIMER_MS);
// uint32_t start_time = 0;
// bool buzzer_triggered = false;
timer_start(TIM_1);
uint32_t start_time = 0;
while (1)
{
uint16_t distance = adc_module_convert();
if (distance >= 20 && distance <= 80)
{
if (gpio_get_level(BEEP)==GPIO_HIGH)
{
// start_time = timer_get(TIM_1);
gpio_set_level(BEEP, GPIO_HIGH);
}
uint32_t current_time = timer_get(TIM_1);
if ((current_time - start_time) >= 5000)
{
gpio_high(BEEP);
system_delay_ms(1000);
gpio_low(BEEP);
// gpio_set_level(BEEP, GPIO_LOW);
}
}
else
{
gpio_set_level(BEEP, GPIO_LOW);
}
}
}
#include "zf_common_headfile.h"
#include "hal_device_registers.h"
#include "zf_driver_timer.h"
#include "isr.h"
#include "zf_driver_gpio.h"
#define IR_SENSOR_PIN GPIO_Pin_4 // 替换为实际连接到红外传感器的引脚
#define BUZZER_PIN GPIO_Pin_9 // 替换为实际连接到蜂鸣器的引脚
static ADC_Type *adc_module = ADC1;
static adc_channel_enum adc_channel = ADC1_CH0_A0;
void adc_module_init()
{
RCC_EnableAPB2Periphs(RCC_APB2_PERIPH_ADC1, true);
ADC_Init_Type adc_init;
adc_init.Resolution = ADC_8BIT;
adc_init.ConvMode = ADC_ConvMode_SingleSlot;
adc_init.Align = ADC_Align_Right;
ADC_Init(adc_module, &adc_init);
ADC_Enable(adc_module, true);
ADC_DoAutoCalib(adc_module);
}
uint16 adc_module_convert()
{
uint32_t data = 0;
ADC_EnableSeqSlot(adc_module, 0, 1);
ADC_DoSwTrigger(adc_module, true);
while ((ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE) !=
(ADC_GetStatus(adc_module) & (ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE)));
ADC_ClearStatus(adc_module, (ADC_STATUS_CONV_SLOT_DONE | ADC_STATUS_CONV_SAMPLE_DONE));
data = ADC_GetSlotConvResult(adc_module, 0, NULL);
return (data >> ADC_8BIT);
}
int main(void)
{
clock_init(SYSTEM_CLOCK_120M);
debug_init();
gpio_init(B4, GPI, 0, GPI_FLOATING_IN);
gpio_init(E9, GPO, 0, GPO_PUSH_PULL);
adc_module_init();
while (1)
{
uint16_t distance = adc_module_convert();
if (distance >= 20 && distance <= 80)
{
uint32_t start_time = timer_get(TIM_1);
while (distance >= 20 && distance <= 80)
{
distance = adc_module_convert();
uint32_t current_time = timer_get(TIM_1);
if ((current_time - start_time) >= 5000)
{
gpio_low(E9);
system_delay_ms(1000);
gpio_high(E9);
break;
}
}
}
}
}有没有大佬能看看这段代码逻辑有没有问题啊,功能一直实现不了
|