- #include "intrins.h"
-
- sbit sda=P0^3;
- sbit scl=P0^1;
-
- void i2c_start(void);
- void i2c_stop(void);
- char ack(void);
- void send_messege_i2c(char messege);
- void w_cmd_i2c(char cmd);
- void w_data_i2c(char data1);
- void init_i2c(void);
- void clear_oled(void);
-
- code char tx[]={
- 0xFF,0xFF,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,0x01,
- 0x01,0x03,0x03,0x03,0x07,0x07,0x05,0x07,0x03,0x03,0x03,0x03,0x03,0x03,0x03,0x01,
- 0x05,0x0F,0x0F,0x0F,0x07,0x05,0x07,0x05,0x07,0x07,0x07,0x0F,0x0F,0x0F,0x0F,0x0F,
- 0x0F,0x4F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,
- 0x1F,0x3F,0x1F,0x1F,0x7F,0x1F,0x1F,0x9F,0x9F,0x3F,0x7F,0x6F,0x6F,0x5F,0x9F,0xBF,
- 0xBF,0xBF,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,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,
- 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,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,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x03,0x1F,0x1F,0x1F,0x3F,0x3F,0x1F,
- 0xCF,0xEF,0xF7,0xD7,0xDF,0xFB,0xFF,0x9F,0x9E,0x9F,0x8F,0x8F,0x9F,0x9F,0x9F,0xBF,
- 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x40,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,0xF0,0xF0,0xF0,0xF0,0xF8,0xF8,0xF8,0xF8,0xFC,0xFC,
- 0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,0xFE,0xFE,0xFE,0xFE,0xFE,0xFE,0xFC,0xE0,0xC0,0x80,
- 0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x01,0x0C,0x44,0x60,0xF5,0xE0,0xC0,0xC0,0xC0,0xC0,0xC0,0xD8,0xFE,0xE7,0xE5,
- 0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x40,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,0x00,0x80,0xC0,0xC0,0x80,0x80,0x00,0x08,0x08,
- 0xC8,0x98,0x98,0x88,0x80,0x80,0xC0,0x81,0x81,0xC7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xDF,0x87,0x87,0xC3,0xC3,0xA3,0x91,0x91,0x11,0x11,0x19,0x99,0x89,0x09,0x09,
- 0x01,0x83,0xC3,0xC2,0x80,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,0x00,0x00,0x03,0x37,0x3D,0xBF,0xBF,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0x00,0x00,0x20,0x40,0x40,0x40,0x40,0x06,0x06,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x02,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x1E,0x3E,0x7C,0x70,0xE0,0xC3,0x1F,0x3F,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,
- 0x7F,0x7F,0x7F,0x7F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0x1F,0x00,0x00,0x40,0x70,0x3C,0x1C,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0xC0,0xA0,0xC0,0xC0,0x00,0xC5,0xFF,0xFF,0xFF,0xFF,0xFD,0xFD,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0x00,0x00,0x00,0x80,0x00,0x80,0x00,0x00,0xC0,0xC0,0x00,0xC0,0xC0,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x08,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x0F,0x3F,0x7F,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xDF,0x9F,0x9F,0x1F,0x1F,0x1E,0x9E,0xDE,0xDE,0xDE,0xDE,0xDE,
- 0xDE,0x1E,0x1C,0x1C,0x1F,0x1F,0x9F,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0x7F,0x3F,0x1F,
- 0x1F,0x07,0x03,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
- 0x40,0x40,0x00,0x00,0x00,0x00,0x30,0x08,0x08,0x03,0x80,0x80,0x80,0xF8,0xF8,0xFE,
- 0xEF,0xC7,0xE7,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFD,0xFD,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0x00,0x00,0xE0,0xEF,0xE0,0xEF,0xE0,0x80,0x0F,0x6F,0x60,0xCF,0xAF,0xBF,
- 0x23,0x84,0xC0,0xA0,0xE0,0xE0,0xE0,0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x02,
- 0x02,0x04,0x00,0x00,0x00,0x00,0x20,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
- 0x01,0x07,0x0F,0x1F,0x3F,0x3F,0x7F,0x7C,0x7C,0xF8,0xF9,0xFB,0xFB,0xFB,0xFB,0xF9,
- 0xF9,0x78,0x78,0x7C,0x3E,0x3F,0x1F,0x1F,0x0F,0x07,0x03,0x01,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,0xC1,0xE1,0xFF,0xFF,0xFE,0xFE,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0x00,0x00,0x9F,0xBF,0xDF,0xFF,0xFF,0xEF,0xE0,0x98,0x98,0x9A,0x87,0xA7,
- 0xA7,0x8F,0x8F,0x9F,0x9F,0x9A,0x9E,0x9C,0xFF,0xFB,0xFA,0xFC,0xFC,0xFC,0xEC,0xE8,
- 0xF8,0xF0,0xB0,0xB0,0xA0,0xE0,0xC0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x00,0x00,0x20,
- 0x00,0x00,0x00,0x40,0xC0,0xC0,0x44,0x04,0x00,0x00,0x00,0x00,0x80,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xE0,0xE0,
- 0xE0,0xD0,0xB0,0xF0,0xF8,0xF8,0xB8,0x9C,0xDC,0xDC,0x7E,0xFC,0xE4,0xEC,0xEC,0xF4,
- 0xF8,0xF8,0xFC,0xFC,0xF4,0xFE,0xFE,0xFE,0xFE,0xFF,0xFD,0xFF,0xFF,0xFF,0xFF,0xFF,
- 0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF,0xFF}; //这是page7的内容
-
- void main(void)
- {
- unsigned char i;
- unsigned int j;
- /*-- 文字: 我 --*/
- /*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
-
-
- init_i2c();
- clear_oled(); //清屏函数
-
- w_cmd_i2c(0x20); //选择页寻址模式
- w_cmd_i2c(0x02);
-
- //w_cmd_i2c(0xB0); //选择page 1011 0xxx
- //w_cmd_i2c(0x00); //设置起始地址 低位
- //w_cmd_i2c(0x10); //设置起始地址 高位
- //w_data_i2c(0x80); //发送数据
-
- for(i=0;i<8;i++){
-
- w_cmd_i2c(0xB0+i); //变换page 共有0-7 8个
- w_cmd_i2c(0x00); //设置起始地址 低位
- w_cmd_i2c(0x10); //设置起始地址 高位
- //i=3;
- for(j=128*i; j<(128*(i+1)); j++){ //变换的是tx中的数据
- w_data_i2c(tx[j]); //tx中的数据
- }
- }
-
- while(1){}
- }
- //下面是函数封装
-
-
- void clear_oled(void)
- {
- char i; int j;
-
- for(i=0;i<8;i++){
- w_cmd_i2c(0xB0+i); //变换page
- w_cmd_i2c(0x00); //设置起始地址 低位
- w_cmd_i2c(0x10); //设置起始地址 高位
- for(j=0;j<128;j++){
- w_data_i2c(0x00); //写0就行
- }
- }
-
- }
-
- void init_i2c(void)
- {
- w_cmd_i2c(0xAE);//--display off
- w_cmd_i2c(0x00);//---set low column address
- w_cmd_i2c(0x10);//---set high column address
- w_cmd_i2c(0x40);//--set start line address
- w_cmd_i2c(0xB0);//--set page address
- w_cmd_i2c(0x81); // contract control
- w_cmd_i2c(0xFF);//--128
- w_cmd_i2c(0xA1);//set segment remap
- w_cmd_i2c(0xA6);//--normal / reverse
- w_cmd_i2c(0xA8);//--set multiplex ratio(1 to 64)
- w_cmd_i2c(0x3F);//--1/32 duty
- w_cmd_i2c(0xC8);//Com scan direction
- w_cmd_i2c(0xD3);//-set display offset
- w_cmd_i2c(0x00);//
- w_cmd_i2c(0xD5);//set osc division
- w_cmd_i2c(0x80);//
- w_cmd_i2c(0xD8);//set area color mode off
- w_cmd_i2c(0x05);//
- w_cmd_i2c(0xD9);//Set Pre-Charge Period
- w_cmd_i2c(0xF1);//
- w_cmd_i2c(0xDA);//set com pin configuartion
- w_cmd_i2c(0x12);//
- w_cmd_i2c(0xDB);//set Vcomh
- w_cmd_i2c(0x30);//
- w_cmd_i2c(0x8D);//set charge pump enable
- w_cmd_i2c(0x14);//
- w_cmd_i2c(0xAF);//--turn on oled panel
- }
-
- void w_cmd_i2c(char cmd)
- {
- i2c_start();
- send_messege_i2c(0x78);
- ack();
- send_messege_i2c(0x00);
- ack();
- send_messege_i2c(cmd);
- ack();
- i2c_stop();
- }
-
- void w_data_i2c(char data1)
- {
- i2c_start();
- send_messege_i2c(0x78);
- ack();
- send_messege_i2c(0x40);
- ack();
- send_messege_i2c(data1);
- ack();
- i2c_stop();
- }
-
- void i2c_start(void)
- {
- scl=1;
- sda=0;
- sda=1; //我的
- /*
- scl=0; //老师的
- sda=1;
- scl=1;*/
-
- _nop_(); //
- sda=0;
- _nop_(); //
- scl=0;
- }
-
- void i2c_stop(void)
- {
- scl=0;
- sda=0;
- scl=1;
- _nop_(); //
- sda=1;
- _nop_(); //
- sda=0; //老师不要这个
- }
-
- char ack(void) //应答信号
- {
- char flag;
- sda=1;
- //_nop_(); //老师要的
- scl=1;
- _nop_();
- flag=sda;
- //_nop_(); //老师要的
- scl=0;
- _nop_();
- return flag;
- }
-
- void send_messege_i2c(char messege)
- {
- char i;
-
- for(i=0;i<8;i++){
- scl=0;
- _nop_(); //必要
- sda= messege & 0x80 ;
- //_nop_(); //老师要的
- scl=1;
- _nop_();
- scl=0;
- messege=messege<<1;
- }
- }
- 原文链接:https://blog.csdn.net/m0_67794575/article/details/130902543