STM32-SPI驱动7针脚OLED,显示中文,数字,图片。
本文为STM32f103rct6利用SPI驱动7针脚oled模块显示中文,数字和图片。
1,如图,为本次驱动的oled模块
2,引脚定义:
GND--电源地;
VCC--电源正极(3.3v~5v);
D0--时钟线;
D1--数据线;
RES--复位线;
DC--数据/命令控制线;
CS--片选线,低电平有效,如果不用,必须接地;
首先我们自己从淘宝或者其他渠道购买的OLED屏幕,都是先找到OLED需要的屏幕资料和驱动代码,如果没有可以找商家要,我们本次驱动的oled为下图的像素为128x64的oled模块。X轴的范围是0-127,单位是列,Y轴的范围是0-63,单位是行。
3,OLED屏幕接线:
// OLED模块 STM32单片机 VCC 接 DC 5V/3.3V //OLED屏电源正 GND 接 GND //OLED屏电源地 D1 接 PB15 //OLED屏SPI写信号 CS 接 PB11 //OLED屏片选控制信号 RES 接 PB12 //OLED屏复位控制信号 DC 接 PB10 //OLED屏数据/命令选择控制信号 D0 接 PB13 //OLED屏SPI时钟信号
因为D1对应的是数据线,我们可以选择主入从出(MISO)或者主出从入(MISI).这里我们选用的是主出从入(MISI)。
4.代码的移植和修改
bmp.h是图片的代码,oled.c是驱动代码,oled.h是引脚代码,oledfont.h是一些字库和汉字代码
这个时候我们初始化引脚。
1,显示数字(GUI_ShowNum)
* @函数: 显示编号 x: 数字的开始坐标x Y: 数字的起始Y坐标 num: (0 ~ 4294967295) Len: 显示号的长度 大小: 显示编号的大小 模式: 0-白底黑字,1-黑底白字 void GUI_ShowNum(u8 x,u8 y,u32 num,u8 len,u8 Size,u8 mode) { u8 t,temp; u8 enshow=0,csize; if(Size == 16) { csize = Size/2; } else if(Size == 8) { csize = Size/2+2; } else { return; } for(t=0;t<len;t++) { temp=(num/mypow(10,len-t-1))%10; if(enshow==0&&t<(len-1)) { if(temp==0) { GUI_ShowChar(x+csize*t,y,' ',Size,mode); continue; }else enshow=1; } GUI_ShowChar(x+csize*t,y,temp+'0',Size,mode); } }
2,显示字符串(GUI_ShowString)
* @函数:显示英文字符串 * @parameters: x: 英文字符串的起始x坐标 y: 英文字符串的起始y坐标 chr: 英文字符串的起始地址 Char_Size: 显示字符大小 模式: 0-白底黑字,1-黑底白字 GUI_ShowString(28,0,"lxl",16,1); ******************************************************************************/ void GUI_ShowString(u8 x,u8 y,u8 *chr,u8 Char_Size,u8 mode) { unsigned char j=0,csize; if(Char_Size == 16) { csize = Char_Size/2; } else if(Char_Size == 8) { csize = Char_Size/2+2; } else { return; } while (chr[j]!='\0') { GUI_ShowChar(x,y,chr[j],Char_Size,mode); x+=csize; if(x>120) { x=0; y+=Char_Size; } j++; } }
3,显示汉字(GUI_ShowCHinese)
void GUI_ShowCHinese(u8 x,u8 y,u8 hsize,u8 *str,u8 mode) { while(*str!='\0') { if(hsize == 16) { GUI_ShowFont16(x,y,str,mode); } else if(hsize == 24) { GUI_ShowFont24(x,y,str,mode); } else if(hsize == 32) { GUI_ShowFont32(x,y,str,mode); } else { return; } x+=hsize; if(x>WIDTH-hsize) { x=0; y+=hsize; } str+=2; } }
4,oled显示图片
想要让oled显示我们想要的汉字和图形就要使用取模软件
这里给大家推荐一个取模软件
第一步,
第二步,
将生成的16进制的数据复制下来
下面是我们的主函数
int main(void) { Usart_Init(); delay_init(); //延时函数初始化 NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 OLED_Init(); OLED_Clear(0); //清屏(全黑) while (1) { GUI_ShowString(0,0,"helloworld",16,1);//oled显示字符串 GUI_ShowNum(20,20,123456,6,16,1); //oled显示数字 GUI_ShowCHinese(40,40,16,"我",1); //显示汉字 } }
编译并烧录到开发板按线复位键结果如图:
5.最后显示图片
int main(void) { Usart_Init(); delay_init(); //延时函数初始化 NVIC_Configuration(); //设置NVIC中断分组2:2位抢占优先级,2位响应优先级 OLED_Init(); OLED_Clear(0); //清屏(全黑) while (1) { GUI_DrawBMP(0, 0, 128, 64, BMP6, 1);//显示图片 OLED_Clear(0); } }
将这一大堆数据复制到bmp.h里面(下图为爱心的字模)
unsigned char BMP6[] = { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x07,0xE0,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x3F,0xFE,0x00,0x07,0xFF,0xC0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x80,0x1F,0xFF,0xF0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x03,0xFF,0xFF,0xE0,0x3F,0xFF,0xFC,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x07,0xFF,0xFF,0xF0,0xFF,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0F,0xF8,0x1F,0xF9,0xFF,0x81,0xFF,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0F,0xE0,0x03,0xFF,0xFC,0x00,0x7F,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x1F,0x80,0x01,0xFF,0xF8,0x00,0x1F,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x7F,0xE0,0x00,0x0F,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x3F,0xC0,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x1F,0x80,0x00,0x07,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x1F,0x80,0x00,0x07,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x0F,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3E,0x00,0x00,0x00,0x00,0x00,0x07,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,0x0F,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x1F,0x00,0x00,0x00,0x00,0x00,0x0F,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x00,0x1F,0x80,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x0F,0xC0,0x00,0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x07,0xE0,0x00,0x00,0x00,0x00,0x7E,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x03,0xF0,0x00,0x00,0x00,0x00,0xFC,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x01,0xFC,0x00,0x00,0x00,0x03,0xF8,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0xFF,0x00,0x00,0x00,0x07,0xF0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x7F,0x80,0x00,0x00,0x1F,0xE0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x3F,0xC0,0x00,0x00,0x3F,0xC0,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x1F,0xF0,0x00,0x00,0xFF,0x80,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x07,0xFC,0x00,0x03,0xFE,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x03,0xFF,0x00,0x0F,0xFC,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xC0,0x3F,0xF8,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xF9,0xFF,0xE0,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x1F,0xFF,0xFF,0x80,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x07,0xFF,0xFE,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x01,0xFF,0xF8,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, };
最后的显示效果文章来源:https://www.toymoban.com/news/detail-446778.html
文章来源地址https://www.toymoban.com/news/detail-446778.html
到了这里,关于STM32-SPI驱动7针脚OLED,显示中文,字符串,数字,图片。的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!