鸿蒙Hi3861学习三-第一个实例程序Hello_world

本文介绍了如何在OpenHarmony环境中创建并编译Hello_World应用。首先,详细说明了项目目录结构,然后逐步指导如何搭建代码架构,包括新建项目文件夹、编写业务代码和编译脚本。最后,提到了编译后的文件及其用途,并概述了烧录和查看日志的步骤。
摘要由CSDN通过智能技术生成

一、简介

        前两章介绍了环境搭建、烧录和编译。这一节,来介绍实现第一个经典代码“hello world”。

        先介绍小熊派的目录结构,该目录结构延续了OpenHarmony官方目录结构。

二、实操

        1.搭建代码架构

        1).新建项目文件夹hello_world

cd bearpi-hm_nano/applications/BearPi/BearPi-HM_Nano/sample/
mkdir hello_world

        创建hello_world目录,用来存放业务源码文件

        2).新增hello_world.c文件

cd hello_world
touch hello_world.c

        创建hello_world.c文件,该文件为业务源码文件

        3).新增BUILD.gn文件

touch BUILD.gn

        创建BUILD.gn文件,该文件为业务源码编译脚本

        4).最终代码架构展示

.
└── applications        
    └── BearPi
        └── BearPi-HM_Nano
            └── sample
                │── hello_world
                │  │── hello_world.c
                │  └── BUILD.gn
                └── BUILD.gn

        2.代码编写

        1).编写业务代码

        在hello_world.c文件中增加如下代码:

#include <stdio.h>
#include "ohos_init.h"

void Hello_World(void)
{
    printf("this is a test demo \r\n Hello World!\r\n");
}

APP_FEATURE_INIT(Hello_World);

        APP_FEATURE_INITOpenHarmony启动恢复模块接口,该接口用来启动业务。也可以使用SYS_RUN来实现,效果是一样的。(APP_FEATURE_INIT和SYS_RUN定义都在ohos_init.h)文件中。

        2).编写用于将业务构建成静态库的BUILD.hn文件

        在hello_world文件夹的BUILD.gn文件中添加如下代码:

static_library("hello_world"){
    sources = [
        "hello_world.c"
    ]
    include_dirs = [
        "//utild/native/lite/include"
    ]
}

        该文件由三部分内容构成,目标源文件头文件路径

        static_library:该文件中指定业务模块的编译结果,为静态库文件libhello_world.a。

         sources:该文件中指定静态库.a所依赖的.c文件及路径,若路径中包含“//”则表示绝对路径代码的根路径,即bearpi-hm_nano文件夹)。若不包含“//”则表示相对路径。

         incllude_dirs:该文件中指定source所依赖的.h文件路径

         3).编写模块BUILD.gn文件,指定需要参与构建的特性模块

        配置sample文件夹中的BUILD.gn文件。在features字段中增加索引"hello_world:hello_world",使目标模块参与编译

        注意屏蔽其他模块

#"Zx_Developer:zx_develop_sample"  ##不参与构建,为开发者贡献演示结构,其中Zx为 Z1,Z2...Z100
        #"Z1_hi3861_uart_ylc:uart_hi3861",
        #"Z2_hi3861_flash_ylc:flash_example",

        "hello_world:hello_world"

        hello_world(前):是相对路径,指向./applications/BearPi/BearPi-HM_Nano/sample/hello_world/BUILD.gn

        hello_world(后):指向 ./applications/BearPi/BearPi-HM_Nano/sample/hello_world/BUILD.gn 中的static_library("hello_world")

        2.编译

        在项目根目录下,运行编译代码:

python build.py BearPi-HM_Nano

         编译成功。

        编译结束后,会在Out/工程名 文件下生成bin文件。

        这里介绍一个各个文件

文件名说明备注
Hi3861_boot_signed.bin签名的bootloader文件        Flash boot
Hi3861_boot_signed_B.bin签名的bootloader备份文件Flash boot备份
Hi3861_wifiiot_app.asmKernel asm文件汇编程序源文件
Hi3861_wifiiot_app.mapKernel map文件程序的全局符号,函数的地址,占用的空间等,用于调试。例如程序崩溃就可以查看这个文件
Hi3861_wifiiot_app.outKernel 输出文件
Hi3861_wifiiot_app_allinone.bin产线烧录文件包含了独立烧写程序和loader程序

包含了2个bin:

Hi3861_boot_signed.bin

Hi3861_wifiiot_app_burn.bin

