本帖最后由 wuboy19 于 2024-3-25 00:29 编辑
引言
首先非常感谢安信可,园长,在免费申请Rd-03E模块时候多给了几个名额,当然很幸运的里面有我,在学校已经实现了所要求的功能,本文章主要是利用Ai-M61-32S-Kit开发板,实现了对Rd-03E距离和是否有人移动,直观效果就是人做面前灯一直亮着,人走后台灯熄灭,之前一直用充电宝加上usb小夜灯照明,所以这次就是即实现了要求,又可以应用于日常生活。
具体实施
开发环境
Clion加上博流sdk
目录结构如下
硬件及实物图:
usb夜灯用了nmos驱动
具体代码
#include "bflb_mtimer.h"
#include "bflb_uart.h"
#include "board.h"
#include "bflb_gpio.h"
#include "bflb_pwm_v2.h"
#include "bflb_clock.h"
#include <string.h>
#include <stdio.h>
void gpio_init();
void UART1_gpio_init();
void UART1_Init();
void led_fanzhuang();
void PWM_SG90();
struct bflb_device_s *pwm;
struct bflb_device_s *uart1_structure;
struct bflb_device_s *gpio;
static volatile uint8_t tx_end_flag = 0;
int sg90_pwm=0;
//static uint8_t uart_txbuf[128] = { 0 };
uint8_t data_count = 0;
uint8_t i_data = 0;
uint8_t distance;
char uart_rxbuf[1024] = {0};
char *uart_rx_wxw=NULL;
void uart_isr(int irq, void *arg)
{
uint32_t intstatus = bflb_uart_get_intstatus(uart1_structure);
if (intstatus & UART_INTSTS_RX_FIFO) {
while (bflb_uart_rxavailable(uart1_structure)) {
uart_rxbuf[data_count++] = bflb_uart_getchar(uart1_structure);
}
}
if (intstatus & UART_INTSTS_RTO) {
bflb_uart_int_clear(uart1_structure, UART_INTCLR_RTO);
for(i_data=data_count;uart_rxbuf[i_data]!='\0';i_data++)
{
uart_rxbuf[i_data]='\0';
}
uart_rx_wxw=uart_rxbuf;
data_count=0;
}
}
int main(void)
{
board_init();
gpio_init();
UART1_Init();
// PWM_SG90();
while (1)
{
if(uart_rx_wxw!=NULL)
{
if((*(uart_rx_wxw)==0XAA)&&(*(uart_rx_wxw+1)==0XAA)&&(*(uart_rx_wxw+5)==0X55)&&(*(uart_rx_wxw+6)==0X55))
{
distance=*(uart_rx_wxw+4)<<8|*(uart_rx_wxw+3);
if(distance<100)
{
if((*(uart_rx_wxw+2)==0x01)||(*(uart_rx_wxw+2)==0x02))
{
bflb_gpio_set(gpio, GPIO_PIN_14);
}
else
{
bflb_gpio_reset(gpio, GPIO_PIN_14);
}
}
else
{
bflb_gpio_reset(gpio, GPIO_PIN_14);
}
printf("%d %d\n",*(uart_rx_wxw+2),distance);
}
uart_rx_wxw=NULL;
}
bflb_mtimer_delay_ms(50);
}
// led_fanzhuang();
}
void gpio_init()
{
gpio = bflb_device_get_by_name("gpio");
printf("gpio output\r\n");//12为板载
// bflb_gpio_init(gpio, GPIO_PIN_12, GPIO_OUTPUT | GPIO_INPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_init(gpio, GPIO_PIN_14, GPIO_OUTPUT | GPIO_INPUT | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_0);
bflb_gpio_reset(gpio, GPIO_PIN_14);
}
void UART1_gpio_init()
{
struct bflb_device_s *gpio_uart1;
gpio_uart1 = bflb_device_get_by_name("gpio");
bflb_gpio_uart_init(gpio_uart1, GPIO_PIN_23, GPIO_UART_FUNC_UART1_TX);
bflb_gpio_uart_init(gpio_uart1, GPIO_PIN_24, GPIO_UART_FUNC_UART1_RX);
}
void UART1_Init()
{
UART1_gpio_init();
uart1_structure = bflb_device_get_by_name("uart1");
struct bflb_uart_config_s cfg;
cfg.baudrate = 256000;
cfg.data_bits = UART_DATA_BITS_8;
cfg.stop_bits = UART_STOP_BITS_1;
cfg.parity = UART_PARITY_NONE;
cfg.flow_ctrl = 0;
cfg.tx_fifo_threshold = 0;
cfg.rx_fifo_threshold = 0;
bflb_uart_init(uart1_structure, &cfg);
bflb_uart_txint_mask(uart1_structure, true);
bflb_uart_rxint_mask(uart1_structure, false);
//#
// bflb_uart_feature_control(uart1_structure, UART_CMD_SET_TX_FREERUN, false);
// bflb_uart_feature_control(uart1_structure, UART_CMD_SET_TX_END_INTERRUPT, true);
//
bflb_irq_attach(uart1_structure->irq_num, uart_isr, NULL);
bflb_irq_enable(uart1_structure->irq_num);
}
void led_fanzhuang()
{
static int a=0;
if(a==2)
{
a=0;
}
if(a%2==0)
{
bflb_gpio_set(gpio, GPIO_PIN_12);
}
else
{
bflb_gpio_reset(gpio, GPIO_PIN_12);
}
a++;
}
void PWM_SG90()
{
struct bflb_device_s *gpio_PWM;
gpio_PWM = bflb_device_get_by_name("gpio");
bflb_gpio_init(gpio_PWM, GPIO_PIN_25, GPIO_FUNC_PWM0 | GPIO_ALTERNATE | GPIO_PULLUP | GPIO_SMT_EN | GPIO_DRV_1);
pwm = bflb_device_get_by_name("pwm_v2_0");
struct bflb_pwm_v2_config_s cfg = {
.clk_source = BFLB_SYSTEM_PBCLK,
.clk_div = 800,
.period = 2000,
};
bflb_pwm_v2_init(pwm, &cfg);
// bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH0, 0, 500); /* duty = (500-100)/1000 = 40% */
// bflb_pwm_v2_channel_set_threshold(pwm, PWM_CH1, 0, 25); /* duty = (400-200)/1000 = 20% */
// bflb_pwm_v2_channel_positive_start(pwm, PWM_CH1);
// bflb_pwm_v2_start(pwm);
}
实现视频
<iframe src="https://player.bilibili.com/player.html?aid=1302217160&bvid=BV1au4m1T7HV&cid=1481048738&p=1" scrolling="no" border="0" frameborder="no" framespacing="0" allowfullscreen="true"> </iframe>
致谢
感谢WT_0213作者的烧录固件,还有一位作者,那个烧录固件不需要一直接着,上前前短接就行,感谢安信可,感谢院长。
工程文件
链接:https://pan.baidu.com/s/15RalmrUTGXnxtb9mI3W27A
提取码:8lk8