- #include "oled.h"
- #include "oledfont.h"
- //向SSD1306寫入一個(gè)字節(jié)。
- //dat:要寫入的數(shù)據(jù)/命令
- //cmd:數(shù)據(jù)/命令標(biāo)志 0,表示命令;1,表示數(shù)據(jù);
- void OLED_WriteCmd(unsigned char cmd)
- {
- unsigned char i;
- DC(0);
- CS(0);
- for(i=0;i<8;i++)
- {
- SCL(0);
- if(cmd&0x80)
- {
- DIN(1);
- }
- else
- DIN(0);
- SCL(1);
- cmd<<=1;
- }
- CS(1);
- DC(1);
- }
- void OLED_Writebyte(unsigned char dat)
- {
- unsigned char i;
- DC(1);
- CS(0);
- for(i=0;i<8;i++)
- {
- SCL(0);
- if(dat&0x80)
- {
- DIN(1);
- }
- else
- DIN(0);
- SCL(1);
- dat<<=1;
- }
- CS(1);
- DC(1);
- }
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WriteCmd(0xb0+y);
- OLED_WriteCmd(((x&0xf0)>>4)|0x10);
- OLED_WriteCmd((x&0x0f)|0x01);
- }
- //開啟OLED顯示
- void OLED_Display_On(void)
- {
- OLED_WriteCmd(0X8D); //SET DCDC命令
- OLED_WriteCmd(0X14); //DCDC ON
- OLED_WriteCmd(0XAF); //DISPLAY ON
- }
- //關(guān)閉OLED顯示
- void OLED_Display_Off(void)
- {
- OLED_WriteCmd(0X8D); //SET DCDC命令
- OLED_WriteCmd(0X10); //DCDC OFF
- OLED_WriteCmd(0XAE); //DISPLAY OFF
- }
- //清屏函數(shù),清完屏,整個(gè)屏幕是黑色的!和沒點(diǎn)亮一樣!!!
- void OLED_Clear(void)
- {
- unsigned char i,n;
- for(i=0;i<8;i++)
- {
- OLED_WriteCmd (0xb0+i); //設(shè)置頁地址(0~7)
- OLED_WriteCmd (0x00); //設(shè)置顯示位置—列低地址
- OLED_WriteCmd (0x10); //設(shè)置顯示位置—列高地址
- for(n=0;n<128;n++)OLED_Writebyte(0);
- } //更新顯示
- }
- //在指定位置顯示一個(gè)字符,包括部分字符
- //x:0~127
- //y:0~63
- //mode:0,反白顯示;1,正常顯示
- //size:選擇字體 16/12
- void OLED_WriteChar(unsigned char x,unsigned char y,unsigned char chr)
- {
- unsigned char c=0,i=0;
- c=chr-' ';//得到偏移后的值
- if(x>127)
- {
- x=0;
- y=y+2;
- }
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_Writebyte(F8X16[c*16+i]);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_Writebyte(F8X16[c*16+i+8]);
- }
- //m^n函數(shù)
- unsigned int oled_pow(unsigned char m,unsigned char n)
- {
- unsigned int result=1;
- while(n--)result*=m;
- return result;
- }
- //顯示2個(gè)數(shù)字
- //x,y :起點(diǎn)坐標(biāo)
- //len :數(shù)字的位數(shù)
- //size:字體大小
- //mode:模式 0,填充模式;1,疊加模式
- //num:數(shù)值(0~4294967295);
- void OLED_WriteNum1(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size2)
- {
- unsigned char t,temp;
- unsigned char enWrite=0;
- for(t=0;t<len;t++)
- {
- temp=(num/oled_pow(10,len-t-1))%10;
- if(enWrite==0&&t<(len-1))
- {
- if(temp==0)
- {
- OLED_WriteChar(x+(size2/2)*t,y,' ');
- continue;
- }else enWrite=1;
- }
- OLED_WriteChar(x+(size2/2)*t,y,temp+'0');
- }
- }
- void OLED_WriteNum2(unsigned char x,unsigned char y,unsigned int num)
- {
- num=num+0x30;
- OLED_WriteChar(x,y,num);
- }
- //顯示一個(gè)字符號(hào)串
- void OLED_WriteString(unsigned char x,unsigned char y,unsigned char *chr)
- {
- unsigned char j=0;
- while (chr[j]!='\0')
- { OLED_WriteChar(x,y,chr[j]);
- x+=8;
- if(x>120){x=0;y+=2;}
- j++;
- }
- }
- //顯示漢字
- void OLED_WriteChinese(unsigned char x,unsigned char y,unsigned char no)
- {
- unsigned char t,adder=0;
- OLED_Set_Pos(x,y);
- for(t=0;t<16;t++)
- {
- OLED_Writebyte(Hzk[2*no][t]);
- adder+=1;
- }
- OLED_Set_Pos(x,y+1);
- for(t=0;t<16;t++)
- {
- OLED_Writebyte(Hzk[2*no+1][t]);
- adder+=1;
- }
- }
- /***********功能描述:顯示顯示BMP圖片128×64起始點(diǎn)坐標(biāo)(x,y),x的范圍0~127,y為頁的范圍0~7*****************/
- void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char BMP[])
- {
- unsigned int j=0;
- unsigned char x,y;
- if(y1%8==0) y=y1/8;
- else y=y1/8+1;
- for(y=y0;y<y1;y++)
- {
- OLED_Set_Pos(x0,y);
- for(x=x0;x<x1;x++)
- {
- OLED_Writebyte(BMP[j++]);
- }
- }
- }
- //初始化SSD1306
- void OLED_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;//定義GPIO結(jié)構(gòu)體
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB, ENABLE); //使能PC,D,G端口時(shí)鐘
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7; //PD3,PD6推挽輸出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化GPIOD3,6
- GPIO_SetBits(GPIOA,GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_7); //PA3,PA6 輸出高
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //PD3,PD6推挽輸出
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//速度50MHz
- GPIO_Init(GPIOB, &GPIO_InitStructure); //初始化GPIOD3,6
- RES(1);
- delay_ms(100);
- RES(0);
- delay_ms(100);
- RES(1);
- OLED_WriteCmd(0xAE);//--turn off oled panel
- OLED_WriteCmd(0x00);//---set low column address
- OLED_WriteCmd(0x10);//---set high column address
- OLED_WriteCmd(0x40);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WriteCmd(0x81);//--set contrast control register
- OLED_WriteCmd(0xCF); // Set SEG Output Current Brightness
- OLED_WriteCmd(0xA1);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WriteCmd(0xC8);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WriteCmd(0xA6);//--set normal display
- OLED_WriteCmd(0xA8);//--set multiplex ratio(1 to 64)
- OLED_WriteCmd(0x3f);//--1/64 duty
- OLED_WriteCmd(0xD3);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WriteCmd(0x00);//-not offset
- OLED_WriteCmd(0xd5);//--set display clock divide ratio/oscillator frequency
- OLED_WriteCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WriteCmd(0xD9);//--set pre-charge period
- OLED_WriteCmd(0xF1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WriteCmd(0xDA);//--set com pins hardware configuration
- OLED_WriteCmd(0x12);
- OLED_WriteCmd(0xDB);//--set vcomh
- OLED_WriteCmd(0x40);//Set VCOM Deselect Level
- OLED_WriteCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WriteCmd(0x02);//
- OLED_WriteCmd(0x8D);//--set Charge Pump enable/disable
- OLED_WriteCmd(0x14);//--set(0x10) disable
- OLED_WriteCmd(0xA4);// Disable Entire Display On (0xa4/0xa5)
- OLED_WriteCmd(0xA6);// Disable Inverse Display On (0xa6/a7)
- OLED_WriteCmd(0xAF);//--turn on oled panel
- OLED_WriteCmd(0xAF); /*display ON*/
- OLED_Clear();
- OLED_Set_Pos(0,0);
- }
- void CS(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_4); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_4);
- }
- }
- void DC(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_1); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_1);
- }
- }
- void RES(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOB,GPIO_Pin_0); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOB,GPIO_Pin_0);
- }
- }
- void DIN(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_7); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_7);
- }
- }
- void SCL(unsigned char i)
- {
- if(i==0)
- {
- GPIO_ResetBits(GPIOA,GPIO_Pin_5); //CS(0)
- }
- if(i==1)
- {
- GPIO_SetBits(GPIOA,GPIO_Pin_5);
- }
- }
復(fù)制代碼
【必讀】版權(quán)免責(zé)聲明
1、本主題所有言論和內(nèi)容純屬會(huì)員個(gè)人意見,與本論壇立場(chǎng)無關(guān)。2、本站對(duì)所發(fā)內(nèi)容真實(shí)性、客觀性、可用性不做任何保證也不負(fù)任何責(zé)任,網(wǎng)友之間僅出于學(xué)習(xí)目的進(jìn)行交流。3、對(duì)提供的數(shù)字內(nèi)容不擁有任何權(quán)利,其版權(quán)歸原著者擁有。請(qǐng)勿將該數(shù)字內(nèi)容進(jìn)行商業(yè)交易、轉(zhuǎn)載等行為,該內(nèi)容只為學(xué)習(xí)所提供,使用后發(fā)生的一切問題與本站無關(guān)。 4、本網(wǎng)站不保證本站提供的下載資源的準(zhǔn)確性、安全性和完整性;同時(shí)本網(wǎng)站也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的損失或傷害。 5、本網(wǎng)站所有軟件和資料均為網(wǎng)友推薦收集整理而來,僅供學(xué)習(xí)用途使用,請(qǐng)務(wù)必下載后兩小時(shí)內(nèi)刪除,禁止商用。6、如有侵犯你版權(quán)的,請(qǐng)及時(shí)聯(lián)系我們(電子郵箱1370723259@qq.com)指出,本站將立即改正。
|