安装SDK资源包
点击工具栏上SDK管理器,在弹窗上依次勾选安装RT-Thread_Source_Code latest、AB32VG1-AB-PROUGEN 1.0.8、ToolChain_Support_Packages/RISC-V-GCC 10.1.0资源包
新建工程
点击文件->新建->RT-Thread项目,弹出新建项目窗口,选择基于开发板,填写项目名AB32VG1_BreathingLamp,开发板选择AB32VG1-AB-PROUGEN,完成新建项目
右击项目工程名,选择下拉菜单中“属性”,弹出属性框,选择MCU->RISC-V Toolchains Paths,修改配置ToolChain folder路径为 D:\RT-ThreadStudio\repo\Extract\ToolChain_Support_Packages\RISC-V\RISC-V-GCC\10.1.0\bin
查看开发板原理图AB32VG1_Prougen_Schematic_V02,RGB LED分别通过短路帽连接到芯片PE1、PE4、PA1引脚,这里通过杜邦线将LED蓝色控制脚连接到芯片PB0 (Timer3PWM)引脚
配置PWM
双击项目的RT-Thread Setting,进入配置界面,点击更多配置
进入更多配置后,选择硬件选项,使能PB0引脚PWM,保存配置
在application文件夹中,新建pwm.c文件
编写pwm程序:
- #define PWM_DEV_NAME "t3pwm" /* PWM设备名称 */
- #define PWM_DEV_CHANNEL 1 /* PWM通道 */
- struct rt_device_pwm *pwm_dev; /* PWM设备句柄 */
- rt_uint32_t period, pulse;
- rt_uint8_t direction = 0;
- void Pwm_Init(void)
- {
- period = 1000; /* 周期值,1M/period kHz */
- pulse = 0; /* PWM脉冲宽度值,占空比=pulse/period */
- //查找t3pwm是否在系统设备注册表中
- pwm_dev = (struct rt_device_pwm *)rt_device_find(PWM_DEV_NAME);
- if (pwm_dev == RT_NULL)
- {
- rt_kprintf("Can't find device:%s\n", PWM_DEV_NAME);
- return;
- }
- /* 设置PWM周期、脉冲宽度 */
- rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
- /* 使能设备 */
- rt_pwm_enable(pwm_dev, PWM_DEV_CHANNEL);
- }
- //PWM线程函数
- static void PWM_Thread_Entry(void *para)
- {
- Pwm_Init();
- while(1)
- {
- //蓝灯呼吸灯闪烁
- if(direction == 0)
- {
- pulse += 10;
- if(pulse >= period - 10)
- {
- direction = 1;
- pulse = period - 10;
- }
- }
- else
- {
- pulse -= 10;
- if(pulse < 1)
- {
- direction = 0;
- pulse = 0;
- }
- }
- rt_pwm_set(pwm_dev, PWM_DEV_CHANNEL, period, pulse);
- rt_thread_mdelay(2);
- }
- }
- //动态创建PWM线程启动函数
- int Pwm_Thread_Init(void)
- {
- rt_thread_t pwm_thread;
- pwm_thread = rt_thread_create(
- "pwm_thread",
- PWM_Thread_Entry,
- RT_NULL,
- 1024,
- 10,
- 10);
- if (pwm_thread != RT_NULL)
- {
- rt_thread_startup(pwm_thread);
- }
- return RT_EOK;
- }
- INIT_APP_EXPORT(Pwm_Thread_Init); //PWM线程初始化添加到系统初始化中
蓝色呼吸灯效果: