梅科尔工作室OpenHarmony设备开发培训笔记-第三章学习笔记

Rishers
发布于 2022-8-7 20:47
浏览
0收藏

GPIO:
扩展实例代码(B1):

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void LedTask(void)
{
//初始化GPIO
GpioInit();
//设置GPIO_2的复用功能为普通GPIO
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
//设置GPIO_2为输出模式
GpioSetDir(WIFI_IOT_GPIO_IDX_2, WIFI_IOT_GPIO_DIR_OUT);
WifiIotGpioDir val = {0};//定义读取变量val
GpioGetDir(WIFI_IOT_GPIO_IDX_2, &val);//获取WIFI_IOT_GPIO_IDX_2的引脚方向,并赋值给变量val
printf("GPIO_2 Dir is %d\r\n", val);
WifiIotGpioValue OutputVal = {0};//定义读取变量OutputVal
while (1)
    {
//设置GPIO_2输出高电平点亮LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 1);
GpioGetOutputVal(WIFI_IOT_GPIO_IDX_2, &OutputVal);//读取WIFI_IOT_GPIO_IDX_2的高电平,并赋值给变量OutputVal
printf("GPIO_2 OutputVal is %d\r\n", OutputVal);
//延时1s
usleep(1000000);
//设置GPIO_2输出低电平熄灭LED灯
GpioSetOutputVal(WIFI_IOT_GPIO_IDX_2, 0);
GpioGetOutputVal(WIFI_IOT_GPIO_IDX_2, &OutputVal);//读取WIFI_IOT_GPIO_IDX_2的低电平,并赋值给OutputVal
printf("GPIO_2 OutputVal is %d\r\n", OutputVal);
//延时1s
usleep(1000000);
    }
}
static void LedExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "LedTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024*2;//要打印字符,需要增大任务的空间
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)LedTask, NULL, &attr) == NULL)
    {
printf("Falied to create LedTask!\n");
    }
}
APP_FEATURE_INIT(LedExampleEntry);


 --------------------------------------------------------------------


GPIO中断:
扩展实例代码(B2):

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
static void F1_Pressed(char *arg)
{
    (void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 1);//点亮LED
printf("This is F1_Pressed\r\n");
GpioSetIsrMode(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_EDGE_RISE_LEVEL_HIGH);//将WIFI_IOT_IO_NAME_GPIO_11的模式修改为电平由低到高才触发中断
}
static void F2_Pressed(char *arg)
{
    (void)arg;
GpioSetOutputVal(WIFI_IOT_IO_NAME_GPIO_2, 0);//熄灭LED
printf("This is F1_Pressed\r\n");
GpioSetIsrMask(WIFI_IOT_IO_NAME_GPIO_12, 1);//屏蔽WIFI_IOT_IO_NAME_GPIO_12的中断功能
}
static void ButtonExampleEntry(void)
{
GpioInit();
//初始化LED灯
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化F1按键,设置为下降沿触发中断(即电平由高到低触发中断)
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_FUNC_GPIO_11_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F1_Pressed, NULL);
//初始化F2按键,设置为下降沿触发中断(即电平由高到低触发中断)
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_FUNC_GPIO_12_GPIO);
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_GPIO_DIR_IN);
IoSetPull(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_IO_PULL_UP);
GpioRegisterIsrFunc(WIFI_IOT_IO_NAME_GPIO_12, WIFI_IOT_INT_TYPE_EDGE, WIFI_IOT_GPIO_EDGE_FALL_LEVEL_LOW, F2_Pressed, NULL);
}
APP_FEATURE_INIT(ButtonExampleEntry);


 --------------------------------------------------------------------


PWM:
扩展实例代码(B3):

#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_pwm.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#define PWM_TASK_STACK_SIZE 512
#define PWM_TASK_PRIO 25
static void PWMTask(void)
{
unsigned int i;
//初始化GPIO
GpioInit();
//设置GPIO_2引脚复用功能为PWM
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_IO_FUNC_GPIO_2_PWM2_OUT);
//设置GPIO_2引脚为输出模式
GpioSetDir(WIFI_IOT_IO_NAME_GPIO_2, WIFI_IOT_GPIO_DIR_OUT);
//初始化PWM2端口
PwmInit(WIFI_IOT_PWM_PORT_PWM2);
uint8_t j = 0;
while (1)
    {
for (i = 0; i < 40000; i += 100)
        {
//输出不同占空比的PWM波
PwmStart(WIFI_IOT_PWM_PORT_PWM2, i, 40000);
//PwmStop(WIFI_IOT_PWM_PORT_PWM2);//在PWM刚输出时即停止,灯仍然闪烁,但非常暗
usleep(10);
        }
i = 0;
j++;
if(j == 5){
PwmDeinit(WIFI_IOT_PWM_PORT_PWM2);//当LED灯闪烁5次后,取消初始化PWM,LED灯始终保持最亮
        }
    }
}
static void PWMExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "PWMTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 512;
attr.priority = 25;
if (osThreadNew((osThreadFunc_t)PWMTask, NULL, &attr) == NULL)
    {
printf("Falied to create PWMTask!\n");
    }
}
APP_FEATURE_INIT(PWMExampleEntry);


 --------------------------------------------------------------------


ADC:
BearPi_HM Nano芯片手册位置:

./my_bearpi_hm_nano/applocations/BearPi/BearPi-HM_Nano/

 

 --------------------------------------------------------------------


I2C:
如果想让手机触碰NFC设备自动打开网页,要将网址写入位置设置为NDEFFirstPos

标签
收藏
回复
举报
回复
    相关推荐