找回密码
 立即注册

QQ登录

只需一步,快速开始

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

pcf8576的单片机例程

[复制链接]
跳转到指定楼层
楼主
ID:433930 发表于 2018-11-27 09:14 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
  1. #include <reg52.h>
  2. #include <intrins.h>

  3. //============================ constant define =============================
  4. bit ACK;
  5. bit Pauseflag;
  6. typedef unsigned char uchar;
  7. typedef unsigned int uint;
  8. unsigned char time_count;
  9. unsigned char mode=6;//1/4duty,1/3bias
  10.                                                                  
  11. unsigned char data Displaybuf[4];
  12. unsigned char code Work_Mode[][4]={
  13. {0x70,0xe0,0Xcd,0x00},  //static,oPen display,normal work mode   //0
  14. {0x70,0xe0,0xce,0x00},  //1/2duty,1/2bias,open display,normal work mode    //1
  15. {0x70,0xe0,0xca,0x00},  //1/2duty,1/3bias,open display,normal work mode    //2
  16. {0x70,0xe0,0xcf,0x00}, //1/3duty,1/2bias,open display,normal work mode    //3
  17. {0x70,0xe0,0xcb,0x00},  //1/3duty,1/3bias,open display,normal work mode    //4
  18. {0x70,0xe0,0xcc,0x00}, //1/4duty,1/2bias,open display,normal work mode    //5
  19. {0x70,0xe0,0xc8,0x00},  //1/4duty,1/3bias,open display,normal work mode    //6
  20. };

  21. unsigned char code Num[10]={
  22. //0   1    2    3     4    5    6    7   8    9   
  23. 0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
  24. };
  25. //uchar code SEG0[]={Hz,_2D,_2F,_2C,_2B,_1D,_1F,_1C,_1B,0};
  26. //uchar code SEG1[]={_2E,_2G,vpm,_2A,_1E,_1G,_1DP,_1A,0};     


  27. //============================= I/0 define =================================
  28. sbit SDA = P1^7;        //Serial Data
  29. sbit SCL = P1^6;        //Serial Clock
  30. sbit Pause_key = P2^5;  //Pause key
  31. //=========================== function prototype ===========================
  32. void Delayms(unsigned int ms);
  33. void Delayus(void);
  34. void I2C_Start(void);
  35. void I2C_Stop(void);
  36. void I2C_Check_ACK(void);
  37. void I2C_SendByte(unsigned char dat);
  38. void Set_Mode(void);
  39. void Init_PCF8576(void);
  40. void DisplayUpdata(void);
  41. void Filled_RAM(unsigned char dat);
  42. void Init_CPU(void);
  43. //void CLR_buf(void);
  44. //void Fill_buf(unsigned char *pp);
  45. void Scan_key(void);
  46. void Delay_ms(unsigned int ms);
  47. //void WritePixe(unsigned char seg,bit ss);
  48. //void WriteStep1();
  49. //void WriteStep2();



  50. //============================ Delayms function ============================
  51. void Delayms(unsigned int ms)
  52. {
  53.     unsigned char i;
  54. while(--ms)
  55. {
  56.   for(i=125;i>0;i--);
  57. }
  58. }
  59. //============================ Delay5us function ============================
  60. void Delayus(void)
  61. {
  62.    _nop_();  
  63.    _nop_();
  64.    _nop_();
  65.    _nop_();
  66. }

  67. //=========================== I2C_Start function ============================
  68. void I2C_Start(void)
  69. {
  70.    SDA = 1;
  71.    SCL = 1;
  72.    Delayus();
  73.    SDA = 0;
  74.    Delayus();
  75.    SCL = 0;
  76. }
  77. //=========================== I2C_Stop function =============================
  78. void I2C_Stop(void)
  79. {
  80.    SDA = 0;
  81.    SCL = 1;
  82.    Delayus();
  83.    SDA = 1;
  84.    Delayus();
  85.    SCL = 0;
  86. }
  87. //========================= I2C_Check_ACK function ===========================
  88. void I2C_Check_ACK(void)
  89. {
  90.     SDA = 1;  
  91.     SCL = 1;
  92. Delayus();
  93.     ACK = 0;
  94.     if (SDA==1) ACK = 1;
  95.     SCL = 0;
  96. }

  97. //========================== I2C_SendByte function ===========================
  98. void I2C_SendByte(unsigned char dat)
  99. {
  100.    unsigned char j;
  101.    for(j=0;j<8;j++)
  102.     {
  103.       SDA = (dat & 0x80) ? 1 : 0;
  104.    SCL = 1;
  105.    Delayus();
  106.    SCL = 0;
  107.    Delayus();
  108.       dat <<= 1;
  109.     }
  110.    I2C_Check_ACK();
  111. }
  112. //=========================== Set_Mode function ===============================
  113. void Set_Mode(void)
  114. {
  115.    unsigned char j;
  116.    I2C_Start();  
  117.    for(j=0;j<4;j++)
  118.     {
  119.       I2C_SendByte(Work_Mode[mode][j]);
  120.     }
  121.    I2C_Stop();
  122. //   Filled_RAM(0xff);
  123. }
  124. //========================== Init_PCF8576 function ============================
  125. void Init_PCF8576(void)
  126. {
  127.     Set_Mode();
  128. //Delay_ms(1000);
  129. }
  130. //========================== DisplayUpdata function ============================
  131. void DisplayUpdata(void)
  132. {
  133. uchar i;
  134. I2C_Start();
  135. I2C_SendByte(0x70);//order  
  136. I2C_SendByte(0xe0);//address
  137. I2C_SendByte(0x00);//address
  138. for(i=0;i<4;i++)
  139. {
  140.   I2C_SendByte(Displaybuf[i]);
  141. }
  142. I2C_Stop();
  143. }
  144. //=========================== Filled_RAM function ==============================
  145. void Filled_RAM(unsigned char dat)
  146. {
  147.   uchar i;
  148. for(i=0;i<4;i++)
  149. {
  150.   Displaybuf[i]=dat;
  151. }
  152. DisplayUpdata();
  153. }
  154. //============================ Init_CPU function ==============================
  155. void Init_CPU(void)
  156. {
  157.    P1 = 0xff;
  158.    Pauseflag=0;
  159.    
  160. }
  161. /*******************************************************************************
  162. //============================= CLR_buf function ==============================
  163. void CLR_buf(void)
  164. {
  165. unsigned char j;
  166. for(j=0;j<27;j++)   
  167.   {
  168.   Displaybuf[j] = 0;
  169.   }
  170. }

  171. //============================= Fill_buf function ==============================
  172. void Fill_buf(unsigned char *pp)
  173. {
  174. CLR_buf();
  175. while(*pp)
  176. {

  177.       Displaybuf[(*pp-1)/8] |= (0x80 >> (*pp-1)%8);
  178.       pp++;
  179. }
  180. }
  181. /*******************************************************************************/
  182. //============================= Scan_key function ==============================
  183. void Scan_key(void)
  184. {
  185.     if(Pause_key==0)
  186.      {
  187.     Delayms(10);
  188.     if(Pause_key==0)Pauseflag=~Pauseflag;
  189.        while(Pause_key==0);
  190.    }
  191. }
  192. //============================ Delay_ms function ============================
  193.                                                                  
  194. void Delay_ms(unsigned int ms)
  195. {
  196. unsigned char i;
  197. while(--ms)
  198. {
  199.   Scan_key();
  200.   for(i=125;i>0;i--);
  201. }
  202. }
  203. /*
  204. //=============================== WritePixe ================================
  205. void WritePixe(unsigned char seg,bit ss) //seg = 1; buf[0]=0x40
  206. {
  207. if(ss)
  208. {
  209.   Displaybuf[(seg-1)/8] |= ( 0x80 >> ((seg-1)%8));
  210. }
  211. else
  212. {
  213.   Displaybuf[(seg-1)/8] &= ~( 0x80 >> ((seg-1)%8));
  214. }

  215. }
  216. /*******************************************************************************
  217. void WriteAll()
  218. {

  219. unsigned char i;
  220. for(i=0;i<193;i++)
  221. {
  222.   WritePixe(X10+i,1);
  223.   DisplayUpdata();
  224.   Delay_ms(1000);
  225. }
  226. }
  227. /*******************************************************************************
  228. //=============================== WriteStep ================================
  229. void WriteStep1()
  230. {
  231. unsigned char *p;

  232. p=&SEG0[0];
  233. while(*p)
  234. {
  235.   WritePixe(*p,1);
  236.   p++;
  237. }
  238. DisplayUpdata();
  239. Delay_ms(2000);
  240. while(Pauseflag==1)Scan_key();
  241.   
  242. }
  243. void WriteStep2()
  244. {
  245. unsigned char *p;

  246. p=&SEG1[0];
  247. while(*p)
  248. {
  249.   WritePixe(*p,1);
  250.   p++;
  251. }
  252. DisplayUpdata();
  253. Delay_ms(2000);
  254. while(Pauseflag==1)Scan_key();
  255.   
  256. }

  257. /*******************************************************************************/
  258. //=============================== main function ================================
  259. void main(void)
  260. {
  261. //   unsigned char i;
  262. Init_CPU();
  263.     Init_PCF8576();
  264. Filled_RAM(0x00);

  265.    
  266. while(1)
  267. {
  268.   

  269.   Filled_RAM(0xFF);
  270.   Delay_ms(3000);
  271.   while(Pauseflag==1)Scan_key();

  272.   Filled_RAM(0x00);
  273.   Filled_RAM(0x5A);
  274.   Delay_ms(2000);
  275.   while(Pauseflag==1)Scan_key();
  276.   Filled_RAM(0x00);
  277.   Filled_RAM(0xA5);
  278.   Delay_ms(2000);
  279.   while(Pauseflag==1)Scan_key();

  280.   
  281.   
  282. };                  

  283. }

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

使用道具 举报

板凳
ID:948375 发表于 2021-7-2 15:57 | 只看该作者

写得真不错,感谢楼主
回复

使用道具 举报

沙发
ID:433930 发表于 2018-11-27 09:15 | 只看该作者
大家看看
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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