优化UART案例,新增OLED模块案例

This commit is contained in:
艾彬 2021-09-29 10:04:17 +08:00
parent ec28efa6de
commit c4509d1e7c
12 changed files with 169 additions and 267 deletions

View File

@ -55,10 +55,10 @@
下面依次对TW1、TW2、TW3、TW4类进行简单介绍
- TW001 - TW099操作系统类
- TW100 - TW199外设驱动类
- TW200 - TW299模块应用类
- TW300 - TW399物联通信类
- TW400 - TW499综合应用类
- TW101 - TW199外设驱动类
- TW201 - TW299模块应用类
- TW301 - TW399物联通信类
- TW401 - TW499综合应用类
案例列表如下所示:
@ -77,6 +77,7 @@
| TW104 | ADC | adc_voltage | [ADC电压采集](applications/app/TW104_ADC_voltage/README.md) | 核心板 + OLED扩展板|
| TW105 | I2C | i2c_sht30 | [I2C温湿度传感器采集](applications/app/TW105_I2C_sht30/README.md) | 核心板 + 马达扩展板|
| TW106 | UART | uart | [UART串口自发自收](applications/app/TW106_UART/README.md) | 核心板 |
| TW201 | Module | oled | [OLED显示屏驱动](applications/app/TW201_Module_oled/README.md) | 核心板 + OLED扩展板|
## 五、源码目录简介

View File

@ -19,16 +19,15 @@ lite_component("app") {
# "TW002_OS_thread:os_thread_example",
# "TW003_OS_timer:os_timer_example",
# "TW004_OS_event:os_event_example",
# "TW005_OS_mutex:mutex_example",
# "TW005_OS_mutex:mutex_example",
# "TW006_OS_semp:semp_example",
# "TW007_OS_message:Msg_example",
# "TW007_OS_message:Msg_example",
# "TW101_GPIO_led:gpio_led_example",
# "TW102_EXTI_key:exti_key_example",
# "TW103_PWM_led:pwm_led_example",
# "TW104_ADC_voltage:adc_voltage",
# "TW105_I2C_sht30:i2c_sht30",
# "TW106_UART:uart_test",
# "TW201_APP_oled:oled_demo",
# "TW202_APP_nfc:app_nfc_example"
# "TW106_UART:uart_example",
# "TW201_Module_oled:module_oled_example",
]
}

View File

@ -5,10 +5,10 @@
下面依次对TW1、TW2、TW3、TW4类进行简单介绍
- TW001 - TW099操作系统类
- TW100 - TW199外设驱动类
- TW200 - TW299模块应用类
- TW300 - TW399物联通信类
- TW400 - TW499综合应用类
- TW101 - TW199外设驱动类
- TW201 - TW299模块应用类
- TW301 - TW399物联通信类
- TW401 - TW499综合应用类
案例列表如下所示:
@ -26,4 +26,5 @@
| TW103 | PWM | pwm_led | [PWM点亮LED呼吸灯](applications/app/TW103_PWM_led/README.md) | 核心板|
| TW104 | ADC | adc_voltage | [ADC电压采集](applications/app/TW104_ADC_voltage/README.md) | 核心板 + OLED扩展板|
| TW105 | I2C | i2c_sht30 | [I2C温湿度传感器采集](applications/app/TW105_I2C_sht30/README.md) | 核心板 + 马达扩展板|
| TW106 | UART | uart | [UART串口自发自收](applications/app/TW106_UART/README.md) | 核心板 |
| TW106 | UART | uart | [UART串口自发自收](applications/app/TW106_UART/README.md) | 核心板 |
| TW201 | Module | oled | [OLED显示屏驱动](applications/app/TW201_Module_oled/README.md) | 核心板 + OLED扩展板|

View File

