OLED根据数据手册显示一条直线

本文详细介绍了如何使用SSD1306 OLED屏进行初始化设置,包括I2C通信协议和数据格式,以及如何通过写入命令和数据来显示一条直线。重点讲解了内存地址设置、列地址选择和数据RAM操作。

OLED根据数据手册显示一条直线

因为OLED屏所用的芯片是SSD1306,SSD1306芯片数据手册篇幅较长,主要看与OLED屏设置有关的部分,也可看经过整理翻译过来的中文手册,专门翻译OLED部分

OLED初始化

OLED屏的初始化比较复杂,数据手册有给出,直接在main函数中调用即可

/**
  * @brief OLED初始化,数据手册已给出
  * @param 无
  * @retval无
  */
void OLED_Init()
{
   
   
	OLED_WritCommand(0xAE);
	OLED_WritCommand(0x00);
	OLED_WritCommand(0x10);
	OLED_WritCommand(0x40);
	OLED_WritCommand(0xB0);
	OLED_WritCommand(0x81);
	OLED_WritCommand(0xFF);
	OLED_WritCommand(0xA1);
	OLED_WritCommand(0xA6);
	OLED_WritCommand(0xA8);
	OLED_WritCommand(0x3F);
	OLED_WritCommand(0xC8);
	OLED_WritCommand(0xD3);
	OLED_WritCommand(0x00);
	OLED_WritCommand(0xD5);
	OLED_WritCommand(0x80);
	OLED_WritCommand(0xD8);
	OLED_WritCommand(0x05);
	OLED_WritCommand(0xD9);
	OLED_WritCommand
Pin Number Symbol I/O Function P Po o we er r S Su up pp pl ly y 9 VDD P P Po o we er r S Su up pp pl ly y f fo or r L Lo og gi ic c This is a voltage supply pin. It must be connected to external source. 8 VSS P G Gr ro ou un nd d o of f L Lo og gi ic c C Ci ir rc cu ui it t This is a ground pin. It acts as a reference for the logic pins. It must be connected to external ground. 28 VCC P P Po o we er r S Su up pp pl ly y f fo or r OE EL L P Pa an ne el l This is the most positive voltage supply pin of the chip. A stabilization capacitor should be connected between this pin and V SS when the converter is used. It must be connected to external source when the converter is not used. 29 VLSS P G Gr ro ou un nd d o of f A An na al lo og g C Ci ir rc cu ui it t This is an analog ground pin. It should be connected to V SS externally. D Dr ri iv ve er r 26 IREF I C Cu ur rr re en nt t R Re ef fe er re en nc ce e f fo or r B Br ri ig gh ht tn ne es ss s A Ad dj ju us st t me en nt t This pin is segment current reference pin. A resistor should be connected between this pin and V SS . Set the current at 12.5μA maximum. 27 VCOMH O V Vo ol lt ta ag ge e Ou ut tp pu ut t Hi ig gh h L Le ev ve el l f fo or r C C O M S Si ig gn na al l This pin is the input pin for the voltage output high level for COM signals. A capacitor should be connected between this pin and V SS . D DC C/ /D DC C C Co on nv ve er rt te er r 6 VDDB P P Po o we er r S Su up pp pl ly y f fo or r DC C/ / DC C C Co on nv ve er rt te er r C Ci ir rc cu ui it t This is the power supply pin for the internal buffer of the DC/DC voltage converter. It must be connected to external source when the converter is used. It should be connected to V DD when the converter is not used. 4 / 5 2 / 3 C1P / C1N C2P / C2N I P Po os si it ti iv ve e T Te er r mi in na al l o of f t th he e F Fl ly yi in ng g I In nv ve er rt ti in ng g C Ca ap pa ac ci it to or r Ne eg ga at ti iv ve e T Te er r mi in na al l o of f t th he e F Fl ly yi in ng g B Bo oo os st t C Ca ap pa ac ci it to or r The charge-pump capacitors are required between the terminals. They must be floated when the converter is not used. I In nt te er rf fa ac ce e 10 11 12 BS0 BS1 BS2 I C Co o m mu un ni ic ca at ti in ng g P Pr ro ot to oc co ol l S Se el le ec ct t These pins are MCU interface selection input. See the following table: BS0 BS1 BS2 I 2 C 0 1 0 3-wire SPI 1 0 0 4-wire SPI 0 0 0 8-bit 68XX Parallel 0 0 1 8-bit 80XX Parallel 0 1 1 14 RES# I P Po o we er r R Re es se et t f fo or r C Co on nt tr ro ol ll le er r a an nd d Dr ri iv ve er r This pin is reset signal input. When the pin is low, initialization of the chip is executed. Keep this pin pull high during normal operation. 13 CS# I C Ch hi ip p S Se el le ec ct t This pin is the chip select input. The chip is enabled for MCU communication only when CS# is pulled low. 15 D/C# I Da at ta a/ /C Co o m ma an nd d C Co on nt tr ro ol l This pin is Data/Command control pin. When the pin is pulled high, the input at D7~D0 is treated as display data. When the pin is pulled low, the input at D7~D0 will be transferred to the command register. When the pin is pulled high and serial interface mode is selected, the data at SDIN will be interpreted as data. When it is pulled low, the data at SDIN will be transferred to the command register. In I 2 C mode, this pin acts as SA0 for slave address selection. For detail relationship to MCU interface signals, please refer to the Timing Characteristics Diagrams. 17 E/RD# I R Re ea ad d/ / Wr ri it te e E En na ab bl le e o or r R Re ea ad d This pin is MCU interface input. When interfacing to a 68XX-series microprocessor, this pin will be used as the Enable (E) signal. Read/write operation is initiated when this pin is pulled high and the CS# is pulled low. When connecting to an 80XX-microprocessor, this pin receives the Read (RD#) signal. Data read operation is initiated when this pin is pulled low and CS# is pulled low. When serial or I 2 C mode is selected, this pin must be connected to V SS . GoldenMorning Electronic 4 1.5 Pin Definition (Continued) Pin Number Symbol I/O Function I In nt te er rf fa ac ce e ( (C Co on nt ti in nu ue ed d) ) 16 R/W# I R Re ea ad d/ / Wr ri it te e S Se el le ec ct t o or r Wr ri it te e This pin is MCU interface input. When interfacing to a 68XX-series microprocessor, this pin will be used as Read/Write (R/W#) selection input. Pull this pin to “High” for read mode and pull it to “Low” for write mode. When 80XX interface mode is selected, this pin will be the Write (WR#) input. Data write operation is initiated when this pin is pulled low and the CS# is pulled low. When serial or I 2 C mode is selected, this pin must be connected to V SS . 18~25 D0~D7 I/O Ho os st t Da at ta a I In np pu ut t/ / Ou ut tp pu ut t B Bu us s These pins are 8-bit bi-directional data bus to be connected to the microprocessor’s data bus. When serial mode is selected, D1 will be the serial data input SDIN and D0 will be the serial clock input SCLK. When I 2 C mode is selected, D2 & D1 should be tired together and serve as SDA out & SDA in in application and D0 is the serial clock input SCL. Unused pins must be connected to V SS except for D2 in serial mode. R Re es se er rv ve e 7 N.C. - R Re es se er rv ve ed d P Pi in n The N.C. pin between function pins are reserved for compatible and flexible design. 1, 30 N.C. (GND) - R Re es se er rv ve ed d P Pi in n ( (S Su up pp po or rt ti in ng g P Pi in n) ) The supporting pins can reduce the influences from stresses on the function pins. These pins must be connected to external ground as the ESD protection circuit.
在Keil5环境下使用OLED显示一条直线,通常需要借助特定的OLED驱动库,以下以常见的I2C接口的128x64 OLED显示屏为例,介绍具体实现方法。 ### 1. 硬件连接 将OLED的SDA(数据)和SCL(时钟)引脚连接到单片机对应的I2C接口引脚,同时连接好电源和地。 ### 2. 引入必要的头文件和定义 需要包含相关的头文件,如`reg52.h`,并定义OLED的相关参数和引脚。 ```c #include <reg52.h> #include <intrins.h> // 定义OLED相关引脚 sbit SDA = P2^0; sbit SCL = P2^1; // 定义OLED的一些常量 #define OLED_ADDRESS 0x78 #define OLED_CMD 0x00 #define OLED_DATA 0x40 ``` ### 3. 实现I2C通信函数 实现I2C的起始、停止、发送字节等基本操作函数。 ```c // I2C起始信号 void I2C_Start() { SDA = 1; SCL = 1; _nop_(); SDA = 0; _nop_(); SCL = 0; } // I2C停止信号 void I2C_Stop() { SDA = 0; SCL = 1; _nop_(); SDA = 1; _nop_(); } // I2C发送一个字节 void I2C_SendByte(unsigned char dat) { unsigned char i; for (i = 0; i < 8; i++) { SDA = (dat & 0x80) >> 7; dat <<= 1; SCL = 1; _nop_(); SCL = 0; } // 等待应答 SDA = 1; SCL = 1; _nop_(); SCL = 0; } // 向OLED发送命令 void OLED_SendCmd(unsigned char cmd) { I2C_Start(); I2C_SendByte(OLED_ADDRESS); I2C_SendByte(OLED_CMD); I2C_SendByte(cmd); I2C_Stop(); } // 向OLED发送数据 void OLED_SendData(unsigned char dat) { I2C_Start(); I2C_SendByte(OLED_ADDRESS); I2C_SendByte(OLED_DATA); I2C_SendByte(dat); I2C_Stop(); } ``` ### 4. 初始化OLEDOLED进行初始化设置。 ```c // OLED初始化 void OLED_Init() { OLED_SendCmd(0xAE); // 关闭显示 OLED_SendCmd(0x20); // 设置内存地址模式 OLED_SendCmd(0x10); // 水平寻址模式 OLED_SendCmd(0xB0); // 设置页地址 OLED_SendCmd(0xC8); // 设置COM扫描方向 OLED_SendCmd(0x00); // 设置低列地址 OLED_SendCmd(0x10); // 设置高列地址 OLED_SendCmd(0x40); // 设置显示开始行 OLED_SendCmd(0x81); // 设置对比度 OLED_SendCmd(0xFF); // 对比度值 OLED_SendCmd(0xA1); // 设置段重映射 OLED_SendCmd(0xA6); // 正常显示 OLED_SendCmd(0xA8); // 设置多路复用率 OLED_SendCmd(0x3F); // 复用率值 OLED_SendCmd(0xA4); // 全局显示开启 OLED_SendCmd(0xD3); // 设置显示偏移 OLED_SendCmd(0x00); // 偏移量 OLED_SendCmd(0xD5); // 设置显示时钟分频比/振荡器频率 OLED_SendCmd(0x80); // 分频比/频率值 OLED_SendCmd(0xD9); // 设置预充电周期 OLED_SendCmd(0xF1); // 预充电周期值 OLED_SendCmd(0xDA); // 设置COM引脚硬件配置 OLED_SendCmd(0x12); // 配置值 OLED_SendCmd(0xDB); // 设置VCOMH取消选择级别 OLED_SendCmd(0x40); // 取消选择级别值 OLED_SendCmd(0x8D); // 设置充电泵 OLED_SendCmd(0x14); // 开启充电泵 OLED_SendCmd(0xAF); // 开启显示 } ``` ### 5. 实现显示直线的函数 根据直线的起点和终点坐标,在OLED显示直线。 ```c // 显示一条直线 void OLED_DrawLine(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2) { unsigned char i; if (y1 == y2) { // 水平线 unsigned char page = y1 / 8; unsigned char start_col = x1; unsigned char end_col = x2; OLED_SendCmd(0xB0 + page); // 设置页地址 OLED_SendCmd(0x00 + (start_col & 0x0F)); // 设置低列地址 OLED_SendCmd(0x10 + ((start_col >> 4) & 0x0F)); // 设置高列地址 for (i = start_col; i <= end_col; i++) { OLED_SendData(0xFF); } } // 这里可以扩展实现垂直线和斜线的绘制 } ``` ### 6. 主函数调用 在主函数中初始化OLED并调用显示直线的函数。 ```c void main() { OLED_Init(); OLED_DrawLine(10, 20, 50, 20); // 显示一条从(10, 20)到(50, 20)的水平线 while (1); } ``` ### 总结 通过以上步骤,就可以在Keil5环境下使用OLED显示一条直线。需要注意的是,上述代码只是一个基本示例,实际应用中可能需要根据具体的OLED型号和硬件连接进行适当调整。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值