学习目标

  • 掌握GPIO的开发流程
  • 掌握PWM的开发流程
  • 掌握蜂鸣器播放音乐

    学习内容

    IO引脚说明

    | 引脚 | Uart | SPI | ADC | PWM | I2S | SDIO | I2C | | —- | —- | —- | —- | —- | —- | —- | —- | | GPIO_07 | CTS_1 | RXD_0 | ADC_3 | PWM_0 | CLK_0 | | | | GPIO_08 | RTS_1 | TXD_0 | | PWM_1 | WS_0 | | | | GPIO_10 | CTS_2 | CLK_0 | | PWM_1 | TX_0 | D3 | SDA_0 | | GPIO_09 | RTS_2 | TXD_0 | ADC_4 | PWM_0 | MCK_0 | D2 | SCL_0 | | GPIO_03 | LOG_TXD_0 | | | | | | | | GPIO_04 | LOG_RXD_0 | | ADC_1 | | | | | | GPIO_02 | | | | PWM_2 | MCK_0 | | | | GPIO_05 | RXD_1 | CSI_0 | ADC_2 | PWM_2 | TX_0 | | | | GPIO_06 | TXD_1 | CLK_0 | | PWM_3 | | | | | GPIO_14 | LOG_RXD_0/CTS_2 | | | PWM_5 | RX_0 | D1 | SCL_0 | | GPIO_11 | TXD_2 | RXD_0 | ADC_5 | PWM_2 | CLK_0 | CMD | | | GPIO_12 | RXD_2 | CSI_0 | ADC_0 | PWM_3 | WS_0 | CLK | | | GPIO_13 | LOG_TXD_0/RTS_2 | | ADC_6 | PWM_4 | | D0 | SDA_0 |

GPIO控制LED

通过GPIO来点亮一盏灯

原理图

110.png

GPIO配置

  1. //初始化引脚
  2. IoTGpioInit(IOT_IO_NAME_2);
  3. //设置IO口功能为GPIO
  4. IoTGpioSetFunc(IOT_IO_NAME_2, IOT_GPIO_FUNC_GPIO_2_GPIO);
  5. //设置IO口输出方向:输出
  6. IoTGpioSetDir(IOT_IO_NAME_2, IOT_GPIO_DIR_OUT);
  1. // 高电平输出
  2. IoTGpioSetOutputVal(IOT_IO_NAME_2,IOT_GPIO_VALUE1);
  3. // 低电平输出
  4. IoTGpioSetOutputVal(IOT_IO_NAME_2,IOT_GPIO_VALUE0);

开发流程

来到应用开发的根目录。
我们编写代码的根目录device/board/itcast/genkipi/app

  1. 根目录下新建gpio_led文件夹,此为项目目录
  2. 项目目录下新建main.c文件
  3. 项目目录下新建BUILD.gn文件
  4. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    include

    include

include “ohos_init.h”

include “cmsis_os2.h”

include “iot_gpio.h”

include “iot_gpio_ex.h”

static void start(void) { //初始化GPIO口 IoTGpioInit(IOT_IO_NAME_2); //设置IO口功能为GPIO IoTGpioSetFunc(IOT_IO_NAME_2, IOT_GPIO_FUNC_GPIO_2_GPIO); //设置IO口输出方向:输出 IoTGpioSetDir(IOT_IO_NAME_2,IOT_GPIO_DIR_OUT); //不断输出高低电压 while (1){ IoTGpioSetOutputVal(IOT_IO_NAME_2,IOT_GPIO_VALUE1); usleep(1000*1000);

  1. IoTGpioSetOutputVal(IOT_IO_NAME_2,IOT_GPIO_VALUE0);
  2. usleep(1000*1000);
  3. }

}

APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("gpio_led") {
  3. sources = [
  4. "main.c"
  5. ]
  6. include_dirs = [
  7. "//base/iothardware/peripheral/interfaces/inner_api",
  8. "../../iot_hardware_hals/include"
  9. ]
  10. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "gpio_led"
  5. ]
  6. }

PWM控制LED

PWM配置

  1. //初始化GPIO口
  2. IoTGpioInit(IOT_IO_NAME_2);
  3. //设置IO口功能为PWM
  4. IoTGpioSetFunc(IOT_IO_NAME_2, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT);
  5. //设置IO口输出方向:输出
  6. IoTGpioSetDir(IOT_IO_NAME_2, IOT_GPIO_DIR_OUT);
  7. //不断输出高低电压
  8. IoTPwmInit(IOT_PWM_NAME_2);
  1. IoTPwmStart(IOT_PWM_NAME_2, i, 10000);

开发流程

