[文章]【OpenHarmony成长计划挑战赛】基于Openarmony的碳侦测无人机:LED

阅读量0
0
4

PWM点亮LED呼吸灯

演示PWM接口的基本使用,效果为哦点亮开发板上的LED灯,可以实现不断闪烁,类似LED灯在呼吸的效果。(本次开发驱动的GPIO引脚是GPIO9)

pwm.png

PWM函数使用

IoTPwmInit()

/**
*@brief Initializes a PWM device.
*@param port Indicates the port number of the PWM device.
*@return Returns {@1ink IOT_SUCCESS} if the PWM device is initialized;
 returns {@1ink IOT_FAILURE) otherwise. For details about otherreturn values, see the chip description 
 *@since 2.2
 *@iversion 2.2
 */
unsigned int IoTPwmInit (unsigned int port);

函数参数值传递需要使用到的引脚号,可选引脚参数有如下:

/**
 *@ingroup iot _ pwm 
 *Enumerates the PWM ports . CNcomment : PWM 端口枚举。 CNend 
 */
typedef enum {
    HI_PWM_PORT_PWM0 =0, /**< PWM port0. Register base address: 0x40040000, multiplexed with GPIOX.CNcomment:PWM0窗口。寄存器基址:0x40040000,复用 GPIOX.CNend */
   HI_PWM_PORT_PWM1 =1, /**< PWM port1. Register base address: 0x40040100, multiplexed with GPIOX.CNcomment:PWM0窗口。寄存器基址:0x40040100,复用 GPIOX.CNend */
   HI_PWM_PORT_PWM2 =2, /**< PWM port2. Register base address: 0x40040200, multiplexed with GPIOX.CNcomment:PWM0窗口。寄存器基址:0x40040200,复用 GPIOX.CNend */
   HI_PWM_PORT_PWM3 =3, /**< PWM port0. Register base address: 0x40040300, multiplexed with GPIOX.CNcomment:PWM3窗口。寄存器基址:0x40040300,复用 GPIOX.CNend */
   HI_PWM_PORT_PWM0 =4, /**< PWM port0. Register base address: 0x40040400, multiplexed with GPIOX.CNcomment:PWM4窗口。寄存器基址:0x40040400,复用 GPIOX.CNend */
   HI_PWM_PORT_PWM0 =5, /**< PWM port5. Register base address: 0x40040500, multiplexed with GPIOX.CNcomment:PWM5窗口。寄存器基址:0x40040500,复用 GPIOX.CNend */
   HI_PWM_PORT——MAX    /**< Maximum value, which cannot be used. CNcomment:最大值,不可使用CNend */
}h_pwm_port;

本次案例使用的是接入了GPIO9号引脚的LED,从芯片手册可以得知,GPIO9使用的是PWM0,所以本次使用HI_PWM_PORT_PWM0作为参数。

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. Cparam 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);

oTPwmStart函数根据输入参数输出PWM信号。可以实现控制引脚上的电压效果。

参数 描述
port PWM端口号
duty 占空比
freq 分频倍数

案例程序解析

如下案例程序函数。

static void *LedLightTask(const char *arg)
{
    (void)arg;
​
    printf("-----entry pwm demo -----\r\n");
​
    IoTGpioInit(IOT_GPIO_IO_GPIO_9);
    IoTIoSetFunc(IOT_GPIO_IO_GPIO_9, HI_IO_FUNC_GPIO_9_PWM0_OUT);
    IoTGpioSetDir(IOT_GPIO_IO_GPIO_9, IOT_GPIO_DIR_OUT);
​
    IoTPwmInit(HI_PWM_PORT_PWM0);
​
    unsigned short duty = 0;
    unsigned int freq = 15000;
    static char plus_status = 0;
​
    while (1)
    {
        if (duty >= 99)
        {
            plus_status = 0;
        }
        else if (duty <= 0)
        {
            plus_status = 1;
        }
​
        if (plus_status)
        {
            duty++;
        }
        else
        {
            duty--;
        }
​
        usleep(LED_INTERVAL_TIME_US);
        IoTPwmStart(HI_PWM_PORT_PWM0, duty, freq);
    }
​
    return NULL;
}

编译调试

修改 BUILD.gn 文件

**修改 **applications\app路径下 BUILD.gn 文件,指定 pwm_led_example 参与编译。

# "TW001_OS_helloworld:helloworld",
#"TW002_OS_thread:os_thread_example",
#"TW003_OS_timer:os_timer_example",
#"TW004_OS_event:os_event_example",
#"TW005_OS_mutex:os_mutex_example",
#"TW006_OS_semp:os_semp_example",
#"TW007_OS_message:os_message_example",
#"TW101_GPIO_led:gpio_led_example",
#"TW102_EXTI_key:exti_key_example",
"TW103_PWM_led:pwm_led_example",
#"TW104_ADC_voltage:adc_voltage_example",
#"TW105_I2C_sht30:i2c_sht30_example",
#"TW106_UART:uart_example",
#"TW301_APP_oled:app_oled_example",
#"TW302_APP_nfc:app_nfc_example"

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,开发板开始正常工作,此时LED会进入呼吸状态,LED亮度由弱变强,再由强变弱,不断循环。

回帖

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容图片侵权或者其他问题,请联系本站作侵删。 侵权投诉
链接复制成功,分享给好友