为OLED屏增加GUI支持1:OLED驱动
扫描二维码
随时随地手机看文章
标签:guioled
2015-12-25 10:511478人阅读评论(1)收藏举报
分类:
STM32(43)
版权声明:本文为博主原创文章,未经博主允许不得转载。
为OLED屏增加GUI支持1:OLED驱动本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.
说明:
一个可穿戴项目中使用到0.96寸OLED屏,这是一种小尺寸的单色屏,不需要复杂的界面。移植了emwin发现小屏幕单色屏很不合适,控件显示不尽如人意,占用的flash又太大。所以我自己编写一个轻量级的GUI。
GUI简介:
目前已有控件:
1.文本框
2.单色bmp图片显示控件
3.进度条
并支持:
1.中文字库
2.基本的2D绘图功能
显示效果图:
下载链接:
http://download.csdn.net/detail/jdh99/9376437
源代码:
驱动函数为GUI提供绘点函数以供调用
drv_lcd.h
/***Copyright(c),2015-2025*@filedrv_lcd.h*@brieflcd驱动层头文件*@authorjdh*@date2015/7/28*@verbatim*硬件连接说明*电路标号单片机引脚LCD引脚*SDIPB15D0*SCLPB13D1*CSPB12CS*NRSTNRSTRES*A0PB1DC*SDIPB13D0*SCLPB15D1*CSPB12CS*NRSTPB10RES*A0PB11DC*@endverbatim*/#ifndef_DRV_LCD_H_#define_DRV_LCD_H_/**********************************************************************头文件**********************************************************************/#include"stm32f10x.h"/**********************************************************************函数**********************************************************************//***@brief初始lcd*/voiddrv_lcd_init(void);/***@brief画点*@paramx:x坐标*@paramy:y坐标*@paramshow:0:不显示,1:显示*/voiddrv_lcd_draw_pixel(uint8_tx,uint8_ty,uint8_tshow);/***@brief打开显示*@param0:关闭,1:打开*/voiddrv_lcd_display_set(uint8_tstate);#en123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
drv_lcd.c
/***Copyright(c),2015-2025*@filedrv_lcd.c*@brieflcd驱动层主文件*@authorjdh*@date2015/7/28*//**********************************************************************头文件**********************************************************************/#include"drv_lcd.h"/**********************************************************************宏定义**********************************************************************//***@brief写入模式*///写命令#defineOLED_CMD0//写数据#defineOLED_DATA1/**********************************************************************静态变量**********************************************************************//***@briefOLED的显存*@verbatim*存放格式如下:*[0]0123...127*[1]0123...127*[2]0123...127*[3]0123...127*[4]0123...127*[5]0123...127*[6]0123...127*[7]0123...127*@endverbatim*/staticuint8_tOLED_Gram[128][8];/**********************************************************************静态函数**********************************************************************//***@brief初始io*/staticvoidinit_io(void);/***@brief初始化spi*/staticvoidinit_spi(void);/***@brief初始化SSD1306*/staticvoidinit_oled(void);/***@brief向SSD1306写入一个字节*@paramdat:要写入的数据/命令*@paramcmd:数据/命令标志0,表示命令;1,表示数据*/staticvoidoled_write_byte(uint8_tdat,uint8_tcmd);/***@brief切换数据或者命令模式*@paramstate:0:命令,1:数据*/staticvoidoled_dc_set(uint8_tstate);/***@brief片选*@parammode:0:片选,1:未选*/staticvoidoled_cs_set(uint8_tstate);/***@brief更新显存到LCD*/staticvoidoled_refresh_gram(void);/***@brief清屏*/staticvoidoled_clear(void);/***@brief设置坐标点*@paramx:x坐标*@paramy:y坐标*/staticvoidoled_set_pos(uint8_tx,uint8_ty);/***@briefspi发送一个字节*@parambyte:发送的字节*@retval读取的字节*/staticuint8_tspi_send_byte(uint8_tbyte);/**********************************************************************函数**********************************************************************//***@brief初始lcd*/voiddrv_lcd_init(void){init_io();init_spi();init_oled();}/***@brief画点*@paramx:x坐标*@paramy:y坐标*@paramshow:0:不显示,1:显示*/voiddrv_lcd_draw_pixel(uint8_tx,uint8_ty,uint8_tshow){uint8_tpos=0;uint8_tbx=0;uint8_ttemp=0;//设置坐标点oled_set_pos(x,y);//p岸段是否超出范围if(x>127||y>64){return;}pos=7-y/8;bx=y%8;temp=1<<(7-bx);if(show){OLED_Gram[x][pos]|=temp;}else{OLED_Gram[x][pos]&=~temp;}//设置显示位置—列高地址oled_write_byte(OLED_Gram[x][pos],OLED_DATA);}/***@brief打开显示*@param0:关闭,1:打开*/voiddrv_lcd_display_set(uint8_tstate){if(state){oled_write_byte(0xAF,OLED_CMD);}else{oled_write_byte(0xAE,OLED_CMD);}}/***@brief初始io*/staticvoidinit_io(void){//定义IO初始化结构体GPIO_InitTypeDefGPIO_InitStructure;//初始化时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//管脚初始化GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//选择GPIO响应速度GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置为输出GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//初始化GPIO_Init(GPIOB,&GPIO_InitStructure);//复位脚//初始化时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//管脚初始化GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//选择GPIO响应速度GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置为输出GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//初始化GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_SetBits(GPIOB,GPIO_Pin_10);}/**********************************************************************初始化spi**********************************************************************/staticvoidinit_spi(void){//定义IO初始化结构体GPIO_InitTypeDefGPIO_InitStructure;//定义SPI初始化结构体SPI_InitTypeDefSPI_InitStructure;//配置其他引脚//配置CSN//开启时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//配置SPI引脚CSNGPIO_InitStructure.GPIO_Pin=GPIO_Pin_12;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);//初始化SPI//关闭SPISPI_Cmd(SPI2,DISABLE);//初始化SPI时钟RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);//设置IO口时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);//配置SPI1引脚:SCK,MISOandMOSIGPIO_InitStructure.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;GPIO_Init(GPIOB,&GPIO_InitStructure);//SPI配置//SPI_I2S_DeInit(SPI2);SPI_InitStructure.SPI_Direction=SPI_Direction_2Lines_FullDuplex;SPI_InitStructure.SPI_Mode=SPI_Mode_Master;SPI_InitStructure.SPI_DataSize=SPI_DataSize_8b;SPI_InitStructure.SPI_CPOL=SPI_CPOL_High;SPI_InitStructure.SPI_CPHA=SPI_CPHA_2Edge;SPI_InitStructure.SPI_NSS=SPI_NSS_Soft;//SPI波特率分频设置SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;//SPI设置成LSB模式SPI_InitStructure.SPI_FirstBit=SPI_FirstBit_MSB;SPI_InitStructure.SPI_CRCPolynomial=7;SPI_Init(SPI2,&SPI_InitStructure);//启动SPISPI_Cmd(SPI2,ENABLE);}/***@brief初始化SSD1306*/staticvoidinit_oled(void){oled_write_byte(0xAE,OLED_CMD);//关闭显示oled_write_byte(0xD5,OLED_CMD);//设置时钟分频因子,震荡频率oled_write_byte(80,OLED_CMD);//[3:0],分频因子;[7:4],震荡频率oled_write_byte(0xA8,OLED_CMD);//设置驱动路数oled_write_byte(0X3F,OLED_CMD);//默认0X3F(1/64)oled_write_byte(0xD3,OLED_CMD);//设置显示偏移oled_write_byte(0X00,OLED_CMD);//默认为0oled_write_byte(0x40,OLED_CMD);//设置显示开始行[5:0],行数.oled_write_byte(0x8D,OLED_CMD);//电荷泵设置oled_write_byte(0x14,OLED_CMD);//bit2,开启/关闭oled_write_byte(0x20,OLED_CMD);//设置内存地址模式oled_write_byte(0x02,OLED_CMD);//[1:0],00,列地址模式;01,行地址模式;10,页地址模式;默认10;oled_write_byte(0xA1,OLED_CMD);//段重定义设置,bit0:0,0->0;1,0->127;oled_write_byte(0xC0,OLED_CMD);//设置COM扫描方向;bit3:0,普通模式;1,重定义模式COM[N-1]->COM0;N:驱动路数oled_write_byte(0xDA,OLED_CMD);//设置COM硬件引脚配置oled_write_byte(0x12,OLED_CMD);//[5:4]配置oled_write_byte(0x81,OLED_CMD);//对比度设置oled_write_byte(0xEF,OLED_CMD);//1~255;默认0X7F(亮度设置,越大越亮)oled_write_byte(0xD9,OLED_CMD);//设置预充电周期oled_write_byte(0xf1,OLED_CMD);//[3:0],PHASE1;[7:4],PHASE2;oled_write_byte(0xDB,OLED_CMD);//设置VCOMH电压倍率oled_write_byte(0x30,OLED_CMD);//[6:4]000,0.65*vcc;001,0.77*vcc;011,0.83*vcc;oled_write_byte(0xA4,OLED_CMD);//全局显示开启;bit0:1,开启;0,关闭;(白屏/黑屏)oled_write_byte(0xA6,OLED_CMD);//设置显示方式;bit0:1,反相显示;0,正常显示oled_write_byte(0xAF,OLED_CMD);//开启显示oled_clear();}/***@brief向SSD1306写入一个字节*@paramdat:要写入的数据/命令*@paramcmd:数据/命令标志0,表示命令;1,表示数据*/staticvoidoled_write_byte(uint8_tdat,uint8_tcmd){//设置传输模式oled_dc_set(cmd);//片选oled_cs_set(0);//发送数据spi_send_byte(dat);//关闭片选oled_cs_set(1);//设置传输模式oled_dc_set(cmd);//设置传输模式:数据oled_dc_set(OLED_DATA);}/***@brief切换数据或者命令模式*@paramstate:0:命令,1:数据*/staticvoidoled_dc_set(uint8_tstate){if(state){GPIO_SetBits(GPIOB,GPIO_Pin_11);}else{GPIO_ResetBits(GPIOB,GPIO_Pin_11);}}/***@brief片选*@parammode:0:片选,1:未选*/staticvoidoled_cs_set(uint8_tstate){if(state){GPIO_SetBits(GPIOB,GPIO_Pin_12);}else{GPIO_ResetBits(GPIOB,GPIO_Pin_12);}}/***@brief更新显存到LCD*/staticvoidoled_refresh_gram(void){uint8_ti=0;uint8_tn=0;for(i=0;i<8;i++){//设置页地址(0~7)oled_write_byte(0xb0+i,OLED_CMD);//设置显示位置—列低地址oled_write_byte(0x02,OLED_CMD);//设置显示位置—列高地址oled_write_byte(0x10,OLED_CMD);//更新数据for(n=0;n<128;n++){oled_write_byte(OLED_Gram[n][i],OLED_DATA);}}}/***@brief清屏*/staticvoidoled_clear(void){uint8_ti=0;uint8_tn=0;for(i=0;i<8;i++){for(n=0;n<128;n++){OLED_Gram[n][i]=0;}}//更新显示oled_refresh_gram();}/***@brief设置坐标点*@paramx:x坐标*@paramy:y坐标*/staticvoidoled_set_pos(uint8_tx,uint8_ty){oled_write_byte(0xb0+(7-y/8),OLED_CMD);oled_write_byte(((x&0xf0)>>4)|0x10,OLED_CMD);oled_write_byte((x&0x0f),OLED_CMD);}/***@briefspi发送一个字节*@parambyte:发送的字节*@retval读取的字节*/staticuint8_tspi_send_byte(uint8_tbyte){while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_TXE)==RESET);SPI_I2S_SendData(SPI2,byte);while(SPI_I2S_GetFlagStatus(SPI2,SPI_I2S_FLAG_RXNE)==RESET);returnSPI_I2S_ReceiveData(SPI2);}123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442