為OLED屏增加GUI支持1:OLED驅(qū)動
標簽:guioled
2015-12-25 10:511478人閱讀評論(1)收藏舉報
分類:
STM32(43)
版權(quán)聲明:本文為博主原創(chuàng)文章,未經(jīng)博主允許不得轉(zhuǎn)載。
為OLED屏增加GUI支持1:OLED驅(qū)動本文博客鏈接:http://blog.csdn.net/jdh99,作者:jdh,轉(zhuǎn)載請注明.
說明:
一個可穿戴項目中使用到0.96寸OLED屏,這是一種小尺寸的單色屏,不需要復雜的界面。移植了emwin發(fā)現(xiàn)小屏幕單色屏很不合適,控件顯示不盡如人意,占用的flash又太大。所以我自己編寫一個輕量級的GUI。
GUI簡介:
目前已有控件:
1.文本框
2.單色bmp圖片顯示控件
3.進度條
并支持:
1.中文字庫
2.基本的2D繪圖功能
顯示效果圖:
下載鏈接:
http://download.csdn.net/detail/jdh99/9376437
源代碼:
驅(qū)動函數(shù)為GUI提供繪點函數(shù)以供調(diào)用
drv_lcd.h
/***Copyright(c),2015-2025*@filedrv_lcd.h*@brieflcd驅(qū)動層頭文件*@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"/**********************************************************************函數(shù)**********************************************************************//***@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:關(guān)閉,1:打開*/voiddrv_lcd_display_set(uint8_tstate);#en123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
drv_lcd.c
/***Copyright(c),2015-2025*@filedrv_lcd.c*@brieflcd驅(qū)動層主文件*@authorjdh*@date2015/7/28*//**********************************************************************頭文件**********************************************************************/#include"drv_lcd.h"/**********************************************************************宏定義**********************************************************************//***@brief寫入模式*///寫命令#defineOLED_CMD0//寫數(shù)據(jù)#defineOLED_DATA1/**********************************************************************靜態(tài)變量**********************************************************************//***@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];/**********************************************************************靜態(tài)函數(shù)**********************************************************************//***@brief初始io*/staticvoidinit_io(void);/***@brief初始化spi*/staticvoidinit_spi(void);/***@brief初始化SSD1306*/staticvoidinit_oled(void);/***@brief向SSD1306寫入一個字節(jié)*@paramdat:要寫入的數(shù)據(jù)/命令*@paramcmd:數(shù)據(jù)/命令標志0,表示命令;1,表示數(shù)據(jù)*/staticvoidoled_write_byte(uint8_tdat,uint8_tcmd);/***@brief切換數(shù)據(jù)或者命令模式*@paramstate:0:命令,1:數(shù)據(jù)*/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設(shè)置坐標點*@paramx:x坐標*@paramy:y坐標*/staticvoidoled_set_pos(uint8_tx,uint8_ty);/***@briefspi發(fā)送一個字節(jié)*@parambyte:發(fā)送的字節(jié)*@retval讀取的字節(jié)*/staticuint8_tspi_send_byte(uint8_tbyte);/**********************************************************************函數(shù)**********************************************************************//***@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;//設(shè)置坐標點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;}//設(shè)置顯示位置—列高地址oled_write_byte(OLED_Gram[x][pos],OLED_DATA);}/***@brief打開顯示*@param0:關(guān)閉,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初始化結(jié)構(gòu)體GPIO_InitTypeDefGPIO_InitStructure;//初始化時鐘RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);//管腳初始化GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;//選擇GPIO響應速度GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//設(shè)置為輸出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;//設(shè)置為輸出GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;//初始化GPIO_Init(GPIOB,&GPIO_InitStructure);GPIO_SetBits(GPIOB,GPIO_Pin_10);}/**********************************************************************初始化spi**********************************************************************/staticvoidinit_spi(void){//定義IO初始化結(jié)構(gòu)體GPIO_InitTypeDefGPIO_InitStructure;//定義SPI初始化結(jié)構(gòu)體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//關(guān)閉SPISPI_Cmd(SPI2,DISABLE);//初始化SPI時鐘RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2,ENABLE);//設(shè)置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波特率分頻設(shè)置SPI_InitStructure.SPI_BaudRatePrescaler=SPI_BaudRatePrescaler_2;//SPI設(shè)置成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);//關(guān)閉顯示oled_write_byte(0xD5,OLED_CMD);//設(shè)置時鐘分頻因子,震蕩頻率oled_write_byte(80,OLED_CMD);//[3:0],分頻因子;[7:4],震蕩頻率oled_write_byte(0xA8,OLED_CMD);//設(shè)置驅(qū)動路數(shù)oled_write_byte(0X3F,OLED_CMD);//默認0X3F(1/64)oled_write_byte(0xD3,OLED_CMD);//設(shè)置顯示偏移oled_write_byte(0X00,OLED_CMD);//默認為0oled_write_byte(0x40,OLED_CMD);//設(shè)置顯示開始行[5:0],行數(shù).oled_write_byte(0x8D,OLED_CMD);//電荷泵設(shè)置oled_write_byte(0x14,OLED_CMD);//bit2,開啟/關(guān)閉oled_write_byte(0x20,OLED_CMD);//設(shè)置內(nèi)存地址模式oled_write_byte(0x02,OLED_CMD);//[1:0],00,列地址模式;01,行地址模式;10,頁地址模式;默認10;oled_write_byte(0xA1,OLED_CMD);//段重定義設(shè)置,bit0:0,0->0;1,0->127;oled_write_byte(0xC0,OLED_CMD);//設(shè)置COM掃描方向;bit3:0,普通模式;1,重定義模式COM[N-1]->COM0;N:驅(qū)動路數(shù)oled_write_byte(0xDA,OLED_CMD);//設(shè)置COM硬件引腳配置oled_write_byte(0x12,OLED_CMD);//[5:4]配置oled_write_byte(0x81,OLED_CMD);//對比度設(shè)置oled_write_byte(0xEF,OLED_CMD);//1~255;默認0X7F(亮度設(shè)置,越大越亮)oled_write_byte(0xD9,OLED_CMD);//設(shè)置預充電周期oled_write_byte(0xf1,OLED_CMD);//[3:0],PHASE1;[7:4],PHASE2;oled_write_byte(0xDB,OLED_CMD);//設(shè)置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,關(guān)閉;(白屏/黑屏)oled_write_byte(0xA6,OLED_CMD);//設(shè)置顯示方式;bit0:1,反相顯示;0,正常顯示oled_write_byte(0xAF,OLED_CMD);//開啟顯示oled_clear();}/***@brief向SSD1306寫入一個字節(jié)*@paramdat:要寫入的數(shù)據(jù)/命令*@paramcmd:數(shù)據(jù)/命令標志0,表示命令;1,表示數(shù)據(jù)*/staticvoidoled_write_byte(uint8_tdat,uint8_tcmd){//設(shè)置傳輸模式oled_dc_set(cmd);//片選oled_cs_set(0);//發(fā)送數(shù)據(jù)spi_send_byte(dat);//關(guān)閉片選oled_cs_set(1);//設(shè)置傳輸模式oled_dc_set(cmd);//設(shè)置傳輸模式:數(shù)據(jù)oled_dc_set(OLED_DATA);}/***@brief切換數(shù)據(jù)或者命令模式*@paramstate:0:命令,1:數(shù)據(jù)*/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++){//設(shè)置頁地址(0~7)oled_write_byte(0xb0+i,OLED_CMD);//設(shè)置顯示位置—列低地址oled_write_byte(0x02,OLED_CMD);//設(shè)置顯示位置—列高地址oled_write_byte(0x10,OLED_CMD);//更新數(shù)據(jù)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設(shè)置坐標點*@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發(fā)送一個字節(jié)*@parambyte:發(fā)送的字節(jié)*@retval讀取的字節(jié)*/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