LED呼吸灯¶
项目编写¶
- 在
applications/genkipi/app
下新建pwm_led01
文件夹 - 在
pwm_led01
下新建main.c
文件 - 在
pwm_led01
下新建BUILD.gn
文件
代码部分¶
main.c
文件内容
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_io.h"
#include "genki_pin.h"
static void start(void) {
//初始化GPIO口
IoTGpioInit(IOT_IO_NAME_2);
//设置IO口功能为GPIO
IoTIoSetFunc(IOT_IO_NAME_2, IOT_IO_FUNC_2_PWM2_OUT);
//设置IO口输出方向:输出
IoTGpioSetDir(IOT_IO_NAME_2, IOT_GPIO_DIR_OUT);
//初始化PWM功能
IoTPwmInit(IOT_IO_NAME_2);
//不断输出pwm方波
while (1) {
for (int i = 0; i < 20; i++) {
IoTPwmStart(IOT_IO_NAME_2, i, 10000);
usleep(0.05 * 1000 * 1000);
}
for (int i = 20; i > 0; i--) {
IoTPwmStart(IOT_IO_NAME_2, i, 10000);
usleep(0.05 * 1000 * 1000);
}
}
}
APP_FEATURE_INIT(start);
项目Build.gn¶
pwm_led01
目录下 BUILD.gn
内容为
static_library("pwm_led01") {
sources = [
"main.c"
]
include_dirs = [
"//utils/native/lite/include",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/itcast/genkipi/interfaces/kits"
]
}
外部Build.gn¶
pwm_led01
文件夹上一级目录下BUILD.gn
内容为
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"pwm_led01"
]
}
外接LED呼吸灯¶
接线要求¶
-
GND连接开发板GND
-
VCC连接开发板3.3V或5V
-
OUT连接GPIO14引脚(其它引脚也可以)
项目编写¶
- 在
applications/genkipi/app
下新建pwm_led02
文件夹 - 在
pwm_led02
下新建main.c
文件 - 在
pwm_led02
下新建BUILD.gn
文件
代码部分¶
main.c
文件内容
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_pwm.h"
#include "iot_io.h"
#include "genki_pin.h"
static void start(void) {
//初始化GPIO口
IoTGpioInit(IOT_IO_NAME_14);
//设置IO口功能为GPIO
IoTIoSetFunc(IOT_IO_NAME_14, IOT_IO_FUNC_14_PWM5_OUT);
//设置IO口输出方向:输出
IoTGpioSetDir(IOT_IO_NAME_14, IOT_GPIO_DIR_OUT);
//初始化PWM功能
IoTPwmInit(IOT_PWM_NAME_5);
//不断输出pwm方波
while (1) {
for (int i = 0; i < 20; i++) {
IoTPwmStart(IOT_PWM_NAME_5, i, 10000);
usleep(0.05 * 1000 * 1000);
}
for (int i = 20; i > 0; i--) {
IoTPwmStart(IOT_PWM_NAME_5, i, 10000);
usleep(0.05 * 1000 * 1000);
}
}
}
APP_FEATURE_INIT(start);
项目Build.gn¶
pwm_led02
目录下 BUILD.gn
内容为
static_library("pwm_led02") {
sources = [
"main.c"
]
include_dirs = [
"//utils/native/lite/include",
"//base/iot_hardware/peripheral/interfaces/kits",
"//device/itcast/genkipi/interfaces/kits"
]
}
外部Build.gn¶
pwm_led02
文件夹上一级目录下BUILD.gn
内容为
import("//build/lite/config/component/lite_component.gni")
lite_component("app") {
features = [
"pwm_led02"
]
}