来到应用开发的根目录。
我们编写代码的根目录device/board/itcast/genkipi/app

  1. 根目录下新建pwm_led文件夹,此为项目目录
  2. 项目目录下新建main.c文件
  3. 项目目录下新建BUILD.gn文件
  4. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    include

    include

include “ohos_init.h”

include “cmsis_os2.h”

include “iot_gpio.h”

include “iot_gpio_ex.h”

include “iot_pwm.h”

static void start(void) { //初始化GPIO口 IoTGpioInit(IOT_IO_NAME_2); //设置IO口功能为PWM IoTGpioSetFunc(IOT_IO_NAME_2, IOT_GPIO_FUNC_GPIO_2_PWM2_OUT); //设置IO口输出方向:输出 IoTGpioSetDir(IOT_IO_NAME_2, IOT_GPIO_DIR_OUT); //不断输出高低电压 IoTPwmInit(IOT_PWM_NAME_2); while (1){ for (int i = 0; i < 20; i++) { IoTPwmStart(IOT_PWM_NAME_2, i, 10000); usleep(0.05 1000 1000); }

  1. for (int i = 20; i > 0; i--) {
  2. IoTPwmStart(IOT_PWM_NAME_2, i, 10000);
  3. usleep(0.05 * 1000 * 1000);
  4. }
  5. }

}

APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("pwm_led") {
  3. sources = [
  4. "main.c"
  5. ]
  6. include_dirs = [
  7. "//base/iothardware/peripheral/interfaces/inner_api",
  8. "../../iot_hardware_hals/include"
  9. ]
  10. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "pwm_led"
  5. ]
  6. }

PWM蜂鸣器

113.png

开发流程

来到应用开发的根目录。
我们编写代码的根目录device/board/itcast/genkipi/app

  1. 根目录下新建pwm_buzzer文件夹,此为项目目录
  2. 项目目录下新建main.c文件
  3. 项目目录下新建BUILD.gn文件
  4. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    include

    include

include “ohos_init.h”

include “cmsis_os2.h”

include “iot_gpio.h”

include “iot_gpio_ex.h”

include “iot_pwm.h”

static void start(void) { //初始化GPIO口 IoTGpioInit(IOT_IO_NAME_14); //设置IO口功能为PWM IoTGpioSetFunc(IOT_IO_NAME_14, IOT_GPIO_FUNC_GPIO_14_PWM5_OUT); //设置IO口输出方向:输出 IoTGpioSetDir(IOT_IO_NAME_14, IOT_GPIO_DIR_OUT); //不断输出高低电压 IoTPwmInit(IOT_PWM_NAME_5); while (1) { IoTPwmStart(IOT_PWM_NAME_5, 50, 10000); usleep(2 1000 1000);

  1. IoTPwmStop(IOT_PWM_NAME_5);
  2. usleep(2 * 1000 * 1000);
  3. }

}

APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("pwm_buzzer") {
  3. sources = [
  4. "main.c"
  5. ]
  6. include_dirs = [
  7. "//base/iothardware/peripheral/interfaces/inner_api",
  8. "../../iot_hardware_hals/include"
  9. ]
  10. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "pwm_buzzer"
  5. ]
  6. }

音乐播放器

开发流程

来到应用开发的根目录。
我们编写代码的根目录device/board/itcast/genkipi/app

  1. 根目录下新建music文件夹,此为项目目录
  2. 项目目录下新建main.c文件
  3. 项目目录下新建BUILD.gn文件
  4. 修改根目录下的BUILD.gn文件
    代码部分
    ```c

    include

    include

    include

include “ohos_init.h”

include “cmsis_os2.h”

include “iot_gpio.h”

include “iot_gpio_ex.h”

include “iot_pwm.h”

static uint8_t notes[] = { 1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5, 5, 6, 5, 4, 3, 1, 5, 6, 5, 4, 3, 1, 1, 5, 1, 1, 5, 1};

static uint8_t durations[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 3, 1, 3, 1, 4, 4, 3, 1, 3, 1, 4, 4, 4, 4, 8, 4, 4, 8};

static uint16_t FREQS[] = {0, 523, 587, 659, 698, 784, 880, 988, 523 * 2};

static void start(void) { //初始化GPIO口 IoTGpioInit(IOT_IO_NAME_14); //设置IO口功能为PWM IoTGpioSetFunc(IOT_IO_NAME_14, IOT_GPIO_FUNC_GPIO_14_PWM5_OUT); //设置IO口输出方向:输出 IoTGpioSetDir(IOT_IO_NAME_14, IOT_GPIO_DIR_OUT); //不断输出高低电压 IoTPwmInit(IOT_PWM_NAME_5); while (1) {

  1. for (size_t i = 0; i < sizeof(notes) / sizeof(notes[0]); i++)
  2. {
  3. uint8_t note = notes[i]; // 音符
  4. uint16_t hz = FREQS[note] * 8; // 频率
  5. uint8_t duration = durations[i]; // 音符节拍
  6. IoTPwmStart(IOT_PWM_NAME_5, 50, hz);
  7. usleep(duration * 125 * 1000);
  8. IoTPwmStop(IOT_PWM_NAME_5);
  9. }
  10. usleep(5 * 1000 * 1000);
  11. }

}

APP_FEATURE_INIT(start);

  1. ```c
  2. static_library("music") {
  3. sources = [
  4. "main.c"
  5. ]
  6. include_dirs = [
  7. "//base/iothardware/peripheral/interfaces/inner_api",
  8. "../../iot_hardware_hals/include"
  9. ]
  10. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "music"
  5. ]
  6. }

