標(biāo)題: PCF8563+LCD1602的單片機(jī)源碼 [打印本頁(yè)]

作者: 1107230861    時(shí)間: 2018-7-7 20:24
標(biāo)題: PCF8563+LCD1602的單片機(jī)源碼
我在網(wǎng)上找的一個(gè)比較完整的程序。自己也進(jìn)行了一點(diǎn)兒完善。注釋比較清晰,需要用的拿去

單片機(jī)源程序如下:
  1. /*PCF8563+1602LCD*/
  2. /*BCD:將一字節(jié)二進(jìn)制空間分為2個(gè)4bit來(lái)存儲(chǔ)數(shù)據(jù),每4bit來(lái)表示一個(gè)一位10進(jìn)制數(shù)*/

  3. #include<reg52.h>
  4. #include<stdio.h>
  5. #include<intrins.h>
  6. #include"I2C.h"
  7. #include "lcd1602.h"

  8. //typedef unsigned int uint;
  9. //typedef unsigned char uchar;

  10. code unsigned char table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40}; //共陰數(shù)碼管 0-9  '-' '熄滅‘表

  11. uchar tmpdisplay[8]; //定義顯示緩存數(shù)組

  12. /*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
  13. /*+++++*/uchar tmpdate[7]={0,39,19,07,6,07,18}; //時(shí)間初始化:秒、分、時(shí)、日、星期、月、年;+++++++++++/**/
  14. /*+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/


  15. uchar rtc_address[7]={0x02,0x03,0x04,0x05,0x06,0x07,0x08};  //PCF8563中的秒、分、時(shí)、日、星期、月、年寄存器地址

  16. /**************************************************
  17. 函數(shù)名稱:set_rtc()
  18. 函數(shù)功能:BCD處理,將十進(jìn)制數(shù)轉(zhuǎn)化為BCD編碼(按十六進(jìn)制數(shù)計(jì)算)
  19. ***************************************************/                                
  20. void set_rtc()          //設(shè)定時(shí)鐘數(shù)據(jù)(pcf8563芯片中的數(shù)據(jù)是以bcd形式存儲(chǔ)和處理)
  21. {
  22.         uchar i,tmp;
  23.         for(i=0;i<7;i++)    //BCD處理,將十進(jìn)制數(shù)轉(zhuǎn)化為BCD編碼(按十六進(jìn)制數(shù)計(jì)算)
  24.         {      
  25.                 tmp=tmpdate[i]/10;                        //將十位分離出
  26.                 tmpdate[i]=tmpdate[i]%10;                //將個(gè)位分離出
  27.                 tmpdate[i]=tmpdate[i]+tmp*16;                        //合成1個(gè)由2個(gè)BCD碼組成的數(shù)
  28.         }  
  29.         ISendStr(0x02,tmpdate,7); //將數(shù)據(jù)寫(xiě)入PCF8563相應(yīng)寄存器
  30. }

  31. /**************************************************
  32. 函數(shù)名稱:rtc_init()
  33. 函數(shù)功能:時(shí)鐘初始化       
  34. ***************************************************/       
  35. void rtc_init()                //時(shí)鐘初始化
  36. {
  37.         set_rtc();                                                        //將預(yù)設(shè)的時(shí)間進(jìn)行DEC->BCD轉(zhuǎn)換
  38.         ISendByte(0x00,0x00);
  39. }

  40. /**************************************************
  41. 函數(shù)名稱:ead_rtc()
  42. 函數(shù)功能:讀時(shí)鐘函數(shù),將讀出的數(shù)據(jù)存入tmpdate緩存數(shù)組中
  43. ***************************************************/       
  44. void read_rtc()          //讀時(shí)鐘函數(shù),將讀出的數(shù)據(jù)存入tmpdate緩存數(shù)組中
  45. {
  46.         IRcvStr(0x02,tmpdate,7);
  47.         tmpdate[0]=tmpdate[0]&0x7f;        //秒
  48.         tmpdate[1]=tmpdate[1]&0x7f;        //分
  49.         tmpdate[2]=tmpdate[2]&0x3f;        //時(shí)
  50.         tmpdate[3]=tmpdate[3]&0x3f;        //日
  51.         tmpdate[4]=tmpdate[4]&0x07;        //星期
  52.         tmpdate[5]=tmpdate[5]&0x0f;        //月
  53.         tmpdate[6]=tmpdate[6]&0xff;        //年  
  54.        
  55. }

  56. /**********************主函數(shù)*******************************************/
  57. void main()
  58. {
  59.         init_lcd1602();                                        //1602初始化
  60. //        rtc_init();                                                        //時(shí)間初始化
  61.        
  62.         while(1)
  63.         {         
  64.                           read_rtc();                                                                                                                //讀時(shí)間并處理

  65.                           write_firstline(5,tmpdate[6]);                    //顯示年
  66.                           write_com(0x80+5);
  67.                           
  68.                           write_firstline(8,tmpdate[5]);                    //顯示月
  69.                           write_com(0x80+8);
  70.                           
  71.                           write_firstline(11,tmpdate[3]);                    //顯示日
  72.                           write_com(0x80+11);
  73.                           
  74.                           write_firstline(14,tmpdate[4]);                    //顯示星期
  75.                           write_com(0x80+14);
  76. ……………………

  77. …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼

所有資料51hei提供下載:
pcf8563 lcd1602.zip (35.86 KB, 下載次數(shù): 85)







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1