HarmonyOS驱动子系统
ADC
API介绍
wifiiot_adc.h接口简介
接口名 | 功能描述 |
---|---|
unsigned int AdcRead(WifiIotAdcChannelIndex channel, unsigned short *data, WifiIotAdcEquModelSel equModel,WifiIotAdcCurBais curBais, unsigned short rstCnt) | 根据输入参数从指定的ADC通道读取一段采样数据 |
参数:
名字 | 描述 |
---|---|
channel | 表示ADC通道. |
data | 表示指向存储读取数据的地址的指针 |
equModel | 表示平均算法的次数 |
curBais | 表示模拟功率控制模式 |
rstCnt | 指示从重置到转换开始的时间计数。一次计数等于334纳秒。值的范围必须从0到0xFF |
ADC读取GPIO的电压值
可以看到GPIO11可以复用为ADC5.
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <math.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_errno.h"
#include "wifiiot_adc.h"
#define ADC_TASK_STACK_SIZE 1024 * 8
#define ADC_TASK_PRIO 24
/***** 获取电压值函数 *****/
static float GetVoltage(void)
{
unsigned int ret;
unsigned short data;
ret = AdcRead(WIFI_IOT_ADC_CHANNEL_5, &data, WIFI_IOT_ADC_EQU_MODEL_8, WIFI_IOT_ADC_CUR_BAIS_DEFAULT, 0xff);
if (ret != WIFI_IOT_SUCCESS)
{
printf("ADC Read Fail\n");
}
return (float)data * 1.8 * 4 / 4096.0;
}
static void ADCTask(void)
{
float voltage;
//上拉,让按键未按下时GPIO_11保持高电平状态
IoSetPull(WIFI_IOT_IO_NAME_GPIO_11, WIFI_IOT_IO_PULL_UP);
while (1)
{
printf("=======================================\r\n");
printf("***************ADC_example*************\r\n");
printf("=======================================\r\n");
//获取电压值
voltage = GetVoltage();
printf("vlt:%.3fV\n", voltage);
//延时1s
usleep(1000000);
}
}
static void ADCExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "ADCTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = ADC_TASK_STACK_SIZE;
attr.priority = ADC_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)ADCTask, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create ADCTask!\n");
}
}
APP_FEATURE_INIT(ADCExampleEntry);
GPIO接口保持高电平值,按下F1时,GPIO接口变成低电平,松开再次变成低电平。
I2C
I2C API介绍
wifiiot_i2c.h接口简介
接口名 | 功能描述 |
---|---|
unsigned int I2cInit(WifiIotI2cIdx id, unsigned int baudrate) | 初始化I2C |
unsigned int I2cDeinit(WifiIotI2cIdx id) | 取消I2C初始化 |
unsigned int I2cWrite(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 将数据写入到I2C设备 |
unsigned int I2cRead(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 从I2C设备读取数据 |
wifiiot_i2c_ex.h接口简介
接口名 | 功能描述 |
---|---|
unsigned int I2cWriteread(WifiIotI2cIdx id, unsigned short deviceAddr, const WifiIotI2cData *i2cData) | 向I2C设备发送数据并接收数据响应 |
unsigned int I2cSetBaudrate(WifiIotI2cIdx id, unsigned int baudrate) | 设置I2C频率 |
查看NFC的I2C对应的GPIO引脚
NFC芯片的I2C对应的GPIO引脚是分别GPIO0和GPIO1,所以需要编写软件使用GPIO_0和GPIO_1,所以需要编写软件使用GPIO_0和GPIO_1产生I2C信号去控制NFC芯片。
I2C读写NFC芯片
/*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_errno.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_i2c.h"
#include "wifiiot_i2c_ex.h"
#include "nfc.h"
#define I2C_TASK_STACK_SIZE 1024 * 8
#define I2C_TASK_PRIO 25
#define TEXT "Welcome to BearPi-HM_Nano!"
#define WEB "harmonyos.com"
static void I2CTask(void)
{
uint8_t ret;
GpioInit();
//GPIO_0复用为I2C1_SDA
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_0, WIFI_IOT_IO_FUNC_GPIO_0_I2C1_SDA);
//GPIO_1复用为I2C1_SCL
IoSetFunc(WIFI_IOT_IO_NAME_GPIO_1, WIFI_IOT_IO_FUNC_GPIO_1_I2C1_SCL);
//baudrate: 400kbps
I2cInit(WIFI_IOT_I2C_IDX_1, 400000);
I2cSetBaudrate(WIFI_IOT_I2C_IDX_1, 400000);
printf("I2C Test Start\n");
ret = storeText(NDEFFirstPos, (uint8_t *)TEXT);
if (ret != 1)
{
printf("NFC Write Data Falied :%d ", ret);
}
ret = storeUrihttp(NDEFLastPos, (uint8_t *)WEB);
if (ret != 1)
{
printf("NFC Write Data Falied :%d ", ret);
}
while (1)
{
printf("=======================================\r\n");
printf("***********I2C_NFC_example**********\r\n");
printf("=======================================\r\n");
printf("Please use the mobile phone with NFC function close to the development board!\r\n");
usleep(1000000);
}
}
static void I2CExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "I2CTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = I2C_TASK_STACK_SIZE;
attr.priority = I2C_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)I2CTask, NULL, &attr) == NULL)
{
printf("Falied to create I2CTask!\n");
}
}
APP_FEATURE_INIT(I2CExampleEntry);
使用手机触碰NFC查看
UART读写
相关API
wifiiot_uart.h接口简介
接口名 | 功能描述 |
---|---|
unsigned int UartInit(WifiIotUartIdx id, const WifiIotUartAttribute *param, const WifiIotUartExtraAttr *extraAttr) | 初始化UART |
unsigned int UartDeinit(WifiIotUartIdx id) | 取消UART初始化 |
int UartRead(WifiIotUartIdx id, unsigned char *data, unsigned int dataLen) | 从UART读取数据 |
int UartWrite(WifiIotUartIdx id, const unsigned char *data, unsigned int dataLen) | 将数据写入UART |
unsigned int UartSetFlowCtrl(WifiIotUartIdx id, WifiIotFlowCtrl flowCtrl) | 设置UART流控制 |
查看UART1对应的GPIO引脚
UART1对应的GPIO引脚是分别是GPIO5和GPIO6,将使用GPIO5和GPIO6进行UART数据的收发
UART读写数据
/*
* 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.
*/
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "wifiiot_errno.h"
#include "wifiiot_gpio.h"
#include "wifiiot_gpio_ex.h"
#include "wifiiot_adc.h"
#include "wifiiot_uart.h"
#define UART_TASK_STACK_SIZE 1024 * 8
#define UART_TASK_PRIO 25
#define UART_BUFF_SIZE 1000
static const char *data = "Hello, BearPi!\r\n";
static void UART_Task(void)
{
uint8_t uart_buff[UART_BUFF_SIZE] = {0};
uint8_t *uart_buff_ptr = uart_buff;
uint32_t ret;
WifiIotUartAttribute uart_attr = {
//baud_rate: 9600
.baudRate = 9600,
//data_bits: 8bits
.dataBits = 8,
.stopBits = 1,
.parity = 0,
};
//Initialize uart driver
ret = UartInit(WIFI_IOT_UART_IDX_1, &uart_attr, NULL);
if (ret != WIFI_IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\n", ret);
return;
}
printf("UART Test Start\n");
while (1)
{
printf("=======================================\r\n");
printf("*************UART_example**************\r\n");
printf("=======================================\r\n");
//通过串口1发送数据
UartWrite(WIFI_IOT_UART_IDX_1, (unsigned char *)data, strlen(data));
//通过串口1接收数据
UartRead(WIFI_IOT_UART_IDX_1, uart_buff_ptr, UART_BUFF_SIZE);
printf("Uart1 read data:%s", uart_buff_ptr);
usleep(1000000);
}
}
static void UART_ExampleEntry(void)
{
osThreadAttr_t attr;
attr.name = "UART_Task";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = UART_TASK_STACK_SIZE;
attr.priority = UART_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)UART_Task, NULL, &attr) == NULL)
{
printf("[ADCExample] Falied to create UART_Task!\n");
}
}
APP_FEATURE_INIT(UART_ExampleEntry);
使用杜邦线,短接IO05和IO06,实现自发自接。