OLED12864驱动程序
暑假电子设计大赛的时候移植到430上的OLED驱动程序,分享给大家。
/******************************************************************** * OLED屏测试程序 * msp430g2553 * * 调用方法: * LCD_Init(); // OLED 初始化 * LCD_Fill(0x00); // 全屏填充 0x00 * LCD_GBK16(0, 2, "1.锄禾日当午,\r2.汗滴禾下土。\r3.谁知盘中餐,"); * LCD_P6x8Str (0,4,"OLED"); // 显示6*8 字符串 * = HowMuchWidth(Num);// 获取某数有几位 * LCD_IntNum(x, y, HowMuchWidth(v), v); // 显示数字 * LCD_16Pic(unsigned char x,unsigned char y,unsigned char width,unsigned char dat[]);//16像素高度图片 * LCD_8Pic(unsigned char x,unsigned char y,unsigned char width,unsigned char ch[]);//8像素高度图片 * LCD_AllPic(unsigned char dat[]);//全局图像显示 * * * 说明:图像取模:纵向取模,字节倒序 * * 作者:星希望(已校验) * 日期:2016年7月25日 **********************************************************************/ #include "OLED.h" #include "OLED_Font.h" #include "msp430g2553.h" #define LCD_SCL_1 P2OUT |= BIT0 #define LCD_SCL_0 P2OUT &= ~BIT0 #define LCD_SDA_1 P2OUT |= BIT1 #define LCD_SDA_0 P2OUT &= ~BIT1 #define LCD_RST_1 P2OUT |= BIT2 #define LCD_RST_0 P2OUT &= ~BIT2 #define LCD_DC_1 P2OUT |= BIT3 #define LCD_DC_0 P2OUT &= ~BIT3 #define XLevelL 0x00 #define XLevelH 0x10 #define XLevel ((XLevelH&0x0F)*16+XLevelL) #define Max_Column 128 #define Max_Row 64 #define Brightness 0xCF #define X_WIDTH 128 #define Y_WIDTH 64 unsigned int hz16_num = 0; // 汉字个数 //====================================== void LCD_WrDat(unsigned char dat) { unsigned char i=8; LCD_DC_1; LCD_SCL_0; while(i--) { if(dat&0x80) {LCD_SDA_1;} else {LCD_SDA_0;} LCD_SCL_1; dat<<=1; LCD_SCL_0; } } void LCD_WrCmd(unsigned char cmd) { unsigned char i=8; LCD_DC_0; LCD_SCL_0; while(i--) { if(cmd&0x80) {LCD_SDA_1;} else {LCD_SDA_0;} LCD_SCL_1; cmd<<=1; LCD_SCL_0; } } void LCD_Set_Pos(unsigned char x, unsigned char y) { LCD_WrCmd(0xb0+y); LCD_WrCmd(((x&0xf0)>>4)|0x10); LCD_WrCmd((x&0x0f)|0x00); } void LCD_Fill(unsigned char bmp_dat) { unsigned char y, x; for(y=0;y<8;y++) { LCD_WrCmd(0xb0+y); LCD_WrCmd(0x01); LCD_WrCmd(0x10); for(x=0;x<X_WIDTH;x++) LCD_WrDat(bmp_dat); } } void LCD_DLY_ms(unsigned int ms) { while(ms--) { __delay_cycles(100); } return; } /********************************************************************************* * OLED 初始化 *********************************************************************************/ void LCD_Init(void) { P2DIR |= BIT0 | BIT1 | BIT2 | BIT3; LCD_SCL_1; //LCD_CS=1; //预制SLK和SS为高电平 LCD_RST_0; LCD_DLY_ms(50); LCD_RST_1; /* 计算汉字库的大小 */ hz16_num = sizeof hz16 / sizeof (struct typFNT_GB162); //从上电到下面开始初始化要有足够的时间,即等待RC复位完毕 LCD_WrCmd(0xae);//--关闭OLED面板 LCD_WrCmd(0x00);//---set low column address置低列地址 LCD_WrCmd(0x10);//---set high column address设置高列地址 LCD_WrCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)设置起始行地址设置映射RAM显示起始行(0×00?0x3F之间) LCD_WrCmd(0x81);//--set contrast control register设置对比度控制寄存器 LCD_WrCmd(0xcf);// Set SEG Output Current Brightness设置SEG输出电流亮度 LCD_WrCmd(0xa1);//--Set SEG/Column Mapping0xa0左右反置 0xa1正常 设置SEG/列映射0XA0左右反置0xa1正常 LCD_WrCmd(0xc8);//Set COM/Row Scan Direction0xc0上下反置 0xc8正常 设置COM/行扫描方向为0xC0上下反置0xc8正常 LCD_WrCmd(0xa6);//--set normal display 设置正常显示 LCD_WrCmd(0xa8);//--set multiplex ratio(1 to 64) 设置复用率(1~64) LCD_WrCmd(0x3f);//--1/64 duty 1/64责任 LCD_WrCmd(0xd3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F) 置显示失调漂移映射RAM计数器(为0x00?0x3F之间) LCD_WrCmd(0x00);//-not offset 不能抵消 LCD_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency 设置显示时钟分频比/振荡器频率 LCD_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec 设置的分频比,设置时钟为100帧/秒 LCD_WrCmd(0xd9);//--set pre-charge period组预充电期间 LCD_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock 设置预充电为15时钟和放电为1时钟 LCD_WrCmd(0xda);//--set com pins hardware configuration组COM插针的硬件配置 LCD_WrCmd(0x12); LCD_WrCmd(0xdb);//--set vcomh LCD_WrCmd(0x40);//Set VCOM Deselect Level 设置VCOM取消等级 LCD_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)设置页面寻址模式(0×00/0×01/0X02) LCD_WrCmd(0x02);// LCD_WrCmd(0x8d);//--set Charge Pump enable/disable 设置电荷泵启用/禁用 LCD_WrCmd(0x14);//--set(0x10) disable 设置(0x10)后关闭 LCD_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)禁用整个显示屏(0xa4/到0x55) LCD_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)禁用逆显示(0xa6/ A7) LCD_WrCmd(0xaf);//--turn on oled panel LCD_Fill(0x00); //初始清屏 LCD_Set_Pos(0,0); } /********************************************************************************* * 函数名:LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char *p) * 功能描述:写入一组标准ASCII字符串 * 参数:显示的位置(x,y),y为页范围0~7,要显示的字符串 * 返回:无 *********************************************************************************/ void LCD_P6x8Str(unsigned char x, unsigned char y, unsigned char ch[]) { unsigned char c=0,i=0,j=0; while (ch[j]!='\0') { c =ch[j]-32; if(x>126 || c==13) // 下一行 {x=0;y++;} LCD_Set_Pos(x,y); for(i=0;i<6;i++)LCD_WrDat(F6x8[c][i]); x+=6; j++; } } /********************************************************************************* * 函数名:LCD_8Pic(unsigned char x,unsigned char y,unsigned char *p) * 功能描述:写入一组高度为8像素的图标 * 参数:显示的位置(x,y),y为页范围0~7, * 返回:无 *********************************************************************************/ void LCD_8Pic(unsigned char x,unsigned char y,unsigned char width,unsigned char ch[]) { unsigned char j=0; LCD_Set_Pos(x,y); while (width) { LCD_WrDat(ch[j]); j++; width--; } } /************************************************************************************* * 显示汉字 及 8x16 ascii码 * 坐标x,坐标y,内容 * 查找 hz16[] 结构体数组 *************************************************************************************/ void LCD_GBK16(unsigned int x, unsigned int y, unsigned char *s) { unsigned char j; unsigned short k,x0; x0=x; while(*s) { if((*s) < 128) // ASC段 { k = *s; if (k==13) //回车 { x = x0; y += 2; } else { if (k>32) k-=32; //从有效字符开始编码,避开前面的非字符区 else k=0; LCD_Set_Pos(x, y); for(j=0; j<8; j++) { LCD_WrDat(F8X16[k*16+j]); } LCD_Set_Pos(x, y+1); for(j=0; j<8; j++) { LCD_WrDat(F8X16[k*16+j+8]); } x += 8; } s++; } else // 汉字段 { for(k=0; k<hz16_num; k++) { if( (hz16[k].Index[0]==*(s)) && (hz16[k].Index[1]==*(s+1)) ) { LCD_Set_Pos(x, y); for(j=0; j<16; j++) //高8位 { LCD_WrDat(hz16[k].Msk[j]); } LCD_Set_Pos(x, y+1); for(j=0; j<16; j++) //低8位 { LCD_WrDat(hz16[k].Msk[j+16]); } break; } } if( k==hz16_num )// 没有找到该汉字 { LCD_Set_Pos(x, y); for(j=0; j<16; j++) //高8位 { LCD_WrDat(UnknowFont[j]); } LCD_Set_Pos(x, y+1); for(j=0; j<16; j++) //低8位 { LCD_WrDat(UnknowFont[j+16]); } } s += 2; x += 16; } if(x>120) {x=0; y+=2;} } } /********************************************************************************* * 函数名:LCD_16Pic(unsigned char x,unsigned char y,unsigned char *p) * 参数:x,y显示的位置,y为页范围0~7, * width图标宽度 * dat[]图像数据 *********************************************************************************/ void LCD_16Pic(unsigned char x,unsigned char y,unsigned char width,unsigned char dat[]) { unsigned char i=0; LCD_Set_Pos(x, y); for(i = 0;i < width;i++) { LCD_WrDat(dat[i]); } LCD_Set_Pos(x, y + 1); for(i = 0;i < width;i++) { LCD_WrDat(dat[i+width]); } } /********************************************************************************* * 全屏图像显示 *********************************************************************************/ void LCD_AllPic(unsigned char dat[]) { unsigned char y, x; for(y=0; y<8; y++) { LCD_Set_Pos(0, y); for(x=0; x<128; x++) { LCD_WrDat(dat[y*128+x]); } } } /************************************************************************************ * 计算某数有几位 ************************************************************************************/ unsigned char HowMuchWidth(unsigned long Num) { unsigned char cnt=0; while(Num) { Num /= 10; cnt++; } return cnt; } /************************************************************************************ * 显示数字 * 参数:x,y - 坐标 * Width- 宽度(有几位数) * v - 数字 * 说明:右对齐 *************************************************************************************/ void LCD_IntNum(unsigned char x, unsigned char y, unsigned char Width, unsigned long v) { unsigned char a[11]; unsigned char *p; // 数位分解 a[10]= 0; a[9] = v% 10 + '0'; a[8] = v /10 % 10 + '0'; a[7] = v /100 % 10 + '0'; a[6] = v /1000 % 10 + '0'; a[5] = v /10000 % 10 + '0'; a[4] = v /100000 % 10 + '0'; a[3] = v /1000000 % 10 + '0'; a[2] = v /10000000 % 10 + '0'; a[1] = v /100000000 % 10 + '0'; a[0] = v /1000000000 % 10 + '0'; p = &a[10-Width]; // 显示 LCD_GBK16(x, y, p); // 显示 8*16 字符串 } /************************************************************************************ * 显示数字 * 参数:x,y - 坐标 * Width- 宽度(有几位数) * v - 数字 * 说明:右对齐 *************************************************************************************/ void LCD_IntNum6x8(unsigned char x, unsigned char y, unsigned char Width, unsigned long v) { unsigned char a[11]; unsigned char *p; // 数位分解 a[10]= 0; a[9] = v% 10 + '0'; a[8] = v /10 % 10 + '0'; a[7] = v /100 % 10 + '0'; a[6] = v /1000 % 10 + '0'; a[5] = v /10000 % 10 + '0'; a[4] = v /100000 % 10 + '0'; a[3] = v /1000000 % 10 + '0'; a[2] = v /10000000 % 10 + '0'; a[1] = v /100000000 % 10 + '0'; a[0] = v /1000000000 % 10 + '0'; p = &a[10-Width]; // 显示 LCD_P6x8Str(x, y, p); // 显示 8*16 字符串 }
#ifndef _OLED_H #define _OLED_H extern unsigned int hz16_num; // 汉字个数 void LCD_Init(void); void LCD_CLS(void); void LCD_P6x8Str(unsigned char x,unsigned char y,unsigned char ch[]); void LCD_GBK16(unsigned int x, unsigned int y, unsigned char *s); unsigned char HowMuchWidth(unsigned long Num); void LCD_IntNum(unsigned char x, unsigned char y, unsigned char Width, unsigned long v); void LCD_IntNum6x8(unsigned char x, unsigned char y, unsigned char Width, unsigned long v); void LCD_8Pic (unsigned char x,unsigned char y,unsigned char width,unsigned char ch[]); void LCD_16Pic(unsigned char x,unsigned char y,unsigned char width,unsigned char dat[]); void LCD_AllPic(unsigned char dat[]); void LCD_Fill(unsigned char dat); #endif
灰小子:
感谢分享,非常好的资料
灰小子:
感谢分享,非常好的资料
chuan chuan:
怎么下载啊?
灰小子:
回复 chuan chuan:
直接复制就可以
hong liu7:
回复 灰小子:
OLED_Font.h,没有呀,那些变量的格式不知道
ymxgd2009:
谢谢分享