[通用8051核FLASH系列] 【芯圣电子HC89S105A测评报告】+OLED

[复制链接]
1070|0
 楼主| 比神乐 发表于 2023-5-21 10:03 | 显示全部楼层 |阅读模式
本帖最后由 芯圣电子官方QQ 于 2023-7-19 16:44 编辑

今天搞了一下OLED,I2C接口。
SCL接P01,SDA接P00。
主程序代码:
  1. #define ALLOCATE_EXTERN
  2. #include "HC89S105AC8.h"
  3. #include "oled.h"

  4. #define DEVICEADD 0x78 //24C02器件地址
  5. #define SCL P0_1             //I2C时钟控制位
  6. #define SDA P0_0             //I2C数据传送位

  7. unsigned char temp1,temp2,temp3;   //用于存放读写数据

  8. void Delay_ms(unsigned int fui_i);                                                                                //延时函数
  9. void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat); //IIC发送一个字节的数据
  10. unsigned char IIC_Read_Byte(unsigned char fuc_Add);                                        //IIC读取一个字节的数据

  11. /***************************************************************************************
  12.   * @实现效果        对24C02进行读写操作
  13. ***************************************************************************************/
  14. void main()
  15. {
  16. /************************************系统初始化****************************************/
  17.         CLKCON = 0x02;                           //选择内部高频RC为系统时钟,内部高频RC  Fosc=32MHz
  18.         CLKDIV = 0x02;                           //Fosc 2分频得到Fcpu,Fcpu=16MHz
  19.         
  20. /**********************************IIC配置初始化***************************************/        
  21.         P0M0 = P0M0 & 0xF0 | 0x0A;  //P00带上拉开漏输出
  22.         P0M0 = P0M0 & 0x0F | 0x80;  //P01推挽输出
  23.         SCL_MAP = 0x01;                           //SCL映射P01口
  24.         SDA_MAP = 0x00;                           //SDA映射P00口
  25.         IICCON = 0x40;                           //启动IIC模块
  26.     OLED_Init();
  27.         OLED_ShowString(10,0,"21ic",16);
  28.     OLED_ShowCHinese(10,2,5);
  29.         OLED_ShowCHinese(26,2,6);
  30.         OLED_ShowCHinese(42,2,7);
  31.         OLED_ShowCHinese(58,2,8);
  32.         OLED_ShowCHinese(74,2,9);
  33.         while (1)
  34.         {
  35.         
  36.         }
  37. }

  38. /***************************************************************************************
  39.   * @说明          IIC发送一个字节的数据
  40.   *        @参数        fuc_Add:存储的地址
  41.   *                        fuc_Dat:存储的数据
  42.   * @返回值 无
  43.   * @注                无
  44. ***************************************************************************************/
  45. void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat)
  46. {
  47.         IICCON &= ~0x08; //清除中断标志位

  48.         IICCON |= 0x20; //起始位发送起始信号

  49.         while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
  50.         IICCON &= ~0x20;        //起始位停止起始信号
  51.         IICCON &= ~0x08;        //清除中断标志位
  52.         IICDAT = DEVICEADD; //写入器件地址
  53.         while (IICSTA != 0x18);
  54.         IICCON &= ~0x08;  //清除中断标志位
  55.         IICDAT = fuc_Add; //写入地址
  56.         while (IICSTA != 0x28);
  57.         IICCON &= ~0x08;  //清除中断标志位
  58.         IICDAT = fuc_Dat; //写入数据
  59.         while (IICSTA != 0x28);
  60.         IICCON &= ~0x08; //清除中断标志位
  61.         IICCON |= 0x10;         //停止位发送停止信号
  62. }

  63. /***************************************************************************************
  64.   * @说明          IIC读取一个字节的数据
  65.   *        @参数        fuc_Add:存储的地址
  66.   * @返回值 存储地址里面的数据
  67.   * @注                无
  68. ***************************************************************************************/
  69. unsigned char IIC_Read_Byte(unsigned char fuc_Add)
  70. {
  71.         unsigned char fuc_Dat;
  72.         IICCON &= ~0x08; //清除中断标志位
  73.         IICCON |= 0x20;         //起始位发送起始信号
  74.         while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
  75.         IICCON &= ~0x20;        //起始位停止起始信号
  76.         IICCON &= ~0x08;        //清除中断标志位
  77.         IICDAT = DEVICEADD; //写入器件地址
  78.         while (IICSTA != 0x18);
  79.         IICCON &= ~0x08;  //清除中断标志位
  80.         IICDAT = fuc_Add; //写入读地址
  81.         while (IICSTA != 0x28);
  82.         IICCON &= ~0x08; //清除中断标志位

  83.         IICCON |= 0x20; //起始位发送起始信号
  84.         while ((!(IICCON & 0x08)) && (IICSTA != 0x08));
  85.         IICCON &= ~0x20;                   //起始位停止起始信号
  86.         IICCON &= ~0x08;                   //清除中断标志位
  87.         IICDAT = DEVICEADD | 0x01; //写入读指令
  88.         while (IICSTA != 0x40);
  89.         IICCON &= ~0x08; //清除中断标志位
  90.         while (IICSTA != 0x58);
  91.         fuc_Dat = IICDAT; //读入数据
  92.         IICCON &= ~0x08;  //清除中断标志位
  93.         IICCON |= 0x10;          //停止位发送停止信号

  94.         return fuc_Dat;
  95. }

  96. /***************************************************************************************
  97.   * @说明          延时函数
  98.   * @参数          fui_i : 延时时间
  99.   * @返回值 无
  100.   * @注         Fcpu = 16MHz,fui_i = 1时,延时时间约为1ms
  101. ***************************************************************************************/
  102. void Delay_ms(unsigned int fui_i)
  103. {
  104.         unsigned int fui_j;
  105.         for (; fui_i > 0; fui_i--)
  106.                 for (fui_j = 1596; fui_j > 0; fui_j--);
  107. }
