找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 1612|回复: 0
收起左侧

ds1302加1602显示的单片机仿真+程序

[复制链接]
ID:515566 发表于 2019-4-18 16:25 | 显示全部楼层 |阅读模式
想知道其他头文件。
0.png

单片机源程序如下:
  1. #include<reg52.h>
  2. #include<stdio.h>
  3. #include<intrins.h>

  4. #define uchar unsigned char
  5. #define uint  unsigned int

  6. sbit rs=P2^0;
  7. sbit rw=P2^1;
  8. sbit e=P2^2;

  9. sbit buz=P2^4;//闹铃位

  10. sbit dat=P2^7;
  11. sbit clk=P2^6;
  12. sbit rst=P2^5;

  13. sbit set=P1^0;//按键
  14. sbit up=P1^1;
  15. sbit down=P1^2;
  16. sbit fn=P1^3;
  17. uchar station=0;//按键状态

  18. bit flag=0;//50ms标志位
  19. bit flag_c=0;//闹钟启动标志位
  20. bit flag_clock=1;//闹钟开,闹钟关

  21. uchar counter_c=0;//闹钟时长计数器
  22. uchar counter_c1=0;
  23. uchar week=0;

  24. char str[16]="Gzy 20  -  -   ";
  25. char str1[16]="c-        :  :";

  26. //以下为1602函数
  27. void delay_us(uchar us);//延时微秒
  28. void delay_ms(uchar ms);//延时毫秒
  29. void write_com(uchar c);//写指令
  30. void write_data(uchar c);//写数据
  31. void show_char(uchar pos,uchar c);//显示单个字符
  32. void show_str(uchar line,char *str);//显示字符串
  33. void ini_lcd();//初始化LCD

  34. //以下为DS1302函数
  35. void write_byte(uchar temp);//写
  36. void write_1302(uchar add,uchar dat);//写1302
  37. uchar read_1302(uchar add);//读1302数据
  38. void update_time();//更新时间
  39. void clock();//闹钟启动标志置1
  40. void alarm();//闹铃
  41. void key_scan();//按键扫描

  42. void InitTimer1(void);

  43. void main()
  44. {
  45.         ini_lcd();
  46.         InitTimer1();
  47.         buz=0;
  48.         show_str(0,str);
  49.         show_str(1,str1);
  50.         while(1){
  51.                 if(flag==1){
  52.                         flag=0;
  53.                         clock();
  54.                         alarm();
  55.                 }
  56.                 key_scan();
  57.         }
  58. }
  59. void Timer1Interrupt(void) interrupt 3
  60. {
  61.     TH1 = 0x3C;
  62.     TL1 = 0x0B0;
  63.     flag=1;
  64.         if((flag_c==1)&&(flag_clock==1)){
  65.                 counter_c++;
  66.                 if(counter_c==19)
  67.                         buz=~buz;
  68.         }
  69. }

  70. //以下为函数实体
  71. void InitTimer1(void)
  72. {
  73.     TMOD = 0x10;
  74.     TH1 = 0x3C;
  75.     TL1 = 0x0B0;
  76.     EA = 1;
  77.     ET1 = 1;
  78.     TR1 = 1;
  79. }
  80. void delay_us(uchar us)
  81. {
  82.         uchar c_us;
  83.         c_us=us>>1;
  84.         while(--c_us);
  85. }
  86. void delay_ms(uchar ms)
  87. {
  88.         while(--ms){
  89.                 delay_us(250);
  90.                 delay_us(250);
  91.                 delay_us(250);
  92.                 delay_us(250);
  93.         }
  94. }
  95. void write_com(uchar c)
  96. {
  97.         delay_ms(5);
  98.         e=0;
  99.         rs=0;
  100.         rw=0;
  101.         _nop_();
  102.         e=1;
  103.         P0=c;
  104.         e=0;
  105. }
  106. void write_data(uchar c)
  107. {
  108.         delay_ms(5);
  109.         e=0;
  110.         rs=1;
  111.         rw=0;
  112.         _nop_();
  113.         e=1;
  114.         P0=c;
  115.         e=0;
  116.         rs=0;
  117. }
  118. void show_char(uchar pos,uchar c)
  119. {
  120.         uchar p;
  121.         if(pos>=0x10)
  122.                 p=pos+0xb0;
  123.         else
  124.                 p=pos+0x80;
  125.         write_com(p);
  126.         write_data(c);
  127. }
  128. void show_str(uchar line,char *str)
  129. {
  130.         uchar l,i;
  131.         l=line<<4;
  132.         for(i=0;i<16;i++)
  133.                 show_char(l++,*(str+i));
  134. }
  135. void ini_lcd()
  136. {
  137.         delay_ms(15);
  138.         write_com(0x38);
  139.         write_com(0x06);//显示光标移动位置
  140.         write_com(0x0c);
  141.         write_com(0x01);//显示清屏
  142. }

  143. void write_byte(uchar temp)
  144. {
  145.         uchar i;
  146.         for(i=0;i<8;i++){
  147.                 clk=0;
  148.                 dat=temp&0x01;
  149.                 temp>>=1;
  150.                 clk=1;
  151.         }
  152. }
  153. void write_1302(uchar add,uchar dat)
  154. {
  155.         rst=0;
  156.         _nop_();
  157.         clk=0;
  158.         _nop_();
  159.         rst=1;
  160.         _nop_();
  161.         write_byte(add);
  162.         write_byte(dat);
  163.         rst=0;
  164.         //_nop_();
  165. }
  166. uchar read_1302(uchar add)
  167. {
  168.         uchar i,temp=0x00;
  169.         rst=0;
  170.         //_nop_();
  171.         clk=0;
  172.         //_nop_();
  173.         rst=1;
  174.         //_nop_();
  175.         write_byte(add);
  176.         for(i=0;i<8;i++){
  177.                 if(dat)
  178.                 temp|=0x80;        //每次传输低字节
  179.                 //clk=0;
  180.                 temp>>=1;
  181.                 clk=1;
  182.                 clk=0;
  183.         }
  184.         clk=1;
  185.         rst=0;
  186.         /*_nop_(); //以下为DS1302复位的稳定时间
  187.         rst=0;
  188.         clk=0;
  189.         _nop_();
  190.         clk=1;
  191.         _nop_();
  192.         dat=0;
  193.         _nop_();
  194.         dat=1;
  195.         _nop_();*/
  196.         return (temp);
  197. }
  198. void update_time()
  199. {
  200.         uchar temp;
  201.         temp=read_1302(0x81);//秒
  202.         if(str1[15]!=temp%16+0x30){
  203.                 str1[15]=temp%16+0x30;
  204.                 str1[14]=temp/16+0x30;
  205.                 show_char(0x1f,str1[15]);
  206.                 show_char(0x1e,str1[14]);
  207.         }
  208.         temp=read_1302(0x83);//分
  209.         if(str1[12]!=temp%16+0x30){
  210.                 str1[12]=temp%16+0x30;
  211.                 str1[11]=temp/16+0x30;
  212.                 show_char(0x1c,str1[12]);
  213.                 show_char(0x1b,str1[11]);
  214.         }
  215.         temp=read_1302(0x85);//时
  216.         if(str1[9]!=temp%16+0x30){
  217.                 str1[9]=temp%16+0x30;
  218.                 str1[8]=temp/16+0x30;
  219.                 show_char(0x19,str1[9]);
  220.                 show_char(0x18,str1[8]);
  221.         }
  222.         temp=read_1302(0x8b);//星期
  223.         if(week!=temp){
  224.                 week=temp;
  225.                 switch (temp){
  226.                         case 2:week=2;str[14]='M';str[15]='o';break;
  227.                         case 3:week=3;str[14]='T';str[15]='u';break;
  228.                         case 4:week=4;str[14]='W';str[15]='e';break;
  229.                         case 5:week=5;str[14]='T';str[15]='h';break;
  230.                         case 6:week=6;str[14]='F';str[15]='r';break;
  231.                         case 7:week=7;str[14]='S';str[15]='a';break;
  232.                         case 1:week=7;str[14]='S';str[15]='u';break;
  233.                 }
  234.                 show_char(0x0f,str[15]);
  235.                 show_char(0x0e,str[14]);
  236.         }
  237.     temp=read_1302(0x8d);//年
  238.         if(str[7]!=temp%16+0x30){
  239.                 str[6]=temp/16+0x30;
  240.                 str[7]=temp%16+0x30;
  241.                 show_char(0x06,str[6]);
  242.                 show_char(0x07,str[7]);
  243.         }
  244.         temp=read_1302(0x89);//月
  245.         if(str[10]!=temp%16+0x30){
  246.                 str[9]=temp/16+0x30;
  247.                 str[10]=temp%16+0x30;
  248.                 show_char(0x09,str[9]);
  249.                 show_char(0x0a,str[10]);
  250.         }
  251.         temp=read_1302(0x87);//日
  252.         if(str[13]!=temp%16+0x30){
  253.                 str[12]=temp/16+0x30;
  254.                 str[13]=temp%16+0x30;
  255.                 show_char(0x0c,str[12]);
  256.                 show_char(0x0d,str[13]);
  257.         }
  258.         if(flag_clock==0){
  259.                 if(str1[2]!='o'){
  260.                         str1[2]='o';
  261.                         str1[3]='f';
  262.                         str1[4]='f';
  263.                         str1[5]=' ';
  264.                         str1[6]=' ';
  265.                         show_char(0x12,str1[2]);
  266.                         show_char(0x13,str1[3]);
  267.                         show_char(0x14,str1[4]);
  268.                         show_char(0x15,str1[5]);
  269.                         show_char(0x16,str1[6]);
  270.                 }
  271.         }
  272.         else{
  273.                 temp=read_1302(0xc1);//闹钟时
  274.                 if(str1[3]!=temp%16+0x30){
  275.                         str1[2]=temp/16+0x30;
  276.                         str1[3]=temp%16+0x30;
  277.                         show_char(0x12,str1[2]);
  278.                         show_char(0x13,str1[3]);
  279.                         str1[4]=':';
  280.                         show_char(0x14,str1[4]);
  281.                 }
  282.                 temp=read_1302(0xc3);//闹钟分
  283.                 if(str1[6]!=temp%16+0x30){
  284.                         str1[5]=temp/16+0x30;
  285.                         str1[6]=temp%16+0x30;
  286.                         show_char(0x15,str1[5]);
  287.                         show_char(0x16,str1[6]);
  288.                 }
  289.                 }

  290. //显示2行
  291.         //show_str(1,str1);
  292.         //show_str(0,str);
  293. }
  294. void clock()
  295. {
  296.         uchar h,m,temp1,temp2;
  297.         h=read_1302(0x85);//时
  298.         m=read_1302(0x83);//分
  299.         temp1=read_1302(0xc1);//读RAM数据0,存闹钟时数据
  300.         temp2=read_1302(0xc3);//读RAM数据2,存闹钟分数据
  301.         if((temp1==h)&&(temp2==m))
  302.                 flag_c=1;
  303. }
  304. void alarm()
  305. {
  306.         if(flag_c==1){
  307.                 if(counter_c>=20)//闹钟响20*50ms=1s
  308.                 {
  309.                         counter_c=0;
  310.                         counter_c1++;
  311.                         if(counter_c1>=60){//60*1s=60s
  312.                                 counter_c1=0;
  313.                                 flag_c=0;
  314.                                 counter_c=0;
  315.                                 buz=0;
  316.                         }
  317.                 }       
  318.         }
  319. }
  320. void key_scan()
  321. {
  322.         uchar temp;
  323.         if(set==0){
  324.                 delay_ms(10);
  325.                 if(set==0){
  326.                         while(set==0);
  327.                         station++;
  328.                         if(station>11)
  329.                                 station=0;
  330.                 }       
  331.         }
  332.         if(fn==0){
  333.                 delay_ms(10);
  334.                 if(fn==0){
  335.                         while(fn==0);
  336.                         station--;
  337.                         if(station>11)
  338.                                 station=1;
  339.                         if(station==0)
  340.                                 station=1;
  341.                 }       
  342.         }
  343.         switch (station){
  344.                 case 0:
  345.                         //temp=read_1302(0x81);
  346.                         //if(temp>>7)
  347.                         //if((temp&0x80)==0x80)
  348.                         //write_1302(0x80,temp&0x7f);
  349.                         //write_com(0x38);
  350.                         //write_com(0x06);//显示光标移动位置
  351.                         //write_com(0x0c);
  352.                         if(str[0]!='G'){
  353.                                 str[0]='G';
  354.                                 str[1]='z';
  355.                                 str[2]='y';
  356.                                 show_char(0x00,str[0]);
  357.                                 show_char(0x01,str[1]);
  358.                                 show_char(0x02,str[2]);
  359.                         }
  360.                         update_time();
  361.                         break;
  362.                 case 1:        //调年
  363.                         if(str[0]!='S'){
  364.                                 str[0]='S';
  365.                                 str[1]='e';
  366.                                 str[2]='t';
  367.                                 show_char(0x00,str[0]);
  368.                                 show_char(0x01,str[1]);
  369.                                 show_char(0x02,str[2]);
  370.                         }
  371.                         temp=read_1302(0x81);
  372.                         write_1302(0x80,temp|0x80);
  373.                         write_com(0x0e); //光标显示
  374.                         write_com(0x80+7);//光标位置
  375.                         if(up==0){
  376.                                 delay_ms(10);
  377.                                 if(up==0){
  378.                                         while(up==0);
  379.                                         temp=read_1302(0x8d);
  380.                                         temp++;
  381.                                         if(temp%16>9)
  382.                                                 temp=(temp&0xf0)+16;//个位数清零,十位数进一
  383.                                         if(temp>0x79)
  384.                                                 temp=0x79;
  385.                                         write_1302(0x8c,temp);
  386.                                         update_time();
  387.                                 }
  388.                         }
  389.                         if(down==0){
  390.                                 delay_ms(10);
  391.                                 if(down==0){
  392.                                         while(down==0);
  393.                                         temp=read_1302(0x8d);
  394.                                         temp--;
  395.                                         if(temp%16>9)
  396.                                                 temp=temp&0xf0+9;
  397.                                         if(temp>79)
  398.                                                 temp=0;
  399.                                         write_1302(0x8c,temp);
  400.                                         update_time();
  401.                                 }
  402.                         }
  403.                         break;
  404.                 case 2:         //调月
  405.                         write_com(0x80+10);
  406.                         if(up==0){
  407.                                 delay_ms(10);
  408.                                 if(up==0){
  409.                                         while(up==0);
  410.                                         temp=read_1302(0x89);
  411.                                         temp++;
  412.                                         if(temp%16>9)
  413.                                                 temp=(temp&0xf0)+16;
  414.                                         if(temp>0x12)
  415.                                                 temp=0x12;
  416.                                         write_1302(0x88,temp);
  417.                                         update_time();
  418.                                 }
  419.                         }
  420.                         if(down==0){
  421.                                 delay_ms(10);
  422.                                 if(down==0){
  423.                                         while(down==0);
  424.                                         temp=read_1302(0x89);
  425.                                         temp--;
  426.                                         if(temp%16>9)
  427.                                                 temp=temp&0xf0+9;
  428.                                         if(temp>0x12)
  429.                                                 temp=0x01;
  430.                                         if(temp==0)
  431.                                                 temp=0x01;
  432.                                         write_1302(0x88,temp);
  433.                                         update_time();
  434.                                 }
  435.                         }
  436.                         break;
  437.                         case 3:         //调日
  438.                         write_com(0x80+13);
  439.                         if(up==0){
  440.                                 delay_ms(10);
  441.                                 if(up==0){
  442.                                         while(up==0);
  443.                                         temp=read_1302(0x87);
  444.                                         temp++;
  445.                                         if(temp%16>9)
  446.                                                 temp=(temp&0xf0)+16;
  447.                                         switch (read_1302(0x89)){
  448.                                                 case 0x01:
  449.                                                 case 0x03:
  450.                                                 case 0x05:
  451.                                                 case 0x07:
  452.                                                 case 0x08:
  453.                                                 case 0x10:
  454.                                                 case 0x12:
  455.                                                         if(temp>0x31)
  456.                                                                 temp=0x31;
  457.                                                         break;
  458.                                                 case 2:
  459.                                                         switch (read_1302(0x8d)%4){
  460.                                                                 case 0:
  461.                                                                         if(temp>0x29)
  462.                                                                                 temp=0x29;
  463.                                                                         break;
  464.                                                                 default :
  465.                                                                         if(temp>0x28)
  466.                                                                                 temp=0x28;
  467.                                                                         break;
  468.                                                         }
  469.                                                 case 0x04:
  470.                                                 case 0x06:
  471.                                                 case 0x11:
  472.                                                         if(temp>0x30)
  473.                                                                 temp=0x30;
  474.                                                         break;
  475.                                         }
  476.                                         write_1302(0x86,temp);
  477.                                         update_time();
  478.                                 }
  479.                         }
  480.                         if(down==0){
  481.                                 delay_ms(10);
  482.                                 if(down==0){
  483.                                         while(down==0);
  484.                                         temp=read_1302(0x87);
  485.                                         temp--;
  486.                                         if(temp%16>9)
  487.                                                 temp=temp&0xf0+9;
  488.                                         if(temp>0x31)
  489.                                                 temp=0x01;
  490.                                         if(temp==0)
  491.                                                 temp=0x01;
  492.                                         write_1302(0x86,temp);
  493.                                         update_time();
  494.                                 }
  495.                         }
  496.                         break;
  497.                         case 4:         //调星期
  498.                         write_com(0x80+15);
  499.                         if(up==0){
  500.                                 delay_ms(10);
  501.                                 if(up==0){
  502.                                         while(up==0);
  503.                                         temp=read_1302(0x8b);
  504.                                         temp++;
  505.                                         if(temp>0x07)
  506.                                                 temp=0x07;
  507.                                         write_1302(0x8a,temp);
  508.                                         update_time();
  509.                                 }
  510.                         }
  511.                         if(down==0){
  512.                                 delay_ms(10);
  513.                                 if(down==0){
  514.                                         while(down==0);
  515.                                         temp=read_1302(0x8b);
  516.                                         temp--;
  517.                                         if(temp==0)
  518.                                                 temp=0x01;
  519.                                         write_1302(0x8a,temp);
  520.                                         update_time();
  521.                                 }
  522.                         }
  523.                         break;
  524.                         case 5:         //闹钟开关
  525.                         write_com(0x80+0x40);
  526.                         if(up==0){
  527.                                 delay_ms(10);
  528.                                 if(up==0){
  529.                                         while(up==0);
  530.                                         flag_clock=~flag_clock;
  531.                                         update_time();
  532.                                 }
  533.                         }
  534.                         if(down==0){
  535.                                 delay_ms(10);
  536.                                 if(down==0){
  537.                                         while(down==0);
  538.                                         flag_clock=~flag_clock;
  539.                                         update_time();
  540.                                 }
  541.                         }
  542.                         break;
  543.                         case 6:        //调闹钟时
  544.                         if(flag_clock==1){
  545.                                 write_com(0x80+0x40+3);
  546.                                 if(up==0){
  547.                                         delay_ms(10);
  548.                                         if(up==0){
  549.                                                 while(up==0);
  550.                                                 temp=read_1302(0xc1);
  551.                                                 temp++;
  552.                                                 if(temp%16>9)
  553.                                                         temp=(temp&0xf0)+16;
  554.                                                 if(temp>0x23)
  555.                                                         temp=0x23;
  556.                                                 write_1302(0xc0,temp);
  557.                                                 update_time();
  558.                                         }
  559.                                 }
  560.                                 if(down==0){
  561.                                         delay_ms(10);
  562.                                         if(down==0){
  563.                                                 while(down==0);
  564.                                                 temp=read_1302(0xc1);
  565.                                                 temp--;
  566.                                                 if(temp%16>9)
  567.                                                         temp=temp&0xf0+9;
  568.                                                 if(temp>0x23)
  569.                                                         temp=0;
  570.                                                 if(temp==0x00)
  571.                                                         temp=0x00;
  572.                                                 write_1302(0xc0,temp);
  573.                                                 update_time();
  574.                                         }
  575.                                 }
  576.                         }
  577.                         else station=8;
  578.                         break;
  579.                         case 7:        //调闹钟分
  580.                         if(flag_clock==1){
  581.                                 write_com(0x80+0x40+6);
  582.                                 if(up==0){
  583.                                         delay_ms(10);
  584.                                         if(up==0){
  585.                                                 while(up==0);
  586.                                                 temp=read_1302(0xc3);
  587.                                                 temp++;
  588.                                                 if(temp%16>9)
  589.                                                         temp=(temp&0xf0)+16;
  590.                                                 if(temp>0x59)
  591.                                                         temp=0x59;
  592.                                                 write_1302(0xc2,temp);
  593.                                                 update_time();
  594.                                         }
  595.                                 }
  596.                                 if(down==0){
  597.                                         delay_ms(10);
  598.                                         if(down==0){
  599.                                                 while(down==0);
  600.                                                 temp=read_1302(0xc3);
  601.                                                 temp--;
  602.                                                 if(temp%16>9)
  603.                                                         temp=temp&0xf0+9;
  604.                                                 if(temp==0x00)
  605.                                                         temp=0;
  606.                                                 if(temp>0x59)
  607.                                                         temp=0x00;
  608.                                                 write_1302(0xc2,temp);
  609.                                                 update_time();
  610.                                         }
  611.                                 }
  612.                         }
  613.                         else station=8;
  614.                         break;
  615.                         case 8:        //调时
  616.                                 write_com(0x80+0x40+9);
  617.                                 if(up==0){
  618.                                         delay_ms(10);
  619.                                         if(up==0){
  620.                                                 while(up==0);
  621.                                                 temp=read_1302(0x85);
  622.                                                 temp++;
  623.                                                 if(temp%16>9)
  624.                                                         temp=(temp&0xf0)+16;
  625.                                                 if(temp>0x23)
  626.                                                         temp=0x23;
  627.                                                 write_1302(0x84,temp);
  628.                                                 update_time();
  629.                                         }
  630.                                 }
  631.                                 if(down==0){
  632.                                         delay_ms(10);
  633.                                         if(down==0){
  634.                                                 while(down==0);
  635.                                                 temp=read_1302(0x85);
  636.                                                 temp--;
  637.                                                 if(temp%16>9)
  638.                                                         temp=temp&0xf0+9;
  639.                                                 if(temp>0x23)
  640.                                                         temp=0;
  641.                                                 if(temp==0x00)
  642.                                                         temp=0;
  643.                                                 write_1302(0x84,temp);
  644.                                                 update_time();
  645.                                         }
  646.                                 }
  647.                                 break;
  648.                         case 9:        //调分
  649.                                 write_com(0x80+0x40+12);
  650.                                 if(up==0){
  651.                                         delay_ms(10);
  652.                                         if(up==0){
  653.                                                 while(up==0);
  654.                                                 temp=read_1302(0x83);
  655.                                                 temp++;
  656.                                                 if(temp%16>9)
  657.                                                         temp=(temp&0xf0)+16;
  658.                                                 if(temp>0x59)
  659.                                                         temp=0x59;
  660.                                                 write_1302(0x82,temp);
  661.                                                 update_time();
  662. ……………………

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

所有资料51hei提供下载:
1602 DS1302.rar (66.87 KB, 下载次数: 22)
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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