重要的内容写在前面:
- 该系列是以up主江协科技的STM32视频教程为基础写下去的,大部分内容都参考了老师的课件,对于一些个人认为比较重要但是老师仅口述的部分,笔者都有用文字的方式记录并标出了重点。
- 文中的图片基本都来源于老师的课件以及开发板和芯片的手册,粘贴过来是为了方便阅读。
- 如果有条件的可以先学习一些相关课程再去看STM32的教程,学起来会更加轻松(不太建议零基础开始直接STM32,听起来可能会有点困难,可以先学51单片机),相关课程有数字电路(强烈推荐先学数电,不然可能会有很多地方理解起来很困难)、模拟电路、计算机组成原理(像寄存器、存储器、中断等在这门课里有很详细的介绍)、计算机网络等。
- 如有错漏欢迎指出。
视频链接:[4-1] OLED调试工具_哔哩哔哩_bilibili
一、OLED屏简介
1、常见的程序调试方式
(1)串口调试:通过串口通信,将调试信息发送到电脑端,电脑使用串口助手显示调试信息。
(2)显示屏调试:直接将显示屏连接到单片机,将调试信息打印在显示屏上。
(3)Keil调试模式:借助Keil软件的调试模式,可使用单步运行、设置断点、查看寄存器及变量等功能。
2、OLED介绍
(1)OLED(Organic Light Emitting Diode)是一种有机发光二极管,而OLED显示屏是性能优异的新型显示屏,具有功耗低、相应速度快、宽视角、轻薄柔韧等特点。
(2)OLED屏有0.96寸OLED显示屏和1.3寸OLED显示屏两种。
①0.96寸OLED显示屏的驱动芯片为SSD1306 / SSD1315。(下面均采用0.96寸OLED显示屏)
②1.3寸OLED显示屏的驱动芯片为SH1106。
(3)OLED模块的一些信息:供电——3~5.5V;通信协议——I2C/SPI;分辨率——128*64。
3、OLED屏的引脚分布
(1)OLED屏分为4针脚和7针脚两种,其中4针脚使用I2C协议,7针脚使用SPI协议。
(2)两种OLED屏的引脚分布如下图所示。
二、使用OLED屏进行简单显示
1、常见的简单OLED屏操作
(1)OLED屏上存在一个坐标系,最左上角的像素点坐标为(0,0),横向向右为X轴,取值范围为0~127,纵向向下为Y轴,取值范围为0~63。但在紧跟着的示例程序中只能显示基本的一些字符,故以字符的大小为单位建立坐标系,最左上角的位置坐标为(1,1),横向向右为X轴,取值范围为1~16,纵向向下为Y轴,取值范围为1~4。
(2)常见的封装函数及其功能:
自定义的函数名 | 作用 |
OLED_Init(); | 初始化OLED屏 |
OLED_Clear(); | 清屏 |
OLED_ShowChar(1, 1, 'A'); | 在坐标(1,1)处显示一个字符’A’ |
OLED_ShowString(1, 3, "HelloWorld!"); | 在坐标(1,3)处开始显示字符串"HelloWorld!" |
OLED_ShowNum(2, 1, 12345, 5); | 在坐标(2,1)处开始显示十进制数12345,占5位 |
OLED_ShowSignedNum(2, 7, -66, 2); | 在坐标(2,7)处开始显示有符号十进制数-66,数字部分占2位 |
OLED_ShowHexNum(3, 1, 0xAA55, 4); | 在坐标(3,1)处开始显示十六进制数0xAA55,数字部分占4位 |
OLED_ShowBinNum(4, 1, 0xAA55, 16); | 在坐标(4,1)处开始以二进制形式显示0xAA55,数字部分占16位 |
2、示例程序
(1)按照下图所示接好线路,并将按键控制LED的工程文件夹作为模板复制一份使用。(使用四针脚的OLED屏进行连接,后面的章节大多都以本例为工程模板,不过需要注意的是,该工程的OLED屏只能显示一些基本的字符,诸如汉字等复杂的字符并不能显示)
(2)在项目的Hardware组中添加三个文件。
①OLED.h文件:
#ifndef __OLED_H
#define __OLED_H
void OLED_Init(void);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char);
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String);
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length);
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length);
#endif
②OLED.c文件:
#include "stm32f10x.h"
#include "OLED_Font.h"
/*引脚配置*/
#define OLED_W_SCL(x) GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(x))
#define OLED_W_SDA(x) GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)(x))
/*引脚初始化*/
void OLED_I2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_Init(GPIOB, &GPIO_InitStructure);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C开始
* @param 无
* @retval 无
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1);
OLED_W_SCL(1);
OLED_W_SDA(0);
OLED_W_SCL(0);
}
/**
* @brief I2C停止
* @param 无
* @retval 无
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0);
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/**
* @brief I2C发送一个字节
* @param Byte 要发送的一个字节
* @retval 无
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i++)
{
OLED_W_SDA(Byte & (0x80 >> i));
OLED_W_SCL(1);
OLED_W_SCL(0);
}
OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
OLED_W_SCL(0);
}
/**
* @brief OLED写命令
* @param Command 要写入的命令
* @retval 无
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x00); //写命令
OLED_I2C_SendByte(Command);
OLED_I2C_Stop();
}
/**
* @brief OLED写数据
* @param Data 要写入的数据
* @retval 无
*/
void OLED_WriteData(uint8_t Data)
{
OLED_I2C_Start();
OLED_I2C_SendByte(0x78); //从机地址
OLED_I2C_SendByte(0x40); //写数据
OLED_I2C_SendByte(Data);
OLED_I2C_Stop();
}
/**
* @brief OLED设置光标位置
* @param Y 以左上角为原点,向下方向的坐标,范围:0~7
* @param X 以左上角为原点,向右方向的坐标,范围:0~127
* @retval 无
*/
void OLED_SetCursor(uint8_t Y, uint8_t X)
{
OLED_WriteCommand(0xB0 | Y); //设置Y位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
}
/**
* @brief OLED清屏
* @param 无
* @retval 无
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j++)
{
OLED_SetCursor(j, 0);
for(i = 0; i < 128; i++)
{
OLED_WriteData(0x00);
}
}
}
/**
* @brief OLED显示一个字符
* @param Line 行位置,范围:1~4
* @param Column 列位置,范围:1~16
* @param Char 要显示的一个字符,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowChar(uint8_t Line, uint8_t Column, char Char)
{
uint8_t i;
OLED_SetCursor((Line - 1) * 2, (Column - 1) * 8); //设置光标位置在上半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //显示上半部分内容
}
OLED_SetCursor((Line - 1) * 2 + 1, (Column - 1) * 8); //设置光标位置在下半部分
for (i = 0; i < 8; i++)
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //显示下半部分内容
}
}
/**
* @brief OLED显示字符串
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param String 要显示的字符串,范围:ASCII可见字符
* @retval 无
*/
void OLED_ShowString(uint8_t Line, uint8_t Column, char *String)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++)
{
OLED_ShowChar(Line, Column + i, String[i]);
}
}
/**
* @brief OLED次方函数
* @retval 返回值等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1;
while (Y--)
{
Result *= X;
}
return Result;
}
/**
* @brief OLED显示数字(十进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~4294967295
* @param Length 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED显示数字(十进制,带符号数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:-2147483648~2147483647
* @param Length 要显示数字的长度,范围:1~10
* @retval 无
*/
void OLED_ShowSignedNum(uint8_t Line, uint8_t Column, int32_t Number, uint8_t Length)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0)
{
OLED_ShowChar(Line, Column, '+');
Number1 = Number;
}
else
{
OLED_ShowChar(Line, Column, '-');
Number1 = -Number;
}
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i + 1, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0');
}
}
/**
* @brief OLED显示数字(十六进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~0xFFFFFFFF
* @param Length 要显示数字的长度,范围:1~8
* @retval 无
*/
void OLED_ShowHexNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++)
{
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10)
{
OLED_ShowChar(Line, Column + i, SingleNumber + '0');
}
else
{
OLED_ShowChar(Line, Column + i, SingleNumber - 10 + 'A');
}
}
}
/**
* @brief OLED显示数字(二进制,正数)
* @param Line 起始行位置,范围:1~4
* @param Column 起始列位置,范围:1~16
* @param Number 要显示的数字,范围:0~1111 1111 1111 1111
* @param Length 要显示数字的长度,范围:1~16
* @retval 无
*/
void OLED_ShowBinNum(uint8_t Line, uint8_t Column, uint32_t Number, uint8_t Length)
{
uint8_t i;
for (i = 0; i < Length; i++)
{
OLED_ShowChar(Line, Column + i, Number / OLED_Pow(2, Length - i - 1) % 2 + '0');
}
}
/**
* @brief OLED初始化
* @param 无
* @retval 无
*/
void OLED_Init(void)
{
uint32_t i, j;
for (i = 0; i < 1000; i++) //上电延时
{
for (j = 0; j < 1000; j++);
}
OLED_I2C_Init(); //端口初始化
OLED_WriteCommand(0xAE); //关闭显示
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40); //设置显示开始行
OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常 0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常 0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度控制
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/倒转显示
OLED_WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示
OLED_Clear(); //OLED清屏
}
③OLED_Font.h文件:
#ifndef __OLED_FONT_H
#define __OLED_FONT_H
/*OLED字模库,宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16]=
{
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
(3)在main.c中粘贴以下代码,编译后将程序下载到开发板中,观察OLED屏幕显示。
#include "stm32f10x.h" // Device headerCmd
#include "OLED.h"
int main()
{
OLED_Init();
OLED_ShowChar(1,1,'A'); //在1行1列显示字符A
OLED_ShowString(1,3,"HelloWorld!"); //在1行3列显示HelloWorld!
OLED_ShowNum(2,1,12345,5); //在2行1列显示5位的无符号数12345
OLED_ShowSignedNum(2,7,-66,2); //在2行7列显示2位的有符号数-66
OLED_ShowBinNum(4,1,0xAA55,16); //在4行1列显示16位的二进制数1010101001010101
//OLED_Clear(); 清屏
while(1)
{
}
}
三、使用OLED屏模块显示稍复杂的内容
1、OLED屏的坐标系
OLED屏上存在一个坐标系,最左上角的像素点坐标为(0,0),横向向右为X轴,取值范围为0~127,纵向向下为Y轴,取值范围为0~63。
2、采用UTF-8编码格式
(1)如果编程采用UTF-8编码格式,首先按下图所示对编码格式进行设置,接着在杂项控制栏→“C/C++”中将控制参数改为“--no-multibyte-chars”(这样,在UTF-8格式下程序中可以出现汉字并且不报错)。
(2)4针脚OLED屏(使用I2C协议)的示例程序:(需要注意的是,如果分文件是从其它工程复制过来的,需要先将它们的编码格式转换成UTF-8)
①按下图所示将电路连接好,并将工程模板复制一份(需要有Delay.c和Delay.h文件)。
②OLED.h文件:
#ifndef __OLED_H
#define __OLED_H
#include <stdint.h>
#include "OLED_Data.h"
/*参数宏定义*********************/
/*FontSize参数取值*/
/*此参数值不仅用于判断,而且用于计算横向字符偏移,默认值为字体像素宽度*/
#define OLED_8X16 8
#define OLED_6X8 6
/*IsFilled参数数值*/
#define OLED_UNFILLED 0
#define OLED_FILLED 1
/*********************参数宏定义*/
/*函数声明*********************/
/*初始化函数*/
void OLED_Init(void);
/*更新函数*/
void OLED_Update(void);
void OLED_UpdateArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
/*显存控制函数*/
void OLED_Clear(void);
void OLED_ClearArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
void OLED_Reverse(void);
void OLED_ReverseArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
/*显示函数*/
void OLED_ShowChar(int16_t X, int16_t Y, char Char, uint8_t FontSize);
void OLED_ShowString(int16_t X, int16_t Y, char *String, uint8_t FontSize);
void OLED_ShowNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowSignedNum(int16_t X, int16_t Y, int32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowHexNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowBinNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowFloatNum(int16_t X, int16_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize);
void OLED_ShowChinese(int16_t X, int16_t Y, char *Chinese);
void OLED_ShowImage(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_Printf(int16_t X, int16_t Y, uint8_t FontSize, char *format, ...);
/*绘图函数*/
void OLED_DrawPoint(int16_t X, int16_t Y);
uint8_t OLED_GetPoint(int16_t X, int16_t Y);
void OLED_DrawLine(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1);
void OLED_DrawRectangle(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled);
void OLED_DrawTriangle(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1, int16_t X2, int16_t Y2, uint8_t IsFilled);
void OLED_DrawCircle(int16_t X, int16_t Y, uint8_t Radius, uint8_t IsFilled);
void OLED_DrawEllipse(int16_t X, int16_t Y, uint8_t A, uint8_t B, uint8_t IsFilled);
void OLED_DrawArc(int16_t X, int16_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled);
/*********************函数声明*/
#endif
③OLED.c文件:(如果要更改OLED屏连接在STM32上的引脚,需要更改程序中与引脚相关的代码,比如GPIO及其时钟的初始化,还有读/写GPIO的操作;如果要更改OLED屏的显示,需要更改程序中OLED屏初始化函数中的一些配置命令)
/***************************************************************************************
* 本程序由江协科技创建并免费开源共享
* 你可以任意查看、使用和修改,并应用到自己的项目之中
* 程序版权归江协科技所有,任何人或组织不得将其据为己有
***************************************************************************************
*/
#include "stm32f10x.h"
#include "OLED.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
/*全局变量*********************/
/**
* OLED显存数组
* 所有的显示函数,都只是对此显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
*/
uint8_t OLED_DisplayBuf[8][128];
/*********************全局变量*/
/*引脚配置*********************/
/**
* 函 数:OLED写SCL高低电平
* 参 数:要写入SCL的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写SCL时,此函数会被调用
* 用户需要根据参数传入的值,将SCL置为高电平或者低电平
* 当参数传入0时,置SCL为低电平,当参数传入1时,置SCL为高电平
*/
void OLED_W_SCL(uint8_t BitValue)
{
/*根据BitValue的值,将SCL置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)BitValue); //OLED屏的SCL引脚连接在PB8上
/*如果单片机速度过快,可在此添加适量延时,以避免超出I2C通信的最大速度*/
//...
}
/**
* 函 数:OLED写SDA高低电平
* 参 数:要写入SDA的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写SDA时,此函数会被调用
* 用户需要根据参数传入的值,将SDA置为高电平或者低电平
* 当参数传入0时,置SDA为低电平,当参数传入1时,置SDA为高电平
*/
void OLED_W_SDA(uint8_t BitValue)
{
/*根据BitValue的值,将SDA置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)BitValue); //OLED屏的SDA引脚连接在PB9上
/*如果单片机速度过快,可在此添加适量延时,以避免超出I2C通信的最大速度*/
//...
}
/**
* 函 数:OLED引脚初始化
* 参 数:无
* 返 回 值:无
* 说 明:当上层函数需要初始化时,此函数会被调用
* 用户需要将SCL和SDA引脚初始化为开漏模式,并释放引脚
*/
void OLED_GPIO_Init(void)
{
uint32_t i, j;
/*在初始化前,加入适量延时,待OLED供电稳定*/
for (i = 0; i < 1000; i ++)
{
for (j = 0; j < 1000; j ++);
}
/*将SCL和SDA引脚初始化为开漏模式*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8; //OLED屏的SCL引脚连接在PB8上
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //OLED屏的SDA引脚连接在PB9上
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*释放SCL和SDA*/
OLED_W_SCL(1);
OLED_W_SDA(1);
}
/*********************引脚配置*/
/*通信协议*********************/
/**
* 函 数:I2C起始
* 参 数:无
* 返 回 值:无
*/
void OLED_I2C_Start(void)
{
OLED_W_SDA(1); //释放SDA,确保SDA为高电平
OLED_W_SCL(1); //释放SCL,确保SCL为高电平
OLED_W_SDA(0); //在SCL高电平期间,拉低SDA,产生起始信号
OLED_W_SCL(0); //起始后把SCL也拉低,即为了占用总线,也为了方便总线时序的拼接
}
/**
* 函 数:I2C终止
* 参 数:无
* 返 回 值:无
*/
void OLED_I2C_Stop(void)
{
OLED_W_SDA(0); //拉低SDA,确保SDA为低电平
OLED_W_SCL(1); //释放SCL,使SCL呈现高电平
OLED_W_SDA(1); //在SCL高电平期间,释放SDA,产生终止信号
}
/**
* 函 数:I2C发送一个字节
* 参 数:Byte 要发送的一个字节数据,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_I2C_SendByte(uint8_t Byte)
{
uint8_t i;
/*循环8次,主机依次发送数据的每一位*/
for (i = 0; i < 8; i++)
{
/*使用掩码的方式取出Byte的指定一位数据并写入到SDA线*/
/*两个!的作用是,让所有非零的值变为1*/
OLED_W_SDA(!!(Byte & (0x80 >> i)));
OLED_W_SCL(1); //释放SCL,从机在SCL高电平期间读取SDA
OLED_W_SCL(0); //拉低SCL,主机开始发送下一位数据
}
OLED_W_SCL(1); //额外的一个时钟,不处理应答信号
OLED_W_SCL(0);
}
/**
* 函 数:OLED写命令
* 参 数:Command 要写入的命令值,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_I2C_Start(); //I2C起始
OLED_I2C_SendByte(0x78); //发送OLED的I2C从机地址
OLED_I2C_SendByte(0x00); //控制字节,给0x00,表示即将写命令
OLED_I2C_SendByte(Command); //写入指定的命令
OLED_I2C_Stop(); //I2C终止
}
/**
* 函 数:OLED写数据
* 参 数:Data 要写入数据的起始地址
* 参 数:Count 要写入数据的数量
* 返 回 值:无
*/
void OLED_WriteData(uint8_t *Data, uint8_t Count)
{
uint8_t i;
OLED_I2C_Start(); //I2C起始
OLED_I2C_SendByte(0x78); //发送OLED的I2C从机地址
OLED_I2C_SendByte(0x40); //控制字节,给0x40,表示即将写数据
/*循环Count次,进行连续的数据写入*/
for (i = 0; i < Count; i ++)
{
OLED_I2C_SendByte(Data[i]); //依次发送Data的每一个数据
}
OLED_I2C_Stop(); //I2C终止
}
/*********************通信协议*/
/*硬件配置*********************/
/**
* 函 数:OLED初始化
* 参 数:无
* 返 回 值:无
* 说 明:使用前,需要调用此初始化函数
*/
void OLED_Init(void)
{
OLED_GPIO_Init(); //先调用底层的端口初始化
/*写入一系列的命令,对OLED进行初始化配置*/
OLED_WriteCommand(0xAE); //设置显示开启/关闭,0xAE关闭,0xAF开启
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80); //0x00~0xFF
OLED_WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F); //0x0E~0x3F
OLED_WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00); //0x00~0x7F
OLED_WriteCommand(0x40); //设置显示开始行,0x40~0x7F
OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常,0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常,0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度
OLED_WriteCommand(0xCF); //0x00~0xFF
OLED_WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/反色显示,0xA6正常,0xA7反色
OLED_WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示
OLED_Clear(); //清空显存数组
OLED_Update(); //更新显示,清屏,防止初始化后未显示内容时花屏
}
/**
* 函 数:OLED设置显示光标位置
* 参 数:Page 指定光标所在的页,范围:0~7
* 参 数:X 指定光标所在的X轴坐标,范围:0~127
* 返 回 值:无
* 说 明:OLED默认的Y轴,只能8个Bit为一组写入,即1页等于8个Y轴坐标
*/
void OLED_SetCursor(uint8_t Page, uint8_t X)
{
/*如果使用此程序驱动1.3寸的OLED显示屏,则需要解除此注释*/
/*因为1.3寸的OLED驱动芯片(SH1106)有132列*/
/*屏幕的起始列接在了第2列,而不是第0列*/
/*所以需要将X加2,才能正常显示*/
// X += 2;
/*通过指令设置页地址和列地址*/
OLED_WriteCommand(0xB0 | Page); //设置页位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
}
/*********************硬件配置*/
/*工具函数*********************/
/*工具函数仅供内部部分函数使用*/
/**
* 函 数:次方函数
* 参 数:X 底数
* 参 数:Y 指数
* 返 回 值:等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1; //结果默认为1
while (Y --) //累乘Y次
{
Result *= X; //每次把X累乘到结果上
}
return Result;
}
/**
* 函 数:判断指定点是否在指定多边形内部
* 参 数:nvert 多边形的顶点数
* 参 数:vertx verty 包含多边形顶点的x和y坐标的数组
* 参 数:testx testy 测试点的X和y坐标
* 返 回 值:指定点是否在指定多边形内部,1:在内部,0:不在内部
*/
uint8_t OLED_pnpoly(uint8_t nvert, int16_t *vertx, int16_t *verty, int16_t testx, int16_t testy)
{
int16_t i, j, c = 0;
/*此算法由W. Randolph Franklin提出*/
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((verty[i] > testy) != (verty[j] > testy)) &&
(testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
{
c = !c;
}
}
return c;
}
/**
* 函 数:判断指定点是否在指定角度内部
* 参 数:X Y 指定点的坐标
* 参 数:StartAngle EndAngle 起始角度和终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 返 回 值:指定点是否在指定角度内部,1:在内部,0:不在内部
*/
uint8_t OLED_IsInAngle(int16_t X, int16_t Y, int16_t StartAngle, int16_t EndAngle)
{
int16_t PointAngle;
PointAngle = atan2(Y, X) / 3.14 * 180; //计算指定点的弧度,并转换为角度表示
if (StartAngle < EndAngle) //起始角度小于终止角度的情况
{
/*如果指定角度在起始终止角度之间,则判定指定点在指定角度*/
if (PointAngle >= StartAngle && PointAngle <= EndAngle)
{
return 1;
}
}
else //起始角度大于于终止角度的情况
{
/*如果指定角度大于起始角度或者小于终止角度,则判定指定点在指定角度*/
if (PointAngle >= StartAngle || PointAngle <= EndAngle)
{
return 1;
}
}
return 0; //不满足以上条件,则判断判定指定点不在指定角度
}
/*********************工具函数*/
/*功能函数*********************/
/**
* 函 数:将OLED显存数组更新到OLED屏幕
* 参 数:无
* 返 回 值:无
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Update(void)
{
uint8_t j;
/*遍历每一页*/
for (j = 0; j < 8; j ++)
{
/*设置光标位置为每一页的第一列*/
OLED_SetCursor(j, 0);
/*连续写入128个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(OLED_DisplayBuf[j], 128);
}
}
/**
* 函 数:将OLED显存数组部分更新到OLED屏幕
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:此函数会至少更新参数指定的区域
* 如果更新区域Y轴只包含部分页,则同一页的剩余部分会跟随一起更新
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_UpdateArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t j;
int16_t Page, Page1;
/*负数坐标在计算页地址时需要加一个偏移*/
/*(Y + Height - 1) / 8 + 1的目的是(Y + Height) / 8并向上取整*/
Page = Y / 8;
Page1 = (Y + Height - 1) / 8 + 1;
if (Y < 0)
{
Page -= 1;
Page1 -= 1;
}
/*遍历指定区域涉及的相关页*/
for (j = Page; j < Page1; j ++)
{
if (X >= 0 && X <= 127 && j >= 0 && j <= 7) //超出屏幕的内容不显示
{
/*设置光标位置为相关页的指定列*/
OLED_SetCursor(j, X);
/*连续写入Width个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(&OLED_DisplayBuf[j][X], Width);
}
}
}
/**
* 函 数:将OLED显存数组全部清零
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] = 0x00; //将显存数组数据全部清零
}
}
}
/**
* 函 数:将OLED显存数组部分清零
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ClearArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t i, j;
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
if (i >= 0 && i <= 127 && j >=0 && j <= 63) //超出屏幕的内容不显示
{
OLED_DisplayBuf[j / 8][i] &= ~(0x01 << (j % 8)); //将显存数组指定数据清零
}
}
}
}
/**
* 函 数:将OLED显存数组全部取反
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Reverse(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] ^= 0xFF; //将显存数组数据全部取反
}
}
}
/**
* 函 数:将OLED显存数组部分取反
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ReverseArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t i, j;
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
if (i >= 0 && i <= 127 && j >=0 && j <= 63) //超出屏幕的内容不显示
{
OLED_DisplayBuf[j / 8][i] ^= 0x01 << (j % 8); //将显存数组指定数据取反
}
}
}
}
/**
* 函 数:OLED显示一个字符
* 参 数:X 指定字符左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定字符左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Char 指定要显示的字符,范围:ASCII码可见字符
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChar(int16_t X, int16_t Y, char Char, uint8_t FontSize)
{
if (FontSize == OLED_8X16) //字体为宽8像素,高16像素
{
/*将ASCII字模库OLED_F8x16的指定数据以8*16的图像格式显示*/
OLED_ShowImage(X, Y, 8, 16, OLED_F8x16[Char - ' ']);
}
else if(FontSize == OLED_6X8) //字体为宽6像素,高8像素
{
/*将ASCII字模库OLED_F6x8的指定数据以6*8的图像格式显示*/
OLED_ShowImage(X, Y, 6, 8, OLED_F6x8[Char - ' ']);
}
}
/**
* 函 数:OLED显示字符串
* 参 数:X 指定字符串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定字符串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:String 指定要显示的字符串,范围:ASCII码可见字符组成的字符串
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowString(int16_t X, int16_t Y, char *String, uint8_t FontSize)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++) //遍历字符串的每个字符
{
/*调用OLED_ShowChar函数,依次显示每个字符*/
OLED_ShowChar(X + i * FontSize, Y, String[i], FontSize);
}
}
/**
* 函 数:OLED显示数字(十进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0~4294967295
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示有符号数字(十进制,整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:-2147483648~2147483647
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowSignedNum(int16_t X, int16_t Y, int32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
Number1 = Number; //Number1直接等于Number
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number1 = -Number; //Number1等于Number取负
}
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number1 / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + (i + 1) * FontSize, Y, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示十六进制数字(十六进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~8
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowHexNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*以十六进制提取数字的每一位*/
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10) //单个数字小于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber + '0', FontSize);
}
else //单个数字大于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ 'A' 可将数字转换为从A开始的十六进制字符*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber - 10 + 'A', FontSize);
}
}
}
/**
* 函 数:OLED显示二进制数字(二进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~16
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowBinNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(2, Length - i - 1) % 2 可以二进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(2, Length - i - 1) % 2 + '0', FontSize);
}
}
/**
* 函 数:OLED显示浮点数字(十进制,小数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:-4294967295.0~4294967295.0
* 参 数:IntLength 指定数字的整数位长度,范围:0~10
* 参 数:FraLength 指定数字的小数位长度,范围:0~9,小数进行四舍五入显示
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowFloatNum(int16_t X, int16_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize)
{
uint32_t PowNum, IntNum, FraNum;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number = -Number; //Number取负
}
/*提取整数部分和小数部分*/
IntNum = Number; //直接赋值给整型变量,提取整数
Number -= IntNum; //将Number的整数减掉,防止之后将小数乘到整数时因数过大造成错误
PowNum = OLED_Pow(10, FraLength); //根据指定小数的位数,确定乘数
FraNum = round(Number * PowNum); //将小数乘到整数,同时四舍五入,避免显示误差
IntNum += FraNum / PowNum; //若四舍五入造成了进位,则需要再加给整数
/*显示整数部分*/
OLED_ShowNum(X + FontSize, Y, IntNum, IntLength, FontSize);
/*显示小数点*/
OLED_ShowChar(X + (IntLength + 1) * FontSize, Y, '.', FontSize);
/*显示小数部分*/
OLED_ShowNum(X + (IntLength + 2) * FontSize, Y, FraNum, FraLength, FontSize);
}
/**
* 函 数:OLED显示汉字串
* 参 数:X 指定汉字串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定汉字串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Chinese 指定要显示的汉字串,范围:必须全部为汉字或者全角字符,不要加入任何半角字符
* 显示的汉字需要在OLED_Data.c里的OLED_CF16x16数组定义
* 未找到指定汉字时,会显示默认图形(一个方框,内部一个问号)
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChinese(int16_t X, int16_t Y, char *Chinese)
{
uint8_t pChinese = 0;
uint8_t pIndex;
uint8_t i;
char SingleChinese[OLED_CHN_CHAR_WIDTH + 1] = {0};
for (i = 0; Chinese[i] != '\0'; i ++) //遍历汉字串
{
SingleChinese[pChinese] = Chinese[i]; //提取汉字串数据到单个汉字数组
pChinese ++; //计次自增
/*当提取次数到达OLED_CHN_CHAR_WIDTH时,即代表提取到了一个完整的汉字*/
if (pChinese >= OLED_CHN_CHAR_WIDTH)
{
pChinese = 0; //计次归零
/*遍历整个汉字字模库,寻找匹配的汉字*/
/*如果找到最后一个汉字(定义为空字符串),则表示汉字未在字模库定义,停止寻找*/
for (pIndex = 0; strcmp(OLED_CF16x16[pIndex].Index, "") != 0; pIndex ++)
{
/*找到匹配的汉字*/
if (strcmp(OLED_CF16x16[pIndex].Index, SingleChinese) == 0)
{
break; //跳出循环,此时pIndex的值为指定汉字的索引
}
}
/*将汉字字模库OLED_CF16x16的指定数据以16*16的图像格式显示*/
OLED_ShowImage(X + ((i + 1) / OLED_CHN_CHAR_WIDTH - 1) * 16, Y, 16, 16, OLED_CF16x16[pIndex].Data);
}
}
}
/**
* 函 数:OLED显示图像
* 参 数:X 指定图像左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定图像左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定图像的宽度,范围:0~128
* 参 数:Height 指定图像的高度,范围:0~64
* 参 数:Image 指定要显示的图像
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowImage(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image)
{
uint8_t i = 0, j = 0;
int16_t Page, Shift;
/*将图像所在区域清空*/
OLED_ClearArea(X, Y, Width, Height);
/*遍历指定图像涉及的相关页*/
/*(Height - 1) / 8 + 1的目的是Height / 8并向上取整*/
for (j = 0; j < (Height - 1) / 8 + 1; j ++)
{
/*遍历指定图像涉及的相关列*/
for (i = 0; i < Width; i ++)
{
if (X + i >= 0 && X + i <= 127) //超出屏幕的内容不显示
{
/*负数坐标在计算页地址和移位时需要加一个偏移*/
Page = Y / 8;
Shift = Y % 8;
if (Y < 0)
{
Page -= 1;
Shift += 8;
}
if (Page + j >= 0 && Page + j <= 7) //超出屏幕的内容不显示
{
/*显示图像在当前页的内容*/
OLED_DisplayBuf[Page + j][X + i] |= Image[j * Width + i] << (Shift);
}
if (Page + j + 1 >= 0 && Page + j + 1 <= 7) //超出屏幕的内容不显示
{
/*显示图像在下一页的内容*/
OLED_DisplayBuf[Page + j + 1][X + i] |= Image[j * Width + i] >> (8 - Shift);
}
}
}
}
}
/**
* 函 数:OLED使用printf函数打印格式化字符串
* 参 数:X 指定格式化字符串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定格式化字符串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 参 数:format 指定要显示的格式化字符串,范围:ASCII码可见字符组成的字符串
* 参 数:... 格式化字符串参数列表
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Printf(int16_t X, int16_t Y, uint8_t FontSize, char *format, ...)
{
char String[256]; //定义字符数组
va_list arg; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始,接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
OLED_ShowString(X, Y, String, FontSize);//OLED显示字符数组(字符串)
}
/**
* 函 数:OLED在指定位置画一个点
* 参 数:X 指定点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawPoint(int16_t X, int16_t Y)
{
if (X >= 0 && X <= 127 && Y >=0 && Y <= 63) //超出屏幕的内容不显示
{
/*将显存数组指定位置的一个Bit数据置1*/
OLED_DisplayBuf[Y / 8][X] |= 0x01 << (Y % 8);
}
}
/**
* 函 数:OLED获取指定位置点的值
* 参 数:X 指定点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:指定位置点是否处于点亮状态,1:点亮,0:熄灭
*/
uint8_t OLED_GetPoint(int16_t X, int16_t Y)
{
if (X >= 0 && X <= 127 && Y >=0 && Y <= 63) //超出屏幕的内容不读取
{
/*判断指定位置的数据*/
if (OLED_DisplayBuf[Y / 8][X] & 0x01 << (Y % 8))
{
return 1; //为1,返回1
}
}
return 0; //否则,返回0
}
/**
* 函 数:OLED画线
* 参 数:X0 指定一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y0 指定一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X1 指定另一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y1 指定另一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawLine(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1)
{
int16_t x, y, dx, dy, d, incrE, incrNE, temp;
int16_t x0 = X0, y0 = Y0, x1 = X1, y1 = Y1;
uint8_t yflag = 0, xyflag = 0;
if (y0 == y1) //横线单独处理
{
/*0号点X坐标大于1号点X坐标,则交换两点X坐标*/
if (x0 > x1) {temp = x0; x0 = x1; x1 = temp;}
/*遍历X坐标*/
for (x = x0; x <= x1; x ++)
{
OLED_DrawPoint(x, y0); //依次画点
}
}
else if (x0 == x1) //竖线单独处理
{
/*0号点Y坐标大于1号点Y坐标,则交换两点Y坐标*/
if (y0 > y1) {temp = y0; y0 = y1; y1 = temp;}
/*遍历Y坐标*/
for (y = y0; y <= y1; y ++)
{
OLED_DrawPoint(x0, y); //依次画点
}
}
else //斜线
{
/*使用Bresenham算法画直线,可以避免耗时的浮点运算,效率更高*/
if (x0 > x1) //0号点X坐标大于1号点X坐标
{
/*交换两点坐标*/
/*交换后不影响画线,但是画线方向由第一、二、三、四象限变为第一、四象限*/
temp = x0; x0 = x1; x1 = temp;
temp = y0; y0 = y1; y1 = temp;
}
if (y0 > y1) //0号点Y坐标大于1号点Y坐标
{
/*将Y坐标取负*/
/*取负后影响画线,但是画线方向由第一、四象限变为第一象限*/
y0 = -y0;
y1 = -y1;
/*置标志位yflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
yflag = 1;
}
if (y1 - y0 > x1 - x0) //画线斜率大于1
{
/*将X坐标与Y坐标互换*/
/*互换后影响画线,但是画线方向由第一象限0~90度范围变为第一象限0~45度范围*/
temp = x0; x0 = y0; y0 = temp;
temp = x1; x1 = y1; y1 = temp;
/*置标志位xyflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
xyflag = 1;
}
/*以下为Bresenham算法画直线*/
/*算法要求,画线方向必须为第一象限0~45度范围*/
dx = x1 - x0;
dy = y1 - y0;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
d = 2 * dy - dx;
x = x0;
y = y0;
/*画起始点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
while (x < x1) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += incrE;
}
else //下一个点在当前点东北方
{
y ++;
d += incrNE;
}
/*画每一个点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
}
}
}
/**
* 函 数:OLED矩形
* 参 数:X 指定矩形左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定矩形左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定矩形的宽度,范围:0~128
* 参 数:Height 指定矩形的高度,范围:0~64
* 参 数:IsFilled 指定矩形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawRectangle(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled)
{
int16_t i, j;
if (!IsFilled) //指定矩形不填充
{
/*遍历上下X坐标,画矩形上下两条线*/
for (i = X; i < X + Width; i ++)
{
OLED_DrawPoint(i, Y);
OLED_DrawPoint(i, Y + Height - 1);
}
/*遍历左右Y坐标,画矩形左右两条线*/
for (i = Y; i < Y + Height; i ++)
{
OLED_DrawPoint(X, i);
OLED_DrawPoint(X + Width - 1, i);
}
}
else //指定矩形填充
{
/*遍历X坐标*/
for (i = X; i < X + Width; i ++)
{
/*遍历Y坐标*/
for (j = Y; j < Y + Height; j ++)
{
/*在指定区域画点,填充满矩形*/
OLED_DrawPoint(i, j);
}
}
}
}
/**
* 函 数:OLED三角形
* 参 数:X0 指定第一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y0 指定第一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X1 指定第二个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y1 指定第二个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X2 指定第三个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y2 指定第三个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:IsFilled 指定三角形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawTriangle(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1, int16_t X2, int16_t Y2, uint8_t IsFilled)
{
int16_t minx = X0, miny = Y0, maxx = X0, maxy = Y0;
int16_t i, j;
int16_t vx[] = {X0, X1, X2};
int16_t vy[] = {Y0, Y1, Y2};
if (!IsFilled) //指定三角形不填充
{
/*调用画线函数,将三个点用直线连接*/
OLED_DrawLine(X0, Y0, X1, Y1);
OLED_DrawLine(X0, Y0, X2, Y2);
OLED_DrawLine(X1, Y1, X2, Y2);
}
else //指定三角形填充
{
/*找到三个点最小的X、Y坐标*/
if (X1 < minx) {minx = X1;}
if (X2 < minx) {minx = X2;}
if (Y1 < miny) {miny = Y1;}
if (Y2 < miny) {miny = Y2;}
/*找到三个点最大的X、Y坐标*/
if (X1 > maxx) {maxx = X1;}
if (X2 > maxx) {maxx = X2;}
if (Y1 > maxy) {maxy = Y1;}
if (Y2 > maxy) {maxy = Y2;}
/*最小最大坐标之间的矩形为可能需要填充的区域*/
/*遍历此区域中所有的点*/
/*遍历X坐标*/
for (i = minx; i <= maxx; i ++)
{
/*遍历Y坐标*/
for (j = miny; j <= maxy; j ++)
{
/*调用OLED_pnpoly,判断指定点是否在指定三角形之中*/
/*如果在,则画点,如果不在,则不做处理*/
if (OLED_pnpoly(3, vx, vy, i, j)) {OLED_DrawPoint(i, j);}
}
}
}
}
/**
* 函 数:OLED画圆
* 参 数:X 指定圆的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定圆的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Radius 指定圆的半径,范围:0~255
* 参 数:IsFilled 指定圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawCircle(int16_t X, int16_t Y, uint8_t Radius, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*使用Bresenham算法画圆,可以避免耗时的浮点运算,效率更高*/
d = 1 - Radius;
x = 0;
y = Radius;
/*画每个八分之一圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - y, Y - x);
if (IsFilled) //指定圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X, Y + j);
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*画每个八分之一圆弧的点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - y, Y - x);
OLED_DrawPoint(X + x, Y - y);
OLED_DrawPoint(X + y, Y - x);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X - y, Y + x);
if (IsFilled) //指定圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X - y, Y + j);
OLED_DrawPoint(X + y, Y + j);
}
}
}
}
/**
* 函 数:OLED画椭圆
* 参 数:X 指定椭圆的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定椭圆的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:A 指定椭圆的横向半轴长度,范围:0~255
* 参 数:B 指定椭圆的纵向半轴长度,范围:0~255
* 参 数:IsFilled 指定椭圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawEllipse(int16_t X, int16_t Y, uint8_t A, uint8_t B, uint8_t IsFilled)
{
int16_t x, y, j;
int16_t a = A, b = B;
float d1, d2;
x = 0;
y = b;
d1 = b * b + a * a * (-b + 0.5);
if (IsFilled) //指定椭圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X, Y + j);
OLED_DrawPoint(X, Y + j);
}
}
/*画椭圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
/*画椭圆中间部分*/
while (b * b * (x + 1) < a * a * (y - 0.5))
{
if (d1 <= 0) //下一个点在当前点东方
{
d1 += b * b * (2 * x + 3);
}
else //下一个点在当前点东南方
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
y --;
}
x ++;
if (IsFilled) //指定椭圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆中间部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
/*画椭圆两侧部分*/
d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b;
while (y > 0)
{
if (d2 <= 0) //下一个点在当前点东方
{
d2 += b * b * (2 * x + 2) + a * a * (-2 * y + 3);
x ++;
}
else //下一个点在当前点东南方
{
d2 += a * a * (-2 * y + 3);
}
y --;
if (IsFilled) //指定椭圆填充
{
/*遍历两侧部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆两侧部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
}
/**
* 函 数:OLED画圆弧
* 参 数:X 指定圆弧的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定圆弧的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Radius 指定圆弧的半径,范围:0~255
* 参 数:StartAngle 指定圆弧的起始角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:EndAngle 指定圆弧的终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:IsFilled 指定圆弧是否填充,填充后为扇形
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawArc(int16_t X, int16_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*此函数借用Bresenham算法画圆的方法*/
d = 1 - Radius;
x = 0;
y = Radius;
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (IsFilled) //指定圆弧填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(0, j, StartAngle, EndAngle)) {OLED_DrawPoint(X, Y + j);}
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (OLED_IsInAngle(x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y - y);}
if (OLED_IsInAngle(y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y - x);}
if (OLED_IsInAngle(-x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + y);}
if (OLED_IsInAngle(-y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + x);}
if (IsFilled) //指定圆弧填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + j);}
if (OLED_IsInAngle(-x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + j);}
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(-y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + j);}
if (OLED_IsInAngle(y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + j);}
}
}
}
}
/*********************功能函数*/
④OLED_Data.h文件:(存放字模库的声明以及其它的信息)
#ifndef __OLED_DATA_H
#define __OLED_DATA_H
#include <stdint.h>
/*中文字符字节宽度*/
#define OLED_CHN_CHAR_WIDTH 3 //UTF-8编码格式给3,GB2312编码格式给2
/*字模基本单元*/
typedef struct
{
char Index[OLED_CHN_CHAR_WIDTH + 1]; //汉字索引
uint8_t Data[32]; //字模数据
} ChineseCell_t;
/*ASCII字模数据声明*/
extern const uint8_t OLED_F8x16[][16];
extern const uint8_t OLED_F6x8[][6];
/*汉字字模数据声明*/
extern const ChineseCell_t OLED_CF16x16[];
/*图像数据声明*/
extern const uint8_t Diode[];
/*按照上面的格式,在这个位置加入新的图像数据声明*/
//...
#endif
⑤OLED_Data.c文件:
#include "OLED_Data.h"
/*ASCII字模数据*********************/
/*宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16] =
{
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,0x16,0x0E,0x00,0x16,0x0E,0x00,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
0x00,0x00,0x00,0x16,0x0E,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,
0x00,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,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x80,0xB0,0x70,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,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
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,0x04,0x08,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
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
0x00,0x20,0x3F,0x21,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,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
};
/*宽6像素,高8像素*/
const uint8_t OLED_F6x8[][6] =
{
0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
};
/*********************ASCII字模数据*/
/*汉字字模数据*********************/
/*相同的汉字只需要定义一次,汉字不分先后顺序*/
/*必须全部为汉字或者全角字符,不要加入任何半角字符*/
/*宽16像素,高16像素*/
const ChineseCell_t OLED_CF16x16[] = {
",",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"。",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,
/*按照上面的格式,在这个位置加入新的汉字数据*/
//...
/*未找到指定汉字时显示的默认图形(一个方框,内部一个问号),请确保其位于数组最末尾*/
"",
0xFF,0x01,0x01,0x01,0x31,0x09,0x09,0x09,0x09,0x89,0x71,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x96,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};
/*********************汉字字模数据*/
/*图像数据*********************/
/*测试图像(一个方框,内部一个二极管符号),宽16像素,高16像素*/
const uint8_t Diode[] = {
0xFF,0x01,0x81,0x81,0x81,0xFD,0x89,0x91,0xA1,0xC1,0xFD,0x81,0x81,0x81,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x9F,0x88,0x84,0x82,0x81,0x9F,0x80,0x80,0x80,0x80,0xFF,
};
/*按照上面的格式,在这个位置加入新的图像数据*/
//...
/*********************图像数据*/
⑥main.c文件:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
int main(void)
{
/*OLED初始化*/
OLED_Init();
/*在(0, 0)位置显示字符'A',字体大小为8*16点阵*/
OLED_ShowChar(0, 0, 'A', OLED_8X16);
/*在(16, 0)位置显示字符串"Hello World!",字体大小为8*16点阵*/
OLED_ShowString(16, 0, "Hello World!", OLED_8X16);
/*在(0, 18)位置显示字符'A',字体大小为6*8点阵*/
OLED_ShowChar(0, 18, 'A', OLED_6X8);
/*在(16, 18)位置显示字符串"Hello World!",字体大小为6*8点阵*/
OLED_ShowString(16, 18, "Hello World!", OLED_6X8);
/*在(0, 28)位置显示数字12345,长度为5,字体大小为6*8点阵*/
OLED_ShowNum(0, 28, 12345, 5, OLED_6X8);
/*在(40, 28)位置显示有符号数字-66,长度为2,字体大小为6*8点阵*/
OLED_ShowSignedNum(40, 28, -66, 2, OLED_6X8);
/*在(70, 28)位置显示十六进制数字0xA5A5,长度为4,字体大小为6*8点阵*/
OLED_ShowHexNum(70, 28, 0xA5A5, 4, OLED_6X8);
/*在(0, 38)位置显示二进制数字0xA5,长度为8,字体大小为6*8点阵*/
OLED_ShowBinNum(0, 38, 0xA5, 8, OLED_6X8);
/*在(60, 38)位置显示浮点数字123.45,整数部分长度为3,小数部分长度为2,字体大小为6*8点阵*/
OLED_ShowFloatNum(60, 38, 123.45, 3, 2, OLED_6X8);
/*在(0, 48)位置显示汉字串"你好,世界。",字体大小为固定的16*16点阵*/
OLED_ShowChinese(0, 48, "你好,世界。");
/*在(96, 48)位置显示图像,宽16像素,高16像素,图像数据为Diode数组*/
OLED_ShowImage(96, 48, 16, 16, Diode);
/*在(96, 18)位置打印格式化字符串,字体大小为6*8点阵,格式化字符串为"[%02d]"*/
OLED_Printf(96, 18, OLED_6X8, "[%02d]", 6);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时3000ms,观察现象*/
Delay_ms(3000);
/*清空OLED显存数组*/
OLED_Clear();
/*在(5, 8)位置画点*/
OLED_DrawPoint(5, 8);
/*获取(5, 8)位置的点*/
if (OLED_GetPoint(5, 8))
{
/*如果指定点点亮,则在(10, 4)位置显示字符串"YES",字体大小为6*8点阵*/
OLED_ShowString(10, 4, "YES", OLED_6X8);
}
else
{
/*如果指定点未点亮,则在(10, 4)位置显示字符串"NO ",字体大小为6*8点阵*/
OLED_ShowString(10, 4, "NO ", OLED_6X8);
}
/*在(40, 0)和(127, 15)位置之间画直线*/
OLED_DrawLine(40, 0, 127, 15);
/*在(40, 15)和(127, 0)位置之间画直线*/
OLED_DrawLine(40, 15, 127, 0);
/*在(0, 20)位置画矩形,宽12像素,高15像素,未填充*/
OLED_DrawRectangle(0, 20, 12, 15, OLED_UNFILLED);
/*在(0, 40)位置画矩形,宽12像素,高15像素,填充*/
OLED_DrawRectangle(0, 40, 12, 15, OLED_FILLED);
/*在(20, 20)、(40, 25)和(30, 35)位置之间画三角形,未填充*/
OLED_DrawTriangle(20, 20, 40, 25, 30, 35, OLED_UNFILLED);
/*在(20, 40)、(40, 45)和(30, 55)位置之间画三角形,填充*/
OLED_DrawTriangle(20, 40, 40, 45, 30, 55, OLED_FILLED);
/*在(55, 27)位置画圆,半径8像素,未填充*/
OLED_DrawCircle(55, 27, 8, OLED_UNFILLED);
/*在(55, 47)位置画圆,半径8像素,填充*/
OLED_DrawCircle(55, 47, 8, OLED_FILLED);
/*在(82, 27)位置画椭圆,横向半轴12像素,纵向半轴8像素,未填充*/
OLED_DrawEllipse(82, 27, 12, 8, OLED_UNFILLED);
/*在(82, 47)位置画椭圆,横向半轴12像素,纵向半轴8像素,填充*/
OLED_DrawEllipse(82, 47, 12, 8, OLED_FILLED);
/*在(110, 18)位置画圆弧,半径15像素,起始角度25度,终止角度125度,未填充*/
OLED_DrawArc(110, 18, 15, 25, 125, OLED_UNFILLED);
/*在(110, 38)位置画圆弧,半径15像素,起始角度25度,终止角度125度,填充*/
OLED_DrawArc(110, 38, 15, 25, 125, OLED_FILLED);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时3000ms,观察现象*/
Delay_ms(3000);
while (1)
{
for (uint8_t i = 0; i < 4; i ++)
{
/*将OLED显存数组部分数据取反,从(0, i * 16)位置开始,宽128像素,高16像素*/
OLED_ReverseArea(0, i * 16, 128, 16);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时1000ms,观察现象*/
Delay_ms(1000);
/*把取反的内容翻转回来*/
OLED_ReverseArea(0, i * 16, 128, 16);
}
/*将OLED显存数组全部数据取反*/
OLED_Reverse();
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时1000ms,观察现象*/
Delay_ms(1000);
}
}
(3)7针脚OLED屏(使用SPI协议)的示例程序:(需要注意的是,如果分文件是从其它工程复制过来的,需要先将它们的编码格式转换成UTF-8)
①按下图所示将电路连接好,并将工程模板复制一份(需要有Delay.c和Delay.h文件)。
②OLED.h文件:
#ifndef __OLED_H
#define __OLED_H
#include <stdint.h>
#include "OLED_Data.h"
/*参数宏定义*********************/
/*FontSize参数取值*/
/*此参数值不仅用于判断,而且用于计算横向字符偏移,默认值为字体像素宽度*/
#define OLED_8X16 8
#define OLED_6X8 6
/*IsFilled参数数值*/
#define OLED_UNFILLED 0
#define OLED_FILLED 1
/*********************参数宏定义*/
/*函数声明*********************/
/*初始化函数*/
void OLED_Init(void);
/*更新函数*/
void OLED_Update(void);
void OLED_UpdateArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
/*显存控制函数*/
void OLED_Clear(void);
void OLED_ClearArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
void OLED_Reverse(void);
void OLED_ReverseArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height);
/*显示函数*/
void OLED_ShowChar(int16_t X, int16_t Y, char Char, uint8_t FontSize);
void OLED_ShowString(int16_t X, int16_t Y, char *String, uint8_t FontSize);
void OLED_ShowNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowSignedNum(int16_t X, int16_t Y, int32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowHexNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowBinNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize);
void OLED_ShowFloatNum(int16_t X, int16_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize);
void OLED_ShowChinese(int16_t X, int16_t Y, char *Chinese);
void OLED_ShowImage(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_Printf(int16_t X, int16_t Y, uint8_t FontSize, char *format, ...);
/*绘图函数*/
void OLED_DrawPoint(int16_t X, int16_t Y);
uint8_t OLED_GetPoint(int16_t X, int16_t Y);
void OLED_DrawLine(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1);
void OLED_DrawRectangle(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled);
void OLED_DrawTriangle(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1, int16_t X2, int16_t Y2, uint8_t IsFilled);
void OLED_DrawCircle(int16_t X, int16_t Y, uint8_t Radius, uint8_t IsFilled);
void OLED_DrawEllipse(int16_t X, int16_t Y, uint8_t A, uint8_t B, uint8_t IsFilled);
void OLED_DrawArc(int16_t X, int16_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled);
/*********************函数声明*/
#endif
③OLED.c文件:(如果要更改OLED屏连接在STM32上的引脚,需要更改程序中与引脚相关的代码,比如GPIO及其时钟的初始化,还有读/写GPIO的操作;如果要更改OLED屏的显示,需要更改程序中OLED屏初始化函数中的一些配置命令)
/***************************************************************************************
* 本程序由江协科技创建并免费开源共享
* 你可以任意查看、使用和修改,并应用到自己的项目之中
* 程序版权归江协科技所有,任何人或组织不得将其据为己有
***************************************************************************************
*/
#include "stm32f10x.h"
#include "OLED.h"
#include <string.h>
#include <math.h>
#include <stdio.h>
#include <stdarg.h>
/*全局变量*********************/
/**
* OLED显存数组
* 所有的显示函数,都只是对此显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
*/
uint8_t OLED_DisplayBuf[8][128];
/*********************全局变量*/
/*引脚配置*********************/
/**
* 函 数:OLED写D0(CLK)高低电平
* 参 数:要写入D0的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写D0时,此函数会被调用
* 用户需要根据参数传入的值,将D0置为高电平或者低电平
* 当参数传入0时,置D0为低电平,当参数传入1时,置D0为高电平
*/
void OLED_W_D0(uint8_t BitValue)
{
/*根据BitValue的值,将D0置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_12, (BitAction)BitValue);
}
/**
* 函 数:OLED写D1(MOSI)高低电平
* 参 数:要写入D1的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写D1时,此函数会被调用
* 用户需要根据参数传入的值,将D1置为高电平或者低电平
* 当参数传入0时,置D1为低电平,当参数传入1时,置D1为高电平
*/
void OLED_W_D1(uint8_t BitValue)
{
/*根据BitValue的值,将D1置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_13, (BitAction)BitValue);
}
/**
* 函 数:OLED写RES高低电平
* 参 数:要写入RES的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写RES时,此函数会被调用
* 用户需要根据参数传入的值,将RES置为高电平或者低电平
* 当参数传入0时,置RES为低电平,当参数传入1时,置RES为高电平
*/
void OLED_W_RES(uint8_t BitValue)
{
/*根据BitValue的值,将RES置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_14, (BitAction)BitValue);
}
/**
* 函 数:OLED写DC高低电平
* 参 数:要写入DC的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写DC时,此函数会被调用
* 用户需要根据参数传入的值,将DC置为高电平或者低电平
* 当参数传入0时,置DC为低电平,当参数传入1时,置DC为高电平
*/
void OLED_W_DC(uint8_t BitValue)
{
/*根据BitValue的值,将DC置高电平或者低电平*/
GPIO_WriteBit(GPIOB, GPIO_Pin_15, (BitAction)BitValue);
}
/**
* 函 数:OLED写CS高低电平
* 参 数:要写入CS的电平值,范围:0/1
* 返 回 值:无
* 说 明:当上层函数需要写CS时,此函数会被调用
* 用户需要根据参数传入的值,将CS置为高电平或者低电平
* 当参数传入0时,置CS为低电平,当参数传入1时,置CS为高电平
*/
void OLED_W_CS(uint8_t BitValue)
{
/*根据BitValue的值,将CS置高电平或者低电平*/
GPIO_WriteBit(GPIOA, GPIO_Pin_8, (BitAction)BitValue);
}
/**
* 函 数:OLED引脚初始化
* 参 数:无
* 返 回 值:无
* 说 明:当上层函数需要初始化时,此函数会被调用
* 用户需要将D0、D1、RES、DC和CS引脚初始化为推挽输出模式
*/
void OLED_GPIO_Init(void)
{
uint32_t i, j;
/*在初始化前,加入适量延时,待OLED供电稳定*/
for (i = 0; i < 1000; i ++)
{
for (j = 0; j < 1000; j ++);
}
/*将D0、D1、RES、DC和CS引脚初始化为推挽输出模式*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/*置引脚默认电平*/
OLED_W_D0(0);
OLED_W_D1(1);
OLED_W_RES(1);
OLED_W_DC(1);
OLED_W_CS(1);
}
/*********************引脚配置*/
/*通信协议*********************/
/**
* 函 数:SPI发送一个字节
* 参 数:Byte 要发送的一个字节数据,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_SPI_SendByte(uint8_t Byte)
{
uint8_t i;
/*循环8次,主机依次发送数据的每一位*/
for (i = 0; i < 8; i++)
{
/*使用掩码的方式取出Byte的指定一位数据并写入到D1线*/
/*两个!的作用是,让所有非零的值变为1*/
OLED_W_D1(!!(Byte & (0x80 >> i)));
OLED_W_D0(1); //拉高D0,从机在D0上升沿读取SDA
OLED_W_D0(0); //拉低D0,主机开始发送下一位数据
}
}
/**
* 函 数:OLED写命令
* 参 数:Command 要写入的命令值,范围:0x00~0xFF
* 返 回 值:无
*/
void OLED_WriteCommand(uint8_t Command)
{
OLED_W_CS(0); //拉低CS,开始通信
OLED_W_DC(0); //拉低DC,表示即将发送命令
OLED_SPI_SendByte(Command); //写入指定命令
OLED_W_CS(1); //拉高CS,结束通信
}
/**
* 函 数:OLED写数据
* 参 数:Data 要写入数据的起始地址
* 参 数:Count 要写入数据的数量
* 返 回 值:无
*/
void OLED_WriteData(uint8_t *Data, uint8_t Count)
{
uint8_t i;
OLED_W_CS(0); //拉低CS,开始通信
OLED_W_DC(1); //拉高DC,表示即将发送数据
/*循环Count次,进行连续的数据写入*/
for (i = 0; i < Count; i ++)
{
OLED_SPI_SendByte(Data[i]); //依次发送Data的每一个数据
}
OLED_W_CS(1); //拉高CS,结束通信
}
/*********************通信协议*/
/*硬件配置*********************/
/**
* 函 数:OLED初始化
* 参 数:无
* 返 回 值:无
* 说 明:使用前,需要调用此初始化函数
*/
void OLED_Init(void)
{
OLED_GPIO_Init(); //先调用底层的端口初始化
/*写入一系列的命令,对OLED进行初始化配置*/
OLED_WriteCommand(0xAE); //设置显示开启/关闭,0xAE关闭,0xAF开启
OLED_WriteCommand(0xD5); //设置显示时钟分频比/振荡器频率
OLED_WriteCommand(0x80); //0x00~0xFF
OLED_WriteCommand(0xA8); //设置多路复用率
OLED_WriteCommand(0x3F); //0x0E~0x3F
OLED_WriteCommand(0xD3); //设置显示偏移
OLED_WriteCommand(0x00); //0x00~0x7F
OLED_WriteCommand(0x40); //设置显示开始行,0x40~0x7F
OLED_WriteCommand(0xA1); //设置左右方向,0xA1正常,0xA0左右反置
OLED_WriteCommand(0xC8); //设置上下方向,0xC8正常,0xC0上下反置
OLED_WriteCommand(0xDA); //设置COM引脚硬件配置
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81); //设置对比度
OLED_WriteCommand(0xCF); //0x00~0xFF
OLED_WriteCommand(0xD9); //设置预充电周期
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB); //设置VCOMH取消选择级别
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4); //设置整个显示打开/关闭
OLED_WriteCommand(0xA6); //设置正常/反色显示,0xA6正常,0xA7反色
OLED_WriteCommand(0x8D); //设置充电泵
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF); //开启显示
OLED_Clear(); //清空显存数组
OLED_Update(); //更新显示,清屏,防止初始化后未显示内容时花屏
}
/**
* 函 数:OLED设置显示光标位置
* 参 数:Page 指定光标所在的页,范围:0~7
* 参 数:X 指定光标所在的X轴坐标,范围:0~127
* 返 回 值:无
* 说 明:OLED默认的Y轴,只能8个Bit为一组写入,即1页等于8个Y轴坐标
*/
void OLED_SetCursor(uint8_t Page, uint8_t X)
{
/*如果使用此程序驱动1.3寸的OLED显示屏,则需要解除此注释*/
/*因为1.3寸的OLED驱动芯片(SH1106)有132列*/
/*屏幕的起始列接在了第2列,而不是第0列*/
/*所以需要将X加2,才能正常显示*/
// X += 2;
/*通过指令设置页地址和列地址*/
OLED_WriteCommand(0xB0 | Page); //设置页位置
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //设置X位置高4位
OLED_WriteCommand(0x00 | (X & 0x0F)); //设置X位置低4位
}
/*********************硬件配置*/
/*工具函数*********************/
/*工具函数仅供内部部分函数使用*/
/**
* 函 数:次方函数
* 参 数:X 底数
* 参 数:Y 指数
* 返 回 值:等于X的Y次方
*/
uint32_t OLED_Pow(uint32_t X, uint32_t Y)
{
uint32_t Result = 1; //结果默认为1
while (Y --) //累乘Y次
{
Result *= X; //每次把X累乘到结果上
}
return Result;
}
/**
* 函 数:判断指定点是否在指定多边形内部
* 参 数:nvert 多边形的顶点数
* 参 数:vertx verty 包含多边形顶点的x和y坐标的数组
* 参 数:testx testy 测试点的X和y坐标
* 返 回 值:指定点是否在指定多边形内部,1:在内部,0:不在内部
*/
uint8_t OLED_pnpoly(uint8_t nvert, int16_t *vertx, int16_t *verty, int16_t testx, int16_t testy)
{
int16_t i, j, c = 0;
/*此算法由W. Randolph Franklin提出*/
for (i = 0, j = nvert - 1; i < nvert; j = i++)
{
if (((verty[i] > testy) != (verty[j] > testy)) &&
(testx < (vertx[j] - vertx[i]) * (testy - verty[i]) / (verty[j] - verty[i]) + vertx[i]))
{
c = !c;
}
}
return c;
}
/**
* 函 数:判断指定点是否在指定角度内部
* 参 数:X Y 指定点的坐标
* 参 数:StartAngle EndAngle 起始角度和终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 返 回 值:指定点是否在指定角度内部,1:在内部,0:不在内部
*/
uint8_t OLED_IsInAngle(int16_t X, int16_t Y, int16_t StartAngle, int16_t EndAngle)
{
int16_t PointAngle;
PointAngle = atan2(Y, X) / 3.14 * 180; //计算指定点的弧度,并转换为角度表示
if (StartAngle < EndAngle) //起始角度小于终止角度的情况
{
/*如果指定角度在起始终止角度之间,则判定指定点在指定角度*/
if (PointAngle >= StartAngle && PointAngle <= EndAngle)
{
return 1;
}
}
else //起始角度大于于终止角度的情况
{
/*如果指定角度大于起始角度或者小于终止角度,则判定指定点在指定角度*/
if (PointAngle >= StartAngle || PointAngle <= EndAngle)
{
return 1;
}
}
return 0; //不满足以上条件,则判断判定指定点不在指定角度
}
/*********************工具函数*/
/*功能函数*********************/
/**
* 函 数:将OLED显存数组更新到OLED屏幕
* 参 数:无
* 返 回 值:无
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Update(void)
{
uint8_t j;
/*遍历每一页*/
for (j = 0; j < 8; j ++)
{
/*设置光标位置为每一页的第一列*/
OLED_SetCursor(j, 0);
/*连续写入128个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(OLED_DisplayBuf[j], 128);
}
}
/**
* 函 数:将OLED显存数组部分更新到OLED屏幕
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:此函数会至少更新参数指定的区域
* 如果更新区域Y轴只包含部分页,则同一页的剩余部分会跟随一起更新
* 说 明:所有的显示函数,都只是对OLED显存数组进行读写
* 随后调用OLED_Update函数或OLED_UpdateArea函数
* 才会将显存数组的数据发送到OLED硬件,进行显示
* 故调用显示函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_UpdateArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t j;
int16_t Page, Page1;
/*负数坐标在计算页地址时需要加一个偏移*/
/*(Y + Height - 1) / 8 + 1的目的是(Y + Height) / 8并向上取整*/
Page = Y / 8;
Page1 = (Y + Height - 1) / 8 + 1;
if (Y < 0)
{
Page -= 1;
Page1 -= 1;
}
/*遍历指定区域涉及的相关页*/
for (j = Page; j < Page1; j ++)
{
if (X >= 0 && X <= 127 && j >= 0 && j <= 7) //超出屏幕的内容不显示
{
/*设置光标位置为相关页的指定列*/
OLED_SetCursor(j, X);
/*连续写入Width个数据,将显存数组的数据写入到OLED硬件*/
OLED_WriteData(&OLED_DisplayBuf[j][X], Width);
}
}
}
/**
* 函 数:将OLED显存数组全部清零
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Clear(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] = 0x00; //将显存数组数据全部清零
}
}
}
/**
* 函 数:将OLED显存数组部分清零
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ClearArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t i, j;
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
if (i >= 0 && i <= 127 && j >=0 && j <= 63) //超出屏幕的内容不显示
{
OLED_DisplayBuf[j / 8][i] &= ~(0x01 << (j % 8)); //将显存数组指定数据清零
}
}
}
}
/**
* 函 数:将OLED显存数组全部取反
* 参 数:无
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Reverse(void)
{
uint8_t i, j;
for (j = 0; j < 8; j ++) //遍历8页
{
for (i = 0; i < 128; i ++) //遍历128列
{
OLED_DisplayBuf[j][i] ^= 0xFF; //将显存数组数据全部取反
}
}
}
/**
* 函 数:将OLED显存数组部分取反
* 参 数:X 指定区域左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定区域左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定区域的宽度,范围:0~128
* 参 数:Height 指定区域的高度,范围:0~64
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ReverseArea(int16_t X, int16_t Y, uint8_t Width, uint8_t Height)
{
int16_t i, j;
for (j = Y; j < Y + Height; j ++) //遍历指定页
{
for (i = X; i < X + Width; i ++) //遍历指定列
{
if (i >= 0 && i <= 127 && j >=0 && j <= 63) //超出屏幕的内容不显示
{
OLED_DisplayBuf[j / 8][i] ^= 0x01 << (j % 8); //将显存数组指定数据取反
}
}
}
}
/**
* 函 数:OLED显示一个字符
* 参 数:X 指定字符左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定字符左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Char 指定要显示的字符,范围:ASCII码可见字符
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChar(int16_t X, int16_t Y, char Char, uint8_t FontSize)
{
if (FontSize == OLED_8X16) //字体为宽8像素,高16像素
{
/*将ASCII字模库OLED_F8x16的指定数据以8*16的图像格式显示*/
OLED_ShowImage(X, Y, 8, 16, OLED_F8x16[Char - ' ']);
}
else if(FontSize == OLED_6X8) //字体为宽6像素,高8像素
{
/*将ASCII字模库OLED_F6x8的指定数据以6*8的图像格式显示*/
OLED_ShowImage(X, Y, 6, 8, OLED_F6x8[Char - ' ']);
}
}
/**
* 函 数:OLED显示字符串
* 参 数:X 指定字符串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定字符串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:String 指定要显示的字符串,范围:ASCII码可见字符组成的字符串
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowString(int16_t X, int16_t Y, char *String, uint8_t FontSize)
{
uint8_t i;
for (i = 0; String[i] != '\0'; i++) //遍历字符串的每个字符
{
/*调用OLED_ShowChar函数,依次显示每个字符*/
OLED_ShowChar(X + i * FontSize, Y, String[i], FontSize);
}
}
/**
* 函 数:OLED显示数字(十进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0~4294967295
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示有符号数字(十进制,整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:-2147483648~2147483647
* 参 数:Length 指定数字的长度,范围:0~10
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowSignedNum(int16_t X, int16_t Y, int32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
uint32_t Number1;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
Number1 = Number; //Number1直接等于Number
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number1 = -Number; //Number1等于Number取负
}
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number1 / OLED_Pow(10, Length - i - 1) % 10 可以十进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + (i + 1) * FontSize, Y, Number1 / OLED_Pow(10, Length - i - 1) % 10 + '0', FontSize);
}
}
/**
* 函 数:OLED显示十六进制数字(十六进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~8
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowHexNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i, SingleNumber;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*以十六进制提取数字的每一位*/
SingleNumber = Number / OLED_Pow(16, Length - i - 1) % 16;
if (SingleNumber < 10) //单个数字小于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber + '0', FontSize);
}
else //单个数字大于10
{
/*调用OLED_ShowChar函数,显示此数字*/
/*+ 'A' 可将数字转换为从A开始的十六进制字符*/
OLED_ShowChar(X + i * FontSize, Y, SingleNumber - 10 + 'A', FontSize);
}
}
}
/**
* 函 数:OLED显示二进制数字(二进制,正整数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:0x00000000~0xFFFFFFFF
* 参 数:Length 指定数字的长度,范围:0~16
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowBinNum(int16_t X, int16_t Y, uint32_t Number, uint8_t Length, uint8_t FontSize)
{
uint8_t i;
for (i = 0; i < Length; i++) //遍历数字的每一位
{
/*调用OLED_ShowChar函数,依次显示每个数字*/
/*Number / OLED_Pow(2, Length - i - 1) % 2 可以二进制提取数字的每一位*/
/*+ '0' 可将数字转换为字符格式*/
OLED_ShowChar(X + i * FontSize, Y, Number / OLED_Pow(2, Length - i - 1) % 2 + '0', FontSize);
}
}
/**
* 函 数:OLED显示浮点数字(十进制,小数)
* 参 数:X 指定数字左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定数字左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Number 指定要显示的数字,范围:-4294967295.0~4294967295.0
* 参 数:IntLength 指定数字的整数位长度,范围:0~10
* 参 数:FraLength 指定数字的小数位长度,范围:0~9,小数进行四舍五入显示
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowFloatNum(int16_t X, int16_t Y, double Number, uint8_t IntLength, uint8_t FraLength, uint8_t FontSize)
{
uint32_t PowNum, IntNum, FraNum;
if (Number >= 0) //数字大于等于0
{
OLED_ShowChar(X, Y, '+', FontSize); //显示+号
}
else //数字小于0
{
OLED_ShowChar(X, Y, '-', FontSize); //显示-号
Number = -Number; //Number取负
}
/*提取整数部分和小数部分*/
IntNum = Number; //直接赋值给整型变量,提取整数
Number -= IntNum; //将Number的整数减掉,防止之后将小数乘到整数时因数过大造成错误
PowNum = OLED_Pow(10, FraLength); //根据指定小数的位数,确定乘数
FraNum = round(Number * PowNum); //将小数乘到整数,同时四舍五入,避免显示误差
IntNum += FraNum / PowNum; //若四舍五入造成了进位,则需要再加给整数
/*显示整数部分*/
OLED_ShowNum(X + FontSize, Y, IntNum, IntLength, FontSize);
/*显示小数点*/
OLED_ShowChar(X + (IntLength + 1) * FontSize, Y, '.', FontSize);
/*显示小数部分*/
OLED_ShowNum(X + (IntLength + 2) * FontSize, Y, FraNum, FraLength, FontSize);
}
/**
* 函 数:OLED显示汉字串
* 参 数:X 指定汉字串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定汉字串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Chinese 指定要显示的汉字串,范围:必须全部为汉字或者全角字符,不要加入任何半角字符
* 显示的汉字需要在OLED_Data.c里的OLED_CF16x16数组定义
* 未找到指定汉字时,会显示默认图形(一个方框,内部一个问号)
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowChinese(int16_t X, int16_t Y, char *Chinese)
{
uint8_t pChinese = 0;
uint8_t pIndex;
uint8_t i;
char SingleChinese[OLED_CHN_CHAR_WIDTH + 1] = {0};
for (i = 0; Chinese[i] != '\0'; i ++) //遍历汉字串
{
SingleChinese[pChinese] = Chinese[i]; //提取汉字串数据到单个汉字数组
pChinese ++; //计次自增
/*当提取次数到达OLED_CHN_CHAR_WIDTH时,即代表提取到了一个完整的汉字*/
if (pChinese >= OLED_CHN_CHAR_WIDTH)
{
pChinese = 0; //计次归零
/*遍历整个汉字字模库,寻找匹配的汉字*/
/*如果找到最后一个汉字(定义为空字符串),则表示汉字未在字模库定义,停止寻找*/
for (pIndex = 0; strcmp(OLED_CF16x16[pIndex].Index, "") != 0; pIndex ++)
{
/*找到匹配的汉字*/
if (strcmp(OLED_CF16x16[pIndex].Index, SingleChinese) == 0)
{
break; //跳出循环,此时pIndex的值为指定汉字的索引
}
}
/*将汉字字模库OLED_CF16x16的指定数据以16*16的图像格式显示*/
OLED_ShowImage(X + ((i + 1) / OLED_CHN_CHAR_WIDTH - 1) * 16, Y, 16, 16, OLED_CF16x16[pIndex].Data);
}
}
}
/**
* 函 数:OLED显示图像
* 参 数:X 指定图像左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定图像左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定图像的宽度,范围:0~128
* 参 数:Height 指定图像的高度,范围:0~64
* 参 数:Image 指定要显示的图像
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_ShowImage(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image)
{
uint8_t i = 0, j = 0;
int16_t Page, Shift;
/*将图像所在区域清空*/
OLED_ClearArea(X, Y, Width, Height);
/*遍历指定图像涉及的相关页*/
/*(Height - 1) / 8 + 1的目的是Height / 8并向上取整*/
for (j = 0; j < (Height - 1) / 8 + 1; j ++)
{
/*遍历指定图像涉及的相关列*/
for (i = 0; i < Width; i ++)
{
if (X + i >= 0 && X + i <= 127) //超出屏幕的内容不显示
{
/*负数坐标在计算页地址和移位时需要加一个偏移*/
Page = Y / 8;
Shift = Y % 8;
if (Y < 0)
{
Page -= 1;
Shift += 8;
}
if (Page + j >= 0 && Page + j <= 7) //超出屏幕的内容不显示
{
/*显示图像在当前页的内容*/
OLED_DisplayBuf[Page + j][X + i] |= Image[j * Width + i] << (Shift);
}
if (Page + j + 1 >= 0 && Page + j + 1 <= 7) //超出屏幕的内容不显示
{
/*显示图像在下一页的内容*/
OLED_DisplayBuf[Page + j + 1][X + i] |= Image[j * Width + i] >> (8 - Shift);
}
}
}
}
}
/**
* 函 数:OLED使用printf函数打印格式化字符串
* 参 数:X 指定格式化字符串左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定格式化字符串左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:FontSize 指定字体大小
* 范围:OLED_8X16 宽8像素,高16像素
* OLED_6X8 宽6像素,高8像素
* 参 数:format 指定要显示的格式化字符串,范围:ASCII码可见字符组成的字符串
* 参 数:... 格式化字符串参数列表
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_Printf(int16_t X, int16_t Y, uint8_t FontSize, char *format, ...)
{
char String[256]; //定义字符数组
va_list arg; //定义可变参数列表数据类型的变量arg
va_start(arg, format); //从format开始,接收参数列表到arg变量
vsprintf(String, format, arg); //使用vsprintf打印格式化字符串和参数列表到字符数组中
va_end(arg); //结束变量arg
OLED_ShowString(X, Y, String, FontSize);//OLED显示字符数组(字符串)
}
/**
* 函 数:OLED在指定位置画一个点
* 参 数:X 指定点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawPoint(int16_t X, int16_t Y)
{
if (X >= 0 && X <= 127 && Y >=0 && Y <= 63) //超出屏幕的内容不显示
{
/*将显存数组指定位置的一个Bit数据置1*/
OLED_DisplayBuf[Y / 8][X] |= 0x01 << (Y % 8);
}
}
/**
* 函 数:OLED获取指定位置点的值
* 参 数:X 指定点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:指定位置点是否处于点亮状态,1:点亮,0:熄灭
*/
uint8_t OLED_GetPoint(int16_t X, int16_t Y)
{
if (X >= 0 && X <= 127 && Y >=0 && Y <= 63) //超出屏幕的内容不读取
{
/*判断指定位置的数据*/
if (OLED_DisplayBuf[Y / 8][X] & 0x01 << (Y % 8))
{
return 1; //为1,返回1
}
}
return 0; //否则,返回0
}
/**
* 函 数:OLED画线
* 参 数:X0 指定一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y0 指定一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X1 指定另一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y1 指定另一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawLine(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1)
{
int16_t x, y, dx, dy, d, incrE, incrNE, temp;
int16_t x0 = X0, y0 = Y0, x1 = X1, y1 = Y1;
uint8_t yflag = 0, xyflag = 0;
if (y0 == y1) //横线单独处理
{
/*0号点X坐标大于1号点X坐标,则交换两点X坐标*/
if (x0 > x1) {temp = x0; x0 = x1; x1 = temp;}
/*遍历X坐标*/
for (x = x0; x <= x1; x ++)
{
OLED_DrawPoint(x, y0); //依次画点
}
}
else if (x0 == x1) //竖线单独处理
{
/*0号点Y坐标大于1号点Y坐标,则交换两点Y坐标*/
if (y0 > y1) {temp = y0; y0 = y1; y1 = temp;}
/*遍历Y坐标*/
for (y = y0; y <= y1; y ++)
{
OLED_DrawPoint(x0, y); //依次画点
}
}
else //斜线
{
/*使用Bresenham算法画直线,可以避免耗时的浮点运算,效率更高*/
if (x0 > x1) //0号点X坐标大于1号点X坐标
{
/*交换两点坐标*/
/*交换后不影响画线,但是画线方向由第一、二、三、四象限变为第一、四象限*/
temp = x0; x0 = x1; x1 = temp;
temp = y0; y0 = y1; y1 = temp;
}
if (y0 > y1) //0号点Y坐标大于1号点Y坐标
{
/*将Y坐标取负*/
/*取负后影响画线,但是画线方向由第一、四象限变为第一象限*/
y0 = -y0;
y1 = -y1;
/*置标志位yflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
yflag = 1;
}
if (y1 - y0 > x1 - x0) //画线斜率大于1
{
/*将X坐标与Y坐标互换*/
/*互换后影响画线,但是画线方向由第一象限0~90度范围变为第一象限0~45度范围*/
temp = x0; x0 = y0; y0 = temp;
temp = x1; x1 = y1; y1 = temp;
/*置标志位xyflag,记住当前变换,在后续实际画线时,再将坐标换回来*/
xyflag = 1;
}
/*以下为Bresenham算法画直线*/
/*算法要求,画线方向必须为第一象限0~45度范围*/
dx = x1 - x0;
dy = y1 - y0;
incrE = 2 * dy;
incrNE = 2 * (dy - dx);
d = 2 * dy - dx;
x = x0;
y = y0;
/*画起始点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
while (x < x1) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += incrE;
}
else //下一个点在当前点东北方
{
y ++;
d += incrNE;
}
/*画每一个点,同时判断标志位,将坐标换回来*/
if (yflag && xyflag){OLED_DrawPoint(y, -x);}
else if (yflag) {OLED_DrawPoint(x, -y);}
else if (xyflag) {OLED_DrawPoint(y, x);}
else {OLED_DrawPoint(x, y);}
}
}
}
/**
* 函 数:OLED矩形
* 参 数:X 指定矩形左上角的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定矩形左上角的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Width 指定矩形的宽度,范围:0~128
* 参 数:Height 指定矩形的高度,范围:0~64
* 参 数:IsFilled 指定矩形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawRectangle(int16_t X, int16_t Y, uint8_t Width, uint8_t Height, uint8_t IsFilled)
{
int16_t i, j;
if (!IsFilled) //指定矩形不填充
{
/*遍历上下X坐标,画矩形上下两条线*/
for (i = X; i < X + Width; i ++)
{
OLED_DrawPoint(i, Y);
OLED_DrawPoint(i, Y + Height - 1);
}
/*遍历左右Y坐标,画矩形左右两条线*/
for (i = Y; i < Y + Height; i ++)
{
OLED_DrawPoint(X, i);
OLED_DrawPoint(X + Width - 1, i);
}
}
else //指定矩形填充
{
/*遍历X坐标*/
for (i = X; i < X + Width; i ++)
{
/*遍历Y坐标*/
for (j = Y; j < Y + Height; j ++)
{
/*在指定区域画点,填充满矩形*/
OLED_DrawPoint(i, j);
}
}
}
}
/**
* 函 数:OLED三角形
* 参 数:X0 指定第一个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y0 指定第一个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X1 指定第二个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y1 指定第二个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:X2 指定第三个端点的横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y2 指定第三个端点的纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:IsFilled 指定三角形是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawTriangle(int16_t X0, int16_t Y0, int16_t X1, int16_t Y1, int16_t X2, int16_t Y2, uint8_t IsFilled)
{
int16_t minx = X0, miny = Y0, maxx = X0, maxy = Y0;
int16_t i, j;
int16_t vx[] = {X0, X1, X2};
int16_t vy[] = {Y0, Y1, Y2};
if (!IsFilled) //指定三角形不填充
{
/*调用画线函数,将三个点用直线连接*/
OLED_DrawLine(X0, Y0, X1, Y1);
OLED_DrawLine(X0, Y0, X2, Y2);
OLED_DrawLine(X1, Y1, X2, Y2);
}
else //指定三角形填充
{
/*找到三个点最小的X、Y坐标*/
if (X1 < minx) {minx = X1;}
if (X2 < minx) {minx = X2;}
if (Y1 < miny) {miny = Y1;}
if (Y2 < miny) {miny = Y2;}
/*找到三个点最大的X、Y坐标*/
if (X1 > maxx) {maxx = X1;}
if (X2 > maxx) {maxx = X2;}
if (Y1 > maxy) {maxy = Y1;}
if (Y2 > maxy) {maxy = Y2;}
/*最小最大坐标之间的矩形为可能需要填充的区域*/
/*遍历此区域中所有的点*/
/*遍历X坐标*/
for (i = minx; i <= maxx; i ++)
{
/*遍历Y坐标*/
for (j = miny; j <= maxy; j ++)
{
/*调用OLED_pnpoly,判断指定点是否在指定三角形之中*/
/*如果在,则画点,如果不在,则不做处理*/
if (OLED_pnpoly(3, vx, vy, i, j)) {OLED_DrawPoint(i, j);}
}
}
}
}
/**
* 函 数:OLED画圆
* 参 数:X 指定圆的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定圆的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Radius 指定圆的半径,范围:0~255
* 参 数:IsFilled 指定圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawCircle(int16_t X, int16_t Y, uint8_t Radius, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*使用Bresenham算法画圆,可以避免耗时的浮点运算,效率更高*/
d = 1 - Radius;
x = 0;
y = Radius;
/*画每个八分之一圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - y, Y - x);
if (IsFilled) //指定圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X, Y + j);
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*画每个八分之一圆弧的点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X + y, Y + x);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - y, Y - x);
OLED_DrawPoint(X + x, Y - y);
OLED_DrawPoint(X + y, Y - x);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X - y, Y + x);
if (IsFilled) //指定圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在指定区域画点,填充部分圆*/
OLED_DrawPoint(X - y, Y + j);
OLED_DrawPoint(X + y, Y + j);
}
}
}
}
/**
* 函 数:OLED画椭圆
* 参 数:X 指定椭圆的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定椭圆的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:A 指定椭圆的横向半轴长度,范围:0~255
* 参 数:B 指定椭圆的纵向半轴长度,范围:0~255
* 参 数:IsFilled 指定椭圆是否填充
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawEllipse(int16_t X, int16_t Y, uint8_t A, uint8_t B, uint8_t IsFilled)
{
int16_t x, y, j;
int16_t a = A, b = B;
float d1, d2;
/*使用Bresenham算法画椭圆,可以避免部分耗时的浮点运算,效率更高*/
x = 0;
y = b;
d1 = b * b + a * a * (-b + 0.5);
if (IsFilled) //指定椭圆填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X, Y + j);
OLED_DrawPoint(X, Y + j);
}
}
/*画椭圆弧的起始点*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
/*画椭圆中间部分*/
while (b * b * (x + 1) < a * a * (y - 0.5))
{
if (d1 <= 0) //下一个点在当前点东方
{
d1 += b * b * (2 * x + 3);
}
else //下一个点在当前点东南方
{
d1 += b * b * (2 * x + 3) + a * a * (-2 * y + 2);
y --;
}
x ++;
if (IsFilled) //指定椭圆填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆中间部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
/*画椭圆两侧部分*/
d2 = b * b * (x + 0.5) * (x + 0.5) + a * a * (y - 1) * (y - 1) - a * a * b * b;
while (y > 0)
{
if (d2 <= 0) //下一个点在当前点东方
{
d2 += b * b * (2 * x + 2) + a * a * (-2 * y + 3);
x ++;
}
else //下一个点在当前点东南方
{
d2 += a * a * (-2 * y + 3);
}
y --;
if (IsFilled) //指定椭圆填充
{
/*遍历两侧部分*/
for (j = -y; j < y; j ++)
{
/*在指定区域画点,填充部分椭圆*/
OLED_DrawPoint(X + x, Y + j);
OLED_DrawPoint(X - x, Y + j);
}
}
/*画椭圆两侧部分圆弧*/
OLED_DrawPoint(X + x, Y + y);
OLED_DrawPoint(X - x, Y - y);
OLED_DrawPoint(X - x, Y + y);
OLED_DrawPoint(X + x, Y - y);
}
}
/**
* 函 数:OLED画圆弧
* 参 数:X 指定圆弧的圆心横坐标,范围:-32768~32767,屏幕区域:0~127
* 参 数:Y 指定圆弧的圆心纵坐标,范围:-32768~32767,屏幕区域:0~63
* 参 数:Radius 指定圆弧的半径,范围:0~255
* 参 数:StartAngle 指定圆弧的起始角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:EndAngle 指定圆弧的终止角度,范围:-180~180
* 水平向右为0度,水平向左为180度或-180度,下方为正数,上方为负数,顺时针旋转
* 参 数:IsFilled 指定圆弧是否填充,填充后为扇形
* 范围:OLED_UNFILLED 不填充
* OLED_FILLED 填充
* 返 回 值:无
* 说 明:调用此函数后,要想真正地呈现在屏幕上,还需调用更新函数
*/
void OLED_DrawArc(int16_t X, int16_t Y, uint8_t Radius, int16_t StartAngle, int16_t EndAngle, uint8_t IsFilled)
{
int16_t x, y, d, j;
/*此函数借用Bresenham算法画圆的方法*/
d = 1 - Radius;
x = 0;
y = Radius;
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (IsFilled) //指定圆弧填充
{
/*遍历起始点Y坐标*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(0, j, StartAngle, EndAngle)) {OLED_DrawPoint(X, Y + j);}
}
}
while (x < y) //遍历X轴的每个点
{
x ++;
if (d < 0) //下一个点在当前点东方
{
d += 2 * x + 1;
}
else //下一个点在当前点东南方
{
y --;
d += 2 * (x - y) + 1;
}
/*在画圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + y);}
if (OLED_IsInAngle(y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + x);}
if (OLED_IsInAngle(-x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y - y);}
if (OLED_IsInAngle(-y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y - x);}
if (OLED_IsInAngle(x, -y, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y - y);}
if (OLED_IsInAngle(y, -x, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y - x);}
if (OLED_IsInAngle(-x, y, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + y);}
if (OLED_IsInAngle(-y, x, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + x);}
if (IsFilled) //指定圆弧填充
{
/*遍历中间部分*/
for (j = -y; j < y; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + x, Y + j);}
if (OLED_IsInAngle(-x, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - x, Y + j);}
}
/*遍历两侧部分*/
for (j = -x; j < x; j ++)
{
/*在填充圆的每个点时,判断指定点是否在指定角度内,在,则画点,不在,则不做处理*/
if (OLED_IsInAngle(-y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X - y, Y + j);}
if (OLED_IsInAngle(y, j, StartAngle, EndAngle)) {OLED_DrawPoint(X + y, Y + j);}
}
}
}
}
/*********************功能函数*/
④OLED_Data.h文件:(存放字模库的声明以及其它的信息)
#ifndef __OLED_DATA_H
#define __OLED_DATA_H
#include <stdint.h>
/*中文字符字节宽度*/
#define OLED_CHN_CHAR_WIDTH 3 //UTF-8编码格式给3,GB2312编码格式给2
/*字模基本单元*/
typedef struct
{
char Index[OLED_CHN_CHAR_WIDTH + 1]; //汉字索引
uint8_t Data[32]; //字模数据
} ChineseCell_t;
/*ASCII字模数据声明*/
extern const uint8_t OLED_F8x16[][16];
extern const uint8_t OLED_F6x8[][6];
/*汉字字模数据声明*/
extern const ChineseCell_t OLED_CF16x16[];
/*图像数据声明*/
extern const uint8_t Diode[];
/*按照上面的格式,在这个位置加入新的图像数据声明*/
//...
#endif
⑤OLED_Data.c文件:
#include "OLED_Data.h"
/*ASCII字模数据*********************/
/*宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16] =
{
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,0x16,0x0E,0x00,0x16,0x0E,0x00,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
0x00,0x00,0x00,0x16,0x0E,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,
0x00,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,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x80,0xB0,0x70,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,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
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,0x04,0x08,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
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
0x00,0x20,0x3F,0x21,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,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
};
/*宽6像素,高8像素*/
const uint8_t OLED_F6x8[][6] =
{
0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
};
/*********************ASCII字模数据*/
/*汉字字模数据*********************/
/*相同的汉字只需要定义一次,汉字不分先后顺序*/
/*必须全部为汉字或者全角字符,不要加入任何半角字符*/
/*宽16像素,高16像素*/
const ChineseCell_t OLED_CF16x16[] = {
",",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x58,0x38,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"。",
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x18,0x24,0x24,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,
/*按照上面的格式,在这个位置加入新的汉字数据*/
//...
/*未找到指定汉字时显示的默认图形(一个方框,内部一个问号),请确保其位于数组最末尾*/
"",
0xFF,0x01,0x01,0x01,0x31,0x09,0x09,0x09,0x09,0x89,0x71,0x01,0x01,0x01,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x80,0x80,0x96,0x81,0x80,0x80,0x80,0x80,0x80,0x80,0xFF,
};
/*********************汉字字模数据*/
/*图像数据*********************/
/*测试图像(一个方框,内部一个二极管符号),宽16像素,高16像素*/
const uint8_t Diode[] = {
0xFF,0x01,0x81,0x81,0x81,0xFD,0x89,0x91,0xA1,0xC1,0xFD,0x81,0x81,0x81,0x01,0xFF,
0xFF,0x80,0x80,0x80,0x80,0x9F,0x88,0x84,0x82,0x81,0x9F,0x80,0x80,0x80,0x80,0xFF,
};
/*按照上面的格式,在这个位置加入新的图像数据*/
//...
/*********************图像数据*/
⑥main.c文件:
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
int main(void)
{
/*OLED初始化*/
OLED_Init();
/*在(0, 0)位置显示字符'A',字体大小为8*16点阵*/
OLED_ShowChar(0, 0, 'A', OLED_8X16);
/*在(16, 0)位置显示字符串"Hello World!",字体大小为8*16点阵*/
OLED_ShowString(16, 0, "Hello World!", OLED_8X16);
/*在(0, 18)位置显示字符'A',字体大小为6*8点阵*/
OLED_ShowChar(0, 18, 'A', OLED_6X8);
/*在(16, 18)位置显示字符串"Hello World!",字体大小为6*8点阵*/
OLED_ShowString(16, 18, "Hello World!", OLED_6X8);
/*在(0, 28)位置显示数字12345,长度为5,字体大小为6*8点阵*/
OLED_ShowNum(0, 28, 12345, 5, OLED_6X8);
/*在(40, 28)位置显示有符号数字-66,长度为2,字体大小为6*8点阵*/
OLED_ShowSignedNum(40, 28, -66, 2, OLED_6X8);
/*在(70, 28)位置显示十六进制数字0xA5A5,长度为4,字体大小为6*8点阵*/
OLED_ShowHexNum(70, 28, 0xA5A5, 4, OLED_6X8);
/*在(0, 38)位置显示二进制数字0xA5,长度为8,字体大小为6*8点阵*/
OLED_ShowBinNum(0, 38, 0xA5, 8, OLED_6X8);
/*在(60, 38)位置显示浮点数字123.45,整数部分长度为3,小数部分长度为2,字体大小为6*8点阵*/
OLED_ShowFloatNum(60, 38, 123.45, 3, 2, OLED_6X8);
/*在(0, 48)位置显示汉字串"你好,世界。",字体大小为固定的16*16点阵*/
OLED_ShowChinese(0, 48, "你好,世界。");
/*在(96, 48)位置显示图像,宽16像素,高16像素,图像数据为Diode数组*/
OLED_ShowImage(96, 48, 16, 16, Diode);
/*在(96, 18)位置打印格式化字符串,字体大小为6*8点阵,格式化字符串为"[%02d]"*/
OLED_Printf(96, 18, OLED_6X8, "[%02d]", 6);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时3000ms,观察现象*/
Delay_ms(3000);
/*清空OLED显存数组*/
OLED_Clear();
/*在(5, 8)位置画点*/
OLED_DrawPoint(5, 8);
/*获取(5, 8)位置的点*/
if (OLED_GetPoint(5, 8))
{
/*如果指定点点亮,则在(10, 4)位置显示字符串"YES",字体大小为6*8点阵*/
OLED_ShowString(10, 4, "YES", OLED_6X8);
}
else
{
/*如果指定点未点亮,则在(10, 4)位置显示字符串"NO ",字体大小为6*8点阵*/
OLED_ShowString(10, 4, "NO ", OLED_6X8);
}
/*在(40, 0)和(127, 15)位置之间画直线*/
OLED_DrawLine(40, 0, 127, 15);
/*在(40, 15)和(127, 0)位置之间画直线*/
OLED_DrawLine(40, 15, 127, 0);
/*在(0, 20)位置画矩形,宽12像素,高15像素,未填充*/
OLED_DrawRectangle(0, 20, 12, 15, OLED_UNFILLED);
/*在(0, 40)位置画矩形,宽12像素,高15像素,填充*/
OLED_DrawRectangle(0, 40, 12, 15, OLED_FILLED);
/*在(20, 20)、(40, 25)和(30, 35)位置之间画三角形,未填充*/
OLED_DrawTriangle(20, 20, 40, 25, 30, 35, OLED_UNFILLED);
/*在(20, 40)、(40, 45)和(30, 55)位置之间画三角形,填充*/
OLED_DrawTriangle(20, 40, 40, 45, 30, 55, OLED_FILLED);
/*在(55, 27)位置画圆,半径8像素,未填充*/
OLED_DrawCircle(55, 27, 8, OLED_UNFILLED);
/*在(55, 47)位置画圆,半径8像素,填充*/
OLED_DrawCircle(55, 47, 8, OLED_FILLED);
/*在(82, 27)位置画椭圆,横向半轴12像素,纵向半轴8像素,未填充*/
OLED_DrawEllipse(82, 27, 12, 8, OLED_UNFILLED);
/*在(82, 47)位置画椭圆,横向半轴12像素,纵向半轴8像素,填充*/
OLED_DrawEllipse(82, 47, 12, 8, OLED_FILLED);
/*在(110, 18)位置画圆弧,半径15像素,起始角度25度,终止角度125度,未填充*/
OLED_DrawArc(110, 18, 15, 25, 125, OLED_UNFILLED);
/*在(110, 38)位置画圆弧,半径15像素,起始角度25度,终止角度125度,填充*/
OLED_DrawArc(110, 38, 15, 25, 125, OLED_FILLED);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时3000ms,观察现象*/
Delay_ms(3000);
while (1)
{
for (uint8_t i = 0; i < 4; i ++)
{
/*将OLED显存数组部分数据取反,从(0, i * 16)位置开始,宽128像素,高16像素*/
OLED_ReverseArea(0, i * 16, 128, 16);
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时1000ms,观察现象*/
Delay_ms(1000);
/*把取反的内容翻转回来*/
OLED_ReverseArea(0, i * 16, 128, 16);
}
/*将OLED显存数组全部数据取反*/
OLED_Reverse();
/*调用OLED_Update函数,将OLED显存数组的内容更新到OLED硬件进行显示*/
OLED_Update();
/*延时1000ms,观察现象*/
Delay_ms(1000);
}
}
3、采用GB2312编码格式
(1)如果编程采用GB2312编码格式,首先按下图所示对编码格式进行设置。
(2)4针脚OLED屏(使用I2C协议)的示例程序:(需要注意的是,如果分文件是从其它工程复制过来的,需要先将它们的编码格式转换成GB2312)
①按下图所示将电路连接好,并将工程模板复制一份(需要有Delay.c和Delay.h文件)。
②OLED.h文件、OLED.c文件、OLED_Data.h文件、OLED_Data.c文件和main.c文件都可沿用UTF-8编码下的4针脚OLED屏(使用I2C协议)的示例程序,不过OLED_Data.h文件中的宏定义OLED_CHN_CHAR_WIDTH需更改为2。
(3)7针脚OLED屏(使用SPI协议)的示例程序:(需要注意的是,如果分文件是从其它工程复制过来的,需要先将它们的编码格式转换成GB2312)
①按下图所示将电路连接好,并将工程模板复制一份(需要有Delay.c和Delay.h文件)。
②OLED.h文件、OLED.c文件、OLED_Data.h文件、OLED_Data.c文件和main.c文件都可沿用UTF-8编码下的7针脚OLED屏(使用SPI协议)的示例程序,不过OLED_Data.h文件中的宏定义OLED_CHN_CHAR_WIDTH需更改为2。
4、复杂字符取模
(1)对于复杂点的字符,比如汉字,在简单显示的示例中OLED字库并没有汉字的字模数据,对此需要对目标汉字取模,获得一连串的二进制数据,接着将它们添加到字模库中,OLED屏可根据这些数据对汉字进行显示。
(2)字符取模步骤:
①打开取模软件,打开“设置”,将字模选项按下图所示进行配置。
②打开取模软件,输入需要取模的字符,点击“生成字模”即可得到字符的字模数据。
③将得到的字模数据复制到OLED_Data.c文件的OLED字模库中,仿照测试汉字的字模数据格式进行修改,这样,OLED屏就能显示需要显示的汉字了,不过由于单片机的存储空间有限,一些不使用的字符不建议添加进字模库中。
5、图片取模
(1)对于彩色图片,OLED屏没办法显示,对此需要将其处理成只有黑白两种颜色的图片,对该图片取模,获得一连串的二进制数据,OLED屏可根据这些数据对图像进行显示。
(2)彩色图片处理步骤:
①首先在Photoshop中打开需要处理的彩色图片。
②点击“滤镜”→“风格化”→“查找边缘”,将图片变成轮廓分明的样式。(这一步看个人需求)
③点击“图像”→“调整”→“阈值”,对图像进行二值化处理,大于阈值的像素变为白色,小于阈值的像素变为黑色。
④调整阈值让图像达到一个适合OLED屏显示的状态,接着点击“图像”→“图像大小”,对图片大小进行设置,这里将图片设置为60×60像素的大小。
⑤图片变小后会出现灰色的像素点,这里需要进行第二次二值化。
⑥点击“文件”→“存储为”,将图片存储为BMP格式。
(3)黑白像素图片取模步骤:
①打开取模软件,打开“设置”,将字模选项按下图所示进行配置。
②导入处理好的黑白像素图片,单击图上的小方块可对像素图进行调整,调整完成后点击“生成字模”即可得到图像的字模数据。
③将该字模数据复制到OLED_Data.c文件中,仿照测试图像的字模数据格式进行修改,接着在OLED_Data.h文件中对该图像的字模数组使用extern关键字声明。
四、深入认识OLED屏的显示原理
1、SSD1306简介
(1)SSD1306是一款OLED/PLED点阵显示屏的控制器,可以嵌入在屏幕中,用于执行接收数据、显示存储、扫描刷新等任务。
(2)SSD1306的驱动接口为128个SEG引脚和64个COM引脚,对应128*64像素点阵显示屏,内置显示存储器(GDDRAM)128*64 bit(128*8 Byte)SRAM,其容量大小同样对应128*64像素点阵显示屏。
(3)SSD1306的供电:VDD=1.65~3.3V(IC逻辑),VCC=7~15V(面板驱动)。
(4)SSD1306的通信接口:8位6800/8080并行接口(使用较少,后面不会介绍)、3/4线SPI接口、I2C接口。
2、SSD1306框图及引脚定义
(1)SSD1306框图的框图如下所示。
①单片机发送给OlED的命令和数据等都由SSD1306的引脚负责接收,通信都是以一个字节为单位的,SSD1306接收到一个字节后可以指定它是作为数据还是命令。
[1]若作为命令,字节将进入MCU下方的命令译码器,译码器根据命令控制SSD1306的内部电路,主要操作有开启显示、关闭显示、设置对比度、设置显示起始位置等(参考命令表即可知道每字节的命令对应什么操作)。
[2]若作为数据,字节将进入MCU右侧的图形显示数据RAM存储器(GDDRAM),该存储器中的内容决定屏幕上显示什么图形。
②Oscillator是振荡器,用于提供刷新屏幕的时钟(因为存储器的内容每时每刻可能都在变化,需要有一个固定的周期将其中的内容更新到屏幕上)。
(2)SSD1306的引脚说明:
引脚 | 功能 |
VDD、VCC、 VSS、VLSS | 供电 VDD=1.65~3.3V,VCC=7~15V(可不接) |
D0~D7 | 6800/8080:8位双向数据总线 3/4线SPI:D0为SCLK,D1为SDIN I2C:D0为SCL,D1为SDAin,D2为SDAout |
BS0~BS2 | 选择通信接口 |
R/W#(WR#) | 6800:R/W#,指定读/写操作 8080:WR#,写使能 |
E(RD#) | 6800:E,读/写使能 8080:RD#,读使能 |
D/C# | 6800/8080/4线SPI:指定传输数据/指令 I2C:SA0,指定I2C从机地址最低位 |
CS# | 片选(低电平选中) |
RES# | 复位 |
(3)通信线接口选择及通信线定义:
3、模块原理图
(1)4针脚I2C接口模块原理图:
(2)7针脚SPI接口模块原理图:
4、通信线接口
(1)4线SPI接口:(建议先学习后面介绍SPI的章节)
①串行模式下只能写不能读,故只规定了写命令和写数据两种操作,引脚E和引脚R/W#不使用,始终接低电平,CS置低电平表示选中芯片。
②D/C#置低电平,表示写命令;D/C#置高电平,表示写数据。
③数据线SDIN进行连续的字节传输,SCLK下降沿主机(单片机)变换数据,SCLK上升沿从机(SSD1306)读取数据线SDIN上的位数据,以此往复8次完成1字节的数据传输(先发送高位数据,再发送低位数据)。
(2)3线SPI接口:(建议先学习后面介绍SPI的章节)
①串行模式下只能写不能读,故只规定了写命令和写数据两种操作,引脚E和引脚R/W#不使用,始终接低电平,CS置低电平表示选中芯片。
②在第一个时钟脉冲下数据线SDIN置低电平,表示写命令;在第一个时钟脉冲下数据线SDIN置高电平,表示写数据。
③在第一个时钟脉冲下选择模式后,数据线SDIN进行连续的字节传输,SCLK下降沿主机(单片机)变换数据,SCLK上升沿从机(SSD1306)读取数据线SDIN上的位数据,以此往复8次完成1字节的数据传输(先发送高位数据,再发送低位数据)。
④一字节的数据传输完成后,重复②③的过程,进行下一个字节的数据传输。
(3)I2C接口:(建议先学习后面介绍I2C的章节)
①串行模式下只能写不能读,故只规定了写命令和写数据两种操作。
②首先构造起始条件,然后主机发送从机设备地址(SSD1306的从机地址为0x011110x,其中x可由引脚SA0配置),并一同发送读/写标识(由于只能进行写操作,故这位固定为0),从机根据这个地址判断主机是否需要与自己通信,如果这个地址是该从机的地址,主机要接收到该从机的应答信号(ACK)。
③I2C接口也没有独立的D/C#引脚,为了区分主机发送的字节是命令还是数据,需要在每字节发送之前发送一个控制字节(Control byte),控制字节的具体内容如上图所示。其高位是连续模式选择位,置为1时主机每发送一个字节前都要发送一个控制字节(比较麻烦,不常用),置为0时主机仅发送一个控制字节即可,后面的字节数据都以先前发送的控制字节进行命令和数据的区分;其次高位正是用于指定后续的字节是命令还是数据,置为1时表示要发送数据字节,置为0时表示要发送命令字节。
④数据发送结束后,构造结束条件,完成一次通信。
5、执行逻辑框图
(1)GDDRAM中的存储单元对应128×64像素点阵显示屏的每个像素,从横向看,坐标的取值范围为0~127,它对应列地址0~127,从纵向看,坐标的取值范围时0~63,但对于纵坐标,GDDRAM会将它们均分为8组,对应页地址0~7,因为通信时是以一个字节为单位的,所以GDDRAM的写操作也应以一个字节为单位(如上图所示,MSB为高位,LSB为低位),一次性写入8位数据。
(2)D/C#=0时,主机发送的是命令字节,SSD1306的译码器根据该命令做出相应的行为。
①命令可以由一个字节或者连续的多个字节组成。
②命令可分为基础命令、滚屏命令、寻址命令、硬件配置命令、时间及驱动命令5大类,具体可参考芯片手册。
(3)D/C#=1时,主机发送的是数据字节,SSD1306根据当前的寻址进行写GDDRAM操作,写完一字节后内部指针指向的列地址会自增1,当列地址溢出时会重新回到本页开头,或者自动换到下一页,这个需要由命令来配置寻址模式实现。
6、OLED屏的初始化过程
五、手搓OLED屏幕代码
1、不使用缓存区(UTF-8编码)
(1)以4针脚OLED屏为例,4针脚OLED屏使用的是I2C协议,需要使用后面介绍I2C时编写好的MyI2C.c和MyI2C.h文件,当然,其中的引脚需要修改。
①MyI2C.h文件:
#ifndef __MYI2C_H
#define __MYI2C_H
void MyI2C_Init(void);
void MyI2C_Start(void);
void MyI2C_Stop(void);
void MyI2C_SendByte(uint8_t Byte);
uint8_t MyI2C_ReceiveByte(void);
void MyI2C_SendAck(uint8_t AckBit);
uint8_t MyI2C_ReceiveAck(void);
#endif
②MyI2C.c文件:
#include "stm32f10x.h" // Device header
#include "Delay.h"
void MyI2C_W_SCL(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)BitValue);
}
void MyI2C_W_SDA(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)BitValue);
}
uint8_t MyI2C_R_SDA(void)
{
uint8_t BitValue;
BitValue = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9);
return BitValue;
}
void MyI2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
}
void MyI2C_Start(void)
{
MyI2C_W_SDA(1);
MyI2C_W_SCL(1);
MyI2C_W_SDA(0);
MyI2C_W_SCL(0);
}
void MyI2C_Stop(void)
{
MyI2C_W_SDA(0);
MyI2C_W_SCL(1);
MyI2C_W_SDA(1);
}
void MyI2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i ++)
{
MyI2C_W_SDA(Byte & (0x80 >> i));
MyI2C_W_SCL(1);
MyI2C_W_SCL(0);
}
}
uint8_t MyI2C_ReceiveByte(void)
{
uint8_t i, Byte = 0x00;
MyI2C_W_SDA(1);
for (i = 0; i < 8; i ++)
{
MyI2C_W_SCL(1);
if (MyI2C_R_SDA() == 1){Byte |= (0x80 >> i);}
MyI2C_W_SCL(0);
}
return Byte;
}
void MyI2C_SendAck(uint8_t AckBit)
{
MyI2C_W_SDA(AckBit);
MyI2C_W_SCL(1);
MyI2C_W_SCL(0);
}
uint8_t MyI2C_ReceiveAck(void)
{
uint8_t AckBit;
MyI2C_W_SDA(1);
MyI2C_W_SCL(1);
AckBit = MyI2C_R_SDA();
MyI2C_W_SCL(0);
return AckBit;
}
(2)手搓OLED.h和OLED.c文件。本例并未使用缓存区,也就是没有在单片机的RAM中备份GDDRAM的数据,每次写GDDRAM都只能以一个字节为单位,不能实现仅改变个别像素点显示的操作。另外在实际应用中,字模库应该另起OLED_Data.c和OLED_Data.h文件存放,这也是为了使项目的架构更加清晰。
①OLED.h文件:
#ifndef __OLED_H
#define __OLED_H
void OLED_WriteCommand(uint8_t Command);
void OLED_WriteData(uint8_t Data);
void OLED_Init(void);
void OLED_SetCursor(uint8_t X, uint8_t Page);
void OLED_Clear(void);
void OLED_ShowChar(uint8_t X, uint8_t Page, char Char, uint8_t FontSize);
void OLED_ShowString(uint8_t X, uint8_t Page, char *String, uint8_t FontSize);
void OLED_ShowImage(uint8_t X, uint8_t Page, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_ShowChinese(uint8_t X, uint8_t Page, char *Chinese);
#endif
②OLED.c文件:
#include "stm32f10x.h" // Device header
#include "MyI2C.h"
#include "Delay.h"
#include <string.h>
/*ASCII字模数据*********************/
/*宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16] =
{
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,0x16,0x0E,0x00,0x16,0x0E,0x00,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
0x00,0x00,0x00,0x16,0x0E,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,
0x00,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,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x80,0xB0,0x70,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,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
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,0x04,0x08,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
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
0x00,0x20,0x3F,0x21,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,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
};
/*宽6像素,高8像素*/
const uint8_t OLED_F6x8[][6] =
{
0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
};
/*********************ASCII字模数据*/
typedef struct {
char Index[4]; //汉字索引(字符数组,一个汉字的编码至少大于1个字节)
uint8_t Data[32]; //字模数据
} ChineseCell_t;
const ChineseCell_t OLED_CF16x16[] = {
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,/*0*/
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,/*1*/
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,/*2*/
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,/*3*/
//...
"",
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
void OLED_WriteCommand(uint8_t Command)
{
MyI2C_Start(); //起始
MyI2C_SendByte(0x78); //寻址
MyI2C_ReceiveAck(); //接收应答
MyI2C_SendByte(0x00); //发送控制字节(不采用连续模式)
MyI2C_ReceiveAck(); //接收应答
MyI2C_SendByte(Command); //发送命令字节
MyI2C_ReceiveAck(); //接收应答
MyI2C_Stop(); //结束
}
void OLED_WriteData(uint8_t Data)
{
MyI2C_Start(); //起始
MyI2C_SendByte(0x78); //寻址
MyI2C_ReceiveAck(); //接收应答
MyI2C_SendByte(0x40); //发送控制字节(不采用连续模式)
MyI2C_ReceiveAck(); //接收应答
MyI2C_SendByte(Data); //发送数据字节
MyI2C_ReceiveAck(); //接收应答
MyI2C_Stop(); //结束
}
void OLED_Init(void)
{
MyI2C_Init(); //初始化用于I2C通信的单片机引脚
Delay_ms(100);
/*根据初始化流程图进行命令字节的发送*/
OLED_WriteCommand(0xAE);
OLED_WriteCommand(0xD5);
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8);
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3);
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40);
OLED_WriteCommand(0xA1);
OLED_WriteCommand(0xC8);
OLED_WriteCommand(0xDA);
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81);
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9);
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB);
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4);
OLED_WriteCommand(0xA6);
OLED_WriteCommand(0x8D);
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF);
Delay_ms(100);
}
void OLED_SetCursor(uint8_t X, uint8_t Page) //设置光标位置(GDDRAM内部指针指向的地址)
{
OLED_WriteCommand(0x00 | (X & 0x0F)); //命令高四位为0000时,低四位表示列地址低位
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4)); //命令高四位为0001时,低四位表示列地址高位
OLED_WriteCommand(0xB0 | Page); //命令码高五位为10110时,低三位表示页地址
}
void OLED_Clear(void) //清屏(让OLED屏不显示任何内容)
{
for (uint8_t j = 0; j < 8; j ++)
{
OLED_SetCursor(0, j); //从每一页的第0列开始
for (uint8_t i = 0; i < 128; i ++)
{
OLED_WriteData(0x00); //逐列清零(每写1字节,GDDRAM的内部指针自增1,自动指向下一列)
}
}
}
void OLED_ShowChar(uint8_t X, uint8_t Page, char Char, uint8_t FontSize) //显示一个字符
{
if (FontSize == 6) //如果显示字符的宽为6
{
OLED_SetCursor(X, Page); //设置光标位置
for (uint8_t i = 0; i < 6; i ++) //字宽为6,需要写6列(列地址会自增)
{
OLED_WriteData(OLED_F6x8[Char - ' '][i]); //字符Char对应的ASCII码与其在数组OLED_F6x8中对应的下标有偏移
}
}
else if (FontSize == 8) //如果显示字符的宽为8
{
OLED_SetCursor(X, Page); //设置光标位置
for (uint8_t i = 0; i < 8; i ++) //字宽为8,需要写8列;字高为16,需要分两页写
{
OLED_WriteData(OLED_F8x16[Char - ' '][i]); //字符Char对应的ASCII码与其在数组OLED_F8x16中对应的下标有偏移
}
OLED_SetCursor(X, Page + 1);
for (uint8_t i = 0; i < 8; i ++) //字宽为8,需要写8列;字高为16,需要分两页写
{
OLED_WriteData(OLED_F8x16[Char - ' '][i + 8]); //字符Char对应的ASCII码与其在数组OLED_F8x16中对应的下标有偏移
}
}
}
void OLED_ShowString(uint8_t X, uint8_t Page, char *String, uint8_t FontSize) //显示字符串
{
for (uint8_t i = 0; String[i] != '\0'; i ++) //函数参数中有字符串显示的首地址,每显示一个字符,下一个字符的首地址偏移一个字宽
{
OLED_ShowChar(X + i * FontSize, Page, String[i], FontSize);
}
}
void OLED_ShowImage(uint8_t X, uint8_t Page, uint8_t Width, uint8_t Height, const uint8_t *Image) //显示图像
{
for (uint8_t j = 0; j < Height; j ++) //显示Height页像素,逐页进行显示
{
OLED_SetCursor(X, Page + j);
for (uint8_t i = 0; i < Width; i ++) //在一页中逐列显示像素,共显示Width列
{
OLED_WriteData(Image[Width * j + i]); //Width * j 是j页数据在Image中的偏移量
}
}
}
void OLED_ShowChinese(uint8_t X, uint8_t Page, char *Chinese) //显示汉字字符串
{
char SigleChinese[4] = {0}; //用于存储一个汉字的字符数组(UTF-8编码中,除了个别生僻字,大多数汉字都由3个字节编码,不算上结束符)
uint8_t pChinese = 0; //SigleChinese数组的下标指针
uint8_t pIndex; //汉字字模库数组的下标指针(不是汉字索引)
for (uint8_t i = 0; Chinese[i] != '\0'; i ++) //遍历字符串
{
SigleChinese[pChinese] = Chinese[i]; //将当前遍历的汉字存于SigleChinese
pChinese ++;
if (pChinese >= 3) //SigleChinese中有3个字节的数据,说明已经“复制”了一个汉字,开始处理
{
pChinese = 0; //指针归零
for (pIndex = 0; strcmp(OLED_CF16x16[pIndex].Index, "") != 0; pIndex ++) //将提取的汉字逐一与汉字字模库数组的汉字索引比较,找到目标汉字在字模库数组中的下标
{
if (strcmp(OLED_CF16x16[pIndex].Index, SigleChinese) == 0)
{
break; //找到目标汉字在字模库数组中的下标,结束匹配
}
}
OLED_ShowImage(X + ((i + 1) / 3 - 1) * 16, Page, 16, 2, OLED_CF16x16[pIndex].Data); //将汉字字模以图像的形式显示
}
}
}
(3)将下面的代码复制到main.c文件中,然后进行调试。
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
int main(void)
{
OLED_Init();
OLED_Clear();
OLED_ShowChinese(10, 2, "你好世界啊");
while (1)
{
;
}
}
2、使用缓存区(UTF-8编码)
(1)以4针脚OLED屏为例,4针脚OLED屏使用的是I2C协议,需要使用后面介绍I2C时编写好的MyI2C.c和MyI2C.h文件,当然,其中的引脚需要修改。
①MyI2C.h文件:
#ifndef __MYI2C_H
#define __MYI2C_H
void MyI2C_Init(void);
void MyI2C_Start(void);
void MyI2C_Stop(void);
void MyI2C_SendByte(uint8_t Byte);
uint8_t MyI2C_ReceiveByte(void);
void MyI2C_SendAck(uint8_t AckBit);
uint8_t MyI2C_ReceiveAck(void);
#endif
②MyI2C.c文件:
#include "stm32f10x.h" // Device header
#include "Delay.h"
void MyI2C_W_SCL(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)BitValue);
}
void MyI2C_W_SDA(uint8_t BitValue)
{
GPIO_WriteBit(GPIOB, GPIO_Pin_9, (BitAction)BitValue);
}
uint8_t MyI2C_R_SDA(void)
{
uint8_t BitValue;
BitValue = GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_9);
return BitValue;
}
void MyI2C_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_SetBits(GPIOB, GPIO_Pin_8 | GPIO_Pin_9);
}
void MyI2C_Start(void)
{
MyI2C_W_SDA(1);
MyI2C_W_SCL(1);
MyI2C_W_SDA(0);
MyI2C_W_SCL(0);
}
void MyI2C_Stop(void)
{
MyI2C_W_SDA(0);
MyI2C_W_SCL(1);
MyI2C_W_SDA(1);
}
void MyI2C_SendByte(uint8_t Byte)
{
uint8_t i;
for (i = 0; i < 8; i ++)
{
MyI2C_W_SDA(Byte & (0x80 >> i));
MyI2C_W_SCL(1);
MyI2C_W_SCL(0);
}
}
uint8_t MyI2C_ReceiveByte(void)
{
uint8_t i, Byte = 0x00;
MyI2C_W_SDA(1);
for (i = 0; i < 8; i ++)
{
MyI2C_W_SCL(1);
if (MyI2C_R_SDA() == 1){Byte |= (0x80 >> i);}
MyI2C_W_SCL(0);
}
return Byte;
}
void MyI2C_SendAck(uint8_t AckBit)
{
MyI2C_W_SDA(AckBit);
MyI2C_W_SCL(1);
MyI2C_W_SCL(0);
}
uint8_t MyI2C_ReceiveAck(void)
{
uint8_t AckBit;
MyI2C_W_SDA(1);
MyI2C_W_SCL(1);
AckBit = MyI2C_R_SDA();
MyI2C_W_SCL(0);
return AckBit;
}
(2)手搓OLED.h和OLED.c文件。本例使用了缓存区,也就是在单片机的RAM中备份GDDRAM的数据,程序可以改变RAM中备份的显存数据,这个操作是可以以位数据为单位的,把备份的显存数据一次性写入GDDRAM,由此可以实现不以字节为单位改变GDDRAM的存储内容。另外在实际应用中,字模库应该另起OLED_Data.c和OLED_Data.h文件存放,这也是为了使项目的架构更加清晰。
①OLED.h文件:
#ifndef __OLED_H
#define __OLED_H
void OLED_WriteCommand(uint8_t Command);
void OLED_WriteData(uint8_t *Data, uint8_t Count);
void OLED_Init(void);
void OLED_SetCursor(uint8_t X, uint8_t Page);
void OLED_Update(void);
void OLED_Clear(void);
void OLED_ClearArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height);
void OLED_ShowChar(uint8_t X, uint8_t Y, char Char, uint8_t FontSize);
void OLED_ShowString(uint8_t X, uint8_t Y, char *String, uint8_t FontSize);
void OLED_ShowImage(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image);
void OLED_ShowChinese(uint8_t X, uint8_t Y, char *Chinese);
void OLED_DrawPoint(uint8_t X, uint8_t Y);
uint8_t OLED_GetPoint(uint8_t X, uint8_t Y);
void OLED_DrawLine(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1);
#endif
②OLED.c文件:
#include "stm32f10x.h" // Device header
#include "MyI2C.h"
#include "Delay.h"
#include "OLED.h"
#include <string.h>
#include <math.h>
/*ASCII字模数据*********************/
/*宽8像素,高16像素*/
const uint8_t OLED_F8x16[][16] =
{
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,0x16,0x0E,0x00,0x16,0x0E,0x00,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
0x00,0x00,0x00,0x16,0x0E,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,
0x00,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,0xC0,0xC0,0x00,0x00,0x00,
0x00,0x00,0x80,0xB0,0x70,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,0x20,0x10,0x08,0x04,0x08,0x10,0x20,
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,0x04,0x08,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
0x00,0x80,0x80,0x00,0x80,0x80,0x00,0x00,
0x00,0x20,0x3F,0x21,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,0x80,0x40,0x40,0x80,0x00,0x00,0x80,
0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x00,// ~ 94
};
/*宽6像素,高8像素*/
const uint8_t OLED_F6x8[][6] =
{
0x00,0x00,0x00,0x00,0x00,0x00,// 0
0x00,0x00,0x00,0x2F,0x00,0x00,// ! 1
0x00,0x00,0x07,0x00,0x07,0x00,// " 2
0x00,0x14,0x7F,0x14,0x7F,0x14,// # 3
0x00,0x24,0x2A,0x7F,0x2A,0x12,// $ 4
0x00,0x23,0x13,0x08,0x64,0x62,// % 5
0x00,0x36,0x49,0x55,0x22,0x50,// & 6
0x00,0x00,0x00,0x07,0x00,0x00,// ' 7
0x00,0x00,0x1C,0x22,0x41,0x00,// ( 8
0x00,0x00,0x41,0x22,0x1C,0x00,// ) 9
0x00,0x14,0x08,0x3E,0x08,0x14,// * 10
0x00,0x08,0x08,0x3E,0x08,0x08,// + 11
0x00,0x00,0x00,0xA0,0x60,0x00,// , 12
0x00,0x08,0x08,0x08,0x08,0x08,// - 13
0x00,0x00,0x60,0x60,0x00,0x00,// . 14
0x00,0x20,0x10,0x08,0x04,0x02,// / 15
0x00,0x3E,0x51,0x49,0x45,0x3E,// 0 16
0x00,0x00,0x42,0x7F,0x40,0x00,// 1 17
0x00,0x42,0x61,0x51,0x49,0x46,// 2 18
0x00,0x21,0x41,0x45,0x4B,0x31,// 3 19
0x00,0x18,0x14,0x12,0x7F,0x10,// 4 20
0x00,0x27,0x45,0x45,0x45,0x39,// 5 21
0x00,0x3C,0x4A,0x49,0x49,0x30,// 6 22
0x00,0x01,0x71,0x09,0x05,0x03,// 7 23
0x00,0x36,0x49,0x49,0x49,0x36,// 8 24
0x00,0x06,0x49,0x49,0x29,0x1E,// 9 25
0x00,0x00,0x36,0x36,0x00,0x00,// : 26
0x00,0x00,0x56,0x36,0x00,0x00,// ; 27
0x00,0x08,0x14,0x22,0x41,0x00,// < 28
0x00,0x14,0x14,0x14,0x14,0x14,// = 29
0x00,0x00,0x41,0x22,0x14,0x08,// > 30
0x00,0x02,0x01,0x51,0x09,0x06,// ? 31
0x00,0x3E,0x49,0x55,0x59,0x2E,// @ 32
0x00,0x7C,0x12,0x11,0x12,0x7C,// A 33
0x00,0x7F,0x49,0x49,0x49,0x36,// B 34
0x00,0x3E,0x41,0x41,0x41,0x22,// C 35
0x00,0x7F,0x41,0x41,0x22,0x1C,// D 36
0x00,0x7F,0x49,0x49,0x49,0x41,// E 37
0x00,0x7F,0x09,0x09,0x09,0x01,// F 38
0x00,0x3E,0x41,0x49,0x49,0x7A,// G 39
0x00,0x7F,0x08,0x08,0x08,0x7F,// H 40
0x00,0x00,0x41,0x7F,0x41,0x00,// I 41
0x00,0x20,0x40,0x41,0x3F,0x01,// J 42
0x00,0x7F,0x08,0x14,0x22,0x41,// K 43
0x00,0x7F,0x40,0x40,0x40,0x40,// L 44
0x00,0x7F,0x02,0x0C,0x02,0x7F,// M 45
0x00,0x7F,0x04,0x08,0x10,0x7F,// N 46
0x00,0x3E,0x41,0x41,0x41,0x3E,// O 47
0x00,0x7F,0x09,0x09,0x09,0x06,// P 48
0x00,0x3E,0x41,0x51,0x21,0x5E,// Q 49
0x00,0x7F,0x09,0x19,0x29,0x46,// R 50
0x00,0x46,0x49,0x49,0x49,0x31,// S 51
0x00,0x01,0x01,0x7F,0x01,0x01,// T 52
0x00,0x3F,0x40,0x40,0x40,0x3F,// U 53
0x00,0x1F,0x20,0x40,0x20,0x1F,// V 54
0x00,0x3F,0x40,0x38,0x40,0x3F,// W 55
0x00,0x63,0x14,0x08,0x14,0x63,// X 56
0x00,0x07,0x08,0x70,0x08,0x07,// Y 57
0x00,0x61,0x51,0x49,0x45,0x43,// Z 58
0x00,0x00,0x7F,0x41,0x41,0x00,// [ 59
0x00,0x02,0x04,0x08,0x10,0x20,// \ 60
0x00,0x00,0x41,0x41,0x7F,0x00,// ] 61
0x00,0x04,0x02,0x01,0x02,0x04,// ^ 62
0x00,0x40,0x40,0x40,0x40,0x40,// _ 63
0x00,0x00,0x01,0x02,0x04,0x00,// ` 64
0x00,0x20,0x54,0x54,0x54,0x78,// a 65
0x00,0x7F,0x48,0x44,0x44,0x38,// b 66
0x00,0x38,0x44,0x44,0x44,0x20,// c 67
0x00,0x38,0x44,0x44,0x48,0x7F,// d 68
0x00,0x38,0x54,0x54,0x54,0x18,// e 69
0x00,0x08,0x7E,0x09,0x01,0x02,// f 70
0x00,0x18,0xA4,0xA4,0xA4,0x7C,// g 71
0x00,0x7F,0x08,0x04,0x04,0x78,// h 72
0x00,0x00,0x44,0x7D,0x40,0x00,// i 73
0x00,0x40,0x80,0x84,0x7D,0x00,// j 74
0x00,0x7F,0x10,0x28,0x44,0x00,// k 75
0x00,0x00,0x41,0x7F,0x40,0x00,// l 76
0x00,0x7C,0x04,0x18,0x04,0x78,// m 77
0x00,0x7C,0x08,0x04,0x04,0x78,// n 78
0x00,0x38,0x44,0x44,0x44,0x38,// o 79
0x00,0xFC,0x24,0x24,0x24,0x18,// p 80
0x00,0x18,0x24,0x24,0x18,0xFC,// q 81
0x00,0x7C,0x08,0x04,0x04,0x08,// r 82
0x00,0x48,0x54,0x54,0x54,0x20,// s 83
0x00,0x04,0x3F,0x44,0x40,0x20,// t 84
0x00,0x3C,0x40,0x40,0x20,0x7C,// u 85
0x00,0x1C,0x20,0x40,0x20,0x1C,// v 86
0x00,0x3C,0x40,0x30,0x40,0x3C,// w 87
0x00,0x44,0x28,0x10,0x28,0x44,// x 88
0x00,0x1C,0xA0,0xA0,0xA0,0x7C,// y 89
0x00,0x44,0x64,0x54,0x4C,0x44,// z 90
0x00,0x00,0x08,0x7F,0x41,0x00,// { 91
0x00,0x00,0x00,0x7F,0x00,0x00,// | 92
0x00,0x00,0x41,0x7F,0x08,0x00,// } 93
0x00,0x08,0x04,0x08,0x10,0x08,// ~ 94
};
/*********************ASCII字模数据*/
typedef struct {
char Index[4];
uint8_t Data[32];
} ChineseCell_t;
const ChineseCell_t OLED_CF16x16[] = {
"你",
0x00,0x80,0x60,0xF8,0x07,0x40,0x20,0x18,0x0F,0x08,0xC8,0x08,0x08,0x28,0x18,0x00,
0x01,0x00,0x00,0xFF,0x00,0x10,0x0C,0x03,0x40,0x80,0x7F,0x00,0x01,0x06,0x18,0x00,/*0*/
"好",
0x10,0x10,0xF0,0x1F,0x10,0xF0,0x00,0x80,0x82,0x82,0xE2,0x92,0x8A,0x86,0x80,0x00,
0x40,0x22,0x15,0x08,0x16,0x61,0x00,0x00,0x40,0x80,0x7F,0x00,0x00,0x00,0x00,0x00,/*1*/
"世",
0x20,0x20,0x20,0xFE,0x20,0x20,0xFF,0x20,0x20,0x20,0xFF,0x20,0x20,0x20,0x20,0x00,
0x00,0x00,0x00,0x7F,0x40,0x40,0x47,0x44,0x44,0x44,0x47,0x40,0x40,0x40,0x00,0x00,/*2*/
"界",
0x00,0x00,0x00,0xFE,0x92,0x92,0x92,0xFE,0x92,0x92,0x92,0xFE,0x00,0x00,0x00,0x00,
0x08,0x08,0x04,0x84,0x62,0x1E,0x01,0x00,0x01,0xFE,0x02,0x04,0x04,0x08,0x08,0x00,/*3*/
//...
"",
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
};
//测试图像(笑脸)
uint8_t Img[] = {
0xFF,0x01,0xE1,0x11,0x49,0x25,0x45,0x05,0x45,0x25,0x49,0x11,0xE1,0x01,0xFF,
0x7F,0x40,0x43,0x44,0x48,0x51,0x52,0x52,0x52,0x51,0x48,0x44,0x43,0x40,0x7F};
uint8_t OLED_DisplayBuf[8][128]; //显存数组(缓存区),备份GDDRAM的数据
void OLED_WriteCommand(uint8_t Command)
{
MyI2C_Start();
MyI2C_SendByte(0x78); //寻址
MyI2C_ReceiveAck();
MyI2C_SendByte(0x00); //Control Byte
MyI2C_ReceiveAck();
MyI2C_SendByte(Command); //Data Byte
MyI2C_ReceiveAck();
MyI2C_Stop();
}
void OLED_WriteData(uint8_t *Data, uint8_t Count)
{
MyI2C_Start();
MyI2C_SendByte(0x78); //寻址
MyI2C_ReceiveAck();
MyI2C_SendByte(0x40); //Control Byte
MyI2C_ReceiveAck();
for (uint8_t i = 0; i < Count; i ++) //连续写入Count个数据字节(列地址会自增)
{
MyI2C_SendByte(Data[i]); //Data Byte
MyI2C_ReceiveAck();
}
MyI2C_Stop();
}
void OLED_Init(void)
{
MyI2C_Init();
Delay_ms(100);
OLED_WriteCommand(0xAE);
OLED_WriteCommand(0xD5);
OLED_WriteCommand(0x80);
OLED_WriteCommand(0xA8);
OLED_WriteCommand(0x3F);
OLED_WriteCommand(0xD3);
OLED_WriteCommand(0x00);
OLED_WriteCommand(0x40);
OLED_WriteCommand(0xA1);
OLED_WriteCommand(0xC8);
OLED_WriteCommand(0xDA);
OLED_WriteCommand(0x12);
OLED_WriteCommand(0x81);
OLED_WriteCommand(0xCF);
OLED_WriteCommand(0xD9);
OLED_WriteCommand(0xF1);
OLED_WriteCommand(0xDB);
OLED_WriteCommand(0x30);
OLED_WriteCommand(0xA4);
OLED_WriteCommand(0xA6);
OLED_WriteCommand(0x8D);
OLED_WriteCommand(0x14);
OLED_WriteCommand(0xAF);
Delay_ms(100);
}
void OLED_SetCursor(uint8_t X, uint8_t Page)
{
OLED_WriteCommand(0x00 | (X & 0x0F));
OLED_WriteCommand(0x10 | ((X & 0xF0) >> 4));
OLED_WriteCommand(0xB0 | Page);
}
void OLED_Update(void) //将STM32的RAM中备份的GDDRAM数据更新到GDDRAM中
{
for (uint8_t j = 0; j < 8; j ++) //8页的GDDRAM逐页进行写入,从第一页开始
{
OLED_SetCursor(0, j);
OLED_WriteData(OLED_DisplayBuf[j], 128); //一口气写128列数据
}
}
void OLED_Clear(void) //清空显示(调用该函数后需要调用OLED_Update函数才能真正地清除显示,后面与显示相关的函数同理)
{
for (uint8_t j = 0; j < 8; j ++)
{
for (uint8_t i = 0; i < 128; i ++)
{
OLED_DisplayBuf[j][i] = 0x00; //显存数组全部清零
}
}
}
void OLED_ClearArea(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height) //清除局部的显示
{
for (uint8_t j = Y; j < Y + Height; j ++)
{
for (uint8_t i = X; i < X + Width; i ++)
{
//坐标(i, j) = 0
OLED_DisplayBuf[j / 8][i] &= ~(0x01 << (j % 8));
}
}
}
void OLED_ShowChar(uint8_t X, uint8_t Y, char Char, uint8_t FontSize) //显示一个字符
{
if (FontSize == 6)
{
OLED_ShowImage(X, Y, 6, 8, OLED_F6x8[Char - ' ']);
}
else if (FontSize == 8)
{
OLED_ShowImage(X, Y, 8, 16, OLED_F8x16[Char - ' ']);
}
}
void OLED_ShowString(uint8_t X, uint8_t Y, char *String, uint8_t FontSize) //显示字符串
{
for (uint8_t i = 0; String[i] != '\0'; i ++)
{
OLED_ShowChar(X + i * FontSize, Y, String[i], FontSize);
}
}
void OLED_ShowImage(uint8_t X, uint8_t Y, uint8_t Width, uint8_t Height, const uint8_t *Image) //显示图像
{
OLED_ClearArea(X, Y, Width, Height); //清空预显示区域
for (uint8_t j = 0; j < (Height - 1) / 8 + 1; j ++) //根据图像的数据及显示位置更新显存数组
{
for (uint8_t i = 0; i < Width; i ++)
{
OLED_DisplayBuf[Y / 8 + j][X + i] |= Image[j * Width + i] << (Y % 8);
OLED_DisplayBuf[Y / 8 + j + 1][X + i] |= Image[j * Width + i] >> (8 - Y % 8);
}
}
}
void OLED_ShowChinese(uint8_t X, uint8_t Y, char *Chinese) //显示汉字字符串
{
char SigleChinese[4] = {0};
uint8_t pChinese = 0;
uint8_t pIndex;
for (uint8_t i = 0; Chinese[i] != '\0'; i ++)
{
SigleChinese[pChinese] = Chinese[i];
pChinese ++;
if (pChinese >= 3)
{
pChinese = 0;
for (pIndex = 0; strcmp(OLED_CF16x16[pIndex].Index, "") != 0; pIndex ++)
{
if (strcmp(OLED_CF16x16[pIndex].Index, SigleChinese) == 0)
{
break;
}
}
OLED_ShowImage(X + ((i + 1) / 3 - 1) * 16, Y, 16, 16, OLED_CF16x16[pIndex].Data);
}
}
}
void OLED_DrawPoint(uint8_t X, uint8_t Y) //显示一个像素点
{
OLED_DisplayBuf[Y / 8][X] |= 0x01 << (Y % 8);
}
uint8_t OLED_GetPoint(uint8_t X, uint8_t Y) //返回屏幕坐标(X,Y)上是否显示像素点(根据显存数组判断)
{
if (OLED_DisplayBuf[Y / 8][X] & 0x01 << (Y % 8))
{
return 1; //屏幕坐标(X,Y)上有显示像素点,返回1
}
return 0; //屏幕坐标(X,Y)上没有显示像素点,返回0
}
void OLED_DrawLine(uint8_t X0, uint8_t Y0, uint8_t X1, uint8_t Y1) //显示线段(未考虑执行效率)
{
float k = (float)(Y1 - Y0) / (X1 - X0); //计算线段在坐标系上的斜率
uint8_t temp;
if (fabs(k) < 1) //如果斜率绝对值小于1
{
if (X0 > X1) //统一让X1大于X0
{
temp = X0;
X0 = X1;
X1 = temp;
temp = Y0;
Y0 = Y1;
Y1 = temp;
}
for (uint8_t x = X0; x <= X1; x ++) //根据X坐标计算Y
{
OLED_DrawPoint(x, round(Y0 + (x - X0) * k));
}
}
else //如果斜率绝对值大于1
{
if (Y0 > Y1) //统一让Y1大于Y0
{
temp = X0;
X0 = X1;
X1 = temp;
temp = Y0;
Y0 = Y1;
Y1 = temp;
}
for (uint8_t y = Y0; y <= Y1; y ++) //根据Y坐标计算X
{
OLED_DrawPoint(round(X0 + (y - Y0) / k), y);
}
}
}
(3)将下面的代码复制到main.c文件中,然后进行调试。
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "OLED.h"
#include <math.h>
int main(void)
{
OLED_Init();
OLED_Clear();
for (uint16_t i = 0; i < 360; i += 20)
{
OLED_DrawLine(64, 32, 64 + 30 * cos(i / 360.0 * 2 * 3.14), 32 + 30 * sin(i / 360.0 * 2 * 3.14));
}
OLED_Update();
while (1)
{
;
}
}