在介绍0.91寸 SSD1306之前先附上模块链接:点击购买SSD1306 OLED显示模块
前面的文章已经详细说明了OLED的原理,指令,代码
0.91寸 SSD1306 OLED介绍(一) — 整体介绍/IIC时序介绍
0.91寸 SSD1306 OLED介绍(二) — 命令介绍
0.91寸 SSD1306 OLED介绍(三) — 代码介绍
本文章我们通过上位机来验证下OLED驱动。
1. OLED驱动更上位机通信格式
目前上位机跟开发板是通过串口来通信,通信格式是json,如果你没听过json,那么自行百度,跟OLED相关的json格式分别如下:
包含:OLED显示,OLED清除,OLED对比度,OLED整体显示,OLED不整体显示,OLED反向显示,OLED正常显示,OLED水平滚动设置,OLED垂直滚动设置,OLED开始滚动,OLED结束滚动。
发送格式示例如下(以OLED清除为例):
{"FUNC":"HW","OPERATE":"OLED_CLEAR","PARAM1":null,"PARAM2":null,"PARAM3":null,"PARAM4":null,"PARAM5":null,"PARAM6":null}
1)上位机json发送代码:
上位机用的c# winform,用的json库using Newtonsoft.Json,此库需要Newtonsoft.Json.dll库
发送代码如下:
private void json_construction_send(string func,string operate,string param1,string param2,string param3,string param4,string param5,string param6)
{
json_commmand cmd = new json_commmand();
cmd.FUNC = func;
cmd.OPERATE = operate;
cmd.PARAM1 = param1;
cmd.PARAM2 = param2;
cmd.PARAM3 = param3;
cmd.PARAM4 = param4;
cmd.PARAM5 = param5;
cmd.PARAM6 = param6;
string json_cmd = JsonConvert.SerializeObject(cmd);
#if CONSOLE_DEBUG
Console.WriteLine(json_cmd);
#endif
if (serialPort1.IsOpen)
{
serialPort1.WriteLine(json_cmd);
}
}
其中OLED的operate字符串包括:
string operate_oled_show = "OLED_SHOW";
string operate_oled_clear = "OLED_CLEAR";
string operate_oled_normal_display = "OLED_NORMAL_DISPLAY";
string operate_oled_unnormal_display = "OLED_INVERSE_DISPLAY";
string operate_oled_entry_ram = "OLED_ENTRY_RAM";
string operate_oled_entry_noram = "OLED_ENTRY_NORAM";
string operate_oled_contrast = "OLED_CONTRAST";
string operate_oled_hscroll_set = "OLED_H_SCROLL_SET";
string operate_oled_vscroll_set = "OLED_V_SCROLL_SET";
string operate_oled_scroll_start = "OLED_SCROLL_START";
string operate_oled_scroll_stop = "OLED_SCROLL_STOP";
2)单片机json接收解析代码
单片机用的json库是cJson,在github上可以找到,单片机接收解析代码框架如下:
uint8_t uart_receive_parse(uint8_t *shell_string)
{
/* 解析Json字符串 */
uint8_t result = HW_ERR_OK;
cJSON* parse_json = cJSON_Parse((const char *)shell_string);
uint8_t* func_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"FUNC"))->valuestring;
uint8_t* operate_value = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"OPERATE"))->valuestring;
uint8_t* para1 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM1"))->valuestring;
uint8_t* para2 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM2"))->valuestring;
uint8_t* para3 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM3"))->valuestring;
uint8_t* para4 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM4"))->valuestring;
uint8_t* para5 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM5"))->valuestring;
uint8_t* para6 = (uint8_t*)((cJSON *)cJSON_GetObjectItem(parse_json,"PARAM6"))->valuestring;
if(hw_strcmp((const char *)func_value,"HW") == 0)
{
if(hw_strcmp((const char *)operate_value,"OLED_SHOW") == 0)
{
/* OLED显示 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_CLEAR") == 0)
{
/* OLED清除 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_CONTRAST") == 0)
{
/* 设置OLED对比度 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_ENTRY_RAM") == 0)
{
/* OLED整体显示 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_ENTRY_NORAM") == 0)
{
/* OLED不整体显示 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_NORMAL_DISPLAY") == 0)
{
/* OLED正常显示 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_INVERSE_DISPLAY") == 0)
{
/* OLED反向显示 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_H_SCROLL_SET") == 0)
{
/* OLED水平滚动设置 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_V_SCROLL_SET") == 0)
{
/* OLED垂直滚动设置 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_SCROLL_START") == 0)
{
/* OLED开始滚动 */
goto exit;
}
if(hw_strcmp((const char *)operate_value,"OLED_SCROLL_STOP") == 0)
{
/* OLED停止滚动 */
goto exit;
}
result = HW_ERR_SHELL_NO_CMD;
}
/* 解析非json字符串 */
if(hw_strcmp((const char *)shell_string,"shop220811498.taobao.com") == 0)
HW_DEBUG("welcome to use our stm32f1 camera wifi board\n");
else
HW_DEBUG("UART PARSE ERR:HW_ERR_SHELL_NO_CMD\n");
result = HW_ERR_SHELL_NO_CMD;
exit:
cJSON_Delete(parse_json);
return result;
}
2.上位机图片
整个上位机是硬件测试例程的上位机,包括LED,OLED,SPI FLASH,摄像头,但是本文章只是拿OLED做说明,整个上位机包括:
①串口配置区->包括串口打开,关闭,包括热插拔的检测
②功能测试区->包括LED,SPI,OLED,摄像头,贴图仅仅是OLED的测试
③串口调试区->包括可以接收开发板打印的log以及发送自定义的字符串给开发板
3.上位机跟驱动交互代码
1)OLED显示
上位机代码如下:
private void b_oled_show_Click(object sender, EventArgs e)
{
int oled_col = c_oled_col.SelectedIndex;
int oled_page = c_oled_page.SelectedIndex;
int oled_format = c_oled_format.SelectedIndex;
if (t_oled_string.Text == "")
{
MessageBox.Show("输入要显示的字符串", "错误提示");
return;
}
string oled_show = t_oled_string.Text;
json_construction_send(hw_func, operate_oled_show, oled_col.ToString(), oled_page.ToString(), oled_format.ToString(), oled_show.Length.ToString(), oled_show, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_SHOW") == 0)
{
uint8_t col,page,format,string_len;
uint8_t show_string[32] = {0};
HW_DEBUG("UART PARSE DEBUG:operate OLED_SHOW\n");
col = atoi((const char*)para1);
page = atoi((const char*)para2);
format = atoi((const char*)para3);
string_len = atoi((const char*)para4);
hw_memcpy(show_string,(const char*)para5,string_len);
hw_oled_show_string(col,page,show_string,format == 0?8:16);
goto exit;
}
2)OLED清除
上位机代码如下:
private void b_oled_clear_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_clear, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_CLEAR") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_CLEAR\n");
hw_oled_clear();
goto exit;
}
3)OLED对比度
上位机代码如下:
private void b_oled_contrast_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_contrast, c_oled_contrast.SelectedItem.ToString(), null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_CONTRAST") == 0)
{
uint8_t contrast_value;
HW_DEBUG("UART PARSE DEBUG:operate OLED_CONTRAST\n");
contrast_value = atoi((const char*)para1);
hw_oled_set_contrast(contrast_value);
goto exit;
}
4)OLED整体显示
上位机代码如下:
private void b_oled_entry_ram_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_entry_ram, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_ENTRY_RAM") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_ENTRY_RAM\n");
hw_oled_entry_display_with_ram();
goto exit;
}
5)OLED不整体显示
上位机代码如下:
private void b_oled_entry_noram_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_entry_noram, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_ENTRY_NORAM") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_ENTRY_NORAM\n");
hw_oled_entry_display_without_ram();
goto exit;
}
6)OLED反向显示
上位机代码如下:
private void b_oled_unnormal_display_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_unnormal_display, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_INVERSE_DISPLAY") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_INVERSE_DISPLAY\n");
hw_oled_set_inverse_display();
goto exit;
}
7)OLED正常显示
上位机代码如下:
private void b_oled_normal_display_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_normal_display, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_NORMAL_DISPLAY") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_NORMAL_DISPLAY\n");
hw_oled_set_normal_display();
goto exit;
}
8)OLED水平滚动设置
上位机代码如下:
private void bh_scroll_set_Click(object sender, EventArgs e)
{
string h_scroll_start_page = ch_start_page.SelectedIndex.ToString();
string h_scroll_stop_page = ch_stop_page.SelectedIndex.ToString();
string h_scroll_dir = ch_direct.SelectedIndex.ToString();
int h_scroll_inteval = ch_inteval.SelectedIndex;
int inteval = 0;
if (h_scroll_inteval == 0)
inteval = 7;
if (h_scroll_inteval == 1)
inteval = 4;
if (h_scroll_inteval == 1)
inteval = 4;
if (h_scroll_inteval == 2)
inteval = 5;
if (h_scroll_inteval == 3)
inteval = 0;
if (h_scroll_inteval == 4)
inteval = 6;
if (h_scroll_inteval == 5)
inteval = 1;
if (h_scroll_inteval == 6)
inteval = 2;
if (h_scroll_inteval == 7)
inteval = 3;
json_construction_send(hw_func, operate_oled_hscroll_set, h_scroll_start_page, h_scroll_stop_page, h_scroll_dir, inteval.ToString(), null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_H_SCROLL_SET") == 0)
{
uint8_t start_page,stop_page,dir,interval;
HW_DEBUG("UART PARSE DEBUG:operate OLED_H_SCROLL_SET\n");
start_page = atoi((const char*)para1);
stop_page = atoi((const char*)para2);
dir = atoi((const char*)para3);
interval = atoi((const char*)para4);
hw_oled_set_horizontal_scroll(dir,start_page,stop_page,interval);
goto exit;
}
9)OLED垂直滚动设置
上位机代码如下:
private void bv_scroll_set_Click(object sender, EventArgs e)
{
string v_scroll_start_page = cv_start_page.SelectedIndex.ToString();
string v_scroll_stop_page = cv_stop_page.SelectedIndex.ToString();
int v_scroll_dir = cv_direct.SelectedIndex;
int scroll_dir = 1;
if (v_scroll_dir == 0)
scroll_dir = 2;
if (v_scroll_dir == 1)
scroll_dir = 1;
int v_scroll_inteval = cv_inteval.SelectedIndex;
string v_scroll_offset = cv_offset.SelectedIndex.ToString();
int inteval = 0;
if (v_scroll_inteval == 0)
inteval = 7;
if (v_scroll_inteval == 1)
inteval = 4;
if (v_scroll_inteval == 1)
inteval = 4;
if (v_scroll_inteval == 2)
inteval = 5;
if (v_scroll_inteval == 3)
inteval = 0;
if (v_scroll_inteval == 4)
inteval = 6;
if (v_scroll_inteval == 5)
inteval = 1;
if (v_scroll_inteval == 6)
inteval = 2;
if (v_scroll_inteval == 7)
inteval = 3;
json_construction_send(hw_func, operate_oled_vscroll_set, v_scroll_start_page, v_scroll_stop_page, scroll_dir.ToString(), inteval.ToString(), v_scroll_offset, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_V_SCROLL_SET") == 0)
{
uint8_t start_page,stop_page,dir,interval,offset;
HW_DEBUG("UART PARSE DEBUG:operate OLED_V_SCROLL_SET\n");
start_page = atoi((const char*)para1);
stop_page = atoi((const char*)para2);
dir = atoi((const char*)para3);
interval = atoi((const char*)para4);
offset = atoi((const char*)para5);
hw_oled_set_vertical_scroll(dir,start_page,stop_page,interval,offset);
goto exit;
}
10)OLED开始滚动
上位机代码如下:
private void bh_scroll_start_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_scroll_start, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_SCROLL_START") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_SCROLL_START\n");
hw_oled_scroll_active();
goto exit;
}
11)OLED结束滚动
上位机代码如下:
private void bh_sroll_stop_Click(object sender, EventArgs e)
{
json_construction_send(hw_func, operate_oled_scroll_stop, null, null, null, null, null, null);
}
单片机代码如下:
if(hw_strcmp((const char *)operate_value,"OLED_SCROLL_STOP") == 0)
{
HW_DEBUG("UART PARSE DEBUG:operate OLED_SCROLL_START\n");
hw_oled_scroll_deactive();
goto exit;
}
OK,就介绍到这里,后续会整理OLED SSD1306资料,发一篇博客
主要包括:
1)OLED驱动源码
2)OLED上位机
3)OLED资料