OpenHarmony智能開發(fā)套件[驅(qū)動開發(fā)篇(下)
前言
前面介紹了OpenHarmony智能開發(fā)套件驅(qū)動開發(fā)篇·上,下面我們接著介紹驅(qū)動開發(fā),本章將介紹如何IO控制溫濕度傳感器,可燃氣體傳感器,RGB燈,人體紅外傳感器,光敏電阻,Oled顯示屏。
驅(qū)動開發(fā)
環(huán)境監(jiān)測板
環(huán)境檢測板上一共有三個模塊,AHT20數(shù)字溫濕度傳感器,蜂鳴器,MQ-2可燃氣體傳感器。蜂鳴器的使用在上一篇已經(jīng)提及過,為了方便學習,本次只介紹溫濕度傳感器,可燃氣體傳感器的IO控制,蜂鳴器的結(jié)合使用可以放在以后的綜合案例中去說。
溫濕度傳感器
案例:每隔1s,獲取一次溫濕度,打印在終端
- 新建樣例目錄
applications/sample/wifi-iot/app/aht20 - 新建源文件和gn文件
applications/sample/wifi-iot/app/aht20/aht20Test.c
applications/sample/wifi-iot/app/aht20/BUILD.gn - 補充文件
applications/sample/wifi-iot/app/aht20/aht20.c
applications/sample/wifi-iot/app/aht20/aht20.h
aht20.c
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* 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 "aht20.h"
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "iot_i2c.h"
#include "iot_errno.h"
#define AHT20_I2C_IDX 0
#define AHT20_STARTUP_TIME 20*1000 // 上電啟動時間
#define AHT20_CALIBRATION_TIME 40*1000 // 初始化(校準)時間
#define AHT20_MEASURE_TIME 75*1000 // 測量時間
#define AHT20_DEVICE_ADDR 0x38
#define AHT20_READ_ADDR ((0x38<<1)|0x1)
#define AHT20_WRITE_ADDR ((0x38<<1)|0x0)
#define AHT20_CMD_CALIBRATION 0xBE // 初始化(校準)命令
#define AHT20_CMD_CALIBRATION_ARG0 0x08
#define AHT20_CMD_CALIBRATION_ARG1 0x00
/**
* 傳感器在采集時需要時間,主機發(fā)出測量指令(0xAC)后,延時75毫秒以上再讀取轉(zhuǎn)換后的數(shù)據(jù)并判斷返回的狀態(tài)位是否正常。
* 若狀態(tài)比特位[Bit7]為0代表數(shù)據(jù)可正常讀取,為1時傳感器為忙狀態(tài),主機需要等待數(shù)據(jù)處理完成。
**/
#define AHT20_CMD_TRIGGER 0xAC // 觸發(fā)測量命令
#define AHT20_CMD_TRIGGER_ARG0 0x33
#define AHT20_CMD_TRIGGER_ARG1 0x00
// 用于在無需關閉和再次打開電源的情況下,重新啟動傳感器系統(tǒng),軟復位所需時間不超過20 毫秒
#define AHT20_CMD_RESET 0xBA // 軟復位命令
#define AHT20_CMD_STATUS 0x71 // 獲取狀態(tài)命令
/**
* STATUS 命令回復:
* 1. 初始化后觸發(fā)測量之前,STATUS 只回復 1B 狀態(tài)值;
* 2. 觸發(fā)測量之后,STATUS 回復6B: 1B 狀態(tài)值 + 2B 濕度 + 4b濕度 + 4b溫度 + 2B 溫度
* RH = Srh / 2^20 * 100%
* T = St / 2^20 * 200 - 50
**/
#define AHT20_STATUS_BUSY_SHIFT 7 // bit[7] Busy indication
#define AHT20_STATUS_BUSY_MASK (0x1<<AHT20_STATUS_BUSY_SHIFT)
#define AHT20_STATUS_BUSY(status) ((status & AHT20_STATUS_BUSY_MASK) >> AHT20_STATUS_BUSY_SHIFT)
#define AHT20_STATUS_MODE_SHIFT 5 // bit[6:5] Mode Status
#define AHT20_STATUS_MODE_MASK (0x3<<AHT20_STATUS_MODE_SHIFT)
#define AHT20_STATUS_MODE(status) ((status & AHT20_STATUS_MODE_MASK) >> AHT20_STATUS_MODE_SHIFT)
// bit[4] Reserved
#define AHT20_STATUS_CALI_SHIFT 3 // bit[3] CAL Enable
#define AHT20_STATUS_CALI_MASK (0x1<<AHT20_STATUS_CALI_SHIFT)
#define AHT20_STATUS_CALI(status) ((status & AHT20_STATUS_CALI_MASK) >> AHT20_STATUS_CALI_SHIFT)
// bit[2:0] Reserved
#define AHT20_STATUS_RESPONSE_MAX 6
#define AHT20_RESLUTION (1<<20) // 2^20
#define AHT20_MAX_RETRY 10
// typedef struct {
// /** Pointer to the buffer storing data to send */
// unsigned char *sendBuf;
// /** Length of data to send */
// unsigned int sendLen;
// /** Pointer to the buffer for storing data to receive */
// unsigned char *receiveBuf;
// /** Length of data received */
// unsigned int receiveLen;
// } IotI2cData;
static uint32_t AHT20_Read(uint8_t* buffer, uint32_t buffLen)
{
// IotI2cData data = { 0 };
// data.receiveBuf = buffer;
// data.receiveLen = buffLen;
// uint32_t retval = I2cRead(AHT20_I2C_IDX, AHT20_READ_ADDR, data.receiveBuf );
// if (retval != IOT_SUCCESS) {
// printf("I2cRead() failed, %0X!\n", retval);
// return retval;
// }
// return IOT_SUCCESS;
uint32_t retval = IoTI2cRead(AHT20_I2C_IDX, AHT20_READ_ADDR, buffer, buffLen);
if (retval != IOT_SUCCESS) {
printf("I2cRead() failed, %0X!\n", retval);
return retval;
}
return IOT_SUCCESS;
}
static uint32_t AHT20_Write(uint8_t* buffer, uint32_t buffLen)
{
// IotI2cData data = { 0 };
// data.sendBuf = buffer;
// data.sendLen = buffLen;
// uint32_t retval = IoTI2cWrite(AHT20_I2C_IDX, AHT20_WRITE_ADDR, &data);
// if (retval != IOT_SUCCESS) {
// printf("I2cWrite(%02X) failed, %0X!\n", buffer[0], retval);
// return retval;
// }
// return IOT_SUCCESS;
uint32_t retval = IoTI2cWrite(AHT20_I2C_IDX, AHT20_READ_ADDR, buffer, buffLen);
if (retval != IOT_SUCCESS) {
printf("I2cRead() failed, %0X!\n", retval);
return retval;
}
return IOT_SUCCESS;
}
// 發(fā)送獲取狀態(tài)命令
static uint32_t AHT20_StatusCommand(void)
{
uint8_t statusCmd[] = { AHT20_CMD_STATUS };
return AHT20_Write(statusCmd, sizeof(statusCmd));
}
// 發(fā)送軟復位命令
static uint32_t AHT20_ResetCommand(void)
{
uint8_t resetCmd[] = {AHT20_CMD_RESET};
return AHT20_Write(resetCmd, sizeof(resetCmd));
}
// 發(fā)送初始化校準命令
static uint32_t AHT20_CalibrateCommand(void)
{
uint8_t clibrateCmd[] = {AHT20_CMD_CALIBRATION, AHT20_CMD_CALIBRATION_ARG0, AHT20_CMD_CALIBRATION_ARG1};
return AHT20_Write(clibrateCmd, sizeof(clibrateCmd));
}
// 讀取溫濕度值之前, 首先要看狀態(tài)字的校準使能位Bit[3]是否為 1(通過發(fā)送0x71可以獲取一個字節(jié)的狀態(tài)字),
// 如果不為1,要發(fā)送0xBE命令(初始化),此命令參數(shù)有兩個字節(jié), 第一個字節(jié)為0x08,第二個字節(jié)為0x00。
uint32_t AHT20_Calibrate(void)
{
uint32_t retval = 0;
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = { AHT20_CMD_STATUS };
memset(&buffer, 0x0, sizeof(buffer));
retval = AHT20_StatusCommand();
if (retval != IOT_SUCCESS) {
return retval;
}
retval = AHT20_Read(buffer, sizeof(buffer));
if (retval != IOT_SUCCESS) {
return retval;
}
if (AHT20_STATUS_BUSY(buffer[0]) || !AHT20_STATUS_CALI(buffer[0])) {
retval = AHT20_ResetCommand();
if (retval != IOT_SUCCESS) {
return retval;
}
usleep(AHT20_STARTUP_TIME);
retval = AHT20_CalibrateCommand();
usleep(AHT20_CALIBRATION_TIME);
return retval;
}
return IOT_SUCCESS;
}
// 發(fā)送 觸發(fā)測量 命令,開始測量
uint32_t AHT20_StartMeasure(void)
{
uint8_t triggerCmd[] = {AHT20_CMD_TRIGGER, AHT20_CMD_TRIGGER_ARG0, AHT20_CMD_TRIGGER_ARG1};
return AHT20_Write(triggerCmd, sizeof(triggerCmd));
}
// 接收測量結(jié)果,拼接轉(zhuǎn)換為標準值
uint32_t AHT20_GetMeasureResult(float* temp, float* humi)
{
uint32_t retval = 0, i = 0;
if (temp == NULL || humi == NULL) {
return IOT_FAILURE;
}
uint8_t buffer[AHT20_STATUS_RESPONSE_MAX] = { 0 };
memset(&buffer, 0x0, sizeof(buffer));
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != IOT_SUCCESS) {
return retval;
}
for (i = 0; AHT20_STATUS_BUSY(buffer[0]) && i < AHT20_MAX_RETRY; i++) {
// printf("AHT20 device busy, retry %d/%d!\r\n", i, AHT20_MAX_RETRY);
usleep(AHT20_MEASURE_TIME);
retval = AHT20_Read(buffer, sizeof(buffer)); // recv status command result
if (retval != IOT_SUCCESS) {
return retval;
}
}
if (i >= AHT20_MAX_RETRY) {
printf("AHT20 device always busy!\r\n");
return IOT_FAILURE;
}
uint32_t humiRaw = buffer[1];
humiRaw = (humiRaw << 8) | buffer[2];
humiRaw = (humiRaw << 4) | ((buffer[3] & 0xF0) >> 4);
*humi = humiRaw / (float)AHT20_RESLUTION * 100;
uint32_t tempRaw = buffer[3] & 0x0F;
tempRaw = (tempRaw << 8) | buffer[4];
tempRaw = (tempRaw << 8) | buffer[5];
*temp = tempRaw / (float)AHT20_RESLUTION * 200 - 50;
// printf("humi = %05X, %f, temp= %05X, %f\r\n", humiRaw, *humi, tempRaw, *temp);
return IOT_SUCCESS;
}
aht20.h
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* 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.
*/
#ifndef AHT20_H
#define AHT20_H
#include <stdint.h>
uint32_t AHT20_Calibrate(void);
uint32_t AHT20_StartMeasure(void);
uint32_t AHT20_GetMeasureResult(float* temp, float* humi);
#endif // AHT20_H
這兩個文件,相當于給我們提供了控制aht20數(shù)字溫濕度傳感器的API,通過網(wǎng)上各種溫濕度傳感器的學習也發(fā)現(xiàn)了,這兩個文件是必備的,筆者也是直接從源碼提供的demo中直接。貼出來了。下面也詳細給大家介紹以下每一個函數(shù)怎么去使用。其中標號1至5是不需要我們手動去調(diào)用他的,6至8是我們比較常用的。
- 從傳感器中讀取數(shù)據(jù)。
static uint32_t AHT20_Read(uint8_t* buffer, uint32_t buffLen)
參數(shù)解釋:
- buffer:存儲讀取數(shù)據(jù)的緩沖區(qū)的指針。
- buffLen:緩沖區(qū)的長度。
- 返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于從傳感器中讀取數(shù)據(jù)。它使用 IoTI2cRead 函數(shù)從指定的地址讀取數(shù)據(jù)到緩沖區(qū)中。
- 向傳感器中寫入數(shù)據(jù)。
static uint32_t AHT20_Write(uint8_t* buffer, uint32_t buffLen)
參數(shù)解釋:
- buffer:存儲要寫入傳感器的數(shù)據(jù)的緩沖區(qū)的指針。
- buffLen:緩沖區(qū)的長度。
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
實現(xiàn)過程:該函數(shù)用于向傳感器寫入數(shù)據(jù)。它使用 IoTI2cWrite 函數(shù)將數(shù)據(jù)從緩沖區(qū)寫入指定的地址。
- 獲取傳感器狀態(tài)
static uint32_t AHT20_StatusCommand(void)
參數(shù):無
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于發(fā)送獲取狀態(tài)命令給傳感器。它構(gòu)造一個包含命令的緩沖區(qū),然后使用 AHT20_Write 函數(shù)發(fā)送該命令。
- 軟復位傳感器。
static uint32_t AHT20_ResetCommand(void)
參數(shù):無
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于發(fā)送軟復位命令給傳感器。它構(gòu)造一個包含命令的緩沖區(qū),然后使用 AHT20_Write 函數(shù)發(fā)送該命令。
- 初始化傳感器。
static uint32_t AHT20_CalibrateCommand(void)
參數(shù):無
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于發(fā)送初始化校準命令給傳感器。它構(gòu)造一個包含命令和參數(shù)的緩沖區(qū),然后使用 AHT20_Write 函數(shù)發(fā)送該命令。
- 校準傳感器。
uint32_t AHT20_Calibrate(void)
參數(shù):無
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于校準傳感器。它首先發(fā)送獲取狀態(tài)命令,然后讀取狀態(tài)字并檢查校準使能位。如果校準使能位不為1或傳感器處于忙狀態(tài),它會發(fā)送軟復位命令并等待一段時間后再發(fā)送初始化校準命令。
- 發(fā)送命令,傳感器開始測量。
uint32_t AHT20_StartMeasure(void)
參數(shù):無
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于發(fā)送觸發(fā)測量命令給傳感器,以開始測量過程。它構(gòu)造一個包含命令和參數(shù)的緩沖區(qū),然后使用 AHT20_Write
函數(shù)發(fā)送該命令。
- 獲取傳感器的測量結(jié)果。
uint32_t AHT20_GetMeasureResult(float* temp, float* humi)
參數(shù)解釋:
- temp:用于存儲溫度值的指針。
- humi:用于存儲濕度值的指針。
返回值:一個 uint32_t 類型的整數(shù),表示執(zhí)行操作的結(jié)果。
該函數(shù)用于接收測量結(jié)果并將其轉(zhuǎn)換為標準值。它首先從傳感器讀取狀態(tài)命令的結(jié)果,然后根據(jù)狀態(tài)字判斷傳感器的忙閑狀態(tài)。如果傳感器忙碌,函數(shù)會等待一段時間后再次讀取狀態(tài)命令的結(jié)果。如果傳感器一直忙碌,函數(shù)返回失敗。如果傳感器空閑,函數(shù)會從緩沖區(qū)中提取濕度和溫度的原始值,并將其轉(zhuǎn)換為標準值,然后存儲在指定的變量中。最后,函數(shù)返回成功或失敗的結(jié)果。
- 編寫源文件aht20Test.c。
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
// I2C控制器
#include "hi_i2c.h"
// aht20溫濕度傳感器
#include "aht20.h"
// 一些宏定義 如 IOT_SUCCESS
#include "iot_errno.h"
// 樣例主函數(shù)
static void ahtMain(void){
// 設置引腳的功能,
hi_io_set_func(HI_IO_NAME_GPIO_13, HI_IO_FUNC_GPIO_13_I2C0_SDA);
hi_io_set_func(HI_IO_NAME_GPIO_14, HI_IO_FUNC_GPIO_14_I2C0_SCL);
// 以特定的波特率初始化I2C設備
hi_i2c_init(HI_I2C_IDX_0, 4000);
// 定義返回值
uint32_t retval = 0;
retval = AHT20_Calibrate();
while(retval != IOT_SUCCESS){
printf("傳感器校準失敗!\r\n");
osDelay(50);
}
while(1){
// 定義溫濕度變量
float temp = 0.0, humi = 0.0;
// 命令傳感器開始工作
retval = AHT20_StartMeasure();
if(retval != IOT_SUCCESS){
printf("傳感器工作異常!\r\n");
}
// 讀取傳感器的測量值
retval = AHT20_GetMeasureResult(&temp, &humi);
if(retval != IOT_SUCCESS){
printf("傳感器數(shù)值讀取失敗!\r\n");
} else {
printf("當前溫度:%.2f 當前濕度:%.2f.\r\n", temp, humi);
}
// 每隔1s讀取一次傳感器數(shù)據(jù)
osDelay(100);
}
}
// 樣例測試入口
static void aht20Test(void){
osThreadAttr_t attr = {"ahtMain", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)ahtMain, NULL, &attr);
}
APP_FEATURE_INIT(aht20Test);
- 編寫B(tài)UILD.gn (app目錄下的BUILD.gn文件記得也修改好,這里就不多演示了)。
static_library("aht20"){
sources = [
"aht20.c",
"aht20Test.c",
]
include_dirs = [
"http://commonlibrary/utils_lite/include/",
"http://device/soc/hisilicon/hi3861v100/hi3861_adapter/kal/cmsis",
"http://base/iothardware/peripheral/interfaces/inner_api",
"http://device/soc/hisilicon/hi3861v100/sdk_liteos/include/"
]
}
- 編譯,燒錄,串口調(diào)試,觀察控制臺上的輸出。
那么到這里,溫濕度傳感器最簡單的一個案例就介紹到這里。
可燃氣體傳感器
案例:每隔1s,獲取一次可燃氣體傳感器測量的電阻值,打印在終端
他會用到hi_adc.h庫下的hi_adc_read這個接口,從單個ADC通道讀取一個數(shù)據(jù)。
hi_u32 hi_adc_read(hi_adc_channel_index channel, hi_u16 *data, hi_adc_equ_model_sel equ_model,
hi_adc_cur_bais cur_bais, hi_u16 delay_cnt);
參數(shù)解釋:
- channel:要讀取的ADC通道,類型為hi_adc_channel_index。
- data:數(shù)據(jù)存儲地址,類型為指向hi_u16類型的指針。
- equ_model:平均算法模式,類型為hi_adc_equ_model_sel。
- cur_bais:模擬電源控制,類型為hi_adc_cur_bais。
- delay_cnt:從配置采樣到啟動采樣的延時時間計數(shù),每次計數(shù)為334納秒,取值范圍為0~0xFF0。
返回值:
- HI_ERR_SUCCESS:成功。
- 其他值:失敗。
其中 hi_adc_equ_model_sel 定義為如下的枚舉類型。
typedef enum {
HI_ADC_EQU_MODEL_1, /**< 0:The average value is not used.
CNcomment:1次平均,即不進行
平均 CNend */
HI_ADC_EQU_MODEL_2, /**< 1:2-time average algorithm mode.
CNcomment:2次平均算法模式 CNend */
HI_ADC_EQU_MODEL_4, /**< 2:4-time average algorithm mode.
CNcomment:4次平均算法模式 CNend */
HI_ADC_EQU_MODEL_8, /**< 3:8-time average algorithm mode.
CNcomment:8次平均算法模式 CNend */
HI_ADC_EQU_MODEL_BUTT,
} hi_adc_equ_model_sel;
hi_adc_cur_bais
typedef enum {
HI_ADC_CUR_BAIS_DEFAULT, /**< 0:Auto control.
CNcomment:自動識別模式 */
HI_ADC_CUR_BAIS_AUTO, /**< 1:Auto control.
CNcomment:自動識別模式 */
HI_ADC_CUR_BAIS_1P8V, /**< 2:Manual control, AVDD=1.8V.
CNcomment:手動控制,AVDD=1.8V */
HI_ADC_CUR_BAIS_3P3V, /**< 3:Manual control, AVDD=3.3V.
CNcomment:手動控制,AVDD=3.3V */
HI_ADC_CUR_BAIS_BUTT,
} hi_adc_cur_bais;
- 新建樣例目錄
applications/sample/wifi-iot/app/gas - 新建源文件和gn文件
applications/sample/wifi-iot/app/gas/gasTest.c
applications/sample/wifi-iot/app/gas/BUILD.gn - 編寫源文件gasTest.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
// adc
#include "hi_adc.h"
// 一些宏定義 如 IOT_SUCCESS
#include "iot_errno.h"
#define GAS_SENSOR_CHAN_NAME 5
// 主函數(shù)
static void gasMain(void){
// 定義數(shù)據(jù)變量
float gasSensorResistance = 0.0f;
while(1){
unsigned short data = 0;
int retval;
retval = hi_adc_read(GAS_SENSOR_CHAN_NAME, &data, 2, 0, 0);
if (retval == IOT_SUCCESS) {
float Vx = data * 1.8 * 4 / 4096;
gasSensorResistance = 5 / Vx - 1;
printf("當前可燃氣體傳感器獲取的電阻值為:%.2f\r\n", gasSensorResistance);
} else {
printf("\r\n hi_adc_read fail, retval=%d", retval);
}
osDelay(100);
}
}
// 樣例測試入口
static void gasTest(void){
osThreadAttr_t attr = {"gasMain", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)gasMain, NULL, &attr);
}
APP_FEATURE_INIT(gasTest);
while循環(huán)中的部分是從源碼提供的demo中貼過來的,涉及到一些電路問題,還沒有研究這一塊。
// Vcc ADC GND
// | ______ | ______ |
// +---| MG-2 |---+---| 1kom |---+
// ------ ------
// 查閱原理圖,ADC 引腳位于 1K 電阻和燃氣傳感器之間,燃氣傳感器另一端接在 5V 電源正極上
// 串聯(lián)電路電壓和阻止成正比:
// Vx / 5 == 1kom / (1kom + Rx)
// => Rx + 1 == 5/Vx
// => Rx = 5/Vx - 1
- 編寫B(tài)UILD.gn (app目錄下的BUILD.gn文件記得也修改好,這里就不多演示了)。
static_library("gas"){
sources = [
"gasTest.c",
]
include_dirs = [
"http://commonlibrary/utils_lite/include/",
"http://device/soc/hisilicon/hi3861v100/hi3861_adapter/kal/cmsis",
"http://base/iothardware/peripheral/interfaces/inner_api",
"http://device/soc/hisilicon/hi3861v100/sdk_liteos/include/"
]
}
- 編譯,燒錄,串口調(diào)試,觀察控制臺上的輸出。
RGB板
三色燈
絕大多數(shù)可見光可用三色光(紅、綠、藍)的不同強度的混合來表示,即RGB顏色模型。
R/G/B取值范圍:0~255。黑色(0,0,0),白色(255,255,255),紅色(255,0,0)、綠色(0,255,0)、藍色(0,0,255)。
- 可表示顏色數(shù):256256256=16777216色。
- 紅色:28引腳,GPIO-10 / PWM1。
- 綠色:29引腳,GPIO-11 / PWM2。
- 藍色:30引腳,GPIO-12 / PWM3。
下面直接上案例吧,RGB的效果,實際上就是改變PWM的占空比,實現(xiàn)紅綠藍的混色效果。
- 新建樣例目錄
applications/sample/wifi-iot/app/rgb - 新建源文件和gn文件
applications/sample/wifi-iot/app/rgb/rgb.c
applications/sample/wifi-iot/app/rgb/BUILD.gn - 編寫源文件rgb.c
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "hi_io.h"
#include "hi_pwm.h"
static void rgbDemo(){
IoTGpioInit(HI_IO_NAME_GPIO_10);
IoTGpioInit(HI_IO_NAME_GPIO_11);
IoTGpioInit(HI_IO_NAME_GPIO_12);
hi_io_set_func(HI_IO_NAME_GPIO_10, HI_IO_FUNC_GPIO_10_PWM1_OUT);
hi_io_set_func(HI_IO_NAME_GPIO_11, HI_IO_FUNC_GPIO_11_PWM2_OUT);
hi_io_set_func(HI_IO_NAME_GPIO_12, HI_IO_FUNC_GPIO_12_PWM3_OUT);
IoTGpioSetDir(HI_IO_NAME_GPIO_10, IOT_GPIO_DIR_OUT);
IoTGpioSetDir(HI_IO_NAME_GPIO_11, IOT_GPIO_DIR_OUT);
IoTGpioSetDir(HI_IO_NAME_GPIO_12, IOT_GPIO_DIR_OUT);
IoTPwmInit(HI_PWM_PORT_PWM1); // 紅
IoTPwmInit(HI_PWM_PORT_PWM2); // 綠
IoTPwmInit(HI_PWM_PORT_PWM3); // 藍
while(1){
for(int i = 1; i < 100; i++){
IoTPwmStart(HI_PWM_PORT_PWM1, 1, 4000);
IoTPwmStart(HI_PWM_PORT_PWM2, 100 - i, 4000);
IoTPwmStart(HI_PWM_PORT_PWM3, i, 4000);
usleep(20 * 1000);
IoTPwmStop(HI_PWM_PORT_PWM1);
IoTPwmStop(HI_PWM_PORT_PWM2);
IoTPwmStop(HI_PWM_PORT_PWM3);
}
for(int i = 1; i < 100; i++){
IoTPwmStart(HI_PWM_PORT_PWM1, i, 4000);
IoTPwmStart(HI_PWM_PORT_PWM2, 1, 4000);
IoTPwmStart(HI_PWM_PORT_PWM3, 100 - i, 4000);
usleep(20 * 1000);
IoTPwmStop(HI_PWM_PORT_PWM1);
IoTPwmStop(HI_PWM_PORT_PWM2);
IoTPwmStop(HI_PWM_PORT_PWM3);
}
for(int i = 1; i < 100; i++){
IoTPwmStart(HI_PWM_PORT_PWM1, 100 - i, 4000);
IoTPwmStart(HI_PWM_PORT_PWM2, i, 4000);
IoTPwmStart(HI_PWM_PORT_PWM3, 1, 4000);
usleep(20 * 1000);
IoTPwmStop(HI_PWM_PORT_PWM1);
IoTPwmStop(HI_PWM_PORT_PWM2);
IoTPwmStop(HI_PWM_PORT_PWM3);
}
}
}
static void rgbTest(void){
osThreadAttr_t attr = {"rgbDemo", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)rgbDemo, NULL, &attr);
}
APP_FEATURE_INIT(rgbTest);
- 編寫B(tài)UILD.gn (app目錄下的BUILD.gn文件記得也修改好,這里就不多演示了)。
static_library("rgb"){
sources = [
"rgb.c"
]
include_dirs = [
"http://commonlibrary/utils_lite/include/",
"http://device/soc/hisilicon/hi3861v100/hi3861_adapter/kal/cmsis",
"http://base/iothardware/peripheral/interfaces/inner_api",
"http://device/soc/hisilicon/hi3861v100/sdk_liteos/include/"
]
}
- 編譯,燒錄,重啟開發(fā)板,觀察三色燈,注意燈光可能比較強。
人體紅外傳感器
和控制可燃氣體傳感器一樣,我們?nèi)匀皇褂玫氖莌i_adc_read的一個API,不同的是,我們在獲取可燃氣體傳感器時,用的是5,而人體紅外傳感器的標號是3,光敏電阻其實也是一樣的,他的標號是4。
這里就直接貼個主體代碼給大家了,看過上面可燃氣體傳感器的,可以直接拷貝一份案例,在上面直接修改。
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
// adc
#include "hi_adc.h"
// 一些宏定義 如 IOT_SUCCESS
#include "iot_errno.h"
#define INFRARED_SENSOR_CHAN_NAME 3
// 主函數(shù)
static void infraredMain(void){
while(1){
unsigned short data = 0;
int retval;
retval = hi_adc_read(INFRARED_SENSOR_CHAN_NAME, &data, 2, 0, 0);
if (retval == IOT_SUCCESS) {
printf("人體紅外傳感器獲取數(shù)據(jù):data = %d\r\n", data);
if(data > 1800){
printf("檢測到人體紅外!\r\n");
}
} else {
printf("\r\n hi_adc_read fail, retval=%d", retval);
}
osDelay(100);
}
}
// 樣例測試入口
static void infraredTest(void){
osThreadAttr_t attr = {"infraredMain", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)infraredMain, NULL, &attr);
}
APP_FEATURE_INIT(infraredTest);
串口調(diào)試效果如下:
在有紅外是,值在1800的樣子,沒有紅外時,值在120的樣子。
光敏電阻
#include <stdio.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "hi_gpio.h"
#include "hi_io.h"
// adc
#include "hi_adc.h"
// 一些宏定義 如 IOT_SUCCESS
#include "iot_errno.h"
#define SHINE_SENSOR_CHAN_NAME 4
// 主函數(shù)
static void shineMain(void){
while(1){
unsigned short data = 0;
int retval;
retval = hi_adc_read(SHINE_SENSOR_CHAN_NAME, &data, 2, 0, 0);
if (retval == IOT_SUCCESS) {
printf("光敏電阻傳感器獲取數(shù)據(jù):data = %d\r\n", data);
} else {
printf("\r\n hi_adc_read fail, retval=%d", retval);
}
osDelay(100);
}
}
// 樣例測試入口
static void shineTest(void){
osThreadAttr_t attr = {"shineMain", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)shineMain, NULL, &attr);
}
APP_FEATURE_INIT(shineTest);
串口調(diào)試效果如下:
在沒有光時,值在1800的樣子,有光時,值在120的樣子。
Oled顯示屏
最后我們講講如何Oled板,主要是如何點亮他,讓他能代替終端為我們顯示一些東西,下方的兩個按鈕暫且不介紹了。
案例:在Oled板上打印“Hello,World”
- 新建樣例目錄
applications/sample/wifi-iot/app/oled - 新建源文件和gn文件
applications/sample/wifi-iot/app/oled/oledTest.c
applications/sample/wifi-iot/app/oled/BUILD.gn - 補充文件
applications/sample/wifi-iot/app/oled/oled_ssd1306.c
applications/sample/wifi-iot/app/oled/oled_ssd1306.h
applications/sample/wifi-iot/app/oled/oled_fonts.h
oled_ssd1306.c
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* 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 <stddef.h>
#include <stdio.h>
#include "oled_ssd1306.h"
#include "iot_gpio.h"
#include "iot_i2c.h"
#include "iot_errno.h"
#include "oled_fonts.h"
#define ARRAY_SIZE(a) sizeof(a)/sizeof(a[0])
#define OLED_I2C_IDX 0
#define OLED_WIDTH (128)
#define OLED_I2C_ADDR 0x78 // 默認地址為 0x78
#define OLED_I2C_CMD 0x00 // 0000 0000 寫命令
#define OLED_I2C_DATA 0x40 // 0100 0000(0x40) 寫數(shù)據(jù)
#define OLED_I2C_BAUDRATE (400*1000) // 400k
#define DELAY_100_MS (100*1000)
// unsigned int I2cSetBaudrate(WifiIotI2cIdx id, unsigned int baudrate);
typedef struct {
/** Pointer to the buffer storing data to send */
unsigned char *sendBuf;
/** Length of data to send */
unsigned int sendLen;
/** Pointer to the buffer for storing data to receive */
unsigned char *receiveBuf;
/** Length of data received */
unsigned int receiveLen;
} IotI2cData;
static uint32_t I2cWiteByte(uint8_t regAddr, uint8_t byte)
{
unsigned int id = OLED_I2C_IDX;
uint8_t buffer[] = {regAddr, byte};
IotI2cData i2cData = {0};
i2cData.sendBuf = buffer;
i2cData.sendLen = sizeof(buffer)/sizeof(buffer[0]);
return IoTI2cWrite(id, OLED_I2C_ADDR, i2cData.sendBuf, i2cData.sendLen);
}
/**
* @brief Write a command byte to OLED device.
*
* @param cmd the commnad byte to be writen.
* @return Returns {@link IOT_SUCCESS} if the operation is successful;
* returns an error code defined in {@link wifiiot_errno.h} otherwise.
*/
static uint32_t WriteCmd(uint8_t cmd)
{
return I2cWiteByte(OLED_I2C_CMD, cmd);
}
/**
* @brief Write a data byte to OLED device.
*
* @param cmd the data byte to be writen.
* @return Returns {@link IOT_SUCCESS} if the operation is successful;
* returns an error code defined in {@link wifiiot_errno.h} otherwise.
*/
static uint32_t WriteData(uint8_t data)
{
return I2cWiteByte(OLED_I2C_DATA, data);
}
/**
* @brief ssd1306 OLED Initialize.
*/
uint32_t OledInit(void)
{
static const uint8_t initCmds[] = {
0xAE, // --display off
0x00, // ---set low column address
0x10, // ---set high column address
0x40, // --set start line address
0xB0, // --set page address
0x81, // contract control
0xFF, // --128
0xA1, // set segment remap
0xA6, // --normal / reverse
0xA8, // --set multiplex ratio(1 to 64)
0x3F, // --1/32 duty
0xC8, // Com scan direction
0xD3, // -set display offset
0x00, //
0xD5, // set osc division
0x80, //
0xD8, // set area color mode off
0x05, //
0xD9, // Set Pre-Charge Period
0xF1, //
0xDA, // set com pin configuartion
0x12, //
0xDB, // set Vcomh
0x30, //
0x8D, // set charge pump enable
0x14, //
0xAF, // --turn on oled panel
};
IoTGpioInit(13);
hi_io_set_func(13, 6);
IoTGpioInit(14);
hi_io_set_func(14, 6);
IoTI2cInit(0, OLED_I2C_BAUDRATE);
for (size_t i = 0; i < ARRAY_SIZE(initCmds); i++) {
uint32_t status = WriteCmd(initCmds[i]);
if (status != IOT_SUCCESS) {
return status;
}
}
return IOT_SUCCESS;
}
void OledSetPosition(uint8_t x, uint8_t y)
{
WriteCmd(0xb0 + y);
WriteCmd(((x & 0xf0) >> 4) | 0x10);
WriteCmd(x & 0x0f);
}
/*全屏填充*/
void OledFillScreen(uint8_t fillData)
{
uint8_t m = 0;
uint8_t n = 0;
for (m=0; m < 8; m++) {
WriteCmd(0xb0 + m);
WriteCmd(0x00);
WriteCmd(0x10);
for (n=0; n < 128; n++) {
WriteData(fillData);
}
}
}
/**
* @brief 8*16 typeface
* @param x: write positon start from x axis
* @param y: write positon start from y axis
* @param ch: write data
* @param font: selected font
*/
void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font)
{
uint8_t c = 0;
uint8_t i = 0;
c = ch - ' '; //得到偏移后的值
if (x > OLED_WIDTH - 1) {
x = 0;
y = y + 2;
}
if (font == FONT8x16) {
OledSetPosition(x, y);
for (i = 0; i < 8; i++){
WriteData(F8X16[c*16 + i]);
}
OledSetPosition(x, y+1);
for (i = 0; i < 8; i++) {
WriteData(F8X16[c*16 + i + 8]);
}
} else {
OledSetPosition(x, y);
for (i = 0; i < 6; i++) {
WriteData(F6x8[c][i]);
}
}
}
void OledShowString(uint8_t x, uint8_t y, const char* str, Font font)
{
uint8_t j = 0;
if (str == NULL) {
printf("param is NULL,Please check!!!\r\n");
return;
}
while (str[j]) {
OledShowChar(x, y, str[j], font);
x += 8;
if (x > 120) {
x = 0;
y += 2;
}
j++;
}
}
oled_ssd1306.h
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* 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.
*/
#ifndef OLED_SSD1306_H
#define OLED_SSD1306_H
#include <stdint.h>
/**
* @brief ssd1306 OLED Initialize.
*/
uint32_t OledInit(void);
/**
* @brief Set cursor position
*
* @param x the horizontal posistion of cursor
* @param y the vertical position of cursor
* @return Returns {@link WIFI_IOT_SUCCESS} if the operation is successful;
* returns an error code defined in {@link wifiiot_errno.h} otherwise.
*/
void OledSetPosition(uint8_t x, uint8_t y);
void OledFillScreen(uint8_t fillData);
enum Font {
FONT6x8 = 1,
FONT8x16
};
typedef enum Font Font;
void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font);
void OledShowString(uint8_t x, uint8_t y, const char* str, Font font);
#endif // OLED_SSD1306_H
oled_fonts.h
/*
* Copyright (C) 2021 HiHope Open Source Organization .
* 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.
*/
#ifndef OLOED_FONTS_H
#define OLOED_FONTS_H
/************************************6*8的點陣************************************/
static unsigned char F6x8[][6] =
{
{ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }, // sp
{ 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00 }, // !
{ 0x00, 0x00, 0x07, 0x00, 0x07, 0x00 }, // "
{ 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14 }, // #
{ 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12 }, // $
{ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23 }, // %
{ 0x00, 0x36, 0x49, 0x55, 0x22, 0x50 }, // &
{ 0x00, 0x00, 0x05, 0x03, 0x00, 0x00 }, // '
{ 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00 }, // (
{ 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00 }, // )
{ 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14 }, // *
{ 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08 }, // +
{ 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00 }, // ,
{ 0x00, 0x08, 0x08, 0x08, 0x08, 0x08 }, // -
{ 0x00, 0x00, 0x60, 0x60, 0x00, 0x00 }, // .
{ 0x00, 0x20, 0x10, 0x08, 0x04, 0x02 }, // /
{ 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E }, // 0
{ 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00 }, // 1
{ 0x00, 0x42, 0x61, 0x51, 0x49, 0x46 }, // 2
{ 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31 }, // 3
{ 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10 }, // 4
{ 0x00, 0x27, 0x45, 0x45, 0x45, 0x39 }, // 5
{ 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30 }, // 6
{ 0x00, 0x01, 0x71, 0x09, 0x05, 0x03 }, // 7
{ 0x00, 0x36, 0x49, 0x49, 0x49, 0x36 }, // 8
{ 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E }, // 9
{ 0x00, 0x00, 0x36, 0x36, 0x00, 0x00 }, // :
{ 0x00, 0x00, 0x56, 0x36, 0x00, 0x00 }, // ;
{ 0x00, 0x08, 0x14, 0x22, 0x41, 0x00 }, // <
{ 0x00, 0x14, 0x14, 0x14, 0x14, 0x14 }, // =
{ 0x00, 0x00, 0x41, 0x22, 0x14, 0x08 }, // >
{ 0x00, 0x02, 0x01, 0x51, 0x09, 0x06 }, // ?
{ 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E }, // @
{ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C }, // A
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36 }, // B
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22 }, // C
{ 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C }, // D
{ 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41 }, // E
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01 }, // F
{ 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A }, // G
{ 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F }, // H
{ 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00 }, // I
{ 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01 }, // J
{ 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41 }, // K
{ 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40 }, // L
{ 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F }, // M
{ 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F }, // N
{ 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E }, // O
{ 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06 }, // P
{ 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E }, // Q
{ 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46 }, // R
{ 0x00, 0x46, 0x49, 0x49, 0x49, 0x31 }, // S
{ 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01 }, // T
{ 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F }, // U
{ 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F }, // V
{ 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F }, // W
{ 0x00, 0x63, 0x14, 0x08, 0x14, 0x63 }, // X
{ 0x00, 0x07, 0x08, 0x70, 0x08, 0x07 }, // Y
{ 0x00, 0x61, 0x51, 0x49, 0x45, 0x43 }, // Z
{ 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00 }, // [
{ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55 }, // 55
{ 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00 }, // ]
{ 0x00, 0x04, 0x02, 0x01, 0x02, 0x04 }, // ^
{ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40 }, // _
{ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00 }, // '
{ 0x00, 0x20, 0x54, 0x54, 0x54, 0x78 }, // a
{ 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38 }, // b
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x20 }, // c
{ 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F }, // d
{ 0x00, 0x38, 0x54, 0x54, 0x54, 0x18 }, // e
{ 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02 }, // f
{ 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C }, // g
{ 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78 }, // h
{ 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00 }, // i
{ 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00 }, // j
{ 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00 }, // k
{ 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00 }, // l
{ 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78 }, // m
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78 }, // n
{ 0x00, 0x38, 0x44, 0x44, 0x44, 0x38 }, // o
{ 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18 }, // p
{ 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC }, // q
{ 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08 }, // r
{ 0x00, 0x48, 0x54, 0x54, 0x54, 0x20 }, // s
{ 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20 }, // t
{ 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C }, // u
{ 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C }, // v
{ 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C }, // w
{ 0x00, 0x44, 0x28, 0x10, 0x28, 0x44 }, // x
{ 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C }, // y
{ 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44 }, // z
{ 0x14, 0x14, 0x14, 0x14, 0x14, 0x14 }, // horiz lines
};
/****************************************8*16的點陣************************************/
static const unsigned char F8X16[]=
{
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x33,0x30,0x00,0x00,0x00,//! 1
0x00,0x10,0x0C,0x06,0x10,0x0C,0x06,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//" 2
0x40,0xC0,0x78,0x40,0xC0,0x78,0x40,0x00,0x04,0x3F,0x04,0x04,0x3F,0x04,0x04,0x00,//# 3
0x00,0x70,0x88,0xFC,0x08,0x30,0x00,0x00,0x00,0x18,0x20,0xFF,0x21,0x1E,0x00,0x00,//$ 4
0xF0,0x08,0xF0,0x00,0xE0,0x18,0x00,0x00,0x00,0x21,0x1C,0x03,0x1E,0x21,0x1E,0x00,//% 5
0x00,0xF0,0x08,0x88,0x70,0x00,0x00,0x00,0x1E,0x21,0x23,0x24,0x19,0x27,0x21,0x10,//& 6
0x10,0x16,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//' 7
0x00,0x00,0x00,0xE0,0x18,0x04,0x02,0x00,0x00,0x00,0x00,0x07,0x18,0x20,0x40,0x00,//( 8
0x00,0x02,0x04,0x18,0xE0,0x00,0x00,0x00,0x00,0x40,0x20,0x18,0x07,0x00,0x00,0x00,//) 9
0x40,0x40,0x80,0xF0,0x80,0x40,0x40,0x00,0x02,0x02,0x01,0x0F,0x01,0x02,0x02,0x00,//* 10
0x00,0x00,0x00,0xF0,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x1F,0x01,0x01,0x01,0x00,//+ 11
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0xB0,0x70,0x00,0x00,0x00,0x00,0x00,//, 12
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,//- 13
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,0x00,0x00,//. 14
0x00,0x00,0x00,0x00,0x80,0x60,0x18,0x04,0x00,0x60,0x18,0x06,0x01,0x00,0x00,0x00,/// 15
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00,//0 16
0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//1 17
0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00,//2 18
0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00,//3 19
0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00,//4 20
0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00,//5 21
0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00,//6 22
0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,//7 23
0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00,//8 24
0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00,//9 25
0x00,0x00,0x00,0xC0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x30,0x30,0x00,0x00,0x00,//: 26
0x00,0x00,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x60,0x00,0x00,0x00,0x00,//; 27
0x00,0x00,0x80,0x40,0x20,0x10,0x08,0x00,0x00,0x01,0x02,0x04,0x08,0x10,0x20,0x00,//< 28
0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00,0x04,0x04,0x04,0x04,0x04,0x04,0x04,0x00,//= 29
0x00,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x20,0x10,0x08,0x04,0x02,0x01,0x00,//> 30
0x00,0x70,0x48,0x08,0x08,0x08,0xF0,0x00,0x00,0x00,0x00,0x30,0x36,0x01,0x00,0x00,//? 31
0xC0,0x30,0xC8,0x28,0xE8,0x10,0xE0,0x00,0x07,0x18,0x27,0x24,0x23,0x14,0x0B,0x00,//@ 32
0x00,0x00,0xC0,0x38,0xE0,0x00,0x00,0x00,0x20,0x3C,0x23,0x02,0x02,0x27,0x38,0x20,//A 33
0x08,0xF8,0x88,0x88,0x88,0x70,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x11,0x0E,0x00,//B 34
0xC0,0x30,0x08,0x08,0x08,0x08,0x38,0x00,0x07,0x18,0x20,0x20,0x20,0x10,0x08,0x00,//C 35
0x08,0xF8,0x08,0x08,0x08,0x10,0xE0,0x00,0x20,0x3F,0x20,0x20,0x20,0x10,0x0F,0x00,//D 36
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x20,0x23,0x20,0x18,0x00,//E 37
0x08,0xF8,0x88,0x88,0xE8,0x08,0x10,0x00,0x20,0x3F,0x20,0x00,0x03,0x00,0x00,0x00,//F 38
0xC0,0x30,0x08,0x08,0x08,0x38,0x00,0x00,0x07,0x18,0x20,0x20,0x22,0x1E,0x02,0x00,//G 39
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x20,0x3F,0x21,0x01,0x01,0x21,0x3F,0x20,//H 40
0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//I 41
0x00,0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,0x00,//J 42
0x08,0xF8,0x88,0xC0,0x28,0x18,0x08,0x00,0x20,0x3F,0x20,0x01,0x26,0x38,0x20,0x00,//K 43
0x08,0xF8,0x08,0x00,0x00,0x00,0x00,0x00,0x20,0x3F,0x20,0x20,0x20,0x20,0x30,0x00,//L 44
0x08,0xF8,0xF8,0x00,0xF8,0xF8,0x08,0x00,0x20,0x3F,0x00,0x3F,0x00,0x3F,0x20,0x00,//M 45
0x08,0xF8,0x30,0xC0,0x00,0x08,0xF8,0x08,0x20,0x3F,0x20,0x00,0x07,0x18,0x3F,0x00,//N 46
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x10,0x20,0x20,0x20,0x10,0x0F,0x00,//O 47
0x08,0xF8,0x08,0x08,0x08,0x08,0xF0,0x00,0x20,0x3F,0x21,0x01,0x01,0x01,0x00,0x00,//P 48
0xE0,0x10,0x08,0x08,0x08,0x10,0xE0,0x00,0x0F,0x18,0x24,0x24,0x38,0x50,0x4F,0x00,//Q 49
0x08,0xF8,0x88,0x88,0x88,0x88,0x70,0x00,0x20,0x3F,0x20,0x00,0x03,0x0C,0x30,0x20,//R 50
0x00,0x70,0x88,0x08,0x08,0x08,0x38,0x00,0x00,0x38,0x20,0x21,0x21,0x22,0x1C,0x00,//S 51
0x18,0x08,0x08,0xF8,0x08,0x08,0x18,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//T 52
0x08,0xF8,0x08,0x00,0x00,0x08,0xF8,0x08,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//U 53
0x08,0x78,0x88,0x00,0x00,0xC8,0x38,0x08,0x00,0x00,0x07,0x38,0x0E,0x01,0x00,0x00,//V 54
0xF8,0x08,0x00,0xF8,0x00,0x08,0xF8,0x00,0x03,0x3C,0x07,0x00,0x07,0x3C,0x03,0x00,//W 55
0x08,0x18,0x68,0x80,0x80,0x68,0x18,0x08,0x20,0x30,0x2C,0x03,0x03,0x2C,0x30,0x20,//X 56
0x08,0x38,0xC8,0x00,0xC8,0x38,0x08,0x00,0x00,0x00,0x20,0x3F,0x20,0x00,0x00,0x00,//Y 57
0x10,0x08,0x08,0x08,0xC8,0x38,0x08,0x00,0x20,0x38,0x26,0x21,0x20,0x20,0x18,0x00,//Z 58
0x00,0x00,0x00,0xFE,0x02,0x02,0x02,0x00,0x00,0x00,0x00,0x7F,0x40,0x40,0x40,0x00,//[ 59
0x00,0x0C,0x30,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x06,0x38,0xC0,0x00,//\ 60
0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x7F,0x00,0x00,0x00,//] 61
0x00,0x00,0x04,0x02,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//^ 62
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x80,//_ 63
0x00,0x02,0x02,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//` 64
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,//a 65
0x08,0xF8,0x00,0x80,0x80,0x00,0x00,0x00,0x00,0x3F,0x11,0x20,0x20,0x11,0x0E,0x00,//b 66
0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,//c 67
0x00,0x00,0x00,0x80,0x80,0x88,0xF8,0x00,0x00,0x0E,0x11,0x20,0x20,0x10,0x3F,0x20,//d 68
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,//e 69
0x00,0x80,0x80,0xF0,0x88,0x88,0x88,0x18,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//f 70
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x6B,0x94,0x94,0x94,0x93,0x60,0x00,//g 71
0x08,0xF8,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//h 72
0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//i 73
0x00,0x00,0x00,0x80,0x98,0x98,0x00,0x00,0x00,0xC0,0x80,0x80,0x80,0x7F,0x00,0x00,//j 74
0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,//k 75
0x00,0x08,0x08,0xF8,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,//l 76
0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,//m 77
0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,//n 78
0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x20,0x20,0x20,0x20,0x1F,0x00,//o 79
0x80,0x80,0x00,0x80,0x80,0x00,0x00,0x00,0x80,0xFF,0xA1,0x20,0x20,0x11,0x0E,0x00,//p 80
0x00,0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x0E,0x11,0x20,0x20,0xA0,0xFF,0x80,//q 81
0x80,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x20,0x20,0x3F,0x21,0x20,0x00,0x01,0x00,//r 82
0x00,0x00,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x33,0x24,0x24,0x24,0x24,0x19,0x00,//s 83
0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,//t 84
0x80,0x80,0x00,0x00,0x00,0x80,0x80,0x00,0x00,0x1F,0x20,0x20,0x20,0x10,0x3F,0x20,//u 85
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x00,0x01,0x0E,0x30,0x08,0x06,0x01,0x00,//v 86
0x80,0x80,0x00,0x80,0x00,0x80,0x80,0x80,0x0F,0x30,0x0C,0x03,0x0C,0x30,0x0F,0x00,//w 87
0x00,0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x31,0x2E,0x0E,0x31,0x20,0x00,//x 88
0x80,0x80,0x80,0x00,0x00,0x80,0x80,0x80,0x80,0x81,0x8E,0x70,0x18,0x06,0x01,0x00,//y 89
0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x00,0x21,0x30,0x2C,0x22,0x21,0x30,0x00,//z 90
0x00,0x00,0x00,0x00,0x80,0x7C,0x02,0x02,0x00,0x00,0x00,0x00,0x00,0x3F,0x40,0x40,//{ 91
0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,//| 92
0x00,0x02,0x02,0x7C,0x80,0x00,0x00,0x00,0x00,0x40,0x40,0x3F,0x00,0x00,0x00,0x00,//} 93
0x00,0x06,0x01,0x01,0x02,0x02,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,//~ 94
};
#endif
具體介紹一下c文件中提供的一些API,其中標號1至3不需要我們手動調(diào)用,4至8比較常用。
- 寫入數(shù)據(jù)字節(jié)。
uint32_t I2cWiteByte(uint8_t regAddr, uint8_t byte)
參數(shù)解釋:
- regAddr:寄存器地址,要寫入的寄存器地址。
- byte:字節(jié)值,要寫入的數(shù)據(jù)字節(jié)。
返回值:
- 返回值類型為uint32_t,表示操作的結(jié)果。返回IOT_SUCCESS表示操作成功,否則返回定義在wifiiot_errno.h中的錯誤代碼。
該函數(shù)封裝了通過I2C總線向OLED設備寫入一個字節(jié)數(shù)據(jù)的操作。它使用了一個結(jié)構(gòu)體變量IotI2cData,并將要寫入的寄存器地址和字節(jié)值存儲在一個緩沖區(qū)buffer中。函數(shù)內(nèi)部調(diào)用了IoTI2cWrite函數(shù),通過I2C總線向OLED設備發(fā)送數(shù)據(jù)。
- 命令寫入。
uint32_t WriteCmd(uint8_t cmd)
參數(shù)解釋:
- cmd:命令字節(jié),要寫入的命令字節(jié)。
返回值:
- 返回值類型為uint32_t,表示操作的結(jié)果。返回IOT_SUCCESS表示操作成功,否則返回定義在wifiiot_errno.h中的錯誤代碼。
該函數(shù)用于向OLED設備寫入命令字節(jié)。它調(diào)用了I2cWiteByte函數(shù),將命令字節(jié)和OLED_I2C_CMD作為參數(shù)傳遞給I2cWiteByte函數(shù)進行發(fā)送。
- 數(shù)據(jù)寫入。
uint32_t WriteData(uint8_t data)
參數(shù)解釋:
- data:數(shù)據(jù)字節(jié),要寫入的數(shù)據(jù)字節(jié)。
返回值:
- 返回值類型為uint32_t,表示操作的結(jié)果。返回IOT_SUCCESS表示操作成功,否則返回定義在wifiiot_errno.h中的錯誤代碼。
該函數(shù)用于向OLED設備寫入數(shù)據(jù)字節(jié)。它調(diào)用了I2cWiteByte函數(shù),將數(shù)據(jù)字節(jié)和OLED_I2C_DATA作為參數(shù)傳遞給I2cWiteByte函數(shù)進行發(fā)送。
- 初始化。
uint32_t OledInit(void)
參數(shù)解釋:無
返回值:
- 返回值類型為uint32_t,表示操作的結(jié)果。返回IOT_SUCCESS表示操作成功,否則返回定義在wifiiot_errno.h中的錯誤代碼。
該函數(shù)用于初始化SSD1306 OLED顯示屏。首先,它初始化GPIO引腳,并將其配置為I2C功能。然后調(diào)用IoTI2cInit函數(shù)初始化I2C控制器。接下來,通過循環(huán)發(fā)送一系列的初始化命令字節(jié)到OLED設備,以完成OLED的初始化過程。
- 設置數(shù)據(jù)起始位置。
void OledSetPosition(uint8_t x, uint8_t y)
參數(shù)解釋:
- x:X軸位置,寫入數(shù)據(jù)的起始列位置。
- y:Y軸位置,寫入數(shù)據(jù)的起始行位置。
返回值:無
該函數(shù)用于設置寫入數(shù)據(jù)的起始位置。它調(diào)用WriteCmd函數(shù)向OLED設備發(fā)送命令字節(jié),設置OLED設備的行和列地址。
- 填充屏幕。
void OledFillScreen(uint8_t fillData)
參數(shù)解釋:
- fillData:填充數(shù)據(jù),要用于填充屏幕的數(shù)據(jù)字節(jié)。
返回值:無
該函數(shù)用于全屏填充屏幕。通過循環(huán)遍歷所有行和列,調(diào)用WriteData函數(shù)向OLED設備發(fā)送填充數(shù)據(jù)字節(jié)。
- 輸出字符。
void OledShowChar(uint8_t x, uint8_t y, uint8_t ch, Font font)
參數(shù)解釋:
- x:X軸位置,寫入字符的起始列位置。
- y:Y軸位置,寫入字符的起始行位置。
- ch:字符,要寫入的字符。
- font:字體,選擇使用的字體。
返回值:無
該函數(shù)用于顯示一個字符。根據(jù)所選字體,它調(diào)用WriteData函數(shù)向OLED設備發(fā)送對應字體的字形數(shù)據(jù)。
- 輸出字符串。
void OledShowString(uint8_t x, uint8_t y, const char* str, Font font)
參數(shù)解釋:
- x:X軸位置,寫入字符串的起始列位置。
- y:Y軸位置,寫入字符串的起始行位置。
- str:字符串,要寫入的字符串。
- font:字體,選擇使用的字體。
返回值:無
該函數(shù)用于顯示一個字符串。它遍歷字符串中的每個字符,調(diào)用OledShowChar函數(shù)逐個顯示字符,并根據(jù)需要更新X和Y軸位置。
- 編寫源文件oledTest.c。
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <unistd.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "oled_ssd1306.h"
static void oledMain(void){
OledInit();
OledFillScreen(0);
OledShowString(0, 0, "Hello,World", 1);
}
static void oledTest(void){
osThreadAttr_t attr = {"oledMain", 0, NULL, 0, NULL, 1024, osPriorityNormal, 0, 0};
osThreadNew((osThreadFunc_t)oledMain, NULL, &attr);
}
APP_FEATURE_INIT(oledTest);
- 編寫B(tài)UILD.gn (app目錄下的BUILD.gn文件記得也修改好,這里就不多演示了)。
static_library("oled") {
sources = [
"oled_ssd1306.c",
"oledTest.c",
]
include_dirs = [
"http://utils/native/lite/include",
"http://kernel/liteos_m/components/cmsis/2.0",
"http://base/iot_hardware/peripheral/interfaces/kits",
"http://device/hisilicon/hispark_pegasus/sdk_liteos/include",
"http://base/iothardware/peripheral/interfaces/inner_api",
]
}
- 編譯,燒錄,重啟開發(fā)板,觀察oled顯示屏。
結(jié)束語
至此驅(qū)動模塊就給大家介紹到這里,大家可以結(jié)合內(nèi)核編程驅(qū)動編程自行開發(fā)一些小案例。后面也會給大家?guī)硪恍┚C合案例的演示。