@ -13,7 +13,7 @@
static_library("uart_test"){
sources =[
"uart_test.c"
"uart_example.c"
]
include_dirs =[

View File

@ -1,4 +1,4 @@
# OpenHarmony驱动子系统开发—ADC电压采集
# OpenHarmony驱动子系统开发—UART收发测试
## 概述
1.UART是通用异步收发传输器Universal Asynchronous Receiver/Transmitter的缩写是通用串行数据总线用于异步通信。该总线双向通信可以实现全双工传输。
@ -49,4 +49,9 @@
flowCtrl: 具体流控设置选项
## 例程原理简介
本例程通过将uart的TX连接RX实现uart的自发自收功能。
本例程通过将uart1的TX引脚连接RX引脚实现uart的自发自收功能。
串口格式默认为 波特率=1152008bit数据位1bit停止位无校验位。
## 注意事项
请先烧录固件完成然后再用杜邦线短接GPIO5(UATR1_RX)引脚和GPIO6(UART1_TX)引脚!
若先短接了UART1_TX和UART1_RX引脚然后再进行固件烧录可能会出现固件检测失败的现象

View File

@ -20,31 +20,30 @@
#include "hi_uart.h"
#include "iot_uart.h"
#include "iot_errno.h"
#include <securec.h>
#define ADC_TASK_STACK_SIZE 4096
#define ADC_TASK_PRIO 25
#define UART_TASK_STACK_SIZE 4096
#define UART_TASK_PRIO 25
#define USER_UART_NUM HI_UART_IDX_0 //定义使用的UART串口号
#define UART_RECV_BUF_SZIE 256 //定义UART接收BUF的长度
//定义uart通信格式
IotUartAttribute uart_attr = {
.baudRate = 9600, //波特率 = 9600
.baudRate = 115200, //波特率 = 115200
.dataBits = IOT_UART_DATA_BIT_8, //数据位 = 8
.stopBits = IOT_UART_STOP_BIT_1, //停止位 = 1
.parity = IOT_UART_PARITY_NONE, //无校验
};
const unsigned char *uart_send_pdata = "hello, this is talkWeb Niobe uart test!\r\n";
unsigned char uart_recv_data[UART_RECV_BUF_SZIE];
static void uartTask(void)
{
int ret;
unsigned char uart_recv_data[UART_RECV_BUF_SZIE];
int recvLen;
//初始化uart0
ret = IoTUartInit(USER_UART_NUM, &uart_attr);
//初始化uart
ret = IoTUartInit(HI_UART_IDX_1, &uart_attr);
if (ret != IOT_SUCCESS)
{
printf("Failed to init uart! Err code = %d\r\n", ret);
@ -55,15 +54,18 @@ static void uartTask(void)
for(;;)
{
//先清空UART接收缓冲区
(void)memset_s(uart_recv_data, 0, sizeof(UART_RECV_BUF_SZIE));
memset(uart_recv_data, 0, sizeof(uart_recv_data));
//UART发送数据
IoTUartWrite(USER_UART_NUM, uart_send_pdata, strlen(uart_send_pdata));
IoTUartWrite(HI_UART_IDX_1, uart_send_pdata, strlen(uart_send_pdata));
//UART接收数据
IoTUartRead(USER_UART_NUM, uart_recv_data, UART_RECV_BUF_SZIE);
printf("uart recv:%s", uart_recv_data);
recvLen = IoTUartRead(HI_UART_IDX_1, uart_recv_data, UART_RECV_BUF_SZIE);
if(recvLen > 0)
{
printf("uart recv len = %d\r\n",recvLen);
printf("uart recv data: %s\r\n", uart_recv_data);
}
usleep(1000000);
}
@ -77,8 +79,8 @@ static void uartEntry(void)
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = ADC_TASK_STACK_SIZE;
attr.priority = ADC_TASK_PRIO;
attr.stack_size = UART_TASK_STACK_SIZE;
attr.priority = UART_TASK_PRIO;
if (osThreadNew((osThreadFunc_t)uartTask, NULL, &attr) == NULL) {
printf("[uartExample] Falied to create uartTask!\n");
@ -86,4 +88,4 @@ static void uartEntry(void)
return;
}
APP_FEATURE_INIT(uartEntry);
SYS_RUN(uartEntry);

View File

@ -1,25 +0,0 @@
# Copyright (c) 2021 Talkweb 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.
static_library("oled_demo"){
sources =[
"oled_demo.c"
]
include_dirs =[
"//utils/native/liteos/include",
"//device/talkweb/niobe/sdk_liteos/include",
"//device/talkweb/niobe/hi3861_adapter/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
]
}

View File

@ -1,38 +0,0 @@
/*
* Copyright (c) 2021 Talkweb 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.
*/
#ifndef __OLED_H
#define __OLED_H
#define OLED_MODE 0
#define SIZE 8
#define XLevelL 0x00
#define XLevelH 0x10
#define Max_Column 128
#define Max_Row 64
#define Brightness 0xFF
#define X_WIDTH 128
#define Y_WIDTH 64
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
#define u8 unsigned char
#define u16 unsigned short
#define u32 unsigned int
#endif

View File

@ -0,0 +1,12 @@
static_library("module_oled_example"){
sources =[
"oled_demo.c"
]
include_dirs =[
"//utils/native/liteos/include",
"//device/talkweb/niobe/sdk_liteos/include",
"//device/talkweb/niobe/hi3861_adapter/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
]
}

View File

@ -1,4 +1,4 @@
# OpenHarmony驱动子系统开发-----IIC_OLED显示
# OpenHarmony驱动子系统开发—IIC_Oled显示
## 概述
I2C(Inter Integrated Circuit)总线是由Philips公司开发的一种简单、双向二线制同步串行总线。

View File

@ -17,21 +17,30 @@
#include "ohos_init.h"
#include "cmsis_os2.h"
#include <hi_types_base.h>
#include <hi_i2c.h>
#include <hi_early_debug.h>
#include <hi_stdlib.h>
#include "oled_demo.h"
#include "hi_i2c.h"
#include "hi_io.h"
#include "hi_errno.h"
#include "iot_gpio.h"
#include "iot_i2c.h"
#include "iot_adc.h"
#include "oledfont.h"
#include <hi_adc.h>
u8 OLED_GRAM[144][8];
#define OLED_TASK_STACK_SIZE 4096
#define OLED_TASK_PRIO 25
hi_u8 g_send_data[2] = { 0 };
#define OLED_CMD 0 //写命令
#define OLED_DATA 1 //写数据
hi_u32 my_i2c_write(hi_i2c_idx id, hi_u16 device_addr, hi_u32 send_len)
osThreadId_t g_oledTaskID = NULL;
unsigned char OLED_GRAM[144][8];
unsigned char g_send_data[2] = { 0 };
/**********************************************
// IIC 写入1个字节
**********************************************/
unsigned int my_i2c_write(hi_i2c_idx id, unsigned short device_addr, unsigned int send_len)
{
hi_u32 status;
unsigned int status;
hi_i2c_data es8311_i2c_data = { 0 };
es8311_i2c_data.send_buf = g_send_data;
@ -46,7 +55,7 @@ hi_u32 my_i2c_write(hi_i2c_idx id, hi_u16 device_addr, hi_u32 send_len)
}
/**********************************************
// IIC Write Command
// IIC 写命令
**********************************************/
void Write_IIC_Command(unsigned char IIC_Command)
{
@ -55,8 +64,9 @@ void Write_IIC_Command(unsigned char IIC_Command)
my_i2c_write(HI_I2C_IDX_0, 0x78, 2);
}
/**********************************************
// IIC Write Data
// IIC 写数据
**********************************************/
void Write_IIC_Data(unsigned char IIC_Data)
{
@ -66,9 +76,12 @@ void Write_IIC_Data(unsigned char IIC_Data)
my_i2c_write(HI_I2C_IDX_0, 0x78, 2);
}
void OLED_WR_Byte(unsigned dat,unsigned cmd)
/**********************************************
// IIC 写函数,包括数据和命令
**********************************************/
void OLED_WR_Byte(unsigned char dat,unsigned char cmd)
{
if(cmd) {
if(cmd == OLED_CMD) {
Write_IIC_Data(dat);
}
else {
@ -76,35 +89,19 @@ void OLED_WR_Byte(unsigned dat,unsigned cmd)
}
}
//反显函数
void OLED_ColorTurn(u8 i)
/**********************************************
// 配置写入数据的起始位置
**********************************************/
void OLED_WR_BP(unsigned char x,unsigned char y)
{
if(i==0) {
OLED_WR_Byte(0xA6,OLED_CMD);//正常显示
}
if(i==1) {
OLED_WR_Byte(0xA7,OLED_CMD);//反色显示
}
OLED_WR_Byte(0xb0+y,OLED_CMD);//设置行起始地址
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
//屏幕旋转180度
void OLED_DisplayTurn(u8 i)
{
if(i==0) {
OLED_WR_Byte(0xC8,OLED_CMD);//正常显示
OLED_WR_Byte(0xA1,OLED_CMD);
}
if(i==1) {
OLED_WR_Byte(0xC0,OLED_CMD);//反转显示
OLED_WR_Byte(0xA0,OLED_CMD);
}
}
//开启OLED显示
/**********************************************
// 开启OLED显示
**********************************************/
void OLED_DisPlay_On(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
@ -112,7 +109,9 @@ void OLED_DisPlay_On(void)
OLED_WR_Byte(0xAF,OLED_CMD);//点亮屏幕
}
//关闭OLED显示
/**********************************************
// 关闭OLED显示
**********************************************/
void OLED_DisPlay_Off(void)
{
OLED_WR_Byte(0x8D,OLED_CMD);//电荷泵使能
@ -120,10 +119,12 @@ void OLED_DisPlay_Off(void)
OLED_WR_Byte(0xAF,OLED_CMD);//关闭屏幕
}
//更新显存到OLED
/**********************************************
// 更新显存到OLED
**********************************************/
void OLED_Refresh(void)
{
u8 i,n;
unsigned char i,n;
for(i=0;i<8;i++)
{
OLED_WR_Byte(0xb0+i,OLED_CMD); //设置行起始地址
@ -133,38 +134,45 @@ void OLED_Refresh(void)
OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
}
}
//清屏函数
/**********************************************
// 清屏函数
**********************************************/
void OLED_Clear(void)
{
u8 i,n;
unsigned char i,n;
for(i=0;i<8;i++)
{
for(n=0;n<128;n++)
{
OLED_GRAM[n][i]=0;//清除所有数据
}
}
OLED_Refresh();//更新显示
{
OLED_GRAM[n][i]=0;//清除所有数据
}
}
OLED_Refresh();//更新显示
}
//画点
/**********************************************
// 画点函数
//x:0~127
//y:0~63
void OLED_DrawPoint(u8 x,u8 y)
**********************************************/
void OLED_DrawPoint(unsigned char x,unsigned char y)
{
u8 i,m,n;
unsigned char i,m,n;
i=y/8;
m=y%8;
n=1<<m;
OLED_GRAM[x][i]|=n;
}
/**********************************************
//清除一个点
//x:0~127
//y:0~63
void OLED_ClearPoint(u8 x,u8 y)
**********************************************/
void OLED_ClearPoint(unsigned char x,unsigned char y)
{
u8 i,m,n;
unsigned char i,m,n;
i=y/8;
m=y%8;
n=1<<m;
@ -173,15 +181,17 @@ void OLED_ClearPoint(u8 x,u8 y)
OLED_GRAM[x][i]=~OLED_GRAM[x][i];
}
/**********************************************
//在指定位置显示一个字符,包括部分字符
//x:0~127
//y:0~63
//size:选择字体 12/16/24
//取模方式 逐列式
void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1)
**********************************************/
void OLED_ShowChar(unsigned char x,unsigned char y,unsigned char chr,unsigned char size1)
{
u8 i,m,temp,size2,chr1;
u8 y0=y;
unsigned char i,m,temp,size2,chr1;
unsigned char y0=y;
size2=(size1/8+((size1%8)?1:0))*(size1/2); //得到字体一个字符对应点阵集所占的字节数
chr1=chr-' '; //计算偏移后的值
for(i=0;i<size2;i++)
@ -209,12 +219,13 @@ void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 size1)
}
}
/**********************************************
//显示字符串
//x,y:起点坐标
//size1:字体大小
//*chr:字符串起始地址
void OLED_ShowString(u8 x,u8 y,char *chr,u8 size1)
**********************************************/
void OLED_ShowString(unsigned char x,unsigned char y,char *chr,unsigned char size1)
{
while((*chr>=' ')&&(*chr<='~'))//判断是不是非法字符!
{
@ -229,51 +240,12 @@ void OLED_ShowString(u8 x,u8 y,char *chr,u8 size1)
}
}
//m^n
u32 OLED_Pow(u8 m,u8 n)
/**********************************************
// oled初始化设置函数用于设置oled的一些基本参数信息具体参考芯片手册
**********************************************/
static void oled_init(void)
{
u32 result=1;
while(n--)
{
result*=m;
}
return result;
}
////显示2个数字
////x,y :起点坐标
////len :数字的位数
////size:字体大小
void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 size1)
{
u8 t,temp;
for(t=0;t<len;t++)
{
temp=(num/OLED_Pow(10,len-t-1))%10;
if(temp==0)
{
OLED_ShowChar(x+(size1/2)*t,y,'0',size1);
}
else
{
OLED_ShowChar(x+(size1/2)*t,y,temp+'0',size1);
}
}
}
//配置写入数据的起始位置
void OLED_WR_BP(u8 x,u8 y)
{
OLED_WR_Byte(0xb0+y,OLED_CMD);//设置行起始地址
OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
OLED_WR_Byte((x&0x0f),OLED_CMD);
}
void led_init(void)
{
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel #
OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel #
OLED_WR_Byte(0x00,OLED_CMD);//---set low column address #
OLED_WR_Byte(0x10,OLED_CMD);//---set high column address #
OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) #
@ -302,78 +274,52 @@ void led_init(void)
OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7) #
OLED_WR_Byte(0xAF,OLED_CMD);// #
OLED_Clear();
}
static void ledTask(void* arg)
static void oledDemoTask(void)
{
(void)arg;
lightsensor();
int ret;
//初始化I2C0 配置速率为400K
IoTGpioInit(IOT_GPIO_IO_GPIO_13);
IoTGpioInit(IOT_GPIO_IO_GPIO_14);
IotIoSetPull(IOT_GPIO_IO_GPIO_13, IOT_IO_PULL_UP);
IotIoSetPull(IOT_GPIO_IO_GPIO_14, IOT_IO_PULL_UP);
IotIoSetFunc(IOT_GPIO_IO_GPIO_13,HI_IO_FUNC_GPIO_13_I2C0_SDA);
IotIoSetFunc(IOT_GPIO_IO_GPIO_14,HI_IO_FUNC_GPIO_14_I2C0_SCL);
hi_i2c_init(HI_I2C_IDX_0, 400*1000);
//oled初始化配置
oled_init();
//显示字符串
OLED_ShowString(10,24,"Hello Harmony",16);
OLED_Refresh();
//删除任务
osThreadTerminate(g_oledTaskID);
}
static void bodyTask(void* arg)
static void oledDemoEntry(void)
{
(void)arg;
bodysensor();
}
static void pwmTask(void* arg)
{
(void)arg;
pwmcontroll();
}
void initGpio5()
{
hi_u16 data = 0;
while(1){
uint32_t ret = hi_adc_read(HI_ADC_CHANNEL_2, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0);
if (ret != HI_ERR_SUCCESS) {
printf("ADC Read Fail\n");
return HI_NULL;
}
printf("gpio data :%d \r\n",data);
hi_sleep(500);
}
}
void my_oled_demo(void)
{
//初始化
hi_i2c_init(HI_I2C_IDX_0, 400*1000); /* baudrate: 100000 */
led_init();
OLED_ColorTurn(0);//0正常显示1 反色显示
OLED_DisplayTurn(0);//0正常显示 1 屏幕翻转显示
OLED_ShowString(8,16,"hello world",16);
OLED_Refresh();
initGpio5();
osThreadAttr_t attr;
attr.name="ColorfulLightTask";
attr.attr_bits=0U;
attr.cb_mem=NULL;
attr.cb_size=0U;
attr.stack_mem=NULL;
attr.stack_size=4096;
attr.priority=osPriorityNormal;
attr.name = "oledDemoTask";
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = OLED_TASK_STACK_SIZE;
attr.priority = OLED_TASK_PRIO;
//if (osThreadNew(ledTask, NULL, &attr) == NULL) {
// printf("create led task thread error\n");
//}
//if (osThreadNew(bodyTask, NULL, &attr) == NULL) {
// printf("create body task thread error\n");
//}
//if (osThreadNew(pwmTask, NULL, &attr) == NULL) {
// printf("create pwm task thread error\n");
//}
g_oledTaskID = osThreadNew((osThreadFunc_t)oledDemoTask, NULL, &attr);
if (g_oledTaskID == NULL) {
printf("[oledDemoExample] Falied to create oledDemoTask!\n");
}
return;
}
APP_FEATURE_INIT(my_oled_demo);
SYS_RUN(oledDemoEntry);

View File

@ -15,7 +15,6 @@
#ifndef __OLEDFONT_H
#define __OLEDFONT_H
/************************************6*8 ASCII************************************/
const unsigned char asc2_1206[95][12]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x3F,0x40,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
@ -114,7 +113,7 @@ const unsigned char asc2_1206[95][12]={
{0x40,0x00,0x80,0x00,0x40,0x00,0x20,0x00,0x20,0x00,0x40,0x00},/*"~",94*/
};
//16*16 ASCII
//16*16 ASCII閻庢稒顨堥渚€姊块崱娆忎化闂傚喛鎷<EFBFBD>
const unsigned char asc2_1608[95][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/