蜂鸣器驱动封装

开发流程

来到应用开发的根目录。
我们编写代码的根目录device/board/itcast/genkipi/app

  1. 根目录下新建music_driver文件夹,此为项目目录
  2. 项目目录下新建main.c,buzzer.h``buzzer.c文件
  3. 项目目录下新建BUILD.gn文件
  4. 修改根目录下的BUILD.gn文件 ```c

    include

    include

    include

include “ohos_init.h”

include “cmsis_os2.h”

include “buzzer.h”

static uint8_t notes[] = { 1, 2, 3, 1, 1, 2, 3, 1, 3, 4, 5, 3, 4, 5, 5, 6, 5, 4, 3, 1, 5, 6, 5, 4, 3, 1, 1, 5, 1, 1, 5, 1};

static uint8_t durations[] = { 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 8, 4, 4, 8, 3, 1, 3, 1, 4, 4, 3, 1, 3, 1, 4, 4, 4, 4, 8, 4, 4, 8};

static uint16_t FREQS[] = {0, 523, 587, 659, 698, 784, 880, 988, 523 * 2};

static void start(void) { Buzzer_init(); while (1) {

  1. for (size_t i = 0; i < sizeof(notes) / sizeof(notes[0]); i++)
  2. {
  3. uint8_t note = notes[i]; // 音符
  4. uint16_t hz = FREQS[note] * 8; // 频率
  5. uint8_t duration = durations[i]; // 音符节拍
  6. Buzzer_beep(hz);
  7. usleep(duration * 125 * 1000);
  8. Buzzer_stop();
  9. }
  10. usleep(5 * 1000 * 1000);
  11. }

}

APP_FEATURE_INIT(start);

  1. ```c
  2. #ifndef __BUZZER_H__
  3. #define __BUZZER_H__
  4. #include <stdint.h>
  5. #include "iot_gpio.h"
  6. #include "iot_gpio_ex.h"
  7. #include "iot_pwm.h"
  8. #define BUZZER_PIN 14
  9. #define BUZZER_PWM_PORT 5
  10. #define BUZZER_PIN_FUNC IOT_GPIO_FUNC_GPIO_14_PWM5_OUT
  11. #define BUZZER_PIN_INIT() \
  12. { \
  13. IoTGpioInit(BUZZER_PIN); \
  14. IoTGpioSetFunc(BUZZER_PIN, BUZZER_PIN_FUNC); \
  15. IoTGpioSetDir(BUZZER_PIN, IOT_GPIO_DIR_OUT); \
  16. IoTPwmInit(BUZZER_PWM_PORT); \
  17. }
  18. #define BUZZER_PWM_START(HZ) {IoTPwmStart(BUZZER_PWM_PORT, 50, HZ);}
  19. #define BUZZER_PWM_STOP() {IoTPwmStop(BUZZER_PWM_PORT);}
  20. void Buzzer_init(void);
  21. void Buzzer_beep(uint16_t hz);
  22. void Buzzer_stop(void);
  23. #endif
  1. #include "buzzer.h"
  2. void Buzzer_init(void) {
  3. BUZZER_PIN_INIT();
  4. }
  5. void Buzzer_beep(uint16_t hz) {
  6. BUZZER_PWM_START(hz);
  7. }
  8. void Buzzer_stop(void) {
  9. BUZZER_PWM_STOP();
  10. }
  1. static_library("music_driver") {
  2. sources = [
  3. "main.c",
  4. "buzzer.c"
  5. ]
  6. include_dirs = [
  7. "//base/iothardware/peripheral/interfaces/inner_api",
  8. "../../iot_hardware_hals/include"
  9. ]
  10. }
  1. import("//build/lite/config/component/lite_component.gni")
  2. lite_component("app") {
  3. features = [
  4. "music_driver"
  5. ]
  6. }

蜂鸣器驱动封装

练习题

  1. gpio控制灯
  2. pwm控制灯
  3. pwm播放音乐
  4. buzzer驱动封装