请下载附件查看
- #include "oled.h"
- #include "font.h"
- //SSD1303 OLED 串行接口驱动代码
- //LCD显存存放格式如下
- //[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 LCD_GRAM[128][8];
- //更新显存到LCD
- void Refresh_Gram(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)
- {
- WriteCommand (0xb0+i); //设置显示位置—行
- WriteCommand (0x02); //设置显示位置—列低地址,偏移了2列
- WriteCommand (0x10); //设置显示位置—列高地址
- for(n=0;n<128;n++)WriteData(LCD_GRAM[n][i]);
- }
- }
- //SSD1303写命令
- //dat:要写入的字节
- void WriteCommand(uint8_t cmd)
- {
- uint8_t i;
- OLED_DC_SET(0); //写命令0,写数据1
- OLED_CS_SET(0);
- for(i=0x80;i>0;i>>=1)
- {
- OLED_SCLK_SET(0); //
- if(cmd&i)OLED_SDIN_SET(1); //写1
- else OLED_SDIN_SET(0); //写0
- OLED_SCLK_SET(1);
- }
- OLED_DC_SET(1);
- OLED_CS_SET(1);
- }
- //SSD1303写数据
- //dat:要写入的字节
- void WriteData( uint8_t dat)
- {
- uint8_t i;
- OLED_DC_SET(1); //写命令0,写数据1
- OLED_CS_SET(0);
- for(i=0x80;i>0;i>>=1)
- {
- OLED_SCLK_SET(0); //
- if(dat&i)OLED_SDIN_SET(1); //写1
- else OLED_SDIN_SET(0); //写0
- OLED_SCLK_SET(1);
- }
- OLED_DC_SET(1);
- OLED_CS_SET(1);
- }
- //打开OLED
- void LCD_Display_On(void)
- {
- WriteCommand(0XAD); //SET DCDC命令
- WriteCommand(0X8B); //DCDC ON
- WriteCommand(0XAF); //DISPLAY ON
- }
- //关闭OLED
- void LCD_Display_Off(void)
- {
- WriteCommand(0XAD); //SET DCDC命令
- WriteCommand(0X8A); //DCDC OFF
- WriteCommand(0XAE); //DISPLAY OFF
- }
- //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!
- void LCD_Clear(void)
- {
- uint8_t i,n;
- for(i=0;i<8;i++)for(n=0;n<128;n++)LCD_GRAM[n][i]=0X00;
- Refresh_Gram();//更新显示
- }
- //画点
- //x:0~127
- //y:0~63
- //t:1 填充 0,清空
- void LCD_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)LCD_GRAM[x][pos]|=temp;
- else LCD_GRAM[x][pos]&=~temp;
- }
- //x1,y1,x2,y2 填充区域的对角坐标
- //确保x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63
- //dot:0,清空;1,填充
- void LCD_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++)LCD_DrawPoint(x,y,dot);
- }
- Refresh_Gram();//更新显示
- }
- //在指定位置显示一个字符,包括部分字符
- //x:0~127
- //y:0~63
- //mode:0,反白显示;1,正常显示
- //size:选择字体 16/12
- void LCD_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<size;t++)
- {
- if(size==12)temp=asc2_1206[chr][t]; //调用1206字体
- else temp=asc2_1608[chr][t]; //调用1608字体
- for(t1=0;t1<8;t1++)
- {
- if(temp&0x80)LCD_DrawPoint(x,y,mode);
- else LCD_DrawPoint(x,y,!mode);
- temp<<=1;
- y++;
- if((y-y0)==size)
- {
- y=y0;
- x++;
- break;
- }
- }
- }
- }
- //m^n函数
- uint32_t mypow(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~65536);
- void LCD_ShowNum(uint8_t x, uint8_t y, uint16_t num, uint8_t len, uint8_t size)
- {
- uint8_t t,temp;
- for(t=0;t<len;t++)
- {
- temp=(num/mypow(10,len-t-1))%10;
- LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
- }
- }
- //显示字符串
- //x,y:起点坐标
- //*p:字符串起始地址
- //用16字体
- void LCD_ShowString(uint8_t x, uint8_t y, const uint8_t *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;LCD_Clear();}
- LCD_ShowChar(x,y,*p,16,1);
- x+=8;
- p++;
- }
- }
- void Oled_GPIO_Configuration(void)
- {
- P3DIR |= OLED_DC + OLED_RST;
- P5DIR |= OLED_SDIN + OLED_SCLK + OLED_CS;
- }
- //初始化SSD1303
- void LCD_Init(void)
- {
- uint32_t i;
- Oled_GPIO_Configuration();
-
- OLED_RST_SET(0);
- for(i=0;i<100000;i++);
- OLED_RST_SET(1);
-
- WriteCommand(0x00);//设置地位列地址,低四位数据
- WriteCommand(0x10);//设置高位列地址,低四位数据
- WriteCommand(0x40);//设置显示开始行地址,低五位有效 0~63
- //设置对比度
- WriteCommand(0x81);
- WriteCommand(0x50);//对比度值:0x00~0xff
- //重映射
- WriteCommand(0xA1);//映射,A0:列0映射到SEG0;A1:列131映射到SEG0
- //全显示设置 ON/OFF
- WriteCommand(0xA4);//A5:全部显示 A4:普通显示
- //普通显示/反相显示
- WriteCommand(0XA6); //A6:普通显示 A7:反相显示
- //多元比率
- WriteCommand(0xA8);//设置多元比率
- WriteCommand(0x3f);//设置为0x3f;16~64
- //设置DCDC
- WriteCommand(0xAD);//设置DCDC
- WriteCommand(0x8A);//8B=ON, 8A=Off
- //显示设置
- WriteCommand(0xAE);// AF=ON , AE=OFF
- //显示偏移设置
- WriteCommand(0xD3);//设置显示偏移
- WriteCommand(0x00);//不偏移
- //设置显示时钟分频因子 (刷新率)
- WriteCommand(0xD5);//分频因子设置
- WriteCommand(0xF0);
- //区域颜色模式
- WriteCommand(0xD8);//设置区域颜色模式
- WriteCommand(0x00);//单色模式
- //COM Pins Hardware Configuration
- WriteCommand(0xDA);// Set Pins HardwareConfiguration
- WriteCommand(0x12);
- //VCOMH设置
- WriteCommand(0xDB);//Set VCOMH
- WriteCommand(0x00);
- //VP设置
- WriteCommand(0xD9);//Set VP
- WriteCommand(0x22);//P1=2 , P2=2
- WriteCommand(0xc0);//配置成标准应用
- LCD_Display_On(); //执行到这里,是看不到任何反应的!!
- LCD_Clear(); //清屏幕
- //LCD_Fill(0,0,128,64,1);//显示出来
- }
复制代码
代码工程下载:
test_1.7z
(676.46 KB, 下载次数: 35)
|