找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 3464|回復(fù): 0
打印 上一主題 下一主題
收起左側(cè)

msp432p401r單片機(jī)的oled顯示函數(shù)

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:425145 發(fā)表于 2021-6-13 09:44 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
請(qǐng)下載附件查看
  1. #include "oled.h"
  2. #include "font.h"

  3. //SSD1303 OLED 串行接口驅(qū)動(dòng)代碼
  4. //LCD顯存存放格式如下
  5. //[0]0 1 2 3 ... 127       
  6. //[1]0 1 2 3 ... 127       
  7. //[2]0 1 2 3 ... 127       
  8. //[3]0 1 2 3 ... 127       
  9. //[4]0 1 2 3 ... 127       
  10. //[5]0 1 2 3 ... 127       
  11. //[6]0 1 2 3 ... 127       
  12. //[7]0 1 2 3 ... 127                   
  13. uint8_t LCD_GRAM[128][8];

  14. //更新顯存到LCD                 
  15. void Refresh_Gram(void)
  16. {
  17.         uint8_t i,n;
  18.         for(i=0;i<8;i++)  
  19.         {  
  20.                 WriteCommand (0xb0+i);    //設(shè)置顯示位置—行
  21.                 WriteCommand (0x02);      //設(shè)置顯示位置—列低地址,偏移了2列
  22.                 WriteCommand (0x10);      //設(shè)置顯示位置—列高地址   
  23.                 for(n=0;n<128;n++)WriteData(LCD_GRAM[n][i]);
  24.         }   
  25. }                   
  26. //SSD1303寫命令
  27. //dat:要寫入的字節(jié)          
  28. void WriteCommand(uint8_t cmd)
  29. {
  30.         uint8_t i;
  31.         OLED_DC_SET(0);   //寫命令0,寫數(shù)據(jù)1  
  32.         OLED_CS_SET(0);  
  33.         for(i=0x80;i>0;i>>=1)
  34.         {
  35.                 OLED_SCLK_SET(0);   //
  36.                 if(cmd&i)OLED_SDIN_SET(1);   //寫1
  37.                 else OLED_SDIN_SET(0);   //寫0
  38.                 OLED_SCLK_SET(1);   
  39.         }                                    
  40.         OLED_DC_SET(1);                  
  41.         OLED_CS_SET(1);
  42. }
  43. //SSD1303寫數(shù)據(jù)
  44. //dat:要寫入的字節(jié)       
  45. void WriteData( uint8_t dat)
  46. {
  47.         uint8_t i;
  48.         OLED_DC_SET(1);   //寫命令0,寫數(shù)據(jù)1  
  49.         OLED_CS_SET(0);  

  50.         for(i=0x80;i>0;i>>=1)
  51.         {
  52.                 OLED_SCLK_SET(0);   //
  53.                 if(dat&i)OLED_SDIN_SET(1);   //寫1
  54.                 else OLED_SDIN_SET(0);   //寫0
  55.                 OLED_SCLK_SET(1);   
  56.         }                                    
  57.         OLED_DC_SET(1);
  58.         OLED_CS_SET(1);
  59. }  
  60. //打開OLED     
  61. void LCD_Display_On(void)
  62. {
  63.         WriteCommand(0XAD);  //SET DCDC命令
  64.         WriteCommand(0X8B);         //DCDC ON
  65.         WriteCommand(0XAF);  //DISPLAY ON
  66. }
  67. //關(guān)閉OLED     
  68. void LCD_Display_Off(void)
  69. {
  70.         WriteCommand(0XAD);  //SET DCDC命令
  71.         WriteCommand(0X8A);         //DCDC OFF
  72.         WriteCommand(0XAE);  //DISPLAY OFF
  73. }                                            
  74. //清屏函數(shù),清完屏,整個(gè)屏幕是黑色的!和沒點(diǎn)亮一樣!!!          
  75. void LCD_Clear(void)  
  76. {  
  77.          uint8_t i,n;
  78.         for(i=0;i<8;i++)for(n=0;n<128;n++)LCD_GRAM[n][i]=0X00;  
  79.         Refresh_Gram();//更新顯示
  80. }
  81. //畫點(diǎn)
  82. //x:0~127
  83. //y:0~63
  84. //t:1 填充 0,清空                                  
  85. void LCD_DrawPoint( uint8_t x, uint8_t y, uint8_t t)
  86. {
  87.          uint8_t pos,bx,temp=0;
  88.         if(x>127||y>63)return;//超出范圍了.
  89.         pos=7-y/8;
  90.         bx=y%8;
  91.         temp=1<<(7-bx);
  92.         if(t)LCD_GRAM[x][pos]|=temp;
  93.         else LCD_GRAM[x][pos]&=~temp;            
  94. }
  95. //x1,y1,x2,y2 填充區(qū)域的對(duì)角坐標(biāo)
  96. //確保x1<=x2;y1<=y2 0<=x1<=127 0<=y1<=63                  
  97. //dot:0,清空;1,填充          
  98. void LCD_Fill( uint8_t x1, uint8_t y1, uint8_t x2, uint8_t y2, uint8_t dot)
  99. {  
  100.          uint8_t x,y;
  101.         for(x=x1;x<=x2;x++)
  102.         {
  103.                 for(y=y1;y<=y2;y++)LCD_DrawPoint(x,y,dot);
  104.         }                                                                                                            
  105.         Refresh_Gram();//更新顯示
  106. }
  107. //在指定位置顯示一個(gè)字符,包括部分字符
  108. //x:0~127
  109. //y:0~63
  110. //mode:0,反白顯示;1,正常顯示                                 
  111. //size:選擇字體 16/12
  112. void LCD_ShowChar(uint8_t x, uint8_t y, uint8_t chr, uint8_t size, uint8_t mode)
  113. {                                  
  114.          uint8_t temp,t,t1;
  115.          uint8_t y0=y;
  116.         chr=chr-' ';//得到偏移后的值                                  
  117.     for(t=0;t<size;t++)
  118.     {   
  119.                 if(size==12)temp=asc2_1206[chr][t];  //調(diào)用1206字體
  120.                 else temp=asc2_1608[chr][t];                 //調(diào)用1608字體                                   
  121.         for(t1=0;t1<8;t1++)
  122.                 {
  123.                         if(temp&0x80)LCD_DrawPoint(x,y,mode);
  124.                         else LCD_DrawPoint(x,y,!mode);
  125.                         temp<<=1;
  126.                         y++;
  127.                         if((y-y0)==size)
  128.                         {
  129.                                 y=y0;
  130.                                 x++;
  131.                                 break;
  132.                         }
  133.                 }           
  134.     }         
  135. }
  136. //m^n函數(shù)
  137. uint32_t mypow(uint8_t m, uint8_t n)
  138. {
  139.         uint32_t result=1;
  140.         while(n--)result*=m;   
  141.         return result;
  142. }                                  
  143. //顯示2個(gè)數(shù)字
  144. //x,y :起點(diǎn)坐標(biāo)         
  145. //len :數(shù)字的位數(shù)
  146. //size:字體大小
  147. //mode:模式        0,填充模式;1,疊加模式
  148. //num:數(shù)值(0~65536);         
  149. void LCD_ShowNum(uint8_t x, uint8_t y, uint16_t num, uint8_t len, uint8_t size)
  150. {                
  151.         uint8_t t,temp;
  152.         for(t=0;t<len;t++)
  153.         {
  154.                 temp=(num/mypow(10,len-t-1))%10;
  155.                  LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,1);
  156.         }
  157. }
  158. //顯示字符串
  159. //x,y:起點(diǎn)坐標(biāo)  
  160. //*p:字符串起始地址
  161. //用16字體
  162. void LCD_ShowString(uint8_t x, uint8_t y, const uint8_t *p)
  163. {
  164. #define MAX_CHAR_POSX 122
  165. #define MAX_CHAR_POSY 58         
  166.     while(*p!='\0')
  167.     {      
  168.         if(x>MAX_CHAR_POSX){x=0;y+=16;}
  169.         if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear();}
  170.         LCD_ShowChar(x,y,*p,16,1);
  171.         x+=8;
  172.         p++;
  173.     }  
  174. }

  175. void Oled_GPIO_Configuration(void)
  176. {
  177.         P3DIR |= OLED_DC + OLED_RST;
  178.         P5DIR |= OLED_SDIN + OLED_SCLK + OLED_CS;
  179. }

  180. //初始化SSD1303                                            
  181. void LCD_Init(void)
  182. {
  183.         uint32_t i;
  184.         Oled_GPIO_Configuration();
  185.        
  186.         OLED_RST_SET(0);
  187.         for(i=0;i<100000;i++);
  188.         OLED_RST_SET(1);
  189.                                                                                   
  190.         WriteCommand(0x00);//設(shè)置地位列地址,低四位數(shù)據(jù)           
  191.         WriteCommand(0x10);//設(shè)置高位列地址,低四位數(shù)據(jù)           
  192.         WriteCommand(0x40);//設(shè)置顯示開始行地址,低五位有效 0~63   
  193.         //設(shè)置對(duì)比度
  194.         WriteCommand(0x81);  
  195.         WriteCommand(0x50);//對(duì)比度值:0x00~0xff
  196.         //重映射
  197.         WriteCommand(0xA1);//映射,A0:列0映射到SEG0;A1:列131映射到SEG0             
  198.         //全顯示設(shè)置 ON/OFF
  199.         WriteCommand(0xA4);//A5:全部顯示  A4:普通顯示
  200.         //普通顯示/反相顯示
  201.         WriteCommand(0XA6); //A6:普通顯示 A7:反相顯示
  202.         //多元比率
  203.         WriteCommand(0xA8);//設(shè)置多元比率
  204.         WriteCommand(0x3f);//設(shè)置為0x3f;16~64
  205.         //設(shè)置DCDC
  206.         WriteCommand(0xAD);//設(shè)置DCDC
  207.         WriteCommand(0x8A);//8B=ON, 8A=Off
  208.         //顯示設(shè)置
  209.         WriteCommand(0xAE);// AF=ON , AE=OFF
  210.         //顯示偏移設(shè)置
  211.         WriteCommand(0xD3);//設(shè)置顯示偏移
  212.         WriteCommand(0x00);//不偏移
  213.         //設(shè)置顯示時(shí)鐘分頻因子 (刷新率)
  214.         WriteCommand(0xD5);//分頻因子設(shè)置
  215.         WriteCommand(0xF0);
  216.         //區(qū)域顏色模式
  217.         WriteCommand(0xD8);//設(shè)置區(qū)域顏色模式
  218.         WriteCommand(0x00);//單色模式
  219.         //COM Pins Hardware Configuration
  220.         WriteCommand(0xDA);// Set Pins HardwareConfiguration
  221.         WriteCommand(0x12);
  222.         //VCOMH設(shè)置
  223.         WriteCommand(0xDB);//Set VCOMH
  224.         WriteCommand(0x00);
  225.         //VP設(shè)置
  226.         WriteCommand(0xD9);//Set VP
  227.         WriteCommand(0x22);//P1=2 , P2=2
  228.         WriteCommand(0xc0);//配置成標(biāo)準(zhǔn)應(yīng)用          
  229.         LCD_Display_On();  //執(zhí)行到這里,是看不到任何反應(yīng)的!!
  230.         LCD_Clear();       //清屏幕
  231.         //LCD_Fill(0,0,128,64,1);//顯示出來
  232. }
復(fù)制代碼

代碼工程下載: test_1.7z (676.46 KB, 下載次數(shù): 35)

評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表