IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 嵌入式 -> STM32利用I2C驱动SSD1306代码 -> 正文阅读

[嵌入式]STM32利用I2C驱动SSD1306代码

前言:

之前购买了一款SSD1306驱动的0.96inchOLED显示屏幕,采用ESP8266使用u8g2库成功驱动。如今想自己摸索一下如何利用I2C驱动SSD1306.

I2C协议

I2C开始
SCL为高电平期间,SDA由高到低

void IIC_START()//IIC_SCL高电平时候 IIC_SDA拉低
{
	IIC_OUT();
	
	IIC_SCL_H;
	IIC_SDA_H;
	delay_us(1);
	IIC_SDA_L;
	delay_us(3);
	IIC_SCL_L;//低电平才允许 IIC_SDA变化
}

I2C停止
SCL为高电平期间,SDA由低到高

void IIC_STOP()//IIC_SCL高电平时候 IIC_SDA升高
{
	IIC_OUT();
	
	IIC_SCL_L;
	IIC_SDA_L;
	delay_us(1);
	IIC_SCL_H;
	delay_us(3);
	IIC_SDA_H;
	delay_us(3);
}

I2C发送ACK

void IIC_ACK()//SCL为高电平时候 IIC_SDA为低电平
{
    IIC_SCL_L;
    IIC_OUT();
    IIC_SDA_L;
    delay_us(3);
    IIC_SCL_H;
    delay_us(3);
    IIC_SCL_L;
}

I2C发送NACK

void IIC_NACK()
{
	IIC_SCL_L;
    IIC_OUT();
    IIC_SDA_H;
    delay_us(3);
    IIC_SCL_H;
    delay_us(3);
    IIC_SCL_L;
}

I2C等待ACK

u8 IIC_WAIT_ACK(void)
{
	int time;
	IIC_IN();
	IIC_SDA_H;//释放SDA
	delay_us(3);
	IIC_SCL_H;
	delay_us(3);
	while(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)   //等待应答信号
	{
		time++;
		if(time>2500){IIC_STOP();return 0;}
	}
	IIC_SCL_L;
	return 1;
}

I2C发送一个bit

void IIC_SEND_BIT(u8 bit)
{
		IIC_OUT();
		IIC_SCL_L;
		if(bit){
			IIC_SDA_H;
		}else{
			IIC_SDA_L;
		}
		IIC_SCL_H;
		delay_us(3);
		IIC_SCL_L;
}

I2C发送一个字节

void IIC_SEND_BYTE(u8 byte)
{
		u8 i=0;
		for(i=0;i<8;i++){
			IIC_SEND_BIT(byte>>(7-i)&0x01);
		}
		IIC_SDA_H;
		delay_us(1);
		IIC_SCL_H;
		delay_us(3);
		IIC_SCL_L;
}

I2C接受一个字节

u8 IIC_RECEIVE_BYTE(u8 ack)
{
		u8 temp,i;
		IIC_IN();
		for(i=0;i<8;i++)
		{
			IIC_SCL_L; 
			delay_us(1);
			IIC_SCL_H;
			temp<<=1;
			if(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)temp&=0x01;   
			delay_us(3); 
		}					 
		if(!ack)
			 IIC_NACK();        //发送nACK
		else
			 IIC_ACK();         //发送ACK   
		return temp;
}

SSD1306

初始化指令

const unsigned char  OLED_init_cmd[25]=
{
  /*0xae,0X00,0X10,0x40,0X81,0XCF,0xff,0xa1,0xa4,
  0xA6,0xc8,0xa8,0x3F,0xd5,0x80,0xd3,0x00,0XDA,0X12,
  0x8d,0x14,0xdb,0x40,0X20,0X02,0xd9,0xf1,0xAF*/
      0xAE,//关闭显示
     
      0xD5,//设置时钟分频因子,震荡频率
      0x80,  //[3:0],分频因子;[7:4],震荡频率
      
      0xA8,//设置驱动路数
      0X3F,//默认0X3F(1/64)
      
      0xD3,//设置显示偏移
      0X00,//默认为0
      0x40,//设置显示开始行 [5:0],行数.    
                                
      0x8D,//电荷泵设置
      0x14,//bit2,开启/关闭
      
      0x20,//设置内存地址模式
      0x02,//[1:0],00,列地址模式   01,行地址模式   10,页地址模式;默认10;
      
      0xA1,//段重定义设置,bit0:0,0->0;1,0->127;  左右反转
  	  0xC8,//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数 上下反转
   
      0xDA,//设置COM硬件引脚配置
      0x12,//[5:4]配置
                  
      0x81,//对比度设置
      0xEF,//1~255;默认0X7F (亮度设置,越大越亮)
      
      0xD9,//设置预充电周期
      0xf1,//[3:0],PHASE 1;[7:4],PHASE 2;
      
      0xDB,//设置VCOMH 电压倍率
      0x30,//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
      
      0xA4,//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
      0xA6,//设置显示方式;bit0:1,反相显示;0,正常显示        
      0xAF,//开启显示     
};
  1. SSD1306发送指令
