pwm 50hz的频率测试正常

This commit is contained in:
lmx
2025-12-21 18:27:34 +08:00
parent 8cb1ca7701
commit 482767064e
12 changed files with 126 additions and 25 deletions

View File

@ -28,7 +28,7 @@ extern "C" {
#include "gpio.h"
static uint8_t LL_led = 0;
#define LLSYNC_SWITCH_PIN IO_PORTA_01
#define LLSYNC_SWITCH_PIN IO_PORTA_00
static int ble_property_power_switch_set(const char *data, uint16_t len)
{
printf("ble_property_power_switch_set led to %d\n", data[0]);

View File

@ -0,0 +1,89 @@
#include "asm/mcpwm.h"
#include "asm/gpio.h"
#include "task_func.h"
#include "./nvs/nvs.h"
#include "timer.h"
#include "system/includes.h"
#include "user_cfg.h"
#include "earphone.h" // 宏定义 EARPHONE_STATE_...
#include "ble_user.h"
#include "le_client_demo.h"
#include "le_common.h"
#include "./ble_handler/client_handler.h"
#include "./RFID/include/rfid_main.h"
#include "./RFID/include/READER_REG.h"
/*
* PWM配置参数:
* - 周期: 50ms (20Hz)
* - 最小脉宽: 500us (1%占空比)
* - 最大脉宽: 2.5ms (5%占空比)
* - PWM引脚: PA1 (IO_PORTA_01)
*/
void pwm_test_init(void)
{
struct pwm_platform_data pwm_data = {0};
// 配置PWM参数
pwm_data.pwm_aligned_mode = pwm_edge_aligned; // 边沿对齐模式
pwm_data.pwm_ch_num = pwm_ch0; // 使用PWM通道0
pwm_data.frequency = 50; // 20Hz (周期50ms)
pwm_data.duty = 100; // 初始占空比1% (500us/50ms = 1%)
pwm_data.h_pin = IO_PORTA_01; // PWM输出引脚PA1
pwm_data.l_pin = -1; // 不使用互补引脚
pwm_data.complementary_en = 0; // 非互补模式
// 初始化PWM
mcpwm_init(&pwm_data);
// 启用PWM通道
mcpwm_open(pwm_ch0);
}
// 设置PWM占空比 (脉宽范围: 500us-2.5ms)
void pwm_test_set_pulse_width_us(u32 pulse_width_us)
{
u16 duty;
// 限制脉宽范围: 500us - 2500us
if (pulse_width_us < 500) {
pulse_width_us = 500;
} else if (pulse_width_us > 2500) {
pulse_width_us = 2500;
}
// 计算占空比: (脉宽/周期) * 10000
// 周期20ms = 20000us
duty = (pulse_width_us * 10000) / 20000;
// 设置占空比
mcpwm_set_duty(pwm_ch0, duty);
}
// 测试函数: 从最小脉宽到最大脉宽循环变化
void pwm_test_sweep(void)
{
u32 pulse_width;
pwm_test_set_pulse_width_us(500);
mdelay(1000);
pwm_test_set_pulse_width_us(2500);
mdelay(50);
}
// 主测试函数
void pwm_test_main(void)
{
// 初始化PWM
pwm_test_init();
// 设置最小脉宽500us
pwm_test_set_pulse_width_us(500);
// 运行脉宽扫描测试
while (1) {
pwm_test_sweep();
os_time_dly(100);
}
}

View File

@ -279,26 +279,26 @@ void contol_key_task(void){
control_key_init(); //遥感按键初始化
while(1){
// extern u16 get_vbat_level(void);
// vbat_value = get_vbat_level(); //当前电量
// control_key_get_value(&Xout_adc, &Yout_adc, &key1_state, NULL); //遥感值
// ble_data_buff[4] = (u8)(Yout_adc & 0xFF);
// ble_data_buff[5] = (u8)((Yout_adc >> 8) & 0xFF);
extern u16 get_vbat_level(void);
vbat_value = get_vbat_level(); //当前电量
control_key_get_value(&Xout_adc, &Yout_adc, &key1_state, NULL); //遥感值
ble_data_buff[4] = (u8)(Yout_adc & 0xFF);
ble_data_buff[5] = (u8)((Yout_adc >> 8) & 0xFF);
// ble_data_buff[6] = (u8)(Xout_adc & 0xFF);
// ble_data_buff[7] = (u8)((Xout_adc >> 8) & 0xFF);
ble_data_buff[6] = (u8)(Xout_adc & 0xFF);
ble_data_buff[7] = (u8)((Xout_adc >> 8) & 0xFF);
// ble_data_buff[8] = key1_state;
// ble_data_buff[9] = 0x01;
ble_data_buff[8] = key1_state;
ble_data_buff[9] = 0x01;
// ble_data_buff[10] = get_vbat_percent();
ble_data_buff[10] = get_vbat_percent();
// ble_data_buff[11] = 0;
// for(int i = 0 ;i < 11; i++){
// ble_data_buff[11] += ble_data_buff[i];
// }
ble_data_buff[11] = 0;
for(int i = 0 ;i < 11; i++){
ble_data_buff[11] += ble_data_buff[i];
}
u8 test_buff[12] = {0xBE,0xBB,0x09,0x01,0x50,0x50,0x50,0x50,0x01,0x01,0x50,0x15};
// u8 test_buff[12] = {0xBE,0xBB,0x09,0x01,0x50,0x50,0x50,0x50,0x01,0x01,0x50,0x15};
g_send_data_to_ble_server(ble_data_buff, sizeof(ble_data_buff));
/*
1000
@ -485,7 +485,9 @@ void test_task(void){
void test_func_main(void){
#if TEST_FUNCTION == 1
os_task_create(test_task, NULL, 1, 1024, 128, "rfid_test");
// os_task_create(test_task, NULL, 1, 1024, 128, "rfid_test");
extern void pwm_test_main(void);
os_task_create(pwm_test_main, NULL, 1, 1024, 128, "pwm_test");
// os_task_create(contol_key_task, NULL, 1, 2048, 128, control_key_task_name);
#endif

View File

@ -139,5 +139,5 @@ void xtell_app_main()
extern void test_func_main(void);
// test_func_main();
test_func_main();
}