本帖最后由 cszzlsw 于 2021-1-3 16:55 编辑
1,上一篇给大家展示了开发板的开箱贴:https://bbs.21ic.com/icview-3061162-1-1.html,那里结合rtthread系统点了个灯,同时给大家把一些坑填了一下
2,趁着元旦,牺牲陪小孩和家人的时间,今天给大家带来i2c驱动ssd1306的效果
3.首先是连线,开发板上有默认的I2C接口,找几根排线连起来就行了:
4.然后就是写程序,用rtthread封装的I2C接口,很快就能把程序原型搭出来,但是官方的代码有个坑,改了好多:
首先把这个代码从下面提上来
5,然后是板子i2c的一个tick没有加上,加上去:
6.然后是ald库的时钟使能初始化:
7.再就是rtthread官方在i2c发送的时候会默认把地址左移一位,所以在搞addr的时候大家要注意了:
8.接着就是写我们的业务了:
oled_thread.c
#include <rtthread.h>
#include "oled_thread.h"
#include "drv_i2c.h"
#include "ssd1306.h"
#include "ssd1306_fonts.h"
#include "dat.c"
struct rt_thread oled_thread;
#define OLED_THREAD_STACK_SIZE 10240
uint8_t oled_thread_stack[OLED_THREAD_STACK_SIZE];
#define OLED_INTERVAL_TIME_US 300
#define OLED_TASK_STACK_SIZE 2048
#define OLED_TASK_PRIO 25
void oled_thread_entry(void *arg)
{
rt_kprintf("oled_thread_entry\n");
(void)arg;
// rt_thread_mdelay(20 );
ssd1306_Init();
//ssd1306_DrawString("Hello HarmonyOS!", Font_7x10, White);
int text_area_width = 64;
int text_area_height = 64;
int text_per_line_num = text_area_height / font_width;
int mod = hope_num % text_per_line_num;
int text_line_num = hope_num / text_per_line_num;
if (mod)
{
text_line_num += 1;
}
rt_kprintf("picture_arr size=%d\n", sizeof(picture_arr));
int text_alredy_show = 0;
// while (1)
{
ssd1306_Fill(Black);
ssd1306_SetCursor(0, 0);
int x = 0;
int y = 0;
rt_kprintf("hope_num =%d\n", hope_num);
// int start_num = text_alredy_show * text_per_line_num;
int showed_num = 0;
for (int i = text_alredy_show; i < hope_num; i++)
{
ssd1306_DrawRegion(x, y, font_height, font_width, hope_arr[i], sizeof(hope_arr[0]), 0);
x += font_width;
if (x >= text_area_width)
{
y += font_height;
x = 0;
}
showed_num++;
if (y >= text_area_height)
{
break;
}
}
ssd1306_DrawRegion(64, 0, 64, 64, picture_21ic, sizeof(picture_21ic), 0);
ssd1306_UpdateScreen();
rt_thread_mdelay(OLED_INTERVAL_TIME_US);
text_alredy_show++;
if (text_alredy_show >= hope_num - text_alredy_show)
{
text_alredy_show = 0;
rt_thread_mdelay(OLED_INTERVAL_TIME_US * 10);
}
}
}
void oled_start(){
rt_err_t err;
err=rt_thread_init(&oled_thread,"olerd_th",oled_thread_entry,NULL,oled_thread_stack,OLED_THREAD_STACK_SIZE,10,100);
if(err == RT_EOK){
rt_kprintf("oled start ok!\n");
rt_thread_startup(&oled_thread);
}else{
rt_kprintf("oled start ret = %d!\n",err);
}
}
至于驱动文件大家去github或者网上去找都行,然后就跑起来就行了(说得轻松,搞了两天半)
效果是这样的:
9.最后把代码里注释调非的while(1)取消注释,画面就动起来了,这就是动画的原理(不停的刷新页面buff,然后让画面update出来)
关于中文取字模,下这个软件:http://www.pc0359.cn/downinfo/110811.html,怎么使用我就不说了,关于图片转码,看这里:http://tools.clz.me/image-to-bitmap-array
相信大家应该都会
好了,今天的分享就到这了,喜欢的同学点个赞呗
|