找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3829|回复: 5
打印 上一主题 下一主题
收起左侧

0.96寸OLED屏幕在合泰单片机芯片上运行 源程序 HT32F1655_56

[复制链接]
跳转到指定楼层
楼主
合泰芯片采用HT32F1655_56 :


合泰单片机驱动0.96寸OLED屏源程序:
  1. #include "ht32.h"
  2. #include "ht32_board.h"
  3. #include "LQ12864.h"

  4. // ------------------------------------------------------------
  5. // IO口模拟I2C通信
  6. // SCL接PA^1
  7. // SDA接PA^2
  8. // ----------------------------------------------------
  9. unsigned char  F16x16[] =                    
  10. {            
  11. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",0*/
  12. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*" ",0*/

  13. 0x00,0x02,0x02,0xFA,0xFA,0xAA,0xAA,0xFF,0xFF,0xAA,0xAA,0xFA,0xFA,0x02,0x02,0x00,
  14. 0x00,0x42,0x72,0x72,0x3A,0x7A,0x42,0x4B,0x5B,0x52,0x62,0x62,0x13,0x77,0x66,0x00,/*"惠",1*/

  15. 0x20,0x3C,0x1C,0xFF,0xFF,0xB0,0xB4,0x24,0x24,0x3F,0x3F,0xE4,0xE4,0x24,0x24,0x20,
  16. 0x02,0x02,0x03,0xFF,0xFF,0x00,0x01,0x05,0x1D,0x59,0xC1,0xFF,0x7F,0x01,0x01,0x01,/*"特",2*/

  17. 0x00,0x00,0x00,0xF8,0xF8,0x48,0x4C,0x4F,0x4B,0x4A,0x48,0x48,0xF8,0xF8,0x00,0x00,
  18. 0x00,0x00,0x00,0xFF,0xFF,0x44,0x44,0x44,0x44,0x44,0x44,0x44,0xFF,0xFF,0x00,0x00,/*"自",3*/


  19. …………限于本文篇幅 余下代码请从51hei下载附件…………


  20. 0x00,0x04,0x04,0x04,0x04,0x0C,0x34,0x04,0x44,0x84,0x7F,0x04,0x04,0x04,0x00,0x00,/*"等",31*/
  21. };

  22. /************************************6*8的点阵************************************/
  23. const unsigned char  F6x8[][6] =
  24. {
  25. 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,// sp



  26. …………限于本文篇幅 余下代码请从51 黑下载附件…………


  27.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  28.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xAA,0xAA,0xAA,
  29.         0x28,0x08,0x00,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  30.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
  31.         0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x7F,0x03,0x0C,0x30,0x0C,0x03,0x7F,
  32.         0x00,0x00,0x26,0x49,0x49,0x49,0x32,0x00,0x00,0x7F,0x02,0x04,0x08,0x10,0x7F,0x00,/*"D:\共享\show2.bmp",0*/
  33. };
  34. /////////////////////////////////////////////////////

  35. #define high 1
  36. #define low 0

  37. #define        Brightness        0xCF
  38. #define X_WIDTH         128
  39. #define Y_WIDTH         64

  40. void delay(unsigned int z)
  41. {
  42.         unsigned int x,y;
  43.         for(x=z;x>0;x--)
  44.                 for(y=110;y>0;y--);
  45. }

  46. void IIC_Init(void)
  47. {               

  48.         CKCU_PeripClockConfig_TypeDef CKCUClock = {{ 0 }};        
  49.         CKCUClock.Bit.PA         = 1;
  50.         CKCUClock.Bit.AFIO       = 1;
  51.         CKCU_PeripClockConfig(CKCUClock, ENABLE);

  52.         AFIO_GPxConfig(GPIO_PA, GPIO_PIN_1, AFIO_MODE_1);
  53.         GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_1, GPIO_DIR_OUT);
  54.         
  55.         AFIO_GPxConfig(GPIO_PA, GPIO_PIN_2, AFIO_MODE_1);
  56.         GPIO_DirectionConfig(HT_GPIOA, GPIO_PIN_2, GPIO_DIR_OUT);
  57. }

  58. void IIC_Start()
  59. {
  60. //   SCL = high;               
  61. //   SDA = high;
  62. //   SDA = low;
  63. //   SCL = low;
  64.         
  65.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, high);        //SCL = high;                SCL=GPIO_PIN_1
  66.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, high); //SDA = high;                SDA=GPIO_PIN_2
  67.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, low);  //SDA = low;
  68.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, low);        //SCL = low;
  69. }

  70. /**********************************************
  71. //IIC Stop
  72. **********************************************/
  73. void IIC_Stop()
  74. {
  75. //   SCL = low;
  76. //   SDA = low;
  77. //   SCL = high;
  78. //   SDA = high;
  79.         
  80.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, low);        //SCL = low;               
  81.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, low);  //SDA = low;
  82.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, high); //SCL = high;
  83.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, high);        //SCA = high;
  84. }

  85. /**********************************************
  86. // 通过I2C总线写一个字节
  87. **********************************************/
  88. void Write_IIC_Byte(unsigned char IIC_Byte)
  89. {
  90.         unsigned char i;
  91.         for(i=0;i<8;i++)
  92.         {
  93.                 if(IIC_Byte & 0x80)
  94.                         //SDA=high;
  95.                 GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, high); //SDA = high;
  96.                 else
  97.                         //SDA=low;
  98.                         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, low);  //SDA = low;
  99.                         //SCL=high;
  100.                         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, high);        //SCL = high;        
  101.                         //SCL=low;
  102.                         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, low);        //SCL = low;
  103.                 IIC_Byte<<=1;
  104.         }
  105. //        SDA=1;
  106. //        SCL=1;
  107. //        SCL=0;
  108.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_2, high); //SDA = high;
  109.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, high);        //SCL = high;
  110.         GPIO_WriteOutBits(HT_GPIOA,GPIO_PIN_1, low);        //SCL = low;
  111. }

  112. /*********************OLED写数据************************************/
  113. void OLED_WrDat(unsigned char IIC_Data)
  114. {
  115.         IIC_Start();
  116.         Write_IIC_Byte(0x78);
  117.         Write_IIC_Byte(0x40);                        //write data
  118.         Write_IIC_Byte(IIC_Data);
  119.         IIC_Stop();
  120. }
  121. /*********************OLED写命令************************************/
  122. void OLED_WrCmd(unsigned char IIC_Command)
  123. {
  124.         IIC_Start();
  125.         Write_IIC_Byte(0x78);            //Slave address,SA0=0
  126.         Write_IIC_Byte(0x00);                        //write command
  127.         Write_IIC_Byte(IIC_Command);
  128.         IIC_Stop();
  129. }
  130. /*********************OLED 设置坐标************************************/
  131. void OLED_Set_Pos(unsigned char x, unsigned char y)
  132. {
  133.         OLED_WrCmd(0xb0+y);
  134.         OLED_WrCmd(((x&0xf0)>>4)|0x10);
  135.         OLED_WrCmd((x&0x0f)|0x01);
  136. }
  137. /*********************OLED全屏************************************/
  138. void OLED_Fill(unsigned char bmp_dat)
  139. {
  140.         unsigned char y,x;
  141.         for(y=0;y<8;y++)
  142.         {
  143.                 OLED_WrCmd(0xb0+y);
  144.                 OLED_WrCmd(0x01);
  145.                 OLED_WrCmd(0x10);
  146.                 for(x=0;x<X_WIDTH;x++)
  147.                 OLED_WrDat(bmp_dat);
  148.         }
  149. }
  150. /*********************OLED复位************************************/
  151. void OLED_CLS(void)
  152. {
  153.         unsigned char y,x;
  154.         for(y=0;y<8;y++)
  155.         {
  156.                 OLED_WrCmd(0xb0+y);
  157.                 OLED_WrCmd(0x01);
  158.                 OLED_WrCmd(0x10);
  159.                 for(x=0;x<X_WIDTH;x++)
  160.                 OLED_WrDat(0);
  161.         }
  162. }
  163. /*********************OLED初始化************************************/
  164. void OLED_Init(void)
  165. {
  166.         
  167.         delay(500);//初始化之前的延时很重要!

  168.         OLED_WrCmd(0xae);//--turn off oled panel
  169.         OLED_WrCmd(0x00);//---set low column address
  170.         OLED_WrCmd(0x10);//---set high column address
  171.         OLED_WrCmd(0x40);//--set start line address  Set Mapping RAM Display Start Line (0x00~0x3F)
  172.         OLED_WrCmd(0x81);//--set contrast control register
  173.         OLED_WrCmd(Brightness); // Set SEG Output Current Brightness
  174.         OLED_WrCmd(0xa1);//--Set SEG/Column Mapping     0xa0左右反置 0xa1正常
  175.         OLED_WrCmd(0xc8);//Set COM/Row Scan Direction   0xc0上下反置 0xc8正常
  176.         OLED_WrCmd(0xa6);//--set normal display
  177.         OLED_WrCmd(0xa8);//--set multiplex ratio(1 to 64)
  178.         OLED_WrCmd(0x3f);//--1/64 duty
  179.         OLED_WrCmd(0xd3);//-set display offset        Shift Mapping RAM Counter (0x00~0x3F)
  180.         OLED_WrCmd(0x00);//-not offset
  181.         OLED_WrCmd(0xd5);//--set display clock divide ratio/oscillator frequency
  182.         OLED_WrCmd(0x80);//--set divide ratio, Set Clock as 100 Frames/Sec
  183.         OLED_WrCmd(0xd9);//--set pre-charge period
  184.         OLED_WrCmd(0xf1);//Set Pre-Charge as 15 Clocks & Discharge as 1 Clock
  185.         OLED_WrCmd(0xda);//--set com pins hardware configuration
  186.         OLED_WrCmd(0x12);
  187.         OLED_WrCmd(0xdb);//--set vcomh
  188.         OLED_WrCmd(0x40);//Set VCOM Deselect Level
  189.         OLED_WrCmd(0x20);//-Set Page Addressing Mode (0x00/0x01/0x02)
  190.         OLED_WrCmd(0x02);//
  191.         OLED_WrCmd(0x8d);//--set Charge Pump enable/disable
  192.         OLED_WrCmd(0x14);//--set(0x10) disable
  193.         OLED_WrCmd(0xa4);// Disable Entire Display On (0xa4/0xa5)
  194.         OLED_WrCmd(0xa6);// Disable Inverse Display On (0xa6/a7)
  195.         OLED_WrCmd(0xaf);//--turn on oled panel
  196.         OLED_Fill(0x00); //初始清屏
  197.         OLED_Set_Pos(0,0);
  198. }

  199. /***************功能描述:显示6*8一组标准ASCII字符串        显示的坐标(x,y),y为页范围0~7****************/
  200. void OLED_P6x8Str(unsigned char x,unsigned char y,unsigned char ch[])
  201. {
  202.         unsigned char c=0,i=0,j=0;
  203.         while (ch[j]!='\0')
  204.         {
  205.                 c =ch[j]-32;
  206.                 if(x>126){x=0;y++;}
  207.                 OLED_Set_Pos(x,y);
  208.                 for(i=0;i<6;i++)
  209.                 OLED_WrDat(F6x8[c][i]);
  210.                 x+=6;
  211.                 j++;
  212.         }
  213. }
  214. /*******************功能描述:显示8*16一组标准ASCII字符串         显示的坐标(x,y),y为页范围0~7****************/
  215. void OLED_P8x16Str(unsigned char x,unsigned char  y,unsigned char ch[])
  216. {
  217.         unsigned char c=0,i=0,j=0;
  218.         while (ch[j]!='\0')
  219.         {
  220.                 c =ch[j]-32;
  221.                 if(x>120){x=0;y++;}
  222.                 OLED_Set_Pos(x,y);
  223.                 for(i=0;i<8;i++)
  224.                 OLED_WrDat(F8X16[c*16+i]);
  225.                 OLED_Set_Pos(x,y+1);
  226.                 for(i=0;i<8;i++)
  227.                 OLED_WrDat(F8X16[c*16+i+8]);
  228.                 x+=8;
  229.                 j++;
  230.         }
  231. }
  232. /*****************功能描述:显示16*16点阵  显示的坐标(x,y),y为页范围0~7****************************/
  233. void OLED_P16x16Ch(unsigned char x,unsigned char  y,unsigned char  N)
  234. {
  235.         unsigned char wm=0;
  236.         unsigned int adder=32*N;
  237.         OLED_Set_Pos(x , y);
  238.         for(wm = 0;wm < 16;wm++)
  239.         {
  240.                 OLED_WrDat(F16x16[adder]);
  241.                 adder += 1;
  242.         }
  243.         OLED_Set_Pos(x,y + 1);
  244.         for(wm = 0;wm < 16;wm++)
  245.         {
  246.                 OLED_WrDat(F16x16[adder]);
  247.                 adder += 1;
  248.         }                  
  249. }

  250. void OLED_Display(int num)
  251. {
  252.         int temp;
  253.         temp=num;
  254.         tmp_buf[0]=temp/10000%10+'0';
  255.         OLED_P6x8Str(0,7,tmp_buf);
  256.         
  257.         tmp_buf[0]=temp/1000%10+'0';
  258.         OLED_P6x8Str(6,7,tmp_buf);



  259. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

完整代码下载:
HARDWARE.zip (7.06 KB, 下载次数: 32)



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:198587 发表于 2017-5-9 13:05 | 只看该作者
合泰芯片采用HT32F1655_56
回复

使用道具 举报

板凳
ID:308560 发表于 2018-4-15 14:35 | 只看该作者
谢谢。。。。。。
回复

使用道具 举报

地板
ID:308560 发表于 2018-4-15 16:18 | 只看该作者
代码有问题?????
回复

使用道具 举报

5#
ID:243237 发表于 2018-4-22 17:17 | 只看该作者
你好,我用了你的程序,编译出错了。
回复

使用道具 举报

6#
ID:243237 发表于 2018-4-22 17:18 | 只看该作者
你好,我用了你的程序,编译时出错了,能解释一下吗?
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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