void OLED_send_cmd(unsigned char cmd)
{
	IIC_START();
	IIC_SEND_BYTE(OLED_ADR);
    IIC_SEND_BYTE(0x00);//   DC位为0 表示是cmd
    IIC_SEND_BYTE(cmd);
    IIC_STOP();	
}
  1. SSD1306发送数据
void OLED_send_data(unsigned char data)
{
	IIC_START();
	IIC_SEND_BYTE(OLED_ADR);
    IIC_SEND_BYTE(0x40);//   DC位为1 表示是data
    IIC_SEND_BYTE(data);
    IIC_STOP();	
}
  1. SSD1306设置GDDRAM模式
void OLED_set_gddram_mode(u8 mode)//0x00 水平地址  0x01垂直地址  0x02页地址
{
	OLED_send_cmd(0x20);
	OLED_send_cmd(mode);
}
  1. SSD1306设置对比度
void OLED_set_contrast(unsigned char constrast)
{
	OLED_send_cmd(0x81);
	OLED_send_cmd(constrast);
}
  1. 水平模式 垂直模式的页寻址和列寻址
void Column_set_VR(u8 Column_Begin,u8 Column_End)
{
	if(Column_Begin>127||Column_End>127||Column_Begin>Column_End)return;
	OLED_send_cmd(0x21);
	OLED_send_cmd(Column_Begin); 
	OLED_send_cmd(Column_End); 
}
void Page_set_VR(u8 Page_Begin,u8 Page_End)
{
	if(Page_Begin>7||Page_End>127||Page_Begin>Page_End)return;
	OLED_send_cmd(0x22);
	OLED_send_cmd(Page_Begin); 
	OLED_send_cmd(Page_End); 
}
  1. 页模式的页寻址以及列寻址
void Column_set(unsigned char column)//列地址
{
	OLED_send_cmd(0x10|(column>>4));    //设置列地址高位
	OLED_send_cmd(0x00|(column&0x0f));   //设置列地址低位         	 
}
void Page_set(unsigned char page)//页地址
{
    OLED_send_cmd(0xB0+page);
}

SSD1306驱动源代码

#include "ssd1306.h"
#include "iic.h"
#include "stm32f10x.h"
#include "font.h"
#define OLED_ADR 0X78

