[活动专区] 【AT-START-F407测评】+ OLED

[复制链接]
 楼主| 比神乐 发表于 2021-1-30 13:13 | 显示全部楼层 |阅读模式
今天捣鼓了一下OLED
代码:
  1. #include "at32f4xx.h"
  2. #include "at32f4xx_i2c_ex.h"
  3. #include "stdio.h"       
  4. #include "string.h"       
  5. #include "at32_board.h"
  6. #include "oled.h"

  7. /** @addtogroup AT32F407_StdPeriph_Examples
  8.   * @{
  9.   */

  10. /** @addtogroup Poll_MA_TX&SLA_RX
  11.   * @{
  12.   */

  13. /* Private typedef -----------------------------------------------------------*/
  14. /* Private define ------------------------------------------------------------*/
  15. #define MASTER_BOARD

  16. /* Private macro -------------------------------------------------------------*/
  17. #define BUF_SIZE      8


  18. /* Private variables ---------------------------------------------------------*/
  19. u8 tx_buf[BUF_SIZE] = {0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8};
  20. u8 rx_buf[BUF_SIZE] = {0};

  21. /* Private function prototypes -----------------------------------------------*/
  22. /* Private functions ---------------------------------------------------------*/

  23. /**
  24.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Error handler program
  25.   * @param  I2C_Status
  26.   * @retval None
  27.   */
  28. void Error_Handler(I2C_StatusType I2C_Status)
  29. {
  30.   printf("Proc error I2C_ERROR_STEP_%d!!!\r\n", I2C_Status);
  31. }

  32. /**
  33.   * @brief  Main program
  34.   * @param  None
  35.   * @retval None
  36.   */
  37. int main(void)
  38. {
  39.   
  40.   
  41.   
  42.   AT32_Board_Init();
  43.   
  44.   //UART_Print_Init(115200);

  45.   I2Cx_Init(I2C_PORT);
  46.   
  47.   //printf("Polling - Master Transmitter & Slave Receiver.\r\n");                  
  48.         OLED_Init();
  49.         fill_picture(0xff);
  50.   Ddl_Delay1ms(1000);
  51.         OLED_Clear();
  52.        
  53.         OLED_ShowCHinese(16,0,5);//
  54.         OLED_ShowCHinese(32,0,6);//
  55.         OLED_ShowCHinese(48,0,7);//
  56.         OLED_ShowCHinese(64,0,8);//
  57.         OLED_ShowCHinese(80,0,9);//
  58.         OLED_ShowCHinese(96,0,10);//
  59.         OLED_ShowString(8,2,"AT32F407--21ic",16);
  60.         //OLED_ShowChar1(0,2,'1',16);
  61.   while(1)
  62.   {
  63.    

  64.    
  65.    


  66.   }   
  67. }
  1. #include "oled.h"



  2. //OLED的显存
  3. //存放格式如下.
  4. //[0]0 1 2 3 ... 127       
  5. //[1]0 1 2 3 ... 127       
  6. //[2]0 1 2 3 ... 127       
  7. //[3]0 1 2 3 ... 127       
  8. //[4]0 1 2 3 ... 127       
  9. //[5]0 1 2 3 ... 127       
  10. //[6]0 1 2 3 ... 127       
  11. //[7]0 1 2 3 ... 127                           
  12. void Ddl_Delay1ms(unsigned int x)
  13. {
  14.         unsigned int i,j;
  15.         for(i=0;i<x;i++)
  16.         for(j=0;j<200;j++);
  17. }
  18. /**
  19.   * @brief  Write_I2C_Data 向OLED写入数据
  20.   * @param  I2C_Data:数据
  21.   * @retval 无
  22.   */
  23. static void Write_I2C_Data(uint8_t I2C_Data)//写数据
  24. {
  25.    
  26.        
  27.        
  28.                 I2C_Master_Transmit1(I2C_PORT, OLED_ADDS, I2C_Data,  10000);
  29. }

  30. /**
  31.   * @brief  Write_I2C_Command,向OLED写入命令
  32.   * @param  I2C_Command
  33.   * @retval 无
  34.   */
  35. static void Write_I2C_Command(uint8_t I2C_Command)//写命令
  36. {
  37.    
  38.        
  39.         I2C_Master_Transmit2(I2C_PORT, OLED_ADDS, I2C_Command,  10000);               
  40. }


  41. void OLED_WR_Byte(unsigned dat,unsigned cmd)
  42. {
  43.         if(cmd)
  44.                 Write_I2C_Data(dat);
  45.         else
  46.                 Write_I2C_Command(dat);
  47. }

  48. /********************************************
  49. // fill_Picture
  50. ********************************************/
  51. void fill_picture(uint8_t fill_Data)
  52. {
  53.         uint8_t m,n;
  54.         for(m=0; m<8; m++)
  55.         {
  56.                 OLED_WR_Byte(0xb0+m,0);                //page0-page1
  57.                 OLED_WR_Byte(0x00,0);                //low column start address
  58.                 OLED_WR_Byte(0x10,0);                //high column start address
  59.                 for(n=0; n<128; n++)
  60.                 {
  61.                         OLED_WR_Byte(fill_Data,1);
  62.                 }
  63.         }
  64. }


  65. //坐标设置
  66. void OLED_Set_Pos(uint8_t x, uint8_t y)
  67. {        
  68.         OLED_WR_Byte(0xb0+y, OLED_CMD);
  69.         OLED_WR_Byte(((x&0xf0)>>4)|0x10, OLED_CMD);
  70.         OLED_WR_Byte((x&0x0f), OLED_CMD);
  71. }             


  72. //开启OLED显示   
  73. void OLED_Display_On(void)
  74. {
  75.         OLED_WR_Byte(0X8D, OLED_CMD);  //SET DCDC命令
  76.         OLED_WR_Byte(0X14, OLED_CMD);  //DCDC ON
  77.         OLED_WR_Byte(0XAF, OLED_CMD);  //DISPLAY ON
  78. }


  79. //关闭OLED显示     
  80. void OLED_Display_Off(void)
  81. {
  82.         OLED_WR_Byte(0X8D, OLED_CMD);  //SET DCDC命令
  83.         OLED_WR_Byte(0X10, OLED_CMD);  //DCDC OFF
  84.         OLED_WR_Byte(0XAE, OLED_CMD);  //DISPLAY OFF
  85. }       


  86. //清屏函数,清完屏,整个屏幕是黑色的!和没点亮一样!!!          
  87. void OLED_Clear(void)  
  88. {  
  89.         uint8_t i,n;                    
  90.         for(i=0; i<8; i++)  
  91.         {  
  92.                 OLED_WR_Byte (0xb0+i, OLED_CMD);    //设置页地址(0~7)
  93.                 OLED_WR_Byte (0x00,   OLED_CMD);      //设置显示位置—列低地址
  94.                 OLED_WR_Byte (0x10,   OLED_CMD);      //设置显示位置—列高地址   
  95.                 for(n=0; n<128; n++)
  96.                         OLED_WR_Byte(0, OLED_DATA);
  97.         }
  98. }


  99. void OLED_ON(void)  
  100. {  
  101.         uint8_t i,n;                    
  102.         for(i=0; i<8; i++)  
  103.         {  
  104.                 OLED_WR_Byte (0xb0+i,OLED_CMD);    //设置页地址(0~7)
  105.                 OLED_WR_Byte (0x00,OLED_CMD);      //设置显示位置—列低地址
  106.                 OLED_WR_Byte (0x10,OLED_CMD);      //设置显示位置—列高地址   
  107.                 for(n=0; n<128; n++)
  108.                         OLED_WR_Byte(1, OLED_DATA);
  109.         }
  110. }


  111. //在指定位置显示一个字符,包括部分字符
  112. //x:0~127
  113. //y:0~63
  114. //mode:0,反白显示;1,正常显示                                 
  115. //size:选择字体 16/12
  116. void OLED_ShowChar(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
  117. {             
  118.         uint8_t c=0,i=0;       
  119.         c=chr-' ';//得到偏移后的值                       
  120.         if(x>Max_Column-1)
  121.         {
  122.                 x = 0;
  123.                 y = y+2;
  124.         }
  125.         if(Char_Size == 16)
  126.         {
  127.                 OLED_Set_Pos(x, y);       
  128.                 for(i=0; i<8; i++)
  129.                         OLED_WR_Byte(F8X16[c*16 + i], OLED_DATA);
  130.                
  131.                 OLED_Set_Pos(x, y+1);
  132.                 for(i=0; i<8; i++)
  133.                         OLED_WR_Byte(F8X16[c*16 + i + 8], OLED_DATA);
  134.         }
  135.         else
  136.         {       
  137.                 OLED_Set_Pos(x, y);
  138.                 for(i=0; i<6; i++)
  139.                         OLED_WR_Byte(F6x8[c][i], OLED_DATA);
  140.         }
  141. }

  142. void OLED_ShowChar1(uint8_t x,uint8_t y,uint8_t chr,uint8_t Char_Size)
  143. {             
  144.         uint8_t c=0,i=0;       
  145.         c=chr-'0';//得到偏移后的值                       
  146.         if(x>Max_Column-1)
  147.         {
  148.                 x = 0;
  149.                 y = y+2;
  150.         }
  151.         if(Char_Size == 16)
  152.         {
  153.                 OLED_Set_Pos(x, y);       
  154.                 for(i=0; i<8; i++)
  155.                         OLED_WR_Byte(F816[c*16 + i], OLED_DATA);
  156.                
  157.                 OLED_Set_Pos(x, y+1);
  158.                 for(i=0; i<8; i++)
  159.                         OLED_WR_Byte(F816[c*16 + i + 8], OLED_DATA);
  160.         }
  161.        
  162. }

  163. //m^n函数
  164. uint32_t OLED_pow(uint8_t m, uint8_t n)
  165. {
  166.         uint32_t result = 1;         
  167.         while(n--)
  168.                 result *= m;   
  169.        
  170.         return result;
  171. }                                  


  172. //显示2个数字
  173. //x,y :起点坐标         
  174. //len :数字的位数
  175. //size:字体大小
  176. //mode:模式        0,填充模式;1,叠加模式
  177. //num:数值(0~4294967295);                           
  178. void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2)
  179. {                
  180.         uint8_t t,temp;
  181.         uint8_t enshow = 0;                                                  
  182.         for(t=0; t<len; t++)
  183.         {
  184.                 temp = (num/OLED_pow(10, len-t-1)) %10;
  185.                 if(enshow == 0 && t<(len-1))
  186.                 {
  187.                         if(temp == 0)
  188.                         {
  189.                                 OLED_ShowChar(x+(size2/2)*t, y, ' ', size2);
  190.                                 continue;
  191.                         }
  192.                         else
  193.                                 enshow = 1;                           
  194.                 }
  195.                  OLED_ShowChar(x+(size2/2)*t, y, temp+'0', size2);
  196.         }
  197. }


  198. //显示一个字符号串
  199. void OLED_ShowString(uint8_t x, uint8_t y, char *chr, uint8_t Char_Size)
  200. {
  201.         uint8_t j=0;
  202.         while (chr[j] != '\0')
  203.         {               
  204.                 OLED_ShowChar(x, y, chr[j], Char_Size);
  205.                 x += 8;
  206.                 if(x>120)
  207.                 {
  208.                         x=0;
  209.                         y+=2;
  210.                 }
  211.                 j++;
  212.         }
  213. }


  214. //显示汉字
  215. void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no)
  216. {                                  
  217.         uint8_t t,adder=0;
  218.         OLED_Set_Pos(x,y);       
  219.         for(t=0;t<16;t++)
  220.         {
  221.                 OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  222.                 adder+=1;
  223.         }       
  224.         OLED_Set_Pos(x,y+1);       
  225.         for(t=0;t<16;t++)
  226.         {       
  227.                 OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  228.                 adder+=1;
  229.         }                                       
  230. }
  231. /***********功能描述:
  232. 显示显示BMP图片128×64起始点坐标(x,y),x的范围0~127,y为页的范围0~7
  233. *****************/
  234. void OLED_DrawBMP(uint8_t x0, uint8_t y0,uint8_t x1, uint8_t y1, uint8_t BMP[])
  235. {        
  236.         unsigned int j=0;
  237.         uint8_t x,y;

  238.         if(y1%8 == 0)
  239.                 y = y1/8;      
  240.         else
  241.                 y = y1/8 + 1;
  242.         for(y=y0; y<y1; y++)
  243.         {
  244.                 OLED_Set_Pos(x0, y);
  245.                 for(x=x0; x<x1; x++)
  246.                 {      
  247.                         OLED_WR_Byte(BMP[j++], OLED_DATA);                   
  248.                 }
  249.         }
  250. }

  251. //初始化SSD1306                                            
  252. void OLED_Init(void)
  253. {        
  254.         Ddl_Delay1ms(100);
  255.         OLED_WR_Byte(0xAE,OLED_CMD);//关闭显示
  256.        
  257.         OLED_WR_Byte(0x40,OLED_CMD);//---set low column address
  258.         OLED_WR_Byte(0xB0,OLED_CMD);//---set high column address

  259.         OLED_WR_Byte(0xC8,OLED_CMD);//-not offset

  260.         OLED_WR_Byte(0x81,OLED_CMD);//设置对比度
  261.         OLED_WR_Byte(0xff,OLED_CMD);

  262.         OLED_WR_Byte(0xa1,OLED_CMD);//段重定向设置

  263.         OLED_WR_Byte(0xa6,OLED_CMD);//
  264.        
  265.         OLED_WR_Byte(0xa8,OLED_CMD);//设置驱动路数
  266.         OLED_WR_Byte(0x1f,OLED_CMD);
  267.        
  268.         OLED_WR_Byte(0xd3,OLED_CMD);
  269.         OLED_WR_Byte(0x00,OLED_CMD);
  270.        
  271.         OLED_WR_Byte(0xd5,OLED_CMD);
  272.         OLED_WR_Byte(0xf0,OLED_CMD);
  273.        
  274.         OLED_WR_Byte(0xd9,OLED_CMD);
  275.         OLED_WR_Byte(0x22,OLED_CMD);
  276.        
  277.         OLED_WR_Byte(0xda,OLED_CMD);
  278.         OLED_WR_Byte(0x02,OLED_CMD);
  279.        
  280.         OLED_WR_Byte(0xdb,OLED_CMD);
  281.         OLED_WR_Byte(0x49,OLED_CMD);
  282.        
  283.         OLED_WR_Byte(0x8d,OLED_CMD);
  284.         OLED_WR_Byte(0x14,OLED_CMD);
  285.        
  286.         OLED_WR_Byte(0xaf,OLED_CMD);
  287.         OLED_Clear();
  288. }  

效果图:
1.jpg


4.jpg


您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3541

帖子

7

粉丝
快速回复 返回顶部 返回列表