深圳做门户网站的网络公司,网站建设背景介绍,平台制作公司,网络网页制作这篇文章是讲解的pwm控制三色灯的部分#xff0c;这部分也是后续全彩智能灯的基础。
硬件原理如下 IO管脚定义在hi-12f_v1.1.2-规格书-20211202.pdf文档中 GPIO API API名称 说明 unsigned int IoTGpioInit(unsigned int id); GPIO模块初始化 hi_u32 hi_io_set_func(hi_i…这篇文章是讲解的pwm控制三色灯的部分这部分也是后续全彩智能灯的基础。
硬件原理如下 IO管脚定义在hi-12f_v1.1.2-规格书-20211202.pdf文档中 GPIO API API名称 说明 unsigned int IoTGpioInit(unsigned int id); GPIO模块初始化 hi_u32 hi_io_set_func(hi_io_name id, hi_u8 val); 配置某个IO的复用功能 unsigned int IoTPwmInit(unsigned int port); 初始化一个PWM设备 unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq); 根据给定的输出频率和占空比从指定端口启动PWM信号输出。 unsigned int IoTPwmStop(unsigned int port); 停止来自指定端口的PWM信号输出。
在官方给的代码中IoTPwmStart占空比参数范围为1-99也就是不能达到全灭和全亮的程度。所以对源码进行修改。
修改D:\DevEcoProjects\test\src\device\hisilicon\hispark_pegasus\hi3861_adapter\hals\iot_hardware\wifiiot_lite\hal_iot_pwm.c
unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq)
{unsigned short hiDuty;unsigned short hiFreq;if ((freq 0) || (duty DUTY_MAX)) {return IOT_FAILURE;}if ((CLK_160M / freq) SHORT_MAX) {return IOT_FAILURE;}hiFreq (unsigned short)(CLK_160M / freq);hiDuty (duty * hiFreq) / DUTY_MAX;return hi_pwm_start((hi_pwm_port)port, hiDuty, hiFreq);
}
使其支持0和100.
修改D:\DevEcoProjects\test\src\device\hisilicon\hispark_pegasus\sdk_liteos\platform\drivers\pwm\hi_pwm.c
hi_u32 hi_pwm_start(hi_pwm_port port, hi_u16 duty, hi_u16 freq)
{hi_u32 ret;if ((pwm_check_port(port) ! HI_ERR_SUCCESS) || (freq 0)|| (duty freq)) {return HI_ERR_PWM_INVALID_PARAMETER;}if (pwm_is_init(port) HI_FALSE) {return HI_ERR_PWM_NO_INIT;}ret pwm_lock(port);if (ret ! HI_ERR_SUCCESS) {return ret;}pwm_set_enable(port, HI_TRUE);pwm_set_freq(port, freq);pwm_set_duty(port, duty);pwm_take_effect(port);return pwm_unlock(port);
}
同样是使其支持0和100.
以上就是对源码的修改。下面是测试代码部分。 修改D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. import(//build/lite/config/component/lite_component.gni)lite_component(demo) {features [#base_00_helloworld:base_helloworld_example,#base_01_led:base_led_example,#base_02_loopkey:base_loopkey_example,#base_03_irqkey:base_irqkey_example,#base_04_adc:base_adc_example,base_05_pwm:base_pwm_example,]
}
创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_05_pwm文件夹
文件夹中创建D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_05_pwm\base_pwm_example.c文件D:\DevEcoProjects\test\src\vendor\rtplay\rt_hi3861\demo\base_05_pwm\BUILD.gn文件
# Copyright (c) 2023 Beijing HuaQing YuanJian Education Technology Co., Ltd
# Licensed under the Apache License, Version 2.0 (the License);
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an AS IS BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License. static_library(base_pwm_example) {sources [base_pwm_example.c,]include_dirs [//utils/native/lite/include,//kernel/liteos_m/kal/cmsis,//base/iot_hardware/peripheral/interfaces/kits,//vendor/hqyj/fs_hi3861/common/bsp/include]
}
/** Copyright (C) 2023 HiHope Open Source Organization .* Licensed under the Apache License, Version 2.0 (the License);* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0** Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an AS IS BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/#include stdio.h
#include unistd.h#include cmsis_os2.h
#include hi_io.h
#include iot_errno.h
#include iot_gpio.h
#include iot_pwm.h
#include ohos_init.h#define RED_LED_PIN_NAME HI_IO_NAME_GPIO_10
#define RED_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_10_GPIO
#define GREEN_LED_PIN_NAME HI_IO_NAME_GPIO_2
#define GREEN_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_2_GPIO
#define BLUE_LED_PIN_NAME HI_IO_NAME_GPIO_7
#define BLUE_LED_PIN_FUNCTION WIFI_IOT_IO_FUNC_GPIO_7_GPIO#define PWM_FREQ_DIVITION 64000
#define DELAY_US 30000
#define STACK_SIZE (4096)
#define PWM0_PORT_NUM (0)
#define PWM1_PORT_NUM (1)
#define PWM2_PORT_NUM (2)static void PWMLedDemoTask(void)
{// pwm占空比记录变量unsigned short duty0 0, duty1 40, duty2 80;// 记录占空比是增加还是减少hi_bool duty0_increase HI_TRUE;hi_bool duty1_increase HI_TRUE;hi_bool duty2_increase HI_TRUE;// 炫彩灯板的红灯hi_io_set_func(RED_LED_PIN_NAME, HI_IO_FUNC_GPIO_10_PWM1_OUT);hi_io_set_func(GREEN_LED_PIN_NAME, HI_IO_FUNC_GPIO_2_PWM2_OUT);hi_io_set_func(BLUE_LED_PIN_NAME, HI_IO_FUNC_GPIO_7_PWM0_OUT);IoTPwmInit(PWM0_PORT_NUM);IoTPwmInit(PWM1_PORT_NUM);IoTPwmInit(PWM2_PORT_NUM);while (1) {// use PWM control RED LED brightness// 蓝灯呼吸代码IoTPwmStart(PWM0_PORT_NUM, duty0, PWM_FREQ_DIVITION);if (duty0_increase) {duty0;if (duty0 100) {// 自增超过100后需要进行自减duty0_increase HI_FALSE;duty0 99;}} else {duty0--;if (duty0 0) {// 自减到0后需要进行自增duty0_increase HI_TRUE;}}// 红灯呼吸代码IoTPwmStart(PWM1_PORT_NUM, duty1, PWM_FREQ_DIVITION);if (duty1_increase) {duty1;if (duty1 100) {duty1_increase HI_FALSE;duty1 99;}} else {duty1--;if (duty1 0) {duty1_increase HI_TRUE;}}// 绿灯呼吸代码IoTPwmStart(PWM2_PORT_NUM, duty2, PWM_FREQ_DIVITION);if (duty2_increase) {duty2;if (duty2 100) {duty2_increase HI_FALSE;duty2 99;}} else {duty2--;if (duty2 0) {duty2_increase HI_TRUE;}}usleep(DELAY_US);}
}static void PWMLedDemo(void)
{osThreadAttr_t attr;// set Red LED pin to GPIO functionIoTGpioInit(RED_LED_PIN_NAME);attr.name PWMLedDemoTask;attr.attr_bits 0U;attr.cb_mem NULL;attr.cb_size 0U;attr.stack_mem NULL;attr.stack_size STACK_SIZE;attr.priority osPriorityNormal;if (osThreadNew(PWMLedDemoTask, NULL, attr) NULL) {printf([ColorfulLightDemo] Falied to create PWMLedDemoTask!\n);}
}
APP_FEATURE_INIT(PWMLedDemo);目录结构
│ config.json
│
├─common
│ └─bsp
│ ├─include
│ └─src
├─demo
│ │ BUILD.gn
│ │
│ ├─base_00_helloworld
│ │ base_helloworld_example.c
│ │ BUILD.gn
│ │
│ ├─base_01_led
│ │ base_led_example.c
│ │ BUILD.gn
│ │
│ ├─base_02_loopkey
│ │ base_loopkey_example.c
│ │ BUILD.gn
│ │
│ ├─base_03_irqkey
│ │ base_irqkey_example.c
│ │ BUILD.gn
│ │
│ ├─base_04_adc
│ │ base_adc_example.c
│ │ BUILD.gn
│ │
│ └─base_05_pwm
│ base_pwm_example.c
│ BUILD.gn
│
└─doc│ HarmonyOS开发板实验指导书 v2.1.pdf│ 华清远见 FS_Hi3861开发指导.md│ 华清远见 FS_Hi3861新手入门手册.md│├─board│ FS-Hi3861-V4.2.pdf│ FS-Hi3861QDB-V3.2.pdf│ hi-12f_kit_v1.1.0规格书-20211025.pdf│ hi-12f_v1.1.2-规格书-20211202.pdf│ nodemcu-hi-07s_12f-kit_v1.1-20210913.pdf│ RTplay2.01_2024-06-14.pdf│└─figures
使用build编译成功后使用upload进行烧录。
运行效果如下三色灯颜色会变化。