/*阴码 逐列式 顺向*/
const unsigned char ascii[][16]={
{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*" ",0*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xCC,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"!",1*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x08,0x00,0x30,0x00,0x40,0x00,0x08,0x00,0x30,0x00,0x40,0x00,0x00,0x00},/*""",2*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x02,0x20,0x03,0xFC,0x1E,0x20,0x02,0x20,0x03,0xFC,0x1E,0x20,0x00,0x00},/*"#",3*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0E,0x18,0x11,0x04,0x11,0x04,0x3F,0xFF,0x10,0x84,0x0C,0x78,0x00,0x00},/*"$",4*/
/* (8 X 16 , 宋体 ) */

{0x0F,0x00,0x10,0x8C,0x0F,0x30,0x01,0xC0,0x06,0x78,0x18,0x84,0x00,0x78,0x00,0x00},/*"%",5*/
/* (8 X 16 , 宋体 ) */

{0x00,0x78,0x0F,0x84,0x10,0xC4,0x11,0x34,0x0E,0x98,0x00,0xE4,0x00,0x84,0x00,0x08},/*"&",6*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x48,0x00,0x70,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"'",7*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x18,0x18,0x20,0x04,0x40,0x02,0x00,0x00},/*"(",8*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x40,0x02,0x20,0x04,0x18,0x18,0x07,0xE0,0x00,0x00,0x00,0x00,0x00,0x00},/*")",9*/
/* (8 X 16 , 宋体 ) */

{0x02,0x40,0x02,0x40,0x01,0x80,0x0F,0xF0,0x01,0x80,0x02,0x40,0x02,0x40,0x00,0x00},/*"*",10*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x07,0xF0,0x00,0x80,0x00,0x80,0x00,0x80},/*"+",11*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x09,0x00,0x0E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*",",12*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x00},/*"-",13*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x0C,0x00,0x0C,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*".",14*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x06,0x00,0x18,0x00,0xE0,0x03,0x00,0x1C,0x00,0x20,0x00,0x00,0x00},/*"/",15*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"0",16*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00},/*"1",17*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0E,0x0C,0x10,0x14,0x10,0x24,0x10,0x44,0x10,0x84,0x0F,0x0C,0x00,0x00},/*"2",18*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0C,0x18,0x10,0x04,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},/*"3",19*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x60,0x01,0xA0,0x02,0x24,0x0C,0x24,0x1F,0xFC,0x00,0x24,0x00,0x24},/*"4",20*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x1F,0x98,0x11,0x04,0x11,0x04,0x11,0x04,0x10,0x88,0x10,0x70,0x00,0x00},/*"5",21*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x07,0xF0,0x08,0x88,0x11,0x04,0x11,0x04,0x09,0x04,0x00,0xF8,0x00,0x00},/*"6",22*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x18,0x00,0x10,0x00,0x10,0x7C,0x11,0x80,0x16,0x00,0x18,0x00,0x00,0x00},/*"7",23*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0E,0x38,0x11,0x44,0x10,0x84,0x10,0x84,0x11,0x44,0x0E,0x38,0x00,0x00},/*"8",24*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0F,0x80,0x10,0x48,0x10,0x44,0x10,0x44,0x08,0x88,0x07,0xF0,0x00,0x00},/*"9",25*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x03,0x0C,0x03,0x0C,0x00,0x00,0x00,0x00,0x00,0x00},/*":",26*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x07,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*";",27*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x80,0x01,0x40,0x02,0x20,0x04,0x10,0x08,0x08,0x10,0x04,0x00,0x00},/*"<",28*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x02,0x40,0x00,0x00},/*"=",29*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x10,0x04,0x08,0x08,0x04,0x10,0x02,0x20,0x01,0x40,0x00,0x80,0x00,0x00},/*">",30*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0E,0x00,0x12,0x00,0x10,0x0C,0x10,0xEC,0x11,0x00,0x0E,0x00,0x00,0x00},/*"?",31*/
/* (8 X 16 , 宋体 ) */

{0x03,0xE0,0x0C,0x18,0x13,0xE4,0x14,0x14,0x17,0xF4,0x08,0x14,0x07,0xE8,0x00,0x00},/*"@",32*/
/* (8 X 16 , 宋体 ) */

{0x00,0x04,0x00,0x3C,0x03,0xC4,0x1C,0x40,0x07,0x40,0x00,0xE4,0x00,0x1C,0x00,0x04},/*"A",33*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x11,0x04,0x0E,0x88,0x00,0x70,0x00,0x00},/*"B",34*/
/* (8 X 16 , 宋体 ) */

{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x04,0x10,0x08,0x1C,0x10,0x00,0x00},/*"C",35*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"D",36*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x04,0x17,0xC4,0x10,0x04,0x08,0x18,0x00,0x00},/*"E",37*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x17,0xC0,0x10,0x00,0x08,0x00,0x00,0x00},/*"F",38*/
/* (8 X 16 , 宋体 ) */

{0x03,0xE0,0x0C,0x18,0x10,0x04,0x10,0x04,0x10,0x44,0x1C,0x78,0x00,0x40,0x00,0x00},/*"G",39*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x10,0x84,0x00,0x80,0x00,0x80,0x10,0x84,0x1F,0xFC,0x10,0x04},/*"H",40*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x10,0x04,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x04,0x00,0x00,0x00,0x00},/*"I",41*/
/* (8 X 16 , 宋体 ) */

{0x00,0x03,0x00,0x01,0x10,0x01,0x10,0x01,0x1F,0xFE,0x10,0x00,0x10,0x00,0x00,0x00},/*"J",42*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x11,0x04,0x03,0x80,0x14,0x64,0x18,0x1C,0x10,0x04,0x00,0x00},/*"K",43*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x10,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x0C,0x00,0x00},/*"L",44*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x1F,0x80,0x00,0x7C,0x1F,0x80,0x1F,0xFC,0x10,0x04,0x00,0x00},/*"M",45*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x0C,0x04,0x03,0x00,0x00,0xE0,0x10,0x18,0x1F,0xFC,0x10,0x00},/*"N",46*/
/* (8 X 16 , 宋体 ) */

{0x07,0xF0,0x08,0x08,0x10,0x04,0x10,0x04,0x10,0x04,0x08,0x08,0x07,0xF0,0x00,0x00},/*"O",47*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x10,0x84,0x10,0x80,0x10,0x80,0x10,0x80,0x0F,0x00,0x00,0x00},/*"P",48*/
/* (8 X 16 , 宋体 ) */

{0x07,0xF0,0x08,0x08,0x10,0x14,0x10,0x14,0x10,0x0C,0x08,0x0A,0x07,0xF2,0x00,0x00},/*"Q",49*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x1F,0xFC,0x11,0x04,0x11,0x00,0x11,0xC0,0x11,0x30,0x0E,0x0C,0x00,0x04},/*"R",50*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x0E,0x1C,0x11,0x04,0x10,0x84,0x10,0x84,0x10,0x44,0x1C,0x38,0x00,0x00},/*"S",51*/
/* (8 X 16 , 宋体 ) */

{0x18,0x00,0x10,0x00,0x10,0x04,0x1F,0xFC,0x10,0x04,0x10,0x00,0x18,0x00,0x00,0x00},/*"T",52*/
/* (8 X 16 , 宋体 ) */

{0x10,0x00,0x1F,0xF8,0x10,0x04,0x00,0x04,0x00,0x04,0x10,0x04,0x1F,0xF8,0x10,0x00},/*"U",53*/
/* (8 X 16 , 宋体 ) */

{0x10,0x00,0x1E,0x00,0x11,0xE0,0x00,0x1C,0x00,0x70,0x13,0x80,0x1C,0x00,0x10,0x00},/*"V",54*/
/* (8 X 16 , 宋体 ) */

{0x10,0x00,0x1F,0xC0,0x00,0x7C,0x1F,0x80,0x00,0x7C,0x1F,0xC0,0x10,0x00,0x00,0x00},/*"W",55*/
/* (8 X 16 , 宋体 ) */

{0x10,0x04,0x18,0x0C,0x16,0x34,0x01,0xC0,0x01,0xC0,0x16,0x34,0x18,0x0C,0x10,0x04},/*"X",56*/
/* (8 X 16 , 宋体 ) */

{0x10,0x00,0x1C,0x00,0x13,0x04,0x00,0xFC,0x13,0x04,0x1C,0x00,0x10,0x00,0x00,0x00},/*"Y",57*/
/* (8 X 16 , 宋体 ) */

{0x08,0x04,0x10,0x1C,0x10,0x64,0x10,0x84,0x13,0x04,0x1C,0x04,0x10,0x18,0x00,0x00},/*"Z",58*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xFE,0x40,0x02,0x40,0x02,0x40,0x02,0x00,0x00},/*"[",59*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x20,0x00,0x1C,0x00,0x03,0x80,0x00,0x60,0x00,0x1C,0x00,0x03,0x00,0x00},/*"\",60*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x40,0x02,0x40,0x02,0x40,0x02,0x7F,0xFE,0x00,0x00,0x00,0x00,0x00,0x00},/*"]",61*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x20,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00},/*"^",62*/
/* (8 X 16 , 宋体 ) */

{0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01},/*"_",63*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"`",64*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x98,0x01,0x24,0x01,0x24,0x01,0x48,0x00,0xFC,0x00,0x04,0x00,0x00},/*"a",65*/
/* (8 X 16 , 宋体 ) */

{0x08,0x00,0x0F,0xFC,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"b",66*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x00},/*"c",67*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x09,0x08,0x0F,0xFC,0x00,0x04},/*"d",68*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0xF8,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x00,0xE8,0x00,0x00},/*"e",69*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x01,0x04,0x01,0x04,0x07,0xFC,0x09,0x04,0x09,0x04,0x04,0x00,0x00,0x00},/*"f",70*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0xD6,0x01,0x29,0x01,0x29,0x01,0x29,0x01,0xC9,0x01,0x06,0x00,0x00},/*"g",71*/
/* (8 X 16 , 宋体 ) */

{0x08,0x04,0x0F,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"h",72*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x01,0x04,0x19,0x04,0x19,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"i",73*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x03,0x00,0x01,0x01,0x01,0x19,0x01,0x19,0xFE,0x00,0x00,0x00,0x00},/*"j",74*/
/* (8 X 16 , 宋体 ) */

{0x08,0x04,0x0F,0xFC,0x00,0x24,0x00,0x60,0x01,0x94,0x01,0x0C,0x01,0x04,0x00,0x00},/*"k",75*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x08,0x04,0x08,0x04,0x1F,0xFC,0x00,0x04,0x00,0x04,0x00,0x00,0x00,0x00},/*"l",76*/
/* (8 X 16 , 宋体 ) */

{0x01,0x04,0x01,0xFC,0x01,0x04,0x01,0x00,0x01,0xFC,0x01,0x04,0x01,0x00,0x00,0xFC},/*"m",77*/
/* (8 X 16 , 宋体 ) */

{0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x00,0x01,0x00,0x01,0x04,0x00,0xFC,0x00,0x04},/*"n",78*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0xF8,0x01,0x04,0x01,0x04,0x01,0x04,0x01,0x04,0x00,0xF8,0x00,0x00},/*"o",79*/
/* (8 X 16 , 宋体 ) */

{0x01,0x01,0x01,0xFF,0x00,0x89,0x01,0x04,0x01,0x04,0x00,0x88,0x00,0x70,0x00,0x00},/*"p",80*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x70,0x00,0x88,0x01,0x04,0x01,0x04,0x00,0x89,0x01,0xFF,0x00,0x01},/*"q",81*/
/* (8 X 16 , 宋体 ) */

{0x01,0x04,0x01,0x04,0x01,0xFC,0x00,0x84,0x01,0x04,0x01,0x00,0x01,0x80,0x00,0x00},/*"r",82*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0xCC,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x24,0x01,0x98,0x00,0x00},/*"s",83*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x01,0x00,0x01,0x00,0x07,0xF8,0x01,0x04,0x01,0x04,0x00,0x08,0x00,0x00},/*"t",84*/
/* (8 X 16 , 宋体 ) */

{0x01,0x00,0x01,0xF8,0x00,0x04,0x00,0x04,0x00,0x04,0x01,0x08,0x01,0xFC,0x00,0x04},/*"u",85*/
/* (8 X 16 , 宋体 ) */

{0x01,0x00,0x01,0xC0,0x01,0x30,0x00,0x0C,0x01,0x30,0x01,0xC0,0x01,0x00,0x00,0x00},/*"v",86*/
/* (8 X 16 , 宋体 ) */

{0x01,0x80,0x01,0x70,0x00,0x0C,0x01,0x30,0x01,0xE0,0x00,0x1C,0x01,0x60,0x01,0x80},/*"w",87*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x01,0x04,0x01,0x8C,0x01,0x70,0x00,0x74,0x01,0x8C,0x01,0x04,0x00,0x00},/*"x",88*/
/* (8 X 16 , 宋体 ) */

{0x01,0x00,0x01,0x81,0x01,0x61,0x00,0x1E,0x00,0x18,0x01,0x60,0x01,0x80,0x01,0x00},/*"y",89*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x01,0x84,0x01,0x0C,0x01,0x34,0x01,0x44,0x01,0x84,0x01,0x0C,0x00,0x00},/*"z",90*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x3F,0x7C,0x40,0x02,0x40,0x02},/*"{",91*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00},/*"|",92*/
/* (8 X 16 , 宋体 ) */

{0x40,0x02,0x40,0x02,0x3F,0x7C,0x00,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"}",93*/
/* (8 X 16 , 宋体 ) */

{0x00,0x00,0x40,0x00,0x80,0x00,0x40,0x00,0x40,0x00,0x20,0x00,0x40,0x00,0x00,0x00},/*"~",94*/
/* (8 X 16 , 宋体 ) */

}

unsigned char GDDRAM[8][128]={0};//最小位是在最前行
/* GDDRAM[0][0]
bit		0
		1
		2
		3
		4
		5
		6
		7
*/
const unsigned char  OLED_init_cmd[25]=
{
  /*0xae,0X00,0X10,0x40,0X81,0XCF,0xff,0xa1,0xa4,
  0xA6,0xc8,0xa8,0x3F,0xd5,0x80,0xd3,0x00,0XDA,0X12,
  0x8d,0x14,0xdb,0x40,0X20,0X02,0xd9,0xf1,0xAF*/
       0xAE,//关闭显示
       0xD5,//设置时钟分频因子,震荡频率
       0x80,  //[3:0],分频因子;[7:4],震荡频率

       0xA8,//设置驱动路数
       0X3F,//默认0X3F(1/64)
       0xD3,//设置显示偏移
       0X00,//默认为0
       0x40,//设置显示开始行 [5:0],行数.                              
       0x8D,//电荷泵设置
       0x14,//bit2,开启/关闭
       0x20,//设置内存地址模式
       0x02,//[1:0],00,列地址模式   01,行地址模式   10,页地址模式;默认10;
       0xA1,//段重定义设置,bit0:0,0->0;1,0->127;  左右反转
	   0xC8,//设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数 上下反转
       0xDA,//设置COM硬件引脚配置
       0x12,//[5:4]配置            
       0x81,//对比度设置
       0xEF,//1~255;默认0X7F (亮度设置,越大越亮)
       0xD9,//设置预充电周期
       0xf1,//[3:0],PHASE 1;[7:4],PHASE 2;
       0xDB,//设置VCOMH 电压倍率
       0x30,//[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
       0xA4,//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
       0xA6,//设置显示方式;bit0:1,反相显示;0,正常显示        
       0xAF,//开启显示     
};

void OLED_init(void)
{	
	unsigned char i;
	IIC_init();
    for(i=0;i<25;i++)
    {
      OLED_send_cmd(OLED_init_cmd[i]);
    }	
}
void OLED_set_gddram_mode(u8 mode)//0x00 水平地址  0x01垂直地址  0x02页地址
{
	OLED_send_cmd(0x20);
	OLED_send_cmd(mode);
}

//页模式寻址
void Column_set(unsigned char column)//列地址
{
	OLED_send_cmd(0x10|(column>>4));    //设置列地址高位
	OLED_send_cmd(0x00|(column&0x0f));   //设置列地址低位         	 
}
void Page_set(unsigned char page)//页地址
{
    OLED_send_cmd(0xB0+page);
}
//水平寻址 垂直寻址
void Column_set_VR(u8 Column_Begin,u8 Column_End)
{
//	if(Column_Begin>127||Column_End>127||Column_Begin>Column_End)return;
	OLED_send_cmd(0x21);
	OLED_send_cmd(Column_Begin); 
	OLED_send_cmd(Column_End); 
}
void Page_set_VR(u8 Page_Begin,u8 Page_End)
{
//	if(Page_Begin>7||Page_End>127||Page_Begin>Page_End)return;
	OLED_send_cmd(0x22);
	OLED_send_cmd(Page_Begin); 
	OLED_send_cmd(Page_End); 
}

void OLED_send_cmd(unsigned char cmd)
{
	IIC_START();
	IIC_SEND_BYTE(OLED_ADR);
    IIC_SEND_BYTE(0x00);//   DC位为0 表示是cmd
    IIC_SEND_BYTE(cmd);
    IIC_STOP();	
}
void OLED_send_data(unsigned char data)
{
	IIC_START();
	IIC_SEND_BYTE(OLED_ADR);
    IIC_SEND_BYTE(0x40);//   DC位为1 表示是data
    IIC_SEND_BYTE(data);
    IIC_STOP();	
}
void OLED_set_contrast(unsigned char constrast)
{
	OLED_send_cmd(0x81);
	OLED_send_cmd(constrast);
}
void OLED_full_OLED(void)
{
	 unsigned char page,column;
	 OLED_set_gddram_mode(0X02);
	 for(page=0;page<8;page++)//page loop
	 { 
		Page_set(page);
		Column_set(0);	  
		for(column=0;column<128;column++)//column loop
		{		
			OLED_send_data(0xff);
			GDDRAM[page][column]=0XFF;
		}
	 }
//	OLED_send_cmd(0xA5);//屏幕全显
//  OLED_send_cmd(0xA6);//屏幕正常模式
}
void OLED_clean_OLED(void)
{
	unsigned char page,column;
	OLED_set_gddram_mode(0X02);
    for(page=0;page<8;page++)//page loop
    { 
      Page_set(page);
      Column_set(0);	  
      for(column=0;column<128;column++)//column loop
      {
        OLED_send_data(0x00);
				GDDRAM[page][column]=0X00;
      }
    }	
}
void OLED_OFF(void)
{
	OLED_send_cmd(0xAE);//开启显示
}
void OLED_ON(void)
{
	OLED_send_cmd(0xAF);//关闭显示
}
void OLED_reverse_All(unsigned char mode)//(1黑底白字,0白底黑字)
{
	if(mode){
		OLED_send_cmd(0xA6);//1亮0灭
	}else{	
		OLED_send_cmd(0xA7);//1灭0亮
	}
}
void OLED_reverse_Part(unsigned char x,unsigned char y,unsigned char width,unsigned char height)
{	
	unsigned char i,j;
	for(i=x;i<x+width;i++){
		for(j=y;j<y+height;j++){
			if(OLED_getP(i,j)){OLED_resetP(i,j);}else{OLED_setP(i,j);}
		}
	}
	
}
void OLED_clean(void)
{
	unsigned char i,j;
	for(i=0;i<128;i++){
		for(j=0;j<64;j++){
			GDDRAM[j][i]=0;
		}
	}
}
void OLED_full(void)
{
	unsigned char i,j;
	for(i=0;i<128;i++){
		for(j=0;j<64;j++){
			GDDRAM[j][i]=0xff;
		}
	}	
}
void OLED_setP(unsigned char x,unsigned char y)
{
	if(x>128||y>64)return;
	GDDRAM[y/8][x]|=(0x01<<(y%8));
}
void OLED_resetP(unsigned char x,unsigned char y)
{
	if(x>128||y>64)return;
	GDDRAM[y/8][x]&=~(0x01<<(y%8));
}
unsigned char OLED_getP(unsigned char x,unsigned char y)
{
	if(x>128||y>64)return 0;
	return (GDDRAM[y/8][x]>>(y%8))&0x01;
}
void OLED_display(void)
{
	unsigned char i,j;
	OLED_set_gddram_mode(2);//页模式
	for(i=0;i<8;i++){
		Page_set(i);
		Column_set(0);
		for(j=0;j<128;j++){
			OLED_send_data(GDDRAM[i][j]);
		}
	}
}
void OLED_drawXbm(unsigned char x,unsigned char y,const unsigned char*data,unsigned char width,unsigned char height)
{
	/*阴码 逐列式 顺向*/
	unsigned char i,j;
	unsigned short offset=0;
	for(i=x;i<x+width;i++){		
		for(j=y;j<y+height;j++){		
			if(((*data)>>(7-offset%8))&0x01){
				OLED_setP(i,j);
			}else{
				OLED_resetP(i,j);
			}
			offset++;
			if(offset%8==0){//换到下一个data数组
				data++;
			}
		}
	}
}
void OLED_drawFrame(unsigned char data[8][128])
{
	unsigned char i,j;
	OLED_set_gddram_mode(0);
	Column_set_VR(0,127);
	Page_set_VR(0,7);
	for(i=0;i<8;i++){
		for(j=0;j<128;j++){
			OLED_send_data(data[i][j]);
		}
	}
}
void OLED_drawChar(unsigned char x,unsigned char y,const char data,unsigned char width,unsigned char height)
{
	/*阴码 逐列式 顺向*/
	unsigned char i,j,offset=0,index=0;
	const unsigned char *p;
	index=(int)data;
	if(index>126||index<32)return;//不在可绘制列表内
	p=&ascii[index-32][0];
	for(i=x;i<x+width;i++){
		for(j=y;j<y+height;j++){		
			if((*p>>(7-offset%8))&0x01){
				OLED_setP(i,j);
			}else{
				OLED_resetP(i,j);
			}
			offset++;
			if(offset%8==0){//换到下一个data数组
				p++;
			}
		}
	}
}
void OLED_drawString(unsigned char x,unsigned char y,const char *data,unsigned char width,unsigned char height)
{
	while(*data!=0){
		OLED_drawChar(x,y,*data,width,height);
		x+=width;
		data++;
	}
}
void OLED_drawP(unsigned char x,unsigned char y)
{
	if(x>128||y>64)return;
	OLED_set_gddram_mode(0X02);
	Page_set(y/8);
	Column_set(x);
	GDDRAM[y/8][x]|=(0x01<<(y%8));
	OLED_send_data(GDDRAM[y/8][x]);
}
void OLED_drawL_V(unsigned char X_B,unsigned char Y_B,unsigned char height)
{
	u8 Y;
	for(Y=Y_B;Y<Y_B+height;Y++)
	{
		OLED_setP(X_B,Y);
	}
}
void OLED_drawL_R(unsigned char X_B,unsigned char Y_B,unsigned char width)
{
	u8 X;
	for(X=X_B;X<X_B+width;X++)
	{
		OLED_setP(X,Y_B);
	}
}
void OLED_drawRect_H(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight)
{
	OLED_drawL_R(xpos,ypos,iwidth);
	OLED_drawL_R(xpos,ypos+iheight-1,iwidth);
	
	OLED_drawL_V(xpos,ypos,iheight);
	OLED_drawL_V(xpos+iwidth-1,ypos,iheight);
}
void OLED_drawRect_S(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight)
{
	u8 i,j;
	for(i=xpos;i<xpos+iwidth;i++){
		for(j=ypos;j<ypos+iheight;j++){
			OLED_setP(i,j);
//			if(i%2==0||j%2==0)OLED_resetP(i,j);
		}
	}
}
void OLED_drawRectdotO(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight,unsigned char density){
		unsigned char i;
    for(i=xpos+2;i<xpos+iwidth-2;i+=density){
      OLED_setP(i, ypos+2);
    }
    for(i=xpos+2;i<xpos+iwidth-2;i+=density){
      OLED_setP(i, ypos+iheight-3);
    }
    for(i=ypos+2;i<ypos+iheight-2;i+=density){
      OLED_setP(xpos+2, i);
    }
    for(i=ypos+2;i<ypos+iheight-2;i+=density){
      OLED_setP(xpos+iwidth-3, i);
    }
    OLED_drawRect_H(xpos,ypos,iwidth,iheight);   
}
void OLED_drawRectdotI(unsigned char xpos,unsigned char ypos,unsigned char iwidth,unsigned char iheight,unsigned char density){
		unsigned char i;
    for(i=xpos;i<xpos+iwidth;i+=density){
      OLED_setP(i, ypos);
    }
    for(i=xpos;i<xpos+iwidth;i+=density){
      OLED_setP(i, ypos+iheight-1);
    }
    for(i=ypos;i<ypos+iheight;i+=density){
      OLED_setP(xpos, i);
    }
    for(i=ypos;i<ypos+iheight;i+=density){
      OLED_setP(xpos+iwidth-1, i);
    }
    OLED_drawRect_H(xpos+2,ypos+2,iwidth-4,iheight-4);
}

I2C驱动源代码

#include"iic.h"
#include"delay.h"
#include "stm32f10x.h"

#define IIC_CLOCK RCC_APB2Periph_GPIOA

#define IIC_SDA  GPIOA
#define IIC_SCL  GPIOA

#define IIC_SDA_PIN GPIO_Pin_5
#define IIC_SCL_PIN GPIO_Pin_7

#define IIC_SDA_H GPIO_SetBits(IIC_SDA,IIC_SDA_PIN)
#define IIC_SDA_L GPIO_ResetBits(IIC_SDA,IIC_SDA_PIN)

#define IIC_SCL_H GPIO_SetBits(IIC_SCL,IIC_SCL_PIN)
#define IIC_SCL_L GPIO_ResetBits(IIC_SCL,IIC_SCL_PIN)
void IIC_init(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	RCC_APB2PeriphClockCmd(IIC_CLOCK,ENABLE);
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  //推挽输出模式
	
	GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
	GPIO_Init(IIC_SDA,&GPIO_InitStructure);
	
	GPIO_InitStructure.GPIO_Pin=IIC_SCL_PIN;
	GPIO_Init(IIC_SCL,&GPIO_InitStructure);
	
	IIC_OUT();
	IIC_SCL_H;
	IIC_SDA_H;   //把两条线都变成高电平
}
void IIC_OUT(void)//SDA是输出方向
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;  //推挽输出模式
	GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}
