繼昨天發的一個貼,都是多功能數顯溫濕度計,但是該貼用到的時OLED顯示,這個不僅可以顯示數字和字符,而且還可以顯示漢字和圖片,當然本設計并未顯示圖片,圖片的顯示太耗內存了,具體的顯示效果如下面兩張圖片。
這個圖片是開機畫面,經過大概5s中后切換到下一個界面,也就是下一張圖。
這個界面主要顯示日期、時間、溫度以及濕度
具體的源代碼在附件里面。
源代碼中有一部分代碼被注銷了,這包括了顯示圖片的代碼和字庫以及顯示字符串的函數,如果需要顯示圖片和字符串的話可以取消注釋,調用顯示圖片的函數。
注意一個問題,由于本設計使用的是15W4k58s4單片機晶振先用內部晶振115200,如果使用89C82請注意程序中對時間有要求的地方,如DS1302的時序。
單片機源程序如下:
- #include "OLED_0.96_spi.h"
- /**************************************
- 功能描述:延時函數
- 入口參數:uint x ,該值為1時,延時1ms
- 返回值:無
- ***************************************/
- void delay_ms(uint16_t x)
- {
- uint16_t j,i;
- for(j=0;j<x;j++)
- {
- for(i=0;i<1100;i++);
- }
- }
- /******************************************************************************
- * 描 述 : 向OLED寫入1字節數據
- * 入 參 : dat:數據;mode:=0:寫入命令,=1:寫入數據
- * 返回值 : 無
- ******************************************************************************/
- void OLED_WrByte(uint8_t dat,uint8_t mode)
- {
- uint8_t i=8, temp=0;
-
- if(mode == OLED_WR_CMD)LCD_DC = 0;
- else LCD_DC = 1;
-
- for(i=0; i<8; i++) //發送一個八位數據
- {
- LCD_SCL = 0;
-
- temp = dat&0x80;
- if (temp == 0)
- {
- LCD_SDA = 0;
- }
- else
- {
- LCD_SDA = 1;
- }
- dat <<= 1;
- LCD_SCL = 1;
- }
- }
-
- /******************************************************************************
- * 描 述 : 設置坐標
- * 入 參 : x:x坐標;y:y坐標
- * 返回值 : 無
- ******************************************************************************/
- void OLED_Set_Pos(uint8_t x, uint8_t y)
- {
- OLED_WrByte((0xb0+y),OLED_WR_CMD);
- OLED_WrByte(((x&0xf0)>>4)|0x10,OLED_WR_CMD);
- OLED_WrByte((x&0x0f)|0x01,OLED_WR_CMD);
- }
- /******************************************************************************
- * 描 述 : LCD初始化
- * 入 參 : 無
- * 返回值 : 無
- ******************************************************************************/
- void OLED_Fill(uint8_t dat)
- {
- uint8_t y,x;
- for(y=0;y<8;y++)
- {
- OLED_WrByte(0xb0+y,OLED_WR_CMD);//設置頁地址(0~7)
- OLED_WrByte(0x02,OLED_WR_CMD); //設置顯示位置—列低地址
- OLED_WrByte(0x10,OLED_WR_CMD); //設置顯示位置—列高地址
- for(x=0; x<X_WIDTH; x++)
- OLED_WrByte(dat,OLED_WR_DAT);
- }
- }
- /******************************************************************************
- * 描 述 : 指定位置顯示一個字符
- * 入 參 : x:列0~127;y:頁地址0~7;
- * 返回值 : 無
- ******************************************************************************/
- void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr)
- {
- uint8_t c=0,i=0;
-
- c = chr-' ';//得到偏移后的值
- if(x > Max_Column-1)
- {
- x = 0;
- y = y+2;
- }
- OLED_Set_Pos(x,y);
- for(i=0; i<8; i++)OLED_WrByte(F8X16[c*16+i],OLED_WR_DAT);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)OLED_WrByte(F8X16[c*16+i+8],OLED_WR_DAT);
- }
- /******************************************************************************
- * 描 述 : 顯示8*16一組標準ASCII字符串
- * 入 參 : x:列0~127;y:頁地址0~7;
- * 返回值 : 無
- ******************************************************************************/
- //void LCD_P8x16Str(uint8_t x, uint8_t y,uint8_t ch[])
- //{
- // uint8_t c=0,i=0,j=0;
- //
- // while (ch[j] != '\0')
- // {
- // c = ch[j]-32;
- // if(x>120){x=0;y++;}
- // OLED_Set_Pos(x,y);
- // for(i=0; i<8; i++)OLED_WrByte(F8X16[c*16+i],OLED_WR_DAT);
- // OLED_Set_Pos(x,y+1);
- // for(i=0;i<8;i++) OLED_WrByte(F8X16[c*16+i+8],OLED_WR_DAT);
- // x += 8;
- // j++;
- // }
- //}
- /******************************************************************************
- * 描 述 : 顯示漢字
- * 入 參 : x:列0~127;y:頁地址0~7;
- * 返回值 : 無
- ******************************************************************************/
- void LCD_P16x16Ch(uint8_t x, uint8_t y, uint8_t N)
- {
- uint8_t wm=0;
- unsigned int adder=32*N;
- OLED_Set_Pos(x , y);
- for(wm = 0;wm < 16;wm++)
- {
- OLED_WrByte(F16x16[adder],OLED_WR_DAT);
- adder += 1;
- }
- OLED_Set_Pos(x,y + 1);
- for(wm = 0;wm < 16;wm++)
- {
- OLED_WrByte(F16x16[adder],OLED_WR_DAT);
- adder += 1;
- }
- }
- /******************************************************************************
- * 描 述 : 顯示BMP圖片128×64
- * 入 參 : 起始點坐標(x,y),x的范圍0~127,y為頁的范圍0~7
- * 返回值 : 無
- ******************************************************************************/
- //void OLED_DrawBMP(uint8_t x0, uint8_t y0,uint8_t x1, uint8_t y1,const uint8_t BMP[])
- //{
- // uint16_t j=0;
- // uint8_t 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_WrByte(BMP[j++],OLED_WR_DAT);
- // }
- // }
- //}
- /******************************************************************************
- * 描 述 : LCD初始化
- * 入 參 : 無
- * 返回值 : 無
- ******************************************************************************/
- void OLED_Init(void)
- {
- //P0DIR &= ~0x04; //配置P0.2為輸出
- //P1DIR &= ~0x70; //配置P1.4 P1.5 P1.6為輸出
- delay_ms(10);
- LCD_CS=1;
- delay_ms(10);
- LCD_CS=0;
-
- delay_ms(200);
- OLED_WrByte(0xae,OLED_WR_CMD);//--turn off oled panel
- OLED_WrByte(0x00,OLED_WR_CMD);//---set low column address
- OLED_WrByte(0x10,OLED_WR_CMD);//---set high column address
- OLED_WrByte(0x40,OLED_WR_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WrByte(0x81,OLED_WR_CMD);//--set contrast control register
- OLED_WrByte(0xcf,OLED_WR_CMD); // Set SEG Output Current Brightness
-
- OLED_WrByte(0xa1,OLED_WR_CMD);//--Set SEG/Column Mapping 0xa0左右反置 0xa1正常
- OLED_WrByte(0xc8,OLED_WR_CMD);//Set COM/Row Scan Direction 0xc0上下反置 0xc8正常
- OLED_WrByte(0xa6,OLED_WR_CMD);//--set normal display
- OLED_WrByte(0xa8,OLED_WR_CMD);//--set multiplex ratio(1 to 64)
- OLED_WrByte(0x3f,OLED_WR_CMD);//--1/64 duty
-
- OLED_WrByte(0xd3,OLED_WR_CMD);//-set display offset Shift Mapping RAM Counter (0x00~0x3F)
- OLED_WrByte(0x00,OLED_WR_CMD);//-not offset
-
- OLED_WrByte(0xd5,OLED_WR_CMD);//--set display clock divide ratio/oscillator frequency
- OLED_WrByte(0x80,OLED_WR_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WrByte(0xd9,OLED_WR_CMD);//--set pre-charge period
-
- OLED_WrByte(0xf1,OLED_WR_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- //OLED_WrByte(0x22,OLED_WR_CMD);
- OLED_WrByte(0xda,OLED_WR_CMD);//--set com pins hardware configuration
- OLED_WrByte(0x12,OLED_WR_CMD);
- OLED_WrByte(0xdb,OLED_WR_CMD);//--set vcomh
-
- OLED_WrByte(0x40,OLED_WR_CMD);//Set VCOM Deselect Level
-
- OLED_WrByte(0x20,OLED_WR_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WrByte(0x02,OLED_WR_CMD);//
- OLED_WrByte(0x8d,OLED_WR_CMD);//--set Charge Pump enable/disable
- OLED_WrByte(0x14,OLED_WR_CMD);//--set(0x10) disable
- OLED_WrByte(0xa4,OLED_WR_CMD);// Disable Entire Display On (0xa4/0xa5)
- OLED_WrByte(0xa6,OLED_WR_CMD);// Disable Inverse Display On (0xa6/a7)
- OLED_WrByte(0xaf,OLED_WR_CMD);//--turn on oled panel
- OLED_Fill(0xff); //初始清屏
- OLED_Set_Pos(0,0);
- }
- /*********************************END FILE*************************************/
復制代碼
所有資料51hei提供下載:
OLED_0.96_spi顯示.zip
(8.22 MB, 下載次數: 120)
2018-3-24 17:17 上傳
點擊文件名下載附件
基于15w系列單片機程序
|