HarmonyOS小熊派 | HarmonyOS基础外设开发--PWM输出
BearPi-HM_Nano开发板基础外设开发——PWM输出
本示例将演示如何在BearPi-HM_Nano开发板上使用GPIO的PWM功能实现呼吸灯的效果
GPIO API分析
本案例主要使用了以下几个API完成GPIO输出功能
1、IoTGpioInit()
/**
* @brief Initializes a GPIO device.
*
* @param id Indicates the GPIO pin number.
* @return Returns {@link IOT_SUCCESS} if the GPIO device is initialized;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTGpioInit(unsigned int id);
描述:
初始化GPIO外设
2、IoTGpioSetFunc()
unsigned int IoTGpioSetFunc(unsigned int id, unsigned char val);
描述:
设置GPIO引脚复用功能
参数:
名字 | 描述 |
---|---|
id | 表示GPIO引脚号. |
val | 表示GPIO复用功能 |
3、IoTGpioSetDir()
/**
* @brief Sets the direction for a GPIO pin.
*
* @param id Indicates the GPIO pin number.
* @param dir Indicates the GPIO input/output direction.
* @return Returns {@link IOT_SUCCESS} if the direction is set;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTGpioSetDir(unsigned int id, IotGpioDir dir);
描述:
设置GPIO输出方向
参数:
名字 | 描述 |
---|---|
id | 表示GPIO引脚号. |
dir | 表示GPIO输出方向. |
4、IoTPwmInit()
/**
* @brief Initializes a PWM device.
*
* @param port Indicates the port number of the PWM device.
* @return Returns {@link IOT_SUCCESS} if the PWM device is initialized;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTPwmInit(unsigned int port);
描述: 初始化PWM功能
参数:
名字 | 描述 |
---|---|
port | 表示GPIO端口号. |
5、IoTPwmStart()
/**
* @brief Starts PWM signal output from a specified port based on the given output frequency and duty cycle.
*
*
*
* @param port Indicates the port number of the PWM device.
* @param duty Indicates the duty cycle for PWM signal output. The value ranges from 1 to 99.
* @param freq Indicates the frequency for PWM signal output.
* @return Returns {@link IOT_SUCCESS} if the PWM signal output is started;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq);
描述:
根据输入参数输出PWM信号。
参数:
名字 | 描述 |
---|---|
port | PWM端口号. |
duty | 占空比. |
freq | 分频倍数. |
6、IoTPwmDeinit()
/**
* @brief Deinitializes a PWM device.
*
* @param port Indicates the port number of the PWM device.
* @return Returns {@link IOT_SUCCESS} if the PWM device is deinitialized;
* returns {@link IOT_FAILURE} otherwise. For details about other return values, see the chip description.
* @since 2.2
* @version 2.2
*/
unsigned int IoTPwmDeinit(unsigned int port);
描述: 初始化PWM功能
参数:
名字 | 描述 |
---|---|
port | 表示GPIO端口号. |
硬件设计
本案例将使用板载的LED来验证GPIO的PWM功能,在BearPi-HM_Nano开发板上LED的连接电路图如下图所示,LED的控制引脚与主控芯片的GPIO_2连接,所以需要编写软件去控制GPIO_2输出PWM波实现呼吸灯的效果。
软件设计
主要代码分析
basicPWMLedTask()为PWM测试主任务,该任务先调用 IoTGpioInit()初始化GPIO,因为LED灯的控制引脚接在GPIO_2上,所以通过调用IoTGpioSetFunc()将GPIO_2复用为PWM功能,并通过IoTPwmInit()初始化PWM2端口,最后在死循环里面间隔100us输出不同占空比的PWM波,实现呼吸灯的效果
yuchuan_basic_pwm_led.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#include "iot_pwm.h"
#define GPIO_LED 2
#define PWM_TASK_STACK_SIZE 512
#define PWM_TASK_PRIORITY 25
void basicPWMLedTask(void *argument)
{
(void)argument;
unsigned int i;
uint8_t j = 0;
//初始化GPIO
IoTGpioInit(GPIO_LED);
//设置GPIO的引脚功能为PWM功能
IoTGpioSetFunc(GPIO_LED,IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
//设置GPIO的输出方向
IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);
//初始化PWM 端口
IoTPwmInit(GPIO_LED);
while (1)
{
/* code */
for (i = 0; i < 400; i++)
{
/* code */
//unsigned int IoTPwmStart(unsigned int port, unsigned short duty, unsigned int freq);
IoTPwmStart(GPIO_LED, i, 40000);
//IoTPwmStop(GPIO_LED);
printf("%d Start PWM init!!!\n",i);
usleep(100);
}
i = 0;
j++;
if (j == 5)
{
/* code */
IoTPwmDeinit(GPIO_LED);
printf("%d Delete PWM init!!!\n",j);
}
//usleep(100);
}
}
static void yuchuanBasicPWMLed(void)
{
osThreadAttr_t threadAttr;
threadAttr.attr_bits = 0U;
threadAttr.cb_mem = NULL;
threadAttr.cb_size = 0U;
threadAttr.stack_mem = NULL;
threadAttr.stack_size = 0U;
threadAttr.priority = 25;
threadAttr.name = "PWMLedTask";
if (osThreadNew((osThreadFunc_t)basicPWMLedTask, NULL, &threadAttr) == NULL)
{
/* code */
printf("Falied to Create PWMLedTask!!!\n");
}
}
APP_FEATURE_INIT(yuchuanBasicPWMLed);
编译调试
//applications/sample/BearPi/BearPi-HM_Nano/sample/B3_YuchuanBasicPWM
static_library("yuchuanPWM"){
sources = [
"yuchuan_basic_pwm_led.c",
]
include_dirs = [
"//base/iot_hardware/peripheral/interfaces/kits",
"//kernel/liteos_m/kal/cmsis",
"//utils/native/lite/include",
]
}
修改 BUILD.gn 文件
修改applications\BearPi\BearPi-HM_Nano\sample
路径下 BUILD.gn 文件,指定 yuchuanPWM参与编译。
# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent 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("sample") {
features = [
#"A1_kernal_thread:thread_example",
#"A2_kernel_timer:timer_example",
#"A3_kernel_event:event_example",
#"A4_kernel_mutex:mutex_example",
#"A5_kernel_semaphore:semaphore_example",
#"A6_kernel_message:message_example",
#"B1_basic_led_blink:led_example",
"B2_basic_button:button_example",
#"B3_basic_pwm_led:pwm_example",
#"B4_basic_adc:adc_example",
#"B5_basic_i2c_nfc:i2c_example",
#"B6_basic_uart:uart_example",
#"C1_e53_sf1_mq2:e53_sf1_example",
#"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
#"C3_e53_sc1_pls:e53_sc1_example",
#"C4_e53_sc2_axis:e53_sc2_example",
#"C5_e53_is1_infrared:e53_is1_example",
#"D1_iot_wifi_ap:wifi_ap",
#"D2_iot_wifi_sta_connect:wifi_sta_connect",
#"D3_iot_udp_client:udp_client",
#"D4_iot_tcp_server:tcp_server",
#"D5_iot_mqtt:iot_mqtt",
#"D6_iot_cloud_oc:oc_mqtt",
#"D7_iot_cloud_onenet:onenet_mqtt",
#"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
#"D9_iot_cloud_oc_light:cloud_oc_light",
#"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
#"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
#"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
#"D13_iot_cloud_oc_gps:cloud_oc_gps",
#"A1_YuchuanThread:yuchuanThread",
#"A2_YuchuanTimer:yuchuanTimer",
#"A4_YuchuanMutex:yuchuanMutex",
#"A5_YuchuanSemaphore:yuchuanSemaphore",
#"A3_YuchuanEvent:yuchuanEvent",
#"A6_YuchuanMessage:yuchuanMessageQueue",
"B3_YuchuanBasicPWM:yuchuanPWM",
]
}
运行结果
示例代码编译烧录代码后,按下开发板的RESET按键,开发板开始正常工作,LED开始不断变化亮度,实现呼吸灯的效果。
- 点赞
- 收藏
- 关注作者
评论(0)