Hi3861_wifiiot_app_burn.binKernel烧写文件,建议直接使用Hi3861_wifiiot_app_allinone.bin默认包含boot、NV、可执行程序镜像
Hi3861_wifiiot_app_flash_boot_ota.binFlash Boot升级文件
Hi3861_wifiiot_app_ota.binKernel升级文件
Hi3861_wifiiot_app_vercfg.binKernel和Boot的版本号文件
Hi3861_loader_signed.bin烧写工具使用的加载文件只用在烧写,位于内存中。烧写至少需要Hi3861_boot_signed.bin和Hi3861_wifiiot_app_burn.bin这两个文件。Hi3861_wifiiot_app_allinone.bin包含了这两个文件,所以推荐使用allinone文件进行烧录。

        3.烧录

        

         4.复位开发板并查看LOG输出

         完成!!!

  • 2
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
可以使用HAL库中的SSD1306驱动来控制0.96寸的OLED屏幕显示变量。以下是相关函数代码和实例代码: 函数代码: void OLED_Init(void) { HAL_Delay(100); OLED_Write_Command(0xAE);//--display off OLED_Write_Command(0x00);//---set low column address OLED_Write_Command(0x10);//---set high column address OLED_Write_Command(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F) OLED_Write_Command(0x81);//--set contrast control register OLED_Write_Command(0xCF); // Set SEG Output Current Brightness OLED_Write_Command(0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 OLED_Write_Command(0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 OLED_Write_Command(0xA6);//--set normal display OLED_Write_Command(0xA8);//--set multiplex ratio(1 to 64) OLED_Write_Command(0x3f);//--1/64 duty OLED_Write_Command(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) OLED_Write_Command(0x00);//-not offset OLED_Write_Command(0xd5);//--set display clock divide ratio/oscillator frequency OLED_Write_Command(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec OLED_Write_Command(0xD9);//--set pre-charge period OLED_Write_Command(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock OLED_Write_Command(0xDA);//--set com pins hardware configuration OLED_Write_Command(0x12); OLED_Write_Command(0xDB);//--set vcomh OLED_Write_Command(0x40);//Set VCOM Deselect Level OLED_Write_Command(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02) OLED_Write_Command(0x02);// OLED_Write_Command(0x8D);//--set Charge Pump enable/disable OLED_Write_Command(0x14);//--set(0x10) disable OLED_Write_Command(0xA4);// Disable Entire Display On (0xa4/0xa5) OLED_Write_Command(0xA6);// Disable Inverse Display On (0xa6/a7) OLED_Write_Command(0xAF);//--turn on oled panel } void OLED_Write_Command(uint8_t cmd) { HAL_I2C_Mem_Write(&hi2c1, OLED_ADDR, 0x00, 1, &cmd, 1, 100); } void OLED_Write_Data(uint8_t data) { HAL_I2C_Mem_Write(&hi2c1, OLED_ADDR, 0x40, 1, &data, 1, 100); } void OLED_Clear_Screen(void) { uint8_t i,j; for(i=0;i<8;i++) { OLED_Write_Command(0xb0+i); OLED_Write_Command(0x00); OLED_Write_Command(0x10); for(j=0;j<128;j++) { OLED_Write_Data(0x00); } } } void OLED_Show_String(uint8_t x,uint8_t y,char *str) { uint8_t i=0,j=0; while(str[i]!='\0') { j=str[i]-32; if(x>120) { x=0;y++; } OLED_Show_Char(x,y,j); x+=6; i++; } } void OLED_Show_Char(uint8_t x,uint8_t y,uint8_t chr) { uint8_t i,j; uint8_t temp; chr=chr-' '; OLED_Write_Command(0xb0+y); OLED_Write_Command(((x&0xf0)>>4)|0x10); OLED_Write_Command(x&0x0f); for(i=0;i<6;i++) { temp=asc2_1206[chr][i]; for(j=0;j<8;j++) { if(temp&0x01) { OLED_Write_Data(0xff); } else { OLED_Write_Data(0x00); } temp>>=1; } } } 实例代码: #include "stm32f1xx_hal.h" #include "oled.h" I2C_HandleTypeDef hi2c1; int main(void) { HAL_Init(); SystemClock_Config(); MX_GPIO_Init(); MX_I2C1_Init(); OLED_Init(); OLED_Clear_Screen(); OLED_Show_String(0,0,"Hello World!"); while (1) { } } 注意:以上代码仅供参考,具体实现需要根据具体的硬件和软件环境进行调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值