void IIC_IN(void)//SDA是输入方向
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
	GPIO_InitStructure.GPIO_Pin=IIC_SDA_PIN;
	GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;  //输入上拉模式
	GPIO_Init(IIC_SDA,&GPIO_InitStructure);
}
void IIC_START()//IIC_SCL高电平时候 IIC_SDA拉低
{
	IIC_OUT();
	
	IIC_SCL_H;
	IIC_SDA_H;
	delay_us(1);
	IIC_SDA_L;
	delay_us(3);
	IIC_SCL_L;//低电平才允许 IIC_SDA变化
}
void IIC_STOP()//IIC_SCL高电平时候 IIC_SDA升高
{
	IIC_OUT();
	
	IIC_SCL_L;
	IIC_SDA_L;
	delay_us(1);
	IIC_SCL_H;
	delay_us(3);
	IIC_SDA_H;
	delay_us(3);
}
void IIC_ACK()//SCL为高电平时候 IIC_SDA为低电平
{
    IIC_SCL_L;
    IIC_OUT();
    IIC_SDA_L;
    delay_us(3);
    IIC_SCL_H;
    delay_us(3);
    IIC_SCL_L;
}
void IIC_NACK()
{
		IIC_SCL_L;
    IIC_OUT();
    IIC_SDA_H;
    delay_us(3);
    IIC_SCL_H;
    delay_us(3);
    IIC_SCL_L;
}
u8 IIC_WAIT_ACK(void)
{
	int time;
	IIC_IN();
	IIC_SDA_H;//释放SDA
	delay_us(3);
	IIC_SCL_H;
	delay_us(3);
	while(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)   //等待应答信号
	{
		time++;
		if(time>2500){IIC_STOP();return 0;}
	}
	IIC_SCL_L;
	return 1;
}
void IIC_SEND_BIT(u8 bit)
{
		IIC_OUT();
		IIC_SCL_L;
		if(bit){
			IIC_SDA_H;
		}else{
			IIC_SDA_L;
		}
		IIC_SCL_H;
		delay_us(3);
		IIC_SCL_L;
}
void IIC_SEND_BYTE(u8 byte)
{
		u8 i=0;
		for(i=0;i<8;i++){
			IIC_SEND_BIT(byte>>(7-i)&0x01);
		}
		IIC_SDA_H;
		delay_us(1);
		IIC_SCL_H;
		delay_us(3);
		IIC_SCL_L;
}
u8 IIC_RECEIVE_BYTE(u8 ack)
{
		u8 temp,i;
		IIC_IN();
		for(i=0;i<8;i++)
		{
			IIC_SCL_L; 
			delay_us(1);
			IIC_SCL_H;
			temp<<=1;
			if(GPIO_ReadInputDataBit(IIC_SDA,IIC_SDA_PIN)==Bit_SET)temp&=0x01;   
			delay_us(3); 
		}					 
		if(!ack)
			 IIC_NACK();        //发送nACK
		else
			 IIC_ACK();         //发送ACK   
		return temp;
}
  嵌入式 最新文章
基于高精度单片机开发红外测温仪方案
89C51单片机与DAC0832
基于51单片机宠物自动投料喂食器控制系统仿
《痞子衡嵌入式半月刊》 第 68 期
多思计组实验实验七 简单模型机实验
CSC7720
启明智显分享| ESP32学习笔记参考--PWM(脉冲
STM32初探
STM32 总结
【STM32】CubeMX例程四---定时器中断(附工
上一篇文章      下一篇文章      查看所有文章
加:2021-07-25 11:50:39  更:2021-07-25 11:51:03 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年12日历 -2024/12/27 10:28:05-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码
数据统计