用的是7Pin的OLED,就那個寶買的那種。用的是不是中景園的程序我就不知道了。芯片應該是用SSD1306,但是用仿真里面的SSD1306總是不對。我隨便試了試SSD1308到時對了!
@9UQEVE[YCR39B78IM17H@P.png (19 KB, 下載次數: 78)
下載附件
2020-5-6 12:21 上傳
單片機源程序如下:
- #include "xianshispi.h"
- #include "oledfont.h"
- void delay_ms(unsigned int ms)
- {
- unsigned int a;
- while(ms)
- {
- a=1800;
- while(a--);
- ms--;
- }
- return;
- }
- void OLED_WR_Byte(unsigned char dat,unsigned char cmd) //寫數據 或 指令
- { // 數據 指令
- unsigned char i;
- if(cmd) {OLED_DC(1);}
- else {OLED_DC(0); }
- OLED_CS(0);
- for(i=0;i<8;i++)
- {
- OLED_SCL(0);
- if(dat&0x80)
- {
- OLED_SDIN(1);
- }
- else
- OLED_SDIN(0);
- OLED_SCL(1);
- dat<<=1;
- }
- OLED_CS(1);
- OLED_DC(1);
- }
- 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_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
- }
- 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
- }
- void OLED_Clear(void) //清屏(全)
- {
- unsigned char i,n;
- for(i=0;i<8;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD);
- OLED_WR_Byte (0x02,OLED_CMD);
- OLED_WR_Byte (0x10,OLED_CMD);
- for(n=0;n<128;n++)OLED_WR_Byte(0x00,OLED_DATA);
- }
- }
- void OLED_Clear2(void) //清屏(一般)
- {
- unsigned char i,n;
- for(i=0;i<2;i++)
- {
- OLED_WR_Byte (0xb0+i,OLED_CMD);
- OLED_WR_Byte (0x02,OLED_CMD);
- OLED_WR_Byte (0x10,OLED_CMD);
- for(n=0;n<128;n++)OLED_WR_Byte(0xff,OLED_DATA);
- }
- }
- void OLED_ShowChar(unsigned char x,unsigned char y,unsigned int chr) //字符取反 (背景和數據)
- {
- unsigned int c=0,i=0;
- c=chr-' ';
- if(x>Max_Column-1){x=0;y=y+2;}
- if(SIZE==16)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);
- }
- else
- {
- OLED_Set_Pos(x,y+1);
- for(i=0;i<6;i++)
- {
- OLED_WR_Byte(F6x8[c][i],OLED_DATA);
- }
- }
- }
- void OLED_ShowChar111(unsigned char x,unsigned char y,unsigned int chr) //字符取反 (背景和數據)
- {
- unsigned int c=0,i=0;
- c=chr-' ';
- if(x>Max_Column-1){x=0;y=y+2;}
- if(SIZE==16)
- {
- OLED_Set_Pos(x,y);
- for(i=0;i<8;i++)
- OLED_WR_Byte(~F8X16[c*16+i],OLED_DATA);
- OLED_Set_Pos(x,y+1);
- for(i=0;i<8;i++)
- OLED_WR_Byte(~F8X16[c*16+i+8],OLED_DATA);
- }
- else
- {
- OLED_Set_Pos(x,y+1);
- for(i=0;i<6;i++)
- {
- OLED_WR_Byte(~F6x8[c][i],OLED_DATA);
- }
- }
- }
- unsigned int oled_pow(unsigned char m,unsigned char n)
- {
- unsigned int result=1;
- while(n--)result*=m;
- return result;
- }
- void OLED_ShowNum(unsigned char x,unsigned char y,unsigned int num,unsigned char len,unsigned char size)
- {
- unsigned char t,temp;
- unsigned char enshow=0;
- 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+(size/2)*t,y,' ');
- continue;
- }else enshow=1;
- }
- OLED_ShowChar(x+(size/2)*t,y,temp+'0');
- }
- }
- //????????
- void OLED_ShowString(unsigned char x,unsigned char y,unsigned char *chr,unsigned char no) //顯示字符串
- {
- unsigned int j=0;
- while (chr[j]!='\0')
- {
- if(no)
- {OLED_ShowChar(x,y,chr[j]);}
- else{OLED_ShowChar111(x,y,chr[j]);}
-
- x+=8;
- if(x>120){x=0;y+=2;}
- j++;
- }
- }
- void OLED_ShowCHinese(unsigned char x,unsigned char y,unsigned char *z,unsigned char no1) //顯示中文
- {
- unsigned char t,i,adder=0;
- while(*z != '\0')
- {
- for(t=0;t<20;t++)
- {
- if(Hzk[t].hzk_2[0]==*z && Hzk[t].hzk_2[1]==*(z+1))
- {
- if(no1 == 1)
- {
- OLED_Set_Pos(x,y); for(i=0;i<16;i++){OLED_WR_Byte(Hzk[t].hzk_32[i],OLED_DATA);adder+=1;}
- OLED_Set_Pos(x,y+1); for(i=0;i<16;i++){OLED_WR_Byte(Hzk[t].hzk_32[i+16],OLED_DATA);adder+=1;}
- }
- else{
- OLED_Set_Pos(x,y); for(i=0;i<16;i++){OLED_WR_Byte(~Hzk[t].hzk_32[i],OLED_DATA);adder+=1;}
- OLED_Set_Pos(x,y+1); for(i=0;i<16;i++){OLED_WR_Byte(~Hzk[t].hzk_32[i+16],OLED_DATA);adder+=1;}
- }
- }
- }
- x+=16;
- z+=2;
- }
- }
- //void OLED_ShowCHinese1(unsigned char x,unsigned char y,unsigned char z,unsigned char no)
- //{
- // unsigned char t,i,adder=0;
- // for(i=0;i<z;i++)
- // {
- //
- // OLED_Set_Pos(x+i*16,y); for(t=0;t<16;t++){OLED_WR_Byte(Hzk1[2*no+i*2][t],OLED_DATA);adder+=1;}
- // OLED_Set_Pos(x+i*16,y+1); for(t=0;t<16;t++){OLED_WR_Byte(Hzk1[2*no+i*2+1][t],OLED_DATA);adder+=1;}
- // }
- //}
- void OLED_DrawBMP(unsigned char x0, unsigned char y0,unsigned char x1, unsigned char y1,unsigned char *p) //顯示圖片
- {
- 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_WR_Byte(*p++,OLED_DATA);
- }
- }
- }
- void OLED_Init(void)
- {
- delay_ms(1);
- OLED_RST(1);
- delay_ms(1);
- OLED_RST(0);
- delay_ms(1);
- OLED_RST(1);
- delay_ms(1);
- OLED_WR_Byte(0xAE,OLED_CMD);//--turn off oled panel
- OLED_WR_Byte(0x02,OLED_CMD);//---set low column address
- OLED_WR_Byte(0x10,OLED_CMD);//---set high column address
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
所有資料51hei提供下載:
OLED_4SPI.zip
(89.91 KB, 下載次數: 463)
2020-5-6 12:22 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|