Files
99_7018_lmx/apps/earphone/xtell_remote_control/pwm_test.c
2025-12-21 18:27:34 +08:00

89 lines
2.3 KiB
C

#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);
}
}