基于STM32F103RCT6的六线OLED显示程序,亲测有效以下为OLED.h#ifndef__OLED_H#define__OLED_H#includesys.h#includestdlib.h#includedeca_types.h#defineOLED_MODE0#defineSIZE16#defineXLevelL0x00#defineXLevelH0x10#defineMax_Column128#defineMax_Row64#defineBrightness0xFF#defineX_WIDTH128#defineY_WIDTH64#defineOLED_CS_Clr()GPIO_ResetBits(GPIOB,GPIO_Pin_10)//CS#defineOLED_CS_Set()GPIO_SetBits(GPIOB,GPIO_Pin_10)#defineOLED_DC_Clr()GPIO_ResetBits(GPIOB,GPIO_Pin_11)//DC#defineOLED_DC_Set()GPIO_SetBits(GPIOB,GPIO_Pin_11)#defineOLED_RST_Clr()GPIO_ResetBits(GPIOB,GPIO_Pin_12)//RES#defineOLED_RST_Set()GPIO_SetBits(GPIOB,GPIO_Pin_12)#defineOLED_SDIN_Clr()GPIO_ResetBits(GPIOB,GPIO_Pin_13)//SDA#defineOLED_SDIN_Set()GPIO_SetBits(GPIOB,GPIO_Pin_13)#defineOLED_SCLK_Clr()GPIO_ResetBits(GPIOB,GPIO_Pin_14)//SCL#defineOLED_SCLK_Set()GPIO_SetBits(GPIOB,GPIO_Pin_14)#defineOLED_CMD0//#defineOLED_DATA1//voidOLED_WR_Byte(u8dat,u8cmd);voidOLED_Display_On(void);voidOLED_Display_Off(void);voidOLED_Init(void);voidOLED_Clear(void);voidOLED_DrawPoint(u8x,u8y,u8t);voidOLED_Fill(u8x1,u8y1,u8x2,u8y2,u8dot);voidOLED_ShowChar(u8x,u8y,u8chr);voidOLED_ShowNum(u8x,u8y,int32num,u8len,u8size);voidOLED_ShowString(u8x,u8y,u8*p);voidOLED_Set_Pos(unsignedcharx,unsignedchary);voidOLED_ShowCHinese(u8x,u8y,u8no);voidOLED_DrawBMP(unsignedcharx0,unsignedchary0,unsignedcharx1,unsignedchary1,unsignedcharBMP[]);voidOLED_ShowHex(u8x,u8y,u8chr);#endifoled.c文件如下#includeoled.h#includestdlib.h#includeoledfont.h#includedeca_sleep.h#ifOLED_MODE==1//向SSD1106写入一个字节。//dat:要写入的数据/命令//cmd:数据/命令标志0,表示命令;1,表示数据;voidOLED_WR_Byte(u8dat,u8cmd){DATAOUT(dat);if(cmd)OLED_DC_Set();elseOLED_DC_Clr();OLED_CS_Clr();OLED_WR_Clr();OLED_WR_Set();OLED_CS_Set();OLED_DC_Set();}#else//向SSD1106写入一个字节。//dat:要写入的数据/命令//cmd:数据/命令标志0,表示命令;1,表示数据;voidOLED_WR_Byte(u8dat,u8cmd){u8i;if(cmd)OLED_DC_Set();elseOLED_DC_Clr();OLED_CS_Clr();for(i=0;i8;i++){OLED_SCLK_Clr();if(dat&0x80)OLED_SDIN_Set();elseOLED_SDIN_Clr();OLED_SCLK_Set();dat=1;}OLED_CS_Set();OLED_DC_Set();}#endifvoidOLED_Set_Pos(unsignedcharx,unsignedchary){OLED_WR_Byte(0xb0+y,OLED_CMD);OLED_WR_Byte(((x&0xf0)4)|0x10,OLED_CMD);OLED_WR_Byte((x&0x0f)|0x01,OLED_CMD);}//开启OLED显示voidOLED_Display_On(void){OLED_WR_Byte(0X8D,OLED_CMD);//SETDCDC命令OLED_WR_Byte(0X14,OLED_CMD);//DCDCONOLED_WR_Byte(0XAF,OLED_CMD);//DISPLAYON}//关闭OLED显示voidOLED_Display_Off(void){OLED_WR_Byte(0X8D,OLED_CMD);//SETDCDC命令OLED_WR_Byte(0X10,OLED_CMD);//DCDCOFFOLED_WR_Byte(0XAE,OLED_CMD);//DISPLAYOFF}//清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!voidOLED_Clear(void){u8i,n;for(i=0;i8;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;n128;n++)OLED_WR_Byte(0,OLED_DATA);}//更新显示}//在指定位置显示一个字符,包括部分字符//x:0~127//y:0~63//mode:0,反白显示;1,正常显示//size:选择字体16/12voidOLED_ShowChar(u8x,u8y,u8chr){unsignedcharc=0,i=0;c=chr-'';//得到偏移后的值if(xMax_Column-1){x=0;y=y+2;}if(SIZE==16){OLED_Set_Pos(x,y);for(i=0;i8;i++)OLED_WR_Byte(F8X16[c*16+i],OLED_DATA);OLED_Set_Pos(x,y+1);for(i=0;i8;i++)OLED_WR_Byte(F8X16[c*16+i+8],OLED_DATA);}else{OLED_Set_Pos(x,y+1);for(i=0;i6;i++)OLED_WR_Byte(F6x8[c][i],OLED_DATA);}}//m^n函数u32oled_pow(u8m,u8n){u32result=1;while(n--)result*=m;returnresult;}//显示2个数字//x,y:起点坐标//len:数字的位数//size:字体大小//mode:模式0,填充模式;1,叠加模式//num:数值(0~4294967295);voidOLED_ShowNum(u8x,u8y,int32num,u8len,u8size){u8t,temp;u8enshow=0;if(num=0){for(t=0;tlen;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;}elseenshow=1;}OLED_ShowChar(x+(size/2)*t,y,temp+'0');}}else{num=(0-num);OLED_ShowChar(x,y,'-');for(t=1;tlen;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;}elseenshow=1;}OLED_ShowChar(x+(size/2)*t,y,temp+'0');}}}//显示一个字符号串voidOLED_ShowString(u8x,u8y,u8*chr){unsignedcharj=0;while(chr[j]!='\0'){OLED_ShowChar(x,y,chr[j]);x+=8;if(x120){x=0;y+=2;}j++;}}//显示汉字voidOLED_ShowCHinese(u8x,u8y,u8no){u8t,adder=0;OLED_Set_Pos(x,y);for(t=0;t16;t++){OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);adder+=1;}OLED_Set_Pos(x,y+1);for(t=0;t16;t++){OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);adder+=1;}}/***********功能描述:显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7*****************/voidOLED_DrawBMP(unsignedcharx0,unsignedchary0,unsignedcharx1,unsignedchary1,unsignedcharBMP[]){unsignedintj=0;unsignedcharx,y;if(y1%8==0)y=y1/8;elsey=y1/8+1;for(y=y0;yy1;y++){OLED_Set_Pos(x0,y);for(x=x0;xx1;x++){OLED_WR_Byte(BMP[j++],OLED_DATA);}}}//初始化SSD1306voidOLED_Init(void){GPIO_InitTypeDefGPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//使能B端口时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//推挽输出GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//速度50MHzGPIO_Init(GPIOB,&GPIO_InitStructure);//初始化GPIOD3,6GPIO_SetBits(GPIOB,GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14);OLED_RST_Set();deca_sleep(100);OLED_RST_Clr();deca_sleep(200);OLED_RST_Set();OLED_