- #include "oled.h"
- #include "oledfont.h"
- //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
- void delay_ms(unsigned int ms)
- {
- unsigned int a;
- while(ms)
- {
- a=1800;
- while(a--);
- ms--;
- }
- return;
- }
- //反顯函數(shù)
- void OLED_ColorTurn(u8 i)
- {
- if(i==0)
- {
- OLED_WR_Byte(0xA6,OLED_CMD);//正常顯示
- }
- if(i==1)
- {
- OLED_WR_Byte(0xA7,OLED_CMD);//反色顯示
- }
- }
- //屏幕旋轉(zhuǎn)180度
- void OLED_DisplayTurn(u8 i)
- {
- if(i==0)
- {
- OLED_WR_Byte(0xC8,OLED_CMD);//正常顯示
- OLED_WR_Byte(0xA1,OLED_CMD);
- }
- if(i==1)
- {
- OLED_WR_Byte(0xC0,OLED_CMD);//反轉(zhuǎn)顯示
- OLED_WR_Byte(0xA0,OLED_CMD);
- }
- }
- //延時
- void IIC_delay(void)
- {
- u8 t=1;
- while(t--);
- }
- //起始信號
- void I2C_Start(void)
- {
- OLED_SDA_Set();
- OLED_SCL_Set();
- IIC_delay();
- OLED_SDA_Clr();
- IIC_delay();
- OLED_SCL_Clr();
-
- }
- //結(jié)束信號
- void I2C_Stop(void)
- {
- OLED_SDA_Clr();
- OLED_SCL_Set();
- IIC_delay();
- OLED_SDA_Set();
- }
- //等待信號響應(yīng)
- void I2C_WaitAck(void) //測數(shù)據(jù)信號的電平
- {
- OLED_SDA_Set();
- IIC_delay();
- OLED_SCL_Set();
- IIC_delay();
- OLED_SCL_Clr();
- IIC_delay();
- }
- //寫入一個字節(jié)
- void Send_Byte(u8 dat)
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- OLED_SCL_Clr();//將時鐘信號設(shè)置為低電平
- if(dat&0x80)//將dat的8位從最高位依次寫入
- {
- OLED_SDA_Set();
- }
- else
- {
- OLED_SDA_Clr();
- }
- IIC_delay();
- OLED_SCL_Set();
- IIC_delay();
- OLED_SCL_Clr();
- dat<<=1;
- }
- }
- //發(fā)送一個字節(jié)
- //向SSD1309寫入一個字節(jié)。
- //mode:數(shù)據(jù)/命令標(biāo)志 0,表示命令;1,表示數(shù)據(jù);
- void OLED_WR_Byte(u8 dat,u8 mode)
- {
- I2C_Start();
- Send_Byte(0x78);
- I2C_WaitAck();
- if(mode){Send_Byte(0x40);}
- else{Send_Byte(0x00);}
- I2C_WaitAck();
- Send_Byte(dat);
- I2C_WaitAck();
- I2C_Stop();
- }
- //坐標(biāo)設(shè)置
- void OLED_Set_Pos(u8 x, u8 y)
- {
- OLED_WR_Byte(0xb0+y,OLED_CMD);
- OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
- OLED_WR_Byte((x&0x0f),OLED_CMD);
- }
- //開啟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
- }
- //關(guān)閉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
- }
- //清屏函數(shù),清完屏,整個屏幕是黑色的!和沒點亮一樣!!!
- void OLED_Clear(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD); //設(shè)置頁地址(0~7)
- OLED_WR_Byte (0x00,OLED_CMD); //設(shè)置顯示位置—列低地址
- OLED_WR_Byte (0x10,OLED_CMD); //設(shè)置顯示位置—列高地址
- for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
- } //更新顯示
- }
- //在指定位置顯示一個字符,包括部分字符
- //x:0~127
- //y:0~63
- //sizey:選擇字體 6x8 8x16
- void OLED_ShowChar(u8 x,u8 y,u8 chr,u8 sizey)
- {
- u8 c=0,sizex=sizey/2;
- u16 i=0,size1;
- if(sizey==8)size1=6;
- else size1=(sizey/8+((sizey%8)?1:0))*(sizey/2);
- c=chr-' ';//得到偏移后的值
- OLED_Set_Pos(x,y);
- for(i=0;i<size1;i++)
- {
- if(i%sizex==0&&sizey!=8) OLED_Set_Pos(x,y++);
- if(sizey==8) OLED_WR_Byte(asc2_0806[c][i],OLED_DATA);//6X8字號
- else if(sizey==16) OLED_WR_Byte(asc2_1608[c][i],OLED_DATA);//8x16字號
- // else if(sizey==xx) OLED_WR_Byte(asc2_xxxx[c][i],OLED_DATA);//用戶添加字號
- else return;
- }
- }
- //m^n函數(shù)
- u32 oled_pow(u8 m,u8 n)
- {
- u32 result=1;
- while(n--)result*=m;
- return result;
- }
- //顯示數(shù)字
- //x,y :起點坐標(biāo)
- //num:要顯示的數(shù)字
- //len :數(shù)字的位數(shù)
- //sizey:字體大小
- void OLED_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 sizey)
- {
- u8 t,temp,m=0;
- u8 enshow=0;
- if(sizey==8)m=2;
- for(t=0;t<len;t++)
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enshow==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_ShowChar(x+(sizey/2+m)*t,y,' ',sizey);
- continue;
- }else enshow=1;
- }
- OLED_ShowChar(x+(sizey/2+m)*t,y,temp+'0',sizey);
- }
- }
- //顯示一個字符號串
- void OLED_ShowString(u8 x,u8 y,u8 *chr,u8 sizey)
- {
- u8 j=0;
- while (chr[j]!='\0')
- {
- OLED_ShowChar(x,y,chr[j++],sizey);
- if(sizey==8)x+=6;
- else x+=sizey/2;
- }
- }
- //顯示漢字
- void OLED_ShowChinese(u8 x,u8 y,u8 no,u8 sizey)
- {
- u16 i,size1=(sizey/8+((sizey%8)?1:0))*sizey;
- for(i=0;i<size1;i++)
- {
- if(i%sizey==0) OLED_Set_Pos(x,y++);
- if(sizey==16) OLED_WR_Byte(Hzk[no][i],OLED_DATA);//16x16字號
- // else if(sizey==xx) OLED_WR_Byte(xxx[c][i],OLED_DATA);//用戶添加字號
- else return;
- }
- }
- //顯示圖片
- //x,y顯示坐標(biāo)
- //sizex,sizey,圖片長寬
- //BMP:要顯示的圖片
- void OLED_DrawBMP(u8 x,u8 y,u8 sizex, u8 sizey,u8 BMP[])
- {
- u16 j=0;
- u8 i,m;
- sizey=sizey/8+((sizey%8)?1:0);
- for(i=0;i<sizey;i++)
- {
- OLED_Set_Pos(x,i+y);
- for(m=0;m<sizex;m++)
- {
- OLED_WR_Byte(BMP[j++],OLED_DATA);
- }
- }
- }
- //初始化
- void OLED_Init(void)
- {
- OLED_RES_Clr();
- delay_ms(200);
- OLED_RES_Set();
-
- OLED_WR_Byte(0xFD,OLED_CMD);
- OLED_WR_Byte(0x12,OLED_CMD);
- OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
- OLED_WR_Byte(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
- OLED_WR_Byte(0xA0,OLED_CMD);
- OLED_WR_Byte(0xA8,OLED_CMD);//--set multiplex ratio(1 to 64)
- OLED_WR_Byte(0x3f,OLED_CMD);//--1/64 duty
- OLED_WR_Byte(0xD3,OLED_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WR_Byte(0x00,OLED_CMD);//-not offset
- OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WR_Byte(0xA1,OLED_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WR_Byte(0xC8,OLED_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
- OLED_WR_Byte(0x12,OLED_CMD);
- OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
- OLED_WR_Byte(0x7F,OLED_CMD);// Set SEG Output Current Brightness
- OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
- OLED_WR_Byte(0x82,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
- OLED_WR_Byte(0x34,OLED_CMD);//Set VCOM Deselect Level
- OLED_WR_Byte(0xA4,OLED_CMD);// Disable Entire Display On (0xa4/0xa5)
- OLED_WR_Byte(0xA6,OLED_CMD);// Disable Inverse Display On (0xa6/a7)
- OLED_Clear();
- OLED_WR_Byte(0xAF,OLED_CMD);
- }
復(fù)制代碼
51hei.png (4.68 KB, 下載次數(shù): 46)
下載附件
2022-3-28 22:18 上傳
以上4個文件下載:
2.42OLED顯示屏51程序.zip
(6.67 KB, 下載次數(shù): 54)
2022-3-28 12:05 上傳
點擊文件名下載附件
2.4寸oled源碼 下載積分: 黑幣 -5
|