Hi3861 OpenHarmony嵌入式应用入门--PWM 三色灯

这篇文章是讲解的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进行烧录。

运行效果如下,三色灯颜色会变化。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/bicheng/30761.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

无引擎游戏开发(2):最简游戏框架 | EasyX制作井字棋小游戏I

一、EasyX中的坐标系 不同于数理中的坐标系&#xff0c;EasyX中的y轴是竖直向下的 二、渲染缓冲区 之前的程序添加了这三个函数改善了绘图时闪烁的情况: 小球在"画布“上移动的过程就是我们在调用绘图函数&#xff0c;这个”画布“就是渲染缓冲区&#xff0c;先绘制的内…

AI在创造音乐

随着人工智能&#xff08;AI&#xff09;技术的飞速发展&#xff0c;其在音乐领域的应用也日益广泛&#xff0c;从音乐生成到声音合成&#xff0c;AI正逐渐成为音乐创作者的有力助手。然而&#xff0c;这一技术的兴起也引发了关于AI是否会毁掉音乐的讨论。以下从几个方面对这一…

【配置】Notion自动化备份到github方案

步骤 打开notion网页&#xff0c;获取到需要的值 token_v2 找到请求getSpaces的 Cookie 值 token_v2 space_id 找到请求getSpaces的响应结果space,如下图&#xff1a; file_token 找个页面点击导出&#xff0c;之后拿到这个配置项 注意&#xff1a;配置项会过期&#xff0c…

基于一种改进熵方法的旋转机械故障诊断模型(MATLAB)

熵的概念起源于热力学&#xff0c;1884年&#xff0c;玻尔兹曼定义熵&#xff0c;用以描述分子热运动的无序性和混乱度。1948年&#xff0c;Shannon在其发表的《AMathematicalTheoryofCommunication》中提出香农熵&#xff0c;首次将“熵”引入信息度量范畴&#xff0c;为信息论…

RK3568技术笔记十三 Ubuntu的编译

Ubuntu文件系统编译 在编译前需要按照前面的方法初始化编译环境&#xff0c;否则会导致编译失败&#xff08;若配置过则无需重复配置&#xff09;。 按下述方法编译的Ubuntu系统&#xff0c;用户名是&#xff1a;dianyu 密码&#xff1a;1 编译Ubuntu&#xff0c;执…

机械臂 CoppeliaSim Simulink联合仿真

实现机械臂在CoppeliaSim&#xff08;以前称为V-REP&#xff09;和Simulink上的联合仿真涉及多个步骤&#xff0c;包括环境设置、模型导入、通信配置、控制算法设计和测试调试。 前期准备 安装软件配置工作环境创建和配置CoppeliaSim场景 导入机械臂模型配置机械臂参数在Simuli…

javafx打包成exe可执行程序

写文章有点累 我做了个视频录制操作流程 把idea的项目迁移到eclipse, 在eclipse中新建项目, 修改pom文件,拷贝对应java文件, 新建一个java类 里面加载自己的javafx启动application修改项目的jdk版本, 拷贝xml 资源文件, 运行程序如果能运行则迁移成功 接下来导出jar包, 拷贝jd…

webp图片格式怎么转换成jpg?值得收藏的四种转换方法介绍!

webp图片格式怎么转换成jpg&#xff1f;在当今数字图像处理领域&#xff0c;新兴的WebP格式以其独特的特性和兼容性&#xff0c;迅速成为备受追捧的图像格式之一&#xff0c;这一格式以其高效的压缩能力和优秀的网络传输速度著称&#xff0c;为网络图像的传输和存储提供了全新的…

【python】基于python聊天工具

目录 一、概述 二、socket通信 2.1 服务器 2.1.1 建立socket类 2.1.2 绑定端口 2.1.3 监听 2.1.4 等待连接 2.2 客户端 2.2.1 连接 2.2.2 发送 2.2.3 接收 三、基于python的聊天程序的流程设计 3.1 服务器端的流程 3.2 客户端的流程 四、基于python的聊天程序的…

Web爬虫-edu_SRC-目标列表爬取

免责声明:本文仅做技术交流与学习... 爬取后,结合暗黑搜索引擎等等进行进一步搜索. edu_src.py import requests, time from bs4 import BeautifulSoup for i in range(1, 20):url fhttps://src.sjtu.edu.cn/rank/firm/0/?page{i}print(f"正在获取第{i}页数据")s …

