实验平台:TivaC LaunchPad
OLED128x64模式:8080,支持4SPI
IDE环境:CCS5.2
引脚定义:pe2-wr
pe3-rd
pe1-cs
pe4-dc
pe5-rst
PB0~7数据端口设为输出
参考资料:《ARM Cortex核 TI微控制器原理与应用》马忠梅等编,SSD1306 Datasheet,正点原子STM32,energia等
模块文件主要有OLED128x64.C和OLED128x64.H两个文件。还有Font字库文件,这里不再写出,附在附件中。
只需修改源文件中红色标注的代码,便可以修改对应引脚,实现需要的硬件定义,程序中有一点需要说明:
PORTE 和PORTB的引脚时钟配置(代码红色加粗部分,在OLED_Init()中.)只能分开配置,不然程序会卡在OLED_Init();
-------------------------------------------oled128x64源文件------------------------------------------------
- /*
- * oled128x64.c
- *
- * Created on: 2013-11-3
- * Author: Administrator
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include "inc/hw_types.h"
- #include "inc/hw_memmap.h"
- #include "driverlib/gpio.h"
- #include "driverlib/sysctl.h"
- #include "oled128x64.h"
- #include "oledfont.h"
- /*********************************************************************************
- *TivaC LaunchPad OLED模块驱动代码
- *SSD1306 OLED 驱动IC驱动代码
- *驱动方式:8080并口/4线串口
- *x-TAB
- *技术论坛:www.x-tab.cn
- *Created on: 2013-11-3
- *版本:V1.0
- *Copyright(C) x-TAB
- *All rights reserved
- *********************************************************************************/
- //-----------------OLED端口定义----------------
- #define OLED_CS_Clr() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1,0)
- #define OLED_CS_Set() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1,0x01<<1)
- #define OLED_RST_Clr() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_5,0)
- #define OLED_RST_Set() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_5,0x01<<5)
- #define OLED_DC_Clr() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_4,0)
- #define OLED_DC_Set() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_4,0x01<<4)
- #define OLED_WR_Clr() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,0)
- #define OLED_WR_Set() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2,0x01<<2)
- #define OLED_RD_Clr() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_3,0)
- #define OLED_RD_Set() GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_3,0x01<<3)
- //PB0~7,作为数据线
- #define DATAOUT(x) GPIOPinWrite(GPIO_PORTB_BASE,0xff,x);//输出
- //使用4线串行接口时使用
- #define OLED_SCLK_Clr() GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0,0)
- #define OLED_SCLK_Set() GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0,1)
- #define OLED_SDIN_Clr() GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_1,0)
- #define OLED_SDIN_Set() GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_1,0x01<<1)
- //OLED的显存
- //存放格式如下.
- //[0]0 1 2 3 ... 127
- //[1]0 1 2 3 ... 127
- //[2]0 1 2 3 ... 127
- //[3]0 1 2 3 ... 127
- //[4]0 1 2 3 ... 127
- //[5]0 1 2 3 ... 127
- //[6]0 1 2 3 ... 127
- //[7]0 1 2 3 ... 127
- uint8_t OLED_GRAM[128][8];
- //更新显存到LCD
- void OLED_Refresh_Gram(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD); //设置页地址(0~7)
- OLED_WR_Byte (0x00,OLED_CMD); //设置显示位置—列低地址
- OLED_WR_Byte (0x10,OLED_CMD); //设置显示位置—列高地址
- for(n=0;n<128;n++)OLED_WR_Byte(OLED_GRAM[n][i],OLED_DATA);
- }
- }
- #if OLED_MODE==1
- //向SSD1306写入一个字节。
- //dat:要写入的数据/命令
- //cmd:数据/命令标志 0,表示命令;1,表示数据;
- void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
- {
- DATAOUT(dat);
- if(cmd)
- OLED_DC_Set();
- else
- OLED_DC_Clr();
- OLED_CS_Clr();
- OLED_WR_Clr();
- OLED_WR_Set();
- OLED_CS_Set();
- OLED_DC_Set();
- }
- #else
- //向SSD1306写入一个字节。
- //dat:要写入的数据/命令
- //cmd:数据/命令标志 0,表示命令;1,表示数据;
- void OLED_WR_Byte(uint8_t dat,uint8_t cmd)
- {
- uint8_t i;
- if(cmd)
- OLED_DC_Set();
- else
- OLED_DC_Clr();
- OLED_CS_Clr();
- for(i=0;i<8;i++)
- {
- OLED_SCLK_Clr();
- if(dat&0x80)
- OLED_SDIN_Set();
- else
- OLED_SDIN_Clr();
- OLED_SCLK_Set();
- dat<<=1;
- }
- OLED_CS_Set();
- OLED_DC_Set();
- }
- #endif
- //开启OLED显示
- void OLED_Display_On(void)
- {
- OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
- OLED_WR_Byte(0X14,OLED_CMD); //DCDC ON
- OLED_WR_Byte(0XAF,OLED_CMD); //DISPLAY ON
- }
- //关闭OLED显示
- void OLED_Display_Off(void)
- {
- OLED_WR_Byte(0X8D,OLED_CMD); //SET DCDC命令
- OLED_WR_Byte(0X10,OLED_CMD); //DCDC OFF
- OLED_WR_Byte(0XAE,OLED_CMD); //DISPLAY OFF
- }
- //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
- void OLED_Clear(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)for(n=0;n<128;n++)OLED_GRAM[n][i]=0x00;
- OLED_Refresh_Gram();//更新显示
- }
- //画点
- //x:0~127
- //y:0~63
- //t:1 填充 0,清空
- void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t)
- {
- uint8_t pos,bx,temp=0;
- if(x>127||y>63)return;//超出范围了.
- pos=7-y/8;
- bx=y%8;
- temp=1<<(7-bx);
- if(t)OLED_GRAM[x][pos]|=temp;
- else OLED_GRAM[x][pos]&=~temp;
- }
- //x1,y1,x2,y2 填充区域的对角坐标
- //确保x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63
- //dot:0,清空;1,填充
- void OLED_Fill(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t dot)
- {
- uint8_t x,y;
- for(x=x1;x<=x2;x++)
- {
- for(y=y1;y<=y2;y++)OLED_DrawPoint(x,y,dot);
- }
- OLED_Refresh_Gram();//更新显示
- }
- //在指定位置显示一个字符,包括部分字符
- //x:0~127
- //y:0~63
- //mode:0,反白显示;1,正常显示
- //size:选择字体 16/12
- void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode)
- {
- uint8_t temp,t,t1;
- uint8_t y0=y;
- chr=chr-' ';//得到偏移后的值
- for(t=0;t
- {
- if(size==12)temp=oled_asc2_1206[chr][t]; //调用1206字体
- else temp=oled_asc2_1608[chr][t]; //调用1608字体
- for(t1=0;t1<8;t1++)
- {
- if(temp&0x80)OLED_DrawPoint(x,y,mode);
- else OLED_DrawPoint(x,y,!mode);
- temp<<=1;
- y++;
- if((y-y0)==size)
- {
- y=y0;
- x++;
- break;
- }
- }
- }
- }
- //m^n函数
- uint32_t oled_pow(uint8_t m,uint8_t n)
- {
- uint32_t result=1;
- while(n--)result*=m;
- return result;
- }
- //显示2个数字
- //x,y :起点坐标
- //len :数字的位数
- //size:字体大小
- //mode:模式 0,填充模式;1,叠加模式
- //num:数值(0~4294967295);
- void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size)
- {
- uint8_t t,temp;
- uint8_t enshow=0;
- for(t=0;t
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_ShowChar(x+(size/2)*t,y,' ',size,1);
- continue;
- }else enshow=1;
- }
- OLED_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
- }
- }
- //显示字符串
- //x,y:起点坐标
- //*p:字符串起始地址
- //用16字体
- void OLED_ShowString(uint8_t x,uint8_t y,const char *p)
- {
- #define MAX_CHAR_POSX 122
- #define MAX_CHAR_POSY 58
- while(*p!='\0')
- {
- if(x>MAX_CHAR_POSX){x=0;y+=16;}
- if(y>MAX_CHAR_POSY){y=x=0;OLED_Clear();}
- OLED_ShowChar(x,y,*p,16,1);
- x+=8;
- p++;
- }
- }
- //婓硌隅弇离珆尨珨跺犖趼
- //x:0~127
- //y:0~63
- //fno:犖趼晤瘍
- //mode:0,毀啞珆尨;1,淏都珆尨
- //22*22湮苤腔趼睫
- void OLED_ShowFont22(uint8_t x,uint8_t y,uint8_t fno,uint8_t mode)
- {
- uint8_t temp,t,t1;
- uint8_t y0=y;
- for(t=0;t<66;t++)
- {
- if(t<33)temp=user_hz[fno*2][t]; //覃蚚22*22犖趼
- else temp=user_hz[fno*2+1][t-33];
- for(t1=0;t1<8;t1++)
- {
- if(temp&0x80)OLED_DrawPoint(x,y,mode);
- else OLED_DrawPoint(x,y,!mode);
- temp<<=1;
- y++;
- if((y-y0)==22)
- {
- y=y0;
- x++;
- break;
- }
- }
- }
- }
- //初始化SSD1306
- void OLED_Init(void)
- {
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
- SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOE);
- GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE,GPIO_PIN_2|GPIO_PIN_3);//PE2,3推挽输出 (pe2-wr,pe3-rd)
- GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_2|GPIO_PIN_3,0xff);//PE2,3输出高
- #if OLED_MODE==1
- GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,0xff);//PB0~7数据端口设为输出
- GPIOPinWrite(GPIO_PORTB_BASE,0xff,0xff);//PB0~7输出高
- GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5);//PE1,4,5推挽输出 (pe1-cs,pe4-dc,pe5-rst)
- GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5,0xff);//PE1,4,5输出高
- #else
- GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE,GPIO_PIN_0|GPIO_PIN_1);//PB0,1输出 (4-Wire SPI pb0-sclk,pb1-sdin)
- GPIOPinWrite(GPIO_PORTB_BASE,GPIO_PIN_0|GPIO_PIN_1,0xff);//PB0,1输出高
- GPIOPinTypeGPIOOutput(GPIO_PORTE_BASE,GPIO_PIN_0|GPIO_PIN_5);//PE5推挽输出 (pe5-rst)
- GPIOPinWrite(GPIO_PORTE_BASE,GPIO_PIN_5,0x01<<5);//PE5输出高
- #endif
- OLED_RST_Clr();
- delay_ms(10);
- OLED_RST_Set();
- OLED_WR_Byte(0xAE,OLED_CMD); //关闭显示
- OLED_WR_Byte(0xD5,OLED_CMD); //设置时钟分频因子,震荡频率
- OLED_WR_Byte(80,OLED_CMD); //[3:0],分频因子;[7:4],震荡频率
- OLED_WR_Byte(0xA8,OLED_CMD); //设置驱动路数
- OLED_WR_Byte(0X3F,OLED_CMD); //默认0X3F(1/64)
- OLED_WR_Byte(0xD3,OLED_CMD); //设置显示偏移
- OLED_WR_Byte(0X00,OLED_CMD); //默认为0
- OLED_WR_Byte(0x40,OLED_CMD); //设置显示开始行 [5:0],行数.
- OLED_WR_Byte(0x8D,OLED_CMD); //电荷泵设置
- OLED_WR_Byte(0x14,OLED_CMD); //bit2,开启/关闭
- OLED_WR_Byte(0x20,OLED_CMD); //设置内存地址模式
- OLED_WR_Byte(0x02,OLED_CMD); //[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;
- OLED_WR_Byte(0xA1,OLED_CMD); //段重定义设置,bit0:0,0->0;1,0->127;
- OLED_WR_Byte(0xC0,OLED_CMD); //设置COM扫描方向;bit3:0,普通模式;1,重定义模式 COM[N-1]->COM0;N:驱动路数
- OLED_WR_Byte(0xDA,OLED_CMD); //设置COM硬件引脚配置
- OLED_WR_Byte(0x12,OLED_CMD); //[5:4]配置
- OLED_WR_Byte(0x81,OLED_CMD); //对比度设置
- OLED_WR_Byte(0xEF,OLED_CMD); //1~255;默认0X7F (亮度设置,越大越亮)
- OLED_WR_Byte(0xD9,OLED_CMD); //设置预充电周期
- OLED_WR_Byte(0xf1,OLED_CMD); //[3:0],PHASE 1;[7:4],PHASE 2;
- OLED_WR_Byte(0xDB,OLED_CMD); //设置VCOMH 电压倍率
- OLED_WR_Byte(0x30,OLED_CMD); //[6:4] 000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;
- OLED_WR_Byte(0xA4,OLED_CMD); //全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)
- OLED_WR_Byte(0xA6,OLED_CMD); //设置显示方式;bit0:1,反相显示;0,正常显示
- OLED_WR_Byte(0xAF,OLED_CMD); //开启显示
- OLED_Clear();
- }
- void delay_ms(uint8_t t) //延时约t*10ms
- {
- SysCtlDelay(t*SysCtlClockGet()/400);
- }
--------------------------------------oled128x64头文件-----------------------------------------------
- /*
- * oled128x64.h
- *
- * Created on: 2013-11-3
- * Author: Administrator
- */
- #ifndef OLED128X64_H_
- #define OLED128X64_H_
- /*********************************************************************************
- *TivaC LaunchPad OLED模块驱动代码
- *SSD1306 OLED 驱动IC驱动代码
- *驱动方式:8080并口/4线串口
- *x-TAB
- *技术论坛:www.x-tab.cn
- *Created on: 2013-11-3
- *版本:V1.0
- *Copyright(C) x-TAB
- *All rights reserved
- *********************************************************************************/
- //OLED模式设置
- //0:4线串行模式
- //1:并行8080模式
- #define OLED_MODE 1
- #define OLED_CMD 0 //写命令
- #define OLED_DATA 1 //写数据
- //OLED控制用函数
- extern void OLED_WR_Byte(uint8_t dat,uint8_t cmd);
- extern void OLED_Display_On(void);
- extern void OLED_Display_Off(void);
- extern void OLED_Refresh_Gram(void);
- extern void OLED_Init(void);
- extern void OLED_Clear(void);
- extern void OLED_DrawPoint(uint8_t x,uint8_t y,uint8_t t);
- extern void OLED_Fill(uint8_t x1,uint8_t y1,uint8_t x2,uint8_t y2,uint8_t dot);
- extern void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t size,uint8_t mode);
- extern void OLED_ShowNum(uint8_t x,uint8_t y,uint32_t num,uint8_t len,uint8_t size);
- extern void OLED_ShowString(uint8_t x,uint8_t y,const char *p);
- extern void OLED_ShowFont22(uint8_t x,uint8_t y,uint8_t fno,uint8_t mode);
- extern void delay_ms(uint8_t t);
- #endif /* OLED128X64_H_ */
main.c文件:
- /*
- * main.c
- */
- #include <stdint.h>
- #include <stdbool.h>
- #include "inc/hw_types.h"
- #include "inc/hw_memmap.h"
- #include "driverlib/gpio.h"
- #include "driverlib/pin_map.h"
- #include "driverlib/rom.h"
- #include "driverlib/sysctl.h"
- #include "driverlib/systick.h"
- #include "oled128x64.h"
- int main(void)
- {
- uint8_t t;
- //
- // Set the clocking to run directly from the crystal.
- //
- SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ |
- SYSCTL_OSC_MAIN);
- OLED_Init(); //場宎趙OLED
- OLED_ShowString(20,10,"X-TAB.cn");
- OLED_ShowString(4,32,"TivaC launchpad");
- //OLED_ShowFont22(59,32,0,1);
- //OLED_ShowFont22(81,32,1,1);
- //OLED_ShowFont22(103,32,2,1);
- OLED_Refresh_Gram();
- delay_ms(250);delay_ms(250);
- OLED_Clear();
- OLED_ShowString(0,0, "0.96' OLED TEST");
- OLED_ShowString(0,16,"X-TAB");
- OLED_ShowString(0,32,"2013/11/3");
- OLED_ShowString(0,48,"ASCII:");
- OLED_ShowString(63,48,"CODE:");
- OLED_Refresh_Gram();
- t=' ';
- while(1)
- {
- OLED_ShowChar(48,48,t,16,1);
- OLED_Refresh_Gram();
- t++;
- if(t>'~')t=' ';
- OLED_ShowNum(103,48,t,3,16);
- delay_ms(30);
- }
- }
[
本帖最后由 Study_Stellaris 于 2013-11-6 13:20 编辑 ]