請下載附件查看
- #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)
2021-6-14 04:56 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|