3.PWM输出测试
该教程演示如何通过开发板的GPIO输出PWM波
一、准备工作
- 准备1块BearPi-Pico H3863开发板
二、编译代码
在Windows下编译操作
点击工具栏中的“系统配置”,打开配置界面.
选择Support PWM Sample
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support I2C Sample. [ ] Support PINCTRL Sample. [*] Support PWM Sample. PWM Sample Configuration --->
按下"Save"键保存配置。
可在代码中修改高低电平时间,已达到输出不同占空比的PWM波
关键代码,位于
application\samples\peripheral\pwm\pwm_demo.c
static void *pwm_task(const char *arg) { UNUSED(arg); pwm_config_t cfg_no_repeat = { 50, //低电平 200, //高电平 0, 0, true }; uapi_pin_set_mode(PWM_GPIO, PWM_PIN_MODE); //设置引脚复用模式为PWM uapi_pwm_init(); //初始化PWM uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); //开启PWM通道 uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_unregister_interrupt(PWM_GROUP_ID);//取消注册PWM中断 uapi_pwm_register_interrupt(PWM_GROUP_ID, pwm_sample_callback);//注册PWM中断 uapi_pwm_set_group(PWM_GROUP_ID, 1 << PWM_CHANNEL); //设置PWM通道对应的组 uapi_pwm_start(PWM_GROUP_ID); //开启PWM uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_close(PWM_CHANNEL); //关闭PWM uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_deinit(); //去初始化PWM return NULL; }
编译烧录固件
参考环境搭建教程编译烧录代码
在Ubuntu下编译操作
在MobaXterm中输入:
./build.py menuconfig ws63-liteos-app
选择Support PWM Sample
Application ---> [*] Enable Sample. [*] Enable the Sample of peripheral. [ ] Enable all the sample of peripheral, it's just for build. [ ] Support ADC Sample. [ ] Support BLINKY Sample. [ ] Support BUTTON Sample. [ ] Support I2C Sample. [ ] Support PINCTRL Sample. [*] Support PWM Sample. PWM Sample Configuration --->
按下"ESC"键退出并保存配置。
可在代码中修改高低电平时间,已达到输出不同占空比的PWM波
关键代码,位于
application\samples\peripheral\pwm\pwm_demo.c
static void *pwm_task(const char *arg) { UNUSED(arg); pwm_config_t cfg_no_repeat = { 50, //低电平 200, //高电平 0, 0, true }; uapi_pin_set_mode(PWM_GPIO, PWM_PIN_MODE); //设置引脚复用模式为PWM uapi_pwm_init(); //初始化PWM uapi_pwm_open(PWM_CHANNEL, &cfg_no_repeat); //开启PWM通道 uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_unregister_interrupt(PWM_GROUP_ID);//取消注册PWM中断 uapi_pwm_register_interrupt(PWM_GROUP_ID, pwm_sample_callback);//注册PWM中断 uapi_pwm_set_group(PWM_GROUP_ID, 1 << PWM_CHANNEL); //设置PWM通道对应的组 uapi_pwm_start(PWM_GROUP_ID); //开启PWM uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_close(PWM_CHANNEL); //关闭PWM uapi_tcxo_delay_ms((uint32_t)TEST_TCXO_DELAY_1000MS); uapi_pwm_deinit(); //去初始化PWM return NULL; }
编译烧录固件
参考环境搭建教程编译烧录代码
三、测试
烧录固件后按下开发的复位按键,用示波器或者逻辑分析查看PWM输出引脚的PWM波,或者通过改变不同的占空比,观察开发板上的LED灯的亮度变化。