一种稀疏贝叶斯学习的旋转机械故障诊断方法(MATLAB)

轴承的故障诊断技术是通过检测轴承故障特征信息来判断轴承的具体故障为位置或损伤程度。在轴承发生损坏时&#xff0c;故障特征信息会随着工作时间的增长变得明显。轴承的损坏过程可以分为四个阶段。第一个阶段为损伤初始阶段&#xff0c;轴承故障特征信号一般无法测量。第二个…

SQLite vs MySQL vs PostgreSQL对比总结

开发业务系统时&#xff0c;是绕不开RDBMS&#xff08;关系型数据库&#xff09;的。虽然现在诞生了各种NoSQL的数据库&#xff0c;RDBMS在业务系统中的严谨和优势依然无法取代。 近几年大大小小的项目中&#xff0c;常用的三种RDBMS&#xff08;SQLite&#xff0c;MySQL&#…

基于SSM+Jsp的书店仓库管理系统

摘要&#xff1a;仓库作为储存货物的核心功能之一&#xff0c;在整个仓储中具有非常重要的作用&#xff0c;是社会物质生产的必要条件。良好的仓库布局环境能够对货物进入下一个环节前的质量起保证作用&#xff0c;能够为货物进入市场作好准备&#xff0c;在设计中我们根据书店…

【人工智能】音乐大模型的深入探讨——当机器有了创意,是机遇还是灾难?

&#x1f440;国内外音乐大模型基本情况&#x1f440; ♥概述♥ ✈✈✈如FreeCompose、一术科技等&#xff0c;这些企业专注于开发人工智能驱动的语音、音效和音乐生成工具&#xff0c;致力于利用核心技术驱动文化产业升级。虽然具体公司未明确提及&#xff0c;但可以预见的是…

产业园区空间优化设计的创新实践者

树莓集团在产业园区运营中的空间优化设计方面&#xff0c;通过全面规划与科学布局、绿色智能与可持续发展、个性化定制与灵活多变、创新实践与数字化升级等措施&#xff0c;为企业提供了高品质、高效率的空间环境和服务支持。 一、全面规划与科学布局 明确产业定位&#xff1a…

Nuxt3 实战 (十一):添加路由 Transition 过渡效果和 Loading 动画

页面过渡效果 Nuxt3 利用 Vue 的 组件 在页面和布局之间应用过渡效果。 nuxt.config.ts 文件配置&#xff1a; export default defineNuxtConfig({app: {pageTransition: { name: page, mode: out-in }}, })在页面之间添加过渡效果&#xff0c;在 app.vue 文件中添加以下 CS…

USAD: 多元时间序列的无监督异常检测

USAD: 多元时间序列的无监督异常检测 原创 小王搬运工 时序课堂 2024-06-20 10:43 四川 论文地址&#xff1a;https://dl.acm.org/doi/abs/10.1145/3394486.3403392 论文源码&#xff1a;https://github.com/manigalati/usad 期刊&#xff1a;KDD 20: Proceedings of the 26…

嵌入式开发二十:定时器之基本定时器

定时器是微控制器中的关键外设&#xff0c;用于精确控制时间和事件。通过配置时钟源、预分频器、计数周期和比较值&#xff0c;可以实现各种时间控制任务&#xff0c;如定时中断、PWM生成和时间测量。理解定时器的工作原理和配置方法是嵌入式系统开发中的基本技能。 STM32F407 …

人工智能的头号威胁:投毒攻击

随着掌管数字生活入口的万亿美元俱乐部企业——苹果公司跳入人工智能&#xff08;AI&#xff09;赛道&#xff0c;AI技术民主化的大幕正式拉开&#xff0c;同时也将AI安全问题推向舆论的风口浪尖。 根据瑞银本周一的智能手机调查报告&#xff0c;在中国以外的智能手机用户中&am…

安装MySQL5.7版本步骤遇到问题

方法一&#xff1a;下载zip版本&#xff08;我用的这个&#xff09; 参考视频&#xff08;已收藏&#xff09;&#xff1a;windows安装MySQL5.7_哔哩哔哩_bilibili 下载zip压缩包的MySQL的网址&#xff1a;上面这个视频中有哦。 my.ini文件内容如下&#xff1a; [client] p…