logo资料库

oled液晶51SPI驱动程序.pdf

第1页 / 共6页
第2页 / 共6页
第3页 / 共6页
第4页 / 共6页
第5页 / 共6页
第6页 / 共6页
资料共6页,全文预览结束
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c /************************************* *◆文件名:oled.c * *◆介 绍:基于51单片机的OLED液晶驱动* *程序,此版本为单文件版本, 所有函数在* *一个文件中便于查看程序思路. * *◆版本号:v1.0 * *◆编译时间:2015/8/22 * *◆作者:ONROAD * *◆开源共享, 共同进步。 * *************************************/ #include /* *全局宏**/ #define OledCmd 0 //命令标志 #define OledDat 1 //数据标志 /* *OLED引脚定义**/ sbit SCLK = P0^0; //spi时钟线,对应模块上的D0 sbit SDIN = P0^1; //spi数据线,对应模块上的D1 sbit RST = P0^2; //液晶复位 sbit DC = P0^3; //数据命令选择端, 1: 数据;0 : 命令 /* *函数声明**/ void writeByte(unsigned char dat, unsigned char cmd); void delay_ms(unsigned int ms); void setOledPos(unsigned char x, unsigned char y); void oledDisplayOn(void); void oledDisplayOff(void); void oledClear(void); void showChar(unsigned char x, unsigned char y, char ch); void oledInit(void); void showString(unsigned char x, unsigned char y, char * str); /* *******6x8字符**************************/ const unsigned char code F6x8[][6] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp 0x00, 0x00, 0x00, 0x2f, 0x00, 0x00,// ! 0x00, 0x00, 0x07, 0x00, 0x07, 0x00,// " 0x00, 0x14, 0x7f, 0x14, 0x7f, 0x14,// # 0x00, 0x24, 0x2a, 0x7f, 0x2a, 0x12,// $ 0x00, 0x62, 0x64, 0x08, 0x13, 0x23,// % 0x00, 0x36, 0x49, 0x55, 0x22, 0x50,// & 0x00, 0x00, 0x05, 0x03, 0x00, 0x00,// ' 0x00, 0x00, 0x1c, 0x22, 0x41, 0x00,// ( 0x00, 0x00, 0x41, 0x22, 0x1c, 0x00,// ) 0x00, 0x14, 0x08, 0x3E, 0x08, 0x14,// * 0x00, 0x08, 0x08, 0x3E, 0x08, 0x08,// + 0x00, 0x00, 0x00, 0xA0, 0x60, 0x00,// , 0x00, 0x08, 0x08, 0x08, 0x08, 0x08,// - Page: 1
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c 0x00, 0x00, 0x60, 0x60, 0x00, 0x00,// . 0x00, 0x20, 0x10, 0x08, 0x04, 0x02,// / 0x00, 0x3E, 0x51, 0x49, 0x45, 0x3E,// 0 0x00, 0x00, 0x42, 0x7F, 0x40, 0x00,// 1 0x00, 0x42, 0x61, 0x51, 0x49, 0x46,// 2 0x00, 0x21, 0x41, 0x45, 0x4B, 0x31,// 3 0x00, 0x18, 0x14, 0x12, 0x7F, 0x10,// 4 0x00, 0x27, 0x45, 0x45, 0x45, 0x39,// 5 0x00, 0x3C, 0x4A, 0x49, 0x49, 0x30,// 6 0x00, 0x01, 0x71, 0x09, 0x05, 0x03,// 7 0x00, 0x36, 0x49, 0x49, 0x49, 0x36,// 8 0x00, 0x06, 0x49, 0x49, 0x29, 0x1E,// 9 0x00, 0x00, 0x36, 0x36, 0x00, 0x00,// : 0x00, 0x00, 0x56, 0x36, 0x00, 0x00,// ; 0x00, 0x08, 0x14, 0x22, 0x41, 0x00,// < 0x00, 0x14, 0x14, 0x14, 0x14, 0x14,// = 0x00, 0x00, 0x41, 0x22, 0x14, 0x08,// > 0x00, 0x02, 0x01, 0x51, 0x09, 0x06,// ? 0x00, 0x32, 0x49, 0x59, 0x51, 0x3E,// @ 0x00, 0x7C, 0x12, 0x11, 0x12, 0x7C,// A 0x00, 0x7F, 0x49, 0x49, 0x49, 0x36,// B 0x00, 0x3E, 0x41, 0x41, 0x41, 0x22,// C 0x00, 0x7F, 0x41, 0x41, 0x22, 0x1C,// D 0x00, 0x7F, 0x49, 0x49, 0x49, 0x41,// E 0x00, 0x7F, 0x09, 0x09, 0x09, 0x01,// F 0x00, 0x3E, 0x41, 0x49, 0x49, 0x7A,// G 0x00, 0x7F, 0x08, 0x08, 0x08, 0x7F,// H 0x00, 0x00, 0x41, 0x7F, 0x41, 0x00,// I 0x00, 0x20, 0x40, 0x41, 0x3F, 0x01,// J 0x00, 0x7F, 0x08, 0x14, 0x22, 0x41,// K 0x00, 0x7F, 0x40, 0x40, 0x40, 0x40,// L 0x00, 0x7F, 0x02, 0x0C, 0x02, 0x7F,// M 0x00, 0x7F, 0x04, 0x08, 0x10, 0x7F,// N 0x00, 0x3E, 0x41, 0x41, 0x41, 0x3E,// O 0x00, 0x7F, 0x09, 0x09, 0x09, 0x06,// P 0x00, 0x3E, 0x41, 0x51, 0x21, 0x5E,// Q 0x00, 0x7F, 0x09, 0x19, 0x29, 0x46,// R 0x00, 0x46, 0x49, 0x49, 0x49, 0x31,// S 0x00, 0x01, 0x01, 0x7F, 0x01, 0x01,// T 0x00, 0x3F, 0x40, 0x40, 0x40, 0x3F,// U 0x00, 0x1F, 0x20, 0x40, 0x20, 0x1F,// V 0x00, 0x3F, 0x40, 0x38, 0x40, 0x3F,// W 0x00, 0x63, 0x14, 0x08, 0x14, 0x63,// X 0x00, 0x07, 0x08, 0x70, 0x08, 0x07,// Y 0x00, 0x61, 0x51, 0x49, 0x45, 0x43,// Z 0x00, 0x00, 0x7F, 0x41, 0x41, 0x00,// [ 0x00, 0x55, 0x2A, 0x55, 0x2A, 0x55,// 55 0x00, 0x00, 0x41, 0x41, 0x7F, 0x00,// ] 0x00, 0x04, 0x02, 0x01, 0x02, 0x04,// ^ 0x00, 0x40, 0x40, 0x40, 0x40, 0x40,// _ 0x00, 0x00, 0x01, 0x02, 0x04, 0x00,// ' 0x00, 0x20, 0x54, 0x54, 0x54, 0x78,// a Page: 2
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c 0x00, 0x7F, 0x48, 0x44, 0x44, 0x38,// b 0x00, 0x38, 0x44, 0x44, 0x44, 0x20,// c 0x00, 0x38, 0x44, 0x44, 0x48, 0x7F,// d 0x00, 0x38, 0x54, 0x54, 0x54, 0x18,// e 0x00, 0x08, 0x7E, 0x09, 0x01, 0x02,// f 0x00, 0x18, 0xA4, 0xA4, 0xA4, 0x7C,// g 0x00, 0x7F, 0x08, 0x04, 0x04, 0x78,// h 0x00, 0x00, 0x44, 0x7D, 0x40, 0x00,// i 0x00, 0x40, 0x80, 0x84, 0x7D, 0x00,// j 0x00, 0x7F, 0x10, 0x28, 0x44, 0x00,// k 0x00, 0x00, 0x41, 0x7F, 0x40, 0x00,// l 0x00, 0x7C, 0x04, 0x18, 0x04, 0x78,// m 0x00, 0x7C, 0x08, 0x04, 0x04, 0x78,// n 0x00, 0x38, 0x44, 0x44, 0x44, 0x38,// o 0x00, 0xFC, 0x24, 0x24, 0x24, 0x18,// p 0x00, 0x18, 0x24, 0x24, 0x18, 0xFC,// q 0x00, 0x7C, 0x08, 0x04, 0x04, 0x08,// r 0x00, 0x48, 0x54, 0x54, 0x54, 0x20,// s 0x00, 0x04, 0x3F, 0x44, 0x40, 0x20,// t 0x00, 0x3C, 0x40, 0x40, 0x20, 0x7C,// u 0x00, 0x1C, 0x20, 0x40, 0x20, 0x1C,// v 0x00, 0x3C, 0x40, 0x30, 0x40, 0x3C,// w 0x00, 0x44, 0x28, 0x10, 0x28, 0x44,// x 0x00, 0x1C, 0xA0, 0xA0, 0xA0, 0x7C,// y 0x00, 0x44, 0x64, 0x54, 0x4C, 0x44,// z 0x14, 0x14, 0x14, 0x14, 0x14, 0x14,// horiz lines }; /* *主函数**/ void main(void){ int i = 0; oledInit(); while(1){ while(i < 100) { showChar(i, 0, 'A'); i = i + 6; } showString(0, 3, "Hello World!"); } } /* *向SSD1306写入一个字节***** **dat : 待写入的数据******** **cmd : 数据与命令的标志位**/ void writeByte(unsigned char dat, unsigned char cmd){ unsigned char i; if(cmd == 1){ DC = 1; //传入数据 } else{ //传入命令 //判断传入的是数据还是命 DC = 0; Page: 3
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c } for(i = 0; i < 8; i++){ SCLK = 0; if(dat & 0x80){ SDIN = 1; } else{ SDIN = 0; } SCLK = 1; dat <<= 1; } DC = 1; } //SCLK为低电平时,准备数据 //SCLK为高,将数据传入RAM /* *延时一段时间**/ void delay_ms(unsigned int ms) { unsigned int a; while(ms) { a = 1800; while(a--); ms--; } return; /* *设置坐标** **函数名:void setOledPos(unsigend char x, unsigend char y) **传入参数:unsigned char x -- x坐标;unsigned char y -- y坐标 ************/ void setOledPos(unsigned char x, unsigned char y){ writeByte (0xb0 + y, OledCmd); writeByte (((x & 0xf0)>>4)|0x10, OledCmd); writeByte ((x & 0x0f)|0x01,OledCmd); } /* *开启OLED显示**/ void oledDisplayOn(void){ writeByte (0X8D, OledCmd); //请求设置DC命令 writeByte (0X14, OledCmd); //设置DC开启 writeByte (0XAF, OledCmd); //显示开启 /* *关闭OLED显示**/ void oledDisplayOff(void){ writeByte (0X8D, OledCmd); //请求设置DC命令 writeByte (0X10, OledCmd); //设置DC关闭 writeByte (0XAE, OledCmd); //显示关闭 } } } Page: 4
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c /* *清屏函数**/ void oledClear(void){ unsigned char i, n; for(i = 0; i < 8; i++){ for(n = 0; n < 128; n++) writeByte(0, OledDat); } } writeByte (0xb0+i, OledCmd); //设置页地址(0~7) writeByte (0x00, OledCmd); //设置显示位置—列低地址 writeByte (0x10, OledCmd); //设置显示位置—列高地址 /* *在指定位置显示一个字符** **函数名称:showChar(unsigned char x, unsigned char y, char ch) **参数:unsigned char x, unsigned char y, char ch 坐标及内容 **x: 0~127; y: 0~7 **显示说明:此处显示6*8字体 *************************/ void showChar(unsigned char x, unsigned char y, char ch){ unsigned char c = 0; unsigned char i = 1; c = (int)ch - 32; if(x > 127) { y = y + 1; //用于存储字符对应字库序号(ASCII码 - 32) //将ch强制转换为整型 //检索一行是否写满,若是,则换行。 } setOledPos(x, y); //设置显示坐标 for(i = 0; i < 6; i++){ writeByte(F6x8[c][i], OledDat); } } /* *从某处开始显示一串字符** **函数名:void showString(unsigned char x, unsigned char y, char * str) **参数: x--x坐标, y--y坐标, str--数据地址 **************************/ void showString(unsigned char x, unsigned char y, char * str){ while(*str != '\0'){ showChar(x, y, *str); x = x + 6; str++; } } /* *OLED初始化函数**/ void oledInit(void){ RST = 1; delay_ms(100); RST = 0; delay_ms(100); RST = 1; Page: 5
G:\MyProject\8bitMCUproject\OLED_51_4WireSPI_Driver\oled.c writeByte (0xAE, OledCmd); //关闭液晶 writeByte (0x00, OledCmd); //设置低位列地址 writeByte (0x10, OledCmd); //设置高位列地址 writeByte (0x40, OledCmd); //--set start line address Set Mapping RAM Display St art Line (0x00~0x3F) writeByte (0x81, OledCmd); //--set contrast control register writeByte (0xCF, OledCmd); // Set SEG Output Current Brightness writeByte (0xA1, OledCmd); //--Set SEG/Column Mapping 0xa0左右反置 0xa1正常 writeByte (0xC8, OledCmd); //Set COM/Row Scan Direction 0xc0上下反置 0xc8正常 writeByte (0xA6, OledCmd); //--set normal display writeByte (0xA8, OledCmd); //--set multiplex ratio(1 to 64) writeByte (0x3f, OledCmd); //--1/64 duty writeByte (0xD3, OledCmd); //-set display offset Shift Mapping RAM Counter (0x writeByte (0x00, OledCmd); //-not offset writeByte (0xd5, OledCmd); //--set display clock divide ratio/oscillator frequenc 00~0x3F) writeByte (0x80, OledCmd); //--set divide ratio, Set Clock as 100 Frames/Sec writeByte (0xD9, OledCmd); //--set pre-charge period writeByte (0xF1, OledCmd); //Set Pre-Charge as 15 Clocks & Discharge as 1 Clock writeByte (0xDA, OledCmd); //--set com pins hardware configuration writeByte (0x12, OledCmd); writeByte (0xDB, OledCmd); //--set vcomh writeByte (0x40, OledCmd); //Set VCOM Deselect Level writeByte (0x20, OledCmd); //-Set Page Addressing Mode (0x00/0x01/0x02) writeByte (0x02, OledCmd); writeByte (0x8D, OledCmd); //--set Charge Pump enable/disable writeByte (0x14, OledCmd); //--set(0x10) disable writeByte (0xA4, OledCmd); // Disable Entire Display On (0xa4/0xa5) writeByte (0xA6, OledCmd); // Disable Inverse Display On (0xa6/a7) writeByte (0xAF, OledCmd); //--turn on oled panel writeByte (0xAF, OledCmd); //display ON oledClear(); setOledPos(0,0); y } Page: 6
分享到:
收藏