1.介绍
????OLED是一种使用LED发光的显示设备,尺寸0.96寸,分辨率128X64,使用IIC接口进行通信,OLED厂家除了提供说明手册以外,也提供参考源码,参考使用IIC控制器来实现的。? ?
2.硬件连接
3.IIC控制器编程
????添加IIC库函数源码:
?—————————————————————————————————————————————————————————————————————————————————————
(1)开启时钟
开启GPIO时钟和 IIC 的时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
(2)将GPIO配置为IIC复用功能
GPIO_Init(...);? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?//初始化
GPIO_PinAFConfig(...);? ? ? ? ? ? ? ? ? ? ?//映射
(3)IIC初始化
void?I2C_Init(I2C_TypeDef*?I2Cx,?I2C_InitTypeDef*?I2C_InitStruct);
参数: ??
??I2Cx?-?哪路IIC ???
?I2C_InitStruct?-?初始化结构 ????
typedef?struct
{ ?
?uint32_t?I2C_ClockSpeed;??????????/*!<?时钟速度??100?~?400kHz?*/ ?
uint16_t?I2C_Mode;????????????????/*!<?I2C模式?@ref?I2C_mode?*/
uint16_t?I2C_DutyCycle;???????????/*!<?高低电平比例?@ref?I2C_duty_cycle_in_fast_mode?*/ uint16_t?I2C_OwnAddress1;?????????/*!<?自己的地址,从设备时使用 */ ??
uint16_t?I2C_Ack;?????????????????/*!<?ACK使能?@ref?I2C_acknowledgement?*/ uint16_t?I2C_AcknowledgedAddress;?/*!<?从设置地址位数?@ref?I2C_acknowledged_address?*/
}I2C_InitTypeDef;???
(4)如果需要接收IIC数据开启应答使能
void?I2C_AcknowledgeConfig(I2C_TypeDef*?I2Cx,?FunctionalState?NewState);
(5)使能IIC
I2C_Cmd(I2C1,ENABLE);
(6)IIC通信
??1)查询总线是否繁忙
I2C_GetFlagStatus(I2C1,?I2C_FLAG_BUSY);
??2)起始信号
void?I2C_GenerateSTART(I2C_TypeDef*?I2Cx,?FunctionalState?NewState);
??3)设置好对应的模式
I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_MODE_SELECT);
当前IIC是作为主设备使用,因此参数应该选择?I2C_EVENT_MASTER_MODE_SELECT这个标志位
??4)设置从设备地址和读写信号
I2C_Send7bitAddress(I2C1,?/*从设备地址*/,?I2C_Direction_Transmitter);//发送 I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED);//主设备发送模式
??5)发送和接收数据
void?I2C_SendData(I2C_TypeDef*?I2Cx,?uint8_t?Data); I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_TRANSMITTED);//等待发送完成标志 I2C_CheckEvent(I2C1,?I2C_EVENT_MASTER_BYTE_RECEIVED); uint8_t?I2C_ReceiveData(I2C_TypeDef*?I2Cx);
??6)停止信号
void?I2C_GenerateSTOP(I2C_TypeDef*?I2Cx,?FunctionalState?NewState);
4.移植OLED
(1)初始化
I2C_Configuration(); OLED_Init();
(2)显示
//全屏填充
void?OLED_Fill(unsigned?char?fill_Data)
//显示ascii字符 void?OLED_ShowStr(unsigned?char?x,?unsigned?char?y,?unsigned?char?ch[],?unsigned?char?TextSize);
//显示汉字
void?OLED_ShowCN(unsigned?char?x,?unsigned?char?y,?unsigned?char?N);? ?
5.制作字模
(1)打开字模软件(分享的STM32资料中)
?需要修改一些配置,如下图所示
?(2)生成字模
只要能用输入法输出的字都能生成字模
(3)将生成的字模拷贝到代码中使用
(4)制作图片图模
????原始图片的大小必须是128X64,必须是黑白图片,最后就像制作字模一样得到图片的数据。
? ? //I2C通信协议实现在OLED显示屏数据显示
? ? //以下来自厂家定义的函数包含I2C初始化,OLED初始化,寄存器实现I2C通信,显示汉字,图片,全屏填充等的函数封装
/************************************************************************************
* Copyright (c), 2014, HelTec Automatic Technology co.,LTD.
* All rights reserved.
*
* Http: www.heltec.cn
* Email: cn.heltec@gmail.com
* WebShop: heltec.taobao.com
*
* File name: OLED_I2C.c
* Project : HelTec.uvprij
* Processor: STM32F103C8T6
* Compiler : MDK fo ARM
*
* Author : 小林
* Version: 1.00
* Date : 2014.4.8
* Email : hello14blog@gmail.com
* Modification: none
*
* Description:128*64点阵的OLED显示屏驱动文件,仅适用于惠特自动化(heltec.taobao.com)的SD1306驱动IIC通信方式显示屏
*
* Others: none;
*
* Function List:
* 1. void I2C_Configuration(void) -- 配置CPU的硬件I2C
* 2. void I2C_WriteByte(uint8_t addr,uint8_t data) -- 向寄存器地址写一个byte的数据
* 3. void WriteCmd(unsigned char I2C_Command) -- 写命令
* 4. void WriteDat(unsigned char I2C_Data) -- 写数据
* 5. void OLED_Init(void) -- OLED屏初始化
* 6. void OLED_SetPos(unsigned char x, unsigned char y) -- 设置起始点坐标
* 7. void OLED_Fill(unsigned char fill_Data) -- 全屏填充
* 8. void OLED_CLS(void) -- 清屏
* 9. void OLED_ON(void) -- 唤醒
* 10. void OLED_OFF(void) -- 睡眠
* 11. void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize) -- 显示字符串(字体大小有6*8和8*16两种)
* 12. void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N) -- 显示中文(中文需要先取模,然后放到codetab.h中)
* 13. void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]) -- BMP图片
*
* History: none;
*
*************************************************************************************/
#include "OLED_I2C.h"
#include "delay.h"
#include "codetab.h"
void I2C_Configuration(void)
{
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
/*STM32F407ZET6芯片的硬件I2C: PB8 -- SCL; PB9 -- SDA */
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//复用模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;//I2C复用必须设置为开漏
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//高速
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;//无上下拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource8,GPIO_AF_I2C1);
GPIO_PinAFConfig(GPIOB,GPIO_PinSource9,GPIO_AF_I2C1);
I2C_DeInit(I2C1);//使用I2C1
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;//主机的I2C地址,随便写的
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 400000;//400K
I2C_Init(I2C1, &I2C_InitStructure);
I2C_Cmd(I2C1, ENABLE);
}
void I2C_WriteByte(uint8_t addr,uint8_t data)
{
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1, ENABLE);//开启I2C1
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_MODE_SELECT));/*EV5,主模式*/
I2C_Send7bitAddress(I2C1, OLED_ADDRESS, I2C_Direction_Transmitter);//器件地址 -- 默认0x78
while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1, addr);//寄存器地址
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1, data);//发送数据
while (!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);//关闭I2C1总线
}
void WriteCmd(unsigned char I2C_Command)//写命令
{
I2C_WriteByte(0x00, I2C_Command);
}
void WriteDat(unsigned char I2C_Data)//写数据
{
I2C_WriteByte(0x40, I2C_Data);
}
void OLED_Init(void)
{
delay_ms(100); //这里的延时很重要
WriteCmd(0xAE); //display off
WriteCmd(0x20); //Set Memory Addressing Mode
WriteCmd(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid
WriteCmd(0xb0); //Set Page Start Address for Page Addressing Mode,0-7
WriteCmd(0xc8); //Set COM Output Scan Direction
WriteCmd(0x00); //---set low column address
WriteCmd(0x10); //---set high column address
WriteCmd(0x40); //--set start line address
WriteCmd(0x81); //--set contrast control register
WriteCmd(0xff); //亮度调节 0x00~0xff
WriteCmd(0xa1); //--set segment re-map 0 to 127
WriteCmd(0xa6); //--set normal display
WriteCmd(0xa8); //--set multiplex ratio(1 to 64)
WriteCmd(0x3F); //
WriteCmd(0xa4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content
WriteCmd(0xd3); //-set display offset
WriteCmd(0x00); //-not offset
WriteCmd(0xd5); //--set display clock divide ratio/oscillator frequency
WriteCmd(0xf0); //--set divide ratio
WriteCmd(0xd9); //--set pre-charge period
WriteCmd(0x22); //
WriteCmd(0xda); //--set com pins hardware configuration
WriteCmd(0x12);
WriteCmd(0xdb); //--set vcomh
WriteCmd(0x20); //0x20,0.77xVcc
WriteCmd(0x8d); //--set DC-DC enable
WriteCmd(0x14); //
WriteCmd(0xaf); //--turn on oled panel
}
void OLED_SetPos(unsigned char x, unsigned char y) //设置起始点坐标
{
WriteCmd(0xb0+y);
WriteCmd(((x&0xf0)>>4)|0x10);
WriteCmd((x&0x0f)|0x01);
}
void OLED_Fill(unsigned char fill_Data)//全屏填充
{
unsigned char m,n;
for(m=0;m<8;m++)
{
WriteCmd(0xb0+m); //page0-page1
WriteCmd(0x00); //low column start address
WriteCmd(0x10); //high column start address
for(n=0;n<128;n++)
{
WriteDat(fill_Data);
}
}
}
void OLED_CLS(void)//清屏
{
OLED_Fill(0x00);
}
//--------------------------------------------------------------
// Prototype : void OLED_ON(void)
// Calls :
// Parameters : none
// Description : 将OLED从休眠中唤醒
//--------------------------------------------------------------
void OLED_ON(void)
{
WriteCmd(0X8D); //设置电荷泵
WriteCmd(0X14); //开启电荷泵
WriteCmd(0XAF); //OLED唤醒
}
//--------------------------------------------------------------
// Prototype : void OLED_OFF(void)
// Calls :
// Parameters : none
// Description : 让OLED休眠 -- 休眠模式下,OLED功耗不到10uA
//--------------------------------------------------------------
void OLED_OFF(void)
{
WriteCmd(0X8D); //设置电荷泵
WriteCmd(0X10); //关闭电荷泵
WriteCmd(0XAE); //OLED休眠
}
//--------------------------------------------------------------
// Prototype : void OLED_ShowChar(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
// Calls :
// Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); ch[] -- 要显示的字符串; TextSize -- 字符大小(1:6*8 ; 2:8*16)
// Description : 显示codetab.h中的ASCII字符,有6*8和8*16可选择
//--------------------------------------------------------------
void OLED_ShowStr(unsigned char x, unsigned char y, unsigned char ch[], unsigned char TextSize)
{
unsigned char c = 0,i = 0,j = 0;
switch(TextSize)
{
case 1:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 126)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<6;i++)
WriteDat(F6x8[c][i]);
x += 6;
j++;
}
}break;
case 2:
{
while(ch[j] != '\0')
{
c = ch[j] - 32;
if(x > 120)
{
x = 0;
y++;
}
OLED_SetPos(x,y);
for(i=0;i<8;i++)
WriteDat(F8X16[c*16+i]);
OLED_SetPos(x,y+1);
for(i=0;i<8;i++)
WriteDat(F8X16[c*16+i+8]);
x += 8;
j++;
}
}break;
}
}
//--------------------------------------------------------------
// Prototype : void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
// Calls :
// Parameters : x,y -- 起始点坐标(x:0~127, y:0~7); N:汉字在codetab.h中的索引
// Description : 显示codetab.h中的汉字,16*16点阵
//--------------------------------------------------------------
void OLED_ShowCN(unsigned char x, unsigned char y, unsigned char N)
{
unsigned char wm=0;
unsigned int adder=32*N;
OLED_SetPos(x , y);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16[adder]);
adder += 1;
}
OLED_SetPos(x,y + 1);
for(wm = 0;wm < 16;wm++)
{
WriteDat(F16x16[adder]);
adder += 1;
}
}
//--------------------------------------------------------------
// Prototype : void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[]);
// Calls :
// Parameters : x0,y0 -- 起始点坐标(x0:0~127, y0:0~7); x1,y1 -- 起点对角线(结束点)的坐标(x1:1~128,y1:1~8)
// Description : 显示BMP位图
//--------------------------------------------------------------
void OLED_DrawBMP(unsigned char x0,unsigned char y0,unsigned char x1,unsigned char y1,unsigned char BMP[])
{
unsigned int j=0;
unsigned char x,y;
if(y1%8==0)
y = y1/8;
else
y = y1/8 + 1;
for(y=y0;y<y1;y++)
{
OLED_SetPos(x0,y);
for(x=x0;x<x1;x++)
{
WriteDat(BMP[j++]);
}
}
}
|