51的OLED顯示
單片機源程序如下:
- #include<reg52.h>
- #include<intrins.h>
- #define u8 unsigned char
- #define u32 unsigned int
- #define OLED_CMD 0 //寫命令
- #define OLED_DATA 1 //寫數據
- #define OLED_MODE 0
- #define OLED_CS_Clr() OLED_CS=0
- #define OLED_CS_Set() OLED_CS=1
- #define OLED_RST_Clr() OLED_RST=0
- #define OLED_RST_Set() OLED_RST=1
- #define OLED_DC_Clr() OLED_DC=0
- #define OLED_DC_Set() OLED_DC=1
- #define OLED_SCLK_Clr() OLED_SCL=0
- #define OLED_SCLK_Set() OLED_SCL=1
- #define OLED_SDIN_Clr() OLED_SDIN=0
- #define OLED_SDIN_Set() OLED_SDIN=1;
- #define SIZE 16
- #define XLevelL 0x02
- #define XLevelH 0x10
- #define Max_Column 128
- #define Max_Row 64
- #define Brightness 0xFF
- #define X_WIDTH 128
- #define Y_WIDTH 64
- sbit OLED_CS=P1^4; //片選
- sbit OLED_RST =P1^2;//復位
- sbit OLED_DC =P1^3;//數據/命令控制
- sbit OLED_SCL=P1^0;//時鐘 D0(SCLK?
- sbit OLED_SDIN=P1^1;//D1(MOSI) 數據
- unsigned char code Hzk[][32]={
- {0x00,0x00,0xF8,0x88,0x88,0x88,0x88,0xFF,0x88,0x88,0x88,0x88,0xF8,0x00,0x00,0x00},
- {0x00,0x00,0x1F,0x08,0x08,0x08,0x08,0x7F,0x88,0x88,0x88,0x88,0x9F,0x80,0xF0,0x00},/*"電",0*/
- {0x10,0x10,0xD0,0xFF,0x90,0x10,0x00,0xFE,0x02,0x02,0x02,0xFE,0x00,0x00,0x00,0x00},
- {0x04,0x03,0x00,0xFF,0x00,0x83,0x60,0x1F,0x00,0x00,0x00,0x3F,0x40,0x40,0x78,0x00},/*"機",1*/
- {0x00,0x08,0x30,0x00,0xFF,0x20,0x20,0x20,0x20,0xFF,0x20,0x20,0x22,0x2C,0x20,0x00},
- {0x04,0x04,0x02,0x01,0xFF,0x80,0x40,0x30,0x0E,0x01,0x06,0x18,0x20,0x40,0x80,0x00},/*"狀",2*/
- {0x00,0x04,0x84,0x84,0x44,0x24,0x54,0x8F,0x14,0x24,0x44,0x84,0x84,0x04,0x00,0x00},
- {0x41,0x39,0x00,0x00,0x3C,0x40,0x40,0x42,0x4C,0x40,0x40,0x70,0x04,0x09,0x31,0x00},/*"態",3*/
- {0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},
- {0x00,0x00,0x00,0x00,0x00,0x00,0x18,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*":",4*/
- {0x00,0x02,0x02,0xC2,0x02,0x02,0x02,0xFE,0x82,0x82,0x82,0x82,0x82,0x02,0x00,0x00},
- {0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x7F,0x40,0x40,0x40,0x40,0x40,0x40,0x40,0x00},/*"正",5*/
- {0xC8,0xB8,0x8F,0xE8,0x88,0x88,0x40,0x48,0x48,0xE8,0x5F,0x48,0x48,0x48,0x40,0x00},
- {0x08,0x18,0x08,0xFF,0x04,0x04,0x00,0x02,0x0B,0x12,0x22,0xD2,0x0A,0x06,0x00,0x00},/*"轉",6*/
- {0x00,0x00,0xFC,0x24,0x24,0xE4,0x24,0x24,0x22,0x22,0x22,0xA3,0x62,0x00,0x00,0x00},
- {0x40,0x30,0x8F,0x80,0x40,0x40,0x23,0x14,0x08,0x14,0x22,0x41,0x40,0x80,0x80,0x00},/*"反",7*/
- };
- void delay_ms(unsigned int ms)
- {
- unsigned int a;
- while(ms)
- {
- a=1800;
- while(a--);
- ms--;
- }
- return;
- }
- void OLED_WR_Byte(u8 dat,u8 cmd)
- {
- u8 i;
- if(cmd)
- OLED_DC_Set();
- else
- OLED_DC_Clr();
- OLED_CS_Clr();
- for(i=0;i<8;i++)
- {
- OLED_SCLK_Clr();
- if(dat&0x80)
- {
- OLED_SDIN_Set();
- }
- else
- OLED_SDIN_Clr();
- OLED_SCLK_Set();
- dat<<=1;
- }
- OLED_CS_Set();
- OLED_DC_Set();
- }
- void OLED_Clear(void)
- {
- u8 i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD); //設置頁地址(0~7)
- OLED_WR_Byte (0x00,OLED_CMD); //設置顯示位置—列低地址
- OLED_WR_Byte (0x10,OLED_CMD); //設置顯示位置—列高地址
- for(n=0;n<128;n++)OLED_WR_Byte(0,OLED_DATA);
- } //更新顯示
- }
- void OLED_Set_Pos(unsigned char x, unsigned char y)
- {
- OLED_WR_Byte(0xb0+y,OLED_CMD);
- OLED_WR_Byte(((x&0xf0)>>4)|0x10,OLED_CMD);
- OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);
- }
- void OLED_ShowCHinese(u8 x,u8 y,u8 no)
- {
- u8 t,adder=0;
- OLED_Set_Pos(x,y);
- for(t=0;t<16;t++)
- {
- OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
- adder+=1;
- }
- OLED_Set_Pos(x,y+1);
- for(t=0;t<16;t++)
- {
- OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
- adder+=1;
- }
- }
- void OLED_Init(void)
- {
- OLED_RST_Set();
- delay_ms(100);
- OLED_RST_Clr();
- delay_ms(100);
- OLED_RST_Set();
- OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
- OLED_WR_Byte(0x00,OLED_CMD);//---set low column address
- OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
- OLED_WR_Byte(0x40,OLED_CMD);//--set start line address Set Mapping RAM Display Start Line (0x00~0x3F)
- OLED_WR_Byte(0x81,OLED_CMD);//--set contrast control register
- OLED_WR_Byte(0xCF,OLED_CMD); // Set SEG Output Current Brightness
- 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(0xA6,OLED_CMD);//--set normal display
- 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(0xd5,OLED_CMD);//--set display clock divide ratio/oscillator frequency
- OLED_WR_Byte(0x80,OLED_CMD);//--set divide ratio, Set Clock as 100 Frames/Sec
- OLED_WR_Byte(0xD9,OLED_CMD);//--set pre-charge period
- OLED_WR_Byte(0xF1,OLED_CMD);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
- OLED_WR_Byte(0xDA,OLED_CMD);//--set com pins hardware configuration
- OLED_WR_Byte(0x12,OLED_CMD);
- OLED_WR_Byte(0xDB,OLED_CMD);//--set vcomh
- OLED_WR_Byte(0x40,OLED_CMD);//Set VCOM Deselect Level
- OLED_WR_Byte(0x20,OLED_CMD);//-Set Page Addressing Mode (0x00/0x01/0x02)
- OLED_WR_Byte(0x02,OLED_CMD);//
- OLED_WR_Byte(0x8D,OLED_CMD);//--set Charge Pump enable/disable
- OLED_WR_Byte(0x14,OLED_CMD);//--set(0x10) disable
- 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_WR_Byte(0xAF,OLED_CMD);//--turn on oled panel
-
- OLED_WR_Byte(0xAF,OLED_CMD); /*display ON*/
- OLED_Clear();
- OLED_Set_Pos(0,0);
- }
- void oled()
- {
- OLED_ShowCHinese(0,3,0);//電
- OLED_ShowCHinese(18,3,1);//機
- OLED_ShowCHinese(36,3,2);//狀
- OLED_ShowCHinese(54,3,3);//態
- OLED_ShowCHinese(72,3,4);//:
- OLED_ShowCHinese(90,3,5);//:
- OLED_ShowCHinese(108,3,6);//轉
- }
- void oled1()
- {
- OLED_ShowCHinese(0,4,0);//電
- OLED_ShowCHinese(18,4,1);//機
- OLED_ShowCHinese(36,4,2);//狀
- OLED_ShowCHinese(54,4,3);//態
- OLED_ShowCHinese(72,4,4);//:
- OLED_ShowCHinese(90,4,7);//:
- OLED_ShowCHinese(108,4,6);//轉
- }
- void main()
- {
- OLED_Init();
- OLED_Clear();
- oled();
- delay_ms(500);
- OLED_Clear();
- oled1();
- while(1);
- }
復制代碼
所有資料51hei提供下載:
oled顯示.zip
(26.32 KB, 下載次數: 254)
2018-12-12 04:27 上傳
點擊文件名下載附件
程序 下載積分: 黑幣 -5
|