找回密码
 立即注册

QQ登录

只需一步,快速开始

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

串行12864加ds1302显示时钟

[复制链接]
跳转到指定楼层
楼主
ID:374056 发表于 2018-7-18 14:36 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
最近在准备电赛,需要准备显示,不想用太多的线,然后就用了串行的lcd12864只需要三根线就好
  1. #include <reg52.h>
  2. #include <stdlib.h>
  3. #include <intrins.h>
  4. #include <stdio.h>

  5. #define uchar unsigned char
  6. #define uint  unsigned int
  7. sbit CS  = P1^0;  //RS
  8. sbit SID = P1^1;  //RW
  9. sbit SCK = P1^2;  //E
  10. uchar code AC_TABLE[]={
  11. 0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,    //???????  
  12. 0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,    //???????  
  13. 0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,    //???????
  14. 0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,    //???????
  15. };




  16. #define WS 0x80
  17. #define WMi 0x82
  18. #define WH 0x84
  19. #define WD 0x86
  20. #define WMo 0x88
  21. #define WW 0x8A
  22. #define WY 0x8C
  23. #define WP 0x8E

  24. #define RS 0x81
  25. #define RMi 0x83
  26. #define RH 0x85
  27. #define RD 0x87
  28. #define RMo 0x89
  29. #define RW 0x8B
  30. #define RY 0x8D
  31. #define RP 0x8F

  32. uchar sec,min,hour,day,mon,week,year;

  33. sbit SCLK = P3^5;
  34. sbit DIO = P3^6;
  35. sbit RST = P3^7;

  36. void InitDS1302();
  37. uchar DS1302Read(uchar cmd);
  38. void DS1302Write(uchar cmd,uchar dat);
  39. void DS1302WriteByte(uchar dat);
  40. uchar DS1302ReadByte();
  41. void ReadTime();
  42. void display();
  43. void delay();




  44. uchar code tables[]={0x77,0x14,0xb3,0xb6,0xd4,0xe6,0xe7,0x34,0xf7,0xf6,0x80,0x00};






  45. /******************************** ????    :SendByte  ????????*****/
  46. void SendByte(uchar Dbyte)
  47. {
  48.      uchar i;
  49.      for(i=0;i<8;i++)
  50.      {
  51.            SCK = 0;
  52.            Dbyte=Dbyte<<1;     
  53.            SID = CY;           
  54.            SCK = 1;
  55.            SCK = 0;
  56.      }
  57. }

  58. /***********??????***/
  59. uchar ReceiveByte(void)
  60. {
  61.      uchar i,temp1,temp2;
  62.      temp1 = 0;
  63.      temp2 = 0;
  64.      for(i=0;i<8;i++)
  65.      {
  66.            temp1=temp1<<1;
  67.            SCK = 0;
  68.            SCK = 1;            
  69.            SCK = 0;
  70.            if(SID) temp1++;
  71.      }
  72.      for(i=0;i<8;i++)
  73.      {
  74.            temp2=temp2<<1;
  75.            SCK = 0;
  76.            SCK = 1;
  77.            SCK = 0;
  78.            if(SID) temp2++;
  79.      }
  80.      return ((0xf0&temp1)+(0x0f&temp2));
  81. }

  82. void CheckBusy( void )
  83. {
  84.      do   SendByte(0xfc);      
  85.      while(0x80&ReceiveByte());   
  86. }

  87. void WriteCommand(uchar Cbyte )
  88. {
  89.      CS = 1;
  90.      CheckBusy();
  91.      SendByte(0xf8);            
  92.      SendByte(0xf0&Cbyte);     
  93.      SendByte(0xf0&Cbyte<<4);
  94.      CS = 0;
  95. }

  96. void WriteData(uchar Dbyte )
  97. {
  98.      CS = 1;
  99.      CheckBusy();
  100.      SendByte(0xfa);            
  101.      SendByte(0xf0&Dbyte);      
  102.      SendByte(0xf0&Dbyte<<4);
  103.      CS = 0;
  104. }

  105. uchar ReadData( void )
  106. {
  107.      CheckBusy();
  108.      SendByte(0xfe);            
  109.      return ReceiveByte();
  110. }

  111. void Delay(uint MS)
  112. {
  113.      uchar us,usn;
  114.      while(MS!=0)           
  115.       {
  116.          usn = 2;
  117.          while(usn!=0)
  118.           {
  119.             us = 0xf5;
  120.             while (us!=0)
  121.              {
  122.                us--;
  123.              };
  124.             usn--;
  125.            }
  126.          MS--;
  127.       }
  128. }



  129. void LcmInit( void )
  130. {
  131.      WriteCommand(0x30);      
  132.      WriteCommand(0x03);      
  133.      WriteCommand(0x0C);      
  134.      WriteCommand(0x01);      
  135.      WriteCommand(0x06);      
  136. }

  137. void LcmClearTXT( void )
  138. {
  139.      uchar i;
  140.      WriteCommand(0x30);     
  141.      WriteCommand(0x80);      
  142.      for(i=0;i<64;i++)
  143.      WriteData(0x20);
  144. }


  145. void PutStr(uchar row,uchar col,uchar *puts)
  146. {
  147.      WriteCommand(0x30);     
  148.      WriteCommand(AC_TABLE[8*row+col]);   
  149.      while(*puts != '\0')      
  150.      {
  151.            if(col==8)           
  152.            {            
  153.                  col='0';
  154.                  row++;
  155.            }
  156.            if(row==4) row='0';     
  157.            WriteCommand(AC_TABLE[8*row+col]);
  158.            WriteData(*puts);      
  159.            puts++;
  160.            WriteData(*puts);
  161.            puts++;
  162.            col++;
  163.      }
  164. }



  165. void Putchar(uchar row,uchar col,uchar puts)
  166. {
  167.                  uchar a,b,c;
  168.      WriteCommand(0x30);     
  169.      WriteCommand(AC_TABLE[8*row+col]);   
  170.        if(col==8)           
  171.            {            
  172.                  col='0';
  173.                  row++;
  174.            }
  175.            if(row==4) row='0';     
  176.            WriteCommand(AC_TABLE[8*row+col]);
  177.                                         // a=puts/100;
  178.                                          b=puts/10;
  179.                                          c=puts%10;
  180.            WriteData(b);
  181.                                          col++;
  182.                                          WriteData(a);
  183.                                          
  184. }

  185. void DisplayDots(uchar DotByte)
  186. {
  187.      uchar i,j;
  188.      WriteCommand(0x34);     
  189.      WriteCommand(0x36);     
  190.      for(i=0;i<32;i++)           
  191.      {
  192.            WriteCommand(0x80|i);     
  193.            WriteCommand(0x80);     
  194.            for(j=0;j<32;j++)     
  195.            {            
  196.               WriteData(DotByte);                  
  197.            }
  198.            DotByte=~DotByte;
  199.      }
  200. }

  201. uchar str0[]="2001-07-01";
  202. uchar str1[]="12:11:11";
  203. void main( void )
  204. {
  205.      Delay(100);   
  206.                  //InitDS1302();
  207.      LcmInit();
  208.      LcmClearTXT();
  209.      while(1)
  210.      {
  211.                                 ReadTime();;
  212.                                 str0[2] = year/10+48;
  213.                           str0[3] = year%10+48;
  214.                           str0[5] = mon/10+48;
  215.                           str0[6] = mon%10+48;
  216.                           str0[8] = day/10+48;
  217.                           str0[9] = day%10+48;
  218.                           str1[0] = hour/10+48;
  219.                           str1[1] = hour%10+48;
  220.                           str1[3] = min/10+48;
  221.                           str1[4] = min%10+48;
  222.                           str1[6] = sec/10+48;
  223.                           str1[7] = sec%10+48;
  224.               PutStr(2,0,str1);
  225.         PutStr(3,0,str0);
  226.      }
  227. }




  228. uchar DS1302Read(uchar cmd){
  229.         uchar dat;
  230.         RST = 0;
  231.         SCLK = 0;
  232.         RST = 1;
  233.         DS1302WriteByte(cmd);
  234.         dat = DS1302ReadByte();
  235.         SCLK = 1;
  236.         RST = 0;
  237.         return dat;
  238. }

  239. void DS1302Write(uchar cmd,uchar dat){
  240.         RST = 0;
  241.         SCLK = 0;
  242.         RST =1;
  243.         DS1302WriteByte(cmd);
  244.         DS1302WriteByte(dat);
  245.         SCLK = 1;
  246.         RST = 0;
  247. }

  248. void DS1302WriteByte(uchar dat){
  249.         uchar i;
  250.         SCLK = 0;
  251.         for(i=0;i<8;i++){
  252.                 DIO = dat&0x01;
  253.                 SCLK = 1;
  254.                 SCLK = 0;
  255.                 dat>>=1;
  256.         }
  257. }

  258. uchar DS1302ReadByte(){
  259.         uchar i,dat;
  260.         for(i=0;i<8;i++){
  261.                 dat>>=1;
  262.                 if(DIO==1)dat|=0x80;
  263.                 SCLK = 1;
  264.                 SCLK = 0;
  265.         }
  266.         return dat;
  267. }

  268. void InitDS1302(){
  269.         DS1302Write(WP,0x00);
  270.         DS1302Write(WS,0x80);
  271.         DS1302Write(WMi,0x10);
  272.         DS1302Write(WH,0x10);
  273.         DS1302Write(WD,0x17);
  274.         DS1302Write(WMo,0x07);
  275.         DS1302Write(WW,0x02);
  276.         DS1302Write(WY,0x18);
  277.         DS1302Write(WS,0x00);
  278.         DS1302Write(WP,0x80);
  279. }

  280. void ReadTime(){
  281.         uchar temp;
  282.         temp = DS1302Read(RS);
  283.         sec = (temp&0x0F)+((temp&0x70)>>4)*10;
  284.         temp =DS1302Read(RMi);
  285.         min = (temp&0x0F)+((temp&0x70)>>4)*10;
  286.         temp =DS1302Read(RH);
  287.         hour = (temp&0x0F)+((temp&0x30)>>4)*10;
  288.         temp =DS1302Read(RD);
  289.         day = (temp&0x0F)+((temp&0x30)>>4)*10;
  290.         temp =DS1302Read(RMo);
  291.         mon = (temp&0x0F)+((temp&0x10)>>4)*10;
  292.         temp =DS1302Read(RW);
  293.         week = temp&0x07;
  294.         temp =DS1302Read(RY);
  295.         year = (temp&0x0F)+(temp>>4)*10;
  296. }


  297. void display(){
  298.         SBUF = tables[sec%10];
  299.         while(!TI);
  300.         TI = 0;
  301.         SBUF = tables[sec/10];
  302.         while(!TI);
  303.         TI = 0;
  304.         SBUF = tables[day%10];
  305.         while(!TI);
  306.         TI = 0;
  307.         SBUF = tables[day/10];
  308.         while(!TI);
  309.         TI = 0;
  310.         SBUF = tables[mon%10];
  311.         while(!TI);
  312.         TI = 0;
  313.         SBUF = tables[mon/10];
  314.         while(!TI);
  315.         TI = 0;
  316.         SBUF = tables[year%10];
  317.         while(!TI);
  318.         TI = 0;
  319.         SBUF = tables[year/10];
  320.         while(!TI);
  321.         TI = 0;
  322. }


  323. void delay(){
  324.         unsigned int i,j;
  325.         for(i=0;i<1000;i++){
  326.                 for(j=0;j<333;j++);
  327.         }
  328. }
复制代码



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

使用道具 举报

沙发
ID:461428 发表于 2019-2-22 19:46 | 只看该作者
记号,,空了继续学习。。
回复

使用道具 举报

板凳
ID:85743 发表于 2019-11-11 18:07 | 只看该作者
学习,谢谢楼主分享。
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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