OLED.C代码:
  1. #include "oled.h"

  2. extern void IIC_Send_Byte(unsigned char fuc_Add, unsigned char fuc_Dat);
  3. uint32_t I2C_TIMEOUT_UserCallback(uint8_t errorCode)
  4. {
  5.    
  6.    
  7.     return 0;
  8. }
  9. //OLED鐨勬樉瀛?
  10. //瀛樻斁鏍煎紡濡備笅.
  11. //[0]0 1 2 3 ... 127        
  12. //[1]0 1 2 3 ... 127        
  13. //[2]0 1 2 3 ... 127        
  14. //[3]0 1 2 3 ... 127        
  15. //[4]0 1 2 3 ... 127        
  16. //[5]0 1 2 3 ... 127        
  17. //[6]0 1 2 3 ... 127        
  18. //[7]0 1 2 3 ... 127                           
  19. void I2C_Write1(uint8_t addr,uint8_t pBuffer)
  20. {
  21.        IIC_Send_Byte(addr, pBuffer);
  22. }
  23. /**
  24.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Write_I2C_Data 鍚慜LED鍐欏叆鏁版嵁
  25.   * @param  I2C_Data锛氭暟鎹?
  26.   * @retval 鏃?
  27.   */
  28. static void Write_I2C_Data(uint8_t I2C_Data)//鍐欐暟鎹?
  29. {
  30.    I2C_Write1(0x40,I2C_Data);               
  31. }

  32. /**
  33.   * [url=home.php?mod=space&uid=247401]@brief[/url]  Write_I2C_Command锛屽悜OLED鍐欏叆鍛戒护
  34.   * @param  I2C_Command
  35.   * @retval 鏃?
  36.   */
  37. static void Write_I2C_Command(uint8_t I2C_Command)//鍐欏懡浠?
  38. {
  39.     I2C_Write1(0x00,I2C_Command);                        
  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. //寮€鍚疧LED鏄剧ず   
  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);    //璁剧疆椤靛湴鍧€锛?~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);    //璁剧疆椤靛湴鍧€锛?~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,姝e父鏄剧ず                                 
  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. //m^n鍑芥暟
  143. uint32_t OLED_pow(uint8_t m, uint8_t n)
  144. {
  145.         uint32_t result = 1;         
  146.         while(n--)
  147.                 result *= m;   
  148.         
  149.         return result;
  150. }                                 


  151. //鏄剧ず2涓暟瀛?
  152. //x,y :璧风偣鍧愭爣         
  153. //len :鏁板瓧鐨勪綅鏁?
  154. //size:瀛椾綋澶у皬
  155. //mode:妯″紡        0,濉厖妯″紡;1,鍙犲姞妯″紡
  156. //num:鏁板€?0~4294967295);                           
  157. void OLED_ShowNum(uint8_t x, uint8_t y, uint32_t num, uint8_t len, uint8_t size2)
  158. {                 
  159.         uint8_t t,temp;
  160.         uint8_t enshow = 0;                                                   
  161.         for(t=0; t<len; t++)
  162.         {
  163.                 temp = (num/OLED_pow(10, len-t-1)) %10;
  164.                 if(enshow == 0 && t<(len-1))
  165.                 {
  166.                         if(temp == 0)
  167.                         {
  168.                                 OLED_ShowChar(x+(size2/2)*t, y, ' ', size2);
  169.                                 continue;
  170.                         }
  171.                         else
  172.                                 enshow = 1;                           
  173.                 }
  174.                  OLED_ShowChar(x+(size2/2)*t, y, temp+'0', size2);
  175.         }
  176. }


  177. //鏄剧ず涓€涓瓧绗﹀彿涓?
  178. void OLED_ShowString(uint8_t x, uint8_t y, char *chr, uint8_t Char_Size)
  179. {
  180.         uint8_t j=0;
  181.         while (chr[j] != '\0')
  182.         {               
  183.                 OLED_ShowChar(x, y, chr[j], Char_Size);
  184.                 x += 8;
  185.                 if(x>120)
  186.                 {
  187.                         x=0;
  188.                         y+=2;
  189.                 }
  190.                 j++;
  191.         }
  192. }


  193. //鏄剧ず姹夊瓧
  194. void OLED_ShowCHinese(uint8_t x, uint8_t y, uint8_t no)
  195. {                                 
  196.         uint8_t t,adder=0;
  197.         OLED_Set_Pos(x,y);        
  198.         for(t=0;t<16;t++)
  199.         {
  200.                 OLED_WR_Byte(Hzk[2*no][t],OLED_DATA);
  201.                 adder+=1;
  202.         }        
  203.         OLED_Set_Pos(x,y+1);        
  204.         for(t=0;t<16;t++)
  205.         {        
  206.                 OLED_WR_Byte(Hzk[2*no+1][t],OLED_DATA);
  207.                 adder+=1;
  208.         }                                       
  209. }
  210. /***********鍔熻兘鎻忚堪锛?
  211. 鏄剧ず鏄剧ずBMP鍥剧墖128脳64璧峰鐐瑰潗鏍?x,y),x鐨勮寖鍥?锝?27锛寉涓洪〉鐨勮寖鍥?锝?
  212. *****************/
  213. void OLED_DrawBMP(uint8_t x0, uint8_t y0,uint8_t x1, uint8_t y1, uint8_t BMP[])
  214. {         
  215.         unsigned int j=0;
  216.         uint8_t x,y;

  217.         if(y1%8 == 0)
  218.                 y = y1/8;      
  219.         else
  220.                 y = y1/8 + 1;
  221.         for(y=y0; y<y1; y++)
  222.         {
  223.                 OLED_Set_Pos(x0, y);
  224.                 for(x=x0; x<x1; x++)
  225.                 {      
  226.                         OLED_WR_Byte(BMP[j++], OLED_DATA);                    
  227.                 }
  228.         }
  229. }

  230. //鍒濆鍖朣SD1306                                            
  231. void OLED_Init(void)
  232. {
  233.         long int i;
  234.         //Ddl_Delay1ms(100);
  235.         for(i=0;i<20000;i++);
  236.         OLED_WR_Byte(0xAE,OLED_CMD);//鍏抽棴鏄剧ず
  237.         
  238.         OLED_WR_Byte(0x40,OLED_CMD);//---set low column address
  239.         OLED_WR_Byte(0xB0,OLED_CMD);//---set high column address

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

  241.         OLED_WR_Byte(0x81,OLED_CMD);//璁剧疆瀵规瘮搴?
  242.         OLED_WR_Byte(0xff,OLED_CMD);

  243.         OLED_WR_Byte(0xa1,OLED_CMD);//娈甸噸瀹氬悜璁剧疆

  244.         OLED_WR_Byte(0xa6,OLED_CMD);//
  245.         
  246.         OLED_WR_Byte(0xa8,OLED_CMD);//璁剧疆椹卞姩璺暟
  247.         OLED_WR_Byte(0x1f,OLED_CMD);
  248.         
  249.         OLED_WR_Byte(0xd3,OLED_CMD);
  250.         OLED_WR_Byte(0x00,OLED_CMD);
  251.         
  252.         OLED_WR_Byte(0xd5,OLED_CMD);
  253.         OLED_WR_Byte(0xf0,OLED_CMD);
  254.         
  255.         OLED_WR_Byte(0xd9,OLED_CMD);
  256.         OLED_WR_Byte(0x22,OLED_CMD);
  257.         
  258.         OLED_WR_Byte(0xda,OLED_CMD);
  259.         OLED_WR_Byte(0x02,OLED_CMD);
  260.         
  261.         OLED_WR_Byte(0xdb,OLED_CMD);
  262.         OLED_WR_Byte(0x49,OLED_CMD);
  263.         
  264.         OLED_WR_Byte(0x8d,OLED_CMD);
  265.         OLED_WR_Byte(0x14,OLED_CMD);
  266.         
  267.         OLED_WR_Byte(0xaf,OLED_CMD);
  268.         OLED_Clear();
  269. }  

效果图:
4.jpg
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部