找回密码
 立即注册

QQ登录

只需一步,快速开始

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

12864指针时钟单片机程序,指针转动时有痕迹没擦除

[复制链接]
ID:245302 发表于 2018-3-25 21:05 | 显示全部楼层 |阅读模式
做的一个12864指针时钟程序,但是指针显示有问题,上一秒指针转到下一秒的位置时,有痕迹没有擦除,那位大佬可以帮给我看一下怎么回事,帮忙改一下。
  1. #include <reg52.h>
  2. #include"math.h"
  3. #define PI 3.1415926
  4. #define uchar unsigned char
  5. #define uint  unsigned int
  6. #define basic   0x30                        //基本指令集
  7. #define extend  0x34                        //扩充指令集
  8. #define drawon  0x36                        //绘图显示关
  9. #define drawoff 0x34

  10. sbit LCD_RS  =  P0^7;            //寄存器选择输入
  11. sbit LCD_RW  =  P0^6;            //液晶读/写控制
  12. sbit LCD_EN  =  P0^5;            //液晶使能控制
  13. sbit LCD_PSB =  P0^4;

  14. uint lay=0;

  15. struct clock
  16. {
  17.         uchar hour;
  18.         uchar minute;
  19.         uchar second;
  20. }point_time[2];

  21. void delay(uint x)                         //延时函数
  22. { int i,j;
  23.   for(i=x;i>0;i--)
  24.     for(j=11;j>0;j--);
  25. }               

  26. void write_com(uchar com)                //写命令
  27. {                          
  28.     LCD_RS = 0;
  29.     LCD_RW = 0;
  30.     LCD_EN = 0;
  31.     P2 = com;
  32.         delay(1);
  33.     LCD_EN = 1;
  34.         delay(1);
  35.     LCD_EN = 0;  
  36. }

  37. void write_dat(uchar dat)           //写数据
  38. {                          
  39.     LCD_RS = 1;
  40.     LCD_RW = 0;
  41.     LCD_EN = 0;
  42.     P2 = dat;
  43.         delay(1);
  44.     LCD_EN = 1;
  45.         delay(1);
  46.     LCD_EN = 0;  
  47. }
  48.    uchar read_date(void)
  49.    { uchar temp;
  50.      P2=0xff;
  51.          LCD_RS = 1;
  52.      LCD_RW = 1;
  53.      LCD_EN = 1;
  54.          delay(1);
  55.          temp=P2;
  56.          LCD_EN = 0;
  57.          return temp;
  58.    }

  59. void lcd_init()                                  //初始化
  60. {
  61.     LCD_PSB = 1;                   //并口方式
  62.         write_com(0x34);           //扩充指令
  63.         delay(10);
  64.     write_com(0x30);      //基本指令操作
  65.     delay(10);
  66.     write_com(0x0C);      //显示开,关光标
  67.     delay(10);
  68.     write_com(0x01);      //清除LCD的显示内容
  69.     delay(10);
  70. }

  71. void screen_clear()                  //清屏
  72. {  
  73.    uchar i,j,k;
  74.    write_com(0x34);
  75.    for(i=0;i<2;i++)
  76.     { for(j=0;j<32;j++)
  77.            { write_com(0x80+j);
  78.              delay(1);
  79.                  if(i==0)
  80.                  {  write_com(0x80);
  81.                     delay(1);
  82.                   }
  83.                  else
  84.                  {
  85.                          write_com(0x88);
  86.                         delay(1);
  87.                  }
  88.                  for(k=0;k<16;k++)
  89.                  {
  90.                          write_dat(0x00);
  91.                         write_dat(0x00);
  92.                         delay(1);
  93.                         }
  94.                 }
  95.           }
  96.              write_com(0x36);
  97.              write_com(0x30);
  98.          }

  99. void point(uchar x,uchar y,uchar color)  //打点函数
  100. {
  101.         uchar row,collum,cbite;
  102.         uchar tempH,tempL;
  103.         write_com(0x34);
  104.         write_com(0x36);
  105.         collum=x>>4;
  106.         cbite=x&0x0f;
  107.         if(y<32)row=y;
  108.         else
  109.         {
  110.           row=y-32;
  111.           collum+=8;
  112.         }
  113.         write_com(0x80+row);
  114.         write_com(0x80+collum);
  115.         read_date();
  116.         tempH=read_date();
  117.         tempL=read_date();
  118.         write_com(0x80+row);
  119.         write_com(0x80+collum);

  120.         if(color)
  121.         {
  122.           if(cbite<8)
  123.            {
  124.            tempH|=(1<<(7-cbite));
  125.            }
  126.            else
  127.            {
  128.            tempL|=(1<<(15-cbite));
  129.             }
  130.         }

  131.          else
  132.          {
  133.          if(cbite<8)
  134.            {
  135.            tempH&=(1<<(7-cbite));
  136.            }
  137.            else
  138.            {
  139.            tempL&=~(1<<(15-cbite));
  140.             }
  141.          }
  142.         write_dat(tempH);
  143.         write_dat(tempL);
  144. }


  145. void xline(uchar x0,uchar x1,uchar y,uchar color)
  146. {
  147.         uchar temp;
  148.         if(x0>x1)
  149.         {
  150.                 temp = x1;
  151.                 x1 = x0;
  152.                 x0 = temp;
  153.         }
  154.         do
  155.         {
  156.                 point(x0,y,color);
  157.                 x0++;
  158.         }while(x1>=x0);
  159. }

  160. void yline(uchar x,uchar y0,uchar y1,uchar color)
  161. {
  162.         uchar temp;
  163.         if(y0>y1)
  164.         {
  165.                 temp = y1;
  166.                 y1 = y0;
  167.                 y0 = temp;
  168.         }
  169.         do
  170.         {
  171.                 point(x,y0,color);
  172.                 y0++;
  173.         }while(y1>=y0);
  174. }

  175. void line(uchar x0,uchar y0,uchar x1,uchar y1,uchar color)
  176. {
  177.         char dx,dy;
  178.   char xsym,ysym;
  179.         char xx2,yx2;
  180.         char di;
  181.         dx = x1-x0;
  182.         dy = y1-y0;
  183.         if(dx>0)
  184.                 xsym = 1;
  185.         else
  186.         {
  187.                 if(dx<0)
  188.                         xsym = -1;
  189.                 else
  190.                 {
  191.                         yline(x0,y0,y1,color);
  192.                         return ;
  193.                 }
  194.         }
  195.         if(dy>0)
  196.                 ysym = 1;
  197.         else
  198.         {
  199.                 if(dy<0)
  200.                         ysym = -1;
  201.                 else
  202.                 {
  203.                         xline(x0,x1,y0,color);
  204.                         return ;
  205.                 }
  206.         }
  207.         dx = xsym*dx;                                                                //取绝对值
  208.         dy = ysym*dy;
  209.         
  210.         xx2 = dx*1;
  211.         yx2 = dy*1;
  212.         if(dx>=dy)
  213.         {
  214.                 di = yx2 - dx;
  215.                 while(x0 != x1)
  216.                 {
  217.                         point(x0,y0,color);
  218.                         x0 += xsym;
  219.                         if(di<0)
  220.                                 di += yx2;
  221.                         else
  222.                         {
  223.                                 di += yx2 - xx2;
  224.                                 y0 += ysym;
  225.                         }
  226.                 }
  227.                 point(x0,y0,color);
  228.         }
  229.         else
  230.         {
  231.                 di = xx2 - dy;
  232.                 while(y0 != y1)
  233.                 {
  234.                         point(x0,y0,color);
  235.                         y0 += ysym;
  236.                         if(di<0)
  237.                                 di += xx2;
  238.                         else
  239.                         {
  240.                                 di += xx2 - yx2;
  241.                                 x0 += xsym;
  242.                         }
  243.                 }
  244.                 point(x0,y0,color);
  245.         }
  246. }

  247. void circle(uchar x0,uchar y0,uchar r)
  248. {
  249.         char a,b;
  250.         char di;
  251.         if(r>31||r==0)
  252.                 return ;
  253.         a = 0;
  254.         b = r;
  255.         di = 3-2*r;
  256.         while(a<=b)
  257.         {
  258.                 point(x0+a,y0-b,1);                                //第一象限上半
  259.                 point(x0+b,y0-a,1);                                //第一象限下半
  260.                 point(x0-b,y0-a,1);                                //第二象限下半
  261.                 point(x0-a,y0-b,1);                                //第二象限上半
  262.                 point(x0-b,y0+a,1);                                //第三象限上半
  263.                 point(x0-a,y0+b,1);                          //第三象限下半
  264.                 point(x0+b,y0+a,1);                                //第四象限上半
  265.                 point(x0+a,y0+b,1);                          //第四象限下半

  266.                 delay(100);
  267.                 a++;
  268.                 if(di<0)
  269.                         di += 4*a+6;
  270.                 else
  271.                 {
  272.                         di += 10+4*(a-b);
  273.                         b--;
  274.                 }
  275.         }
  276. }

  277. uchar timex(uchar circle_x,uchar Length,uchar Angle)
  278. {
  279.    uchar x;
  280.    if((Angle>0) && (Angle<=15))
  281.    {  
  282.      x = circle_x + Length * (sin(PI * Angle / 30));   
  283.    }
  284.    else if(Angle > 15 && Angle <= 30)   
  285.    {  
  286.       x = circle_x + Length * cos((PI * Angle) / 30 - (PI / 2 ));
  287.    }
  288.    else if(Angle > 30 && Angle <= 45)
  289.    {
  290.        x = circle_x - Length * sin((PI * Angle) / 30- PI);
  291.    }
  292.    else
  293.    {
  294.        x = circle_x-Length * cos((PI * Angle) / 30 - ((3 * PI) / 2));
  295.    }  
  296.         return x;                       
  297. }

  298. uchar timey(uchar circle_y,uchar Length,uchar Angle)
  299. {
  300.    uchar y;
  301.    if((Angle>0) && (Angle<=15))
  302.    {  
  303.       y = circle_y - Length * (cos(PI * Angle / 30));   
  304.    }
  305.    else if(Angle > 15 && Angle <= 30)   
  306.    {  
  307.       y = circle_y + Length * sin((PI * Angle) / 30 - (PI / 2 ));
  308.    }
  309.    else if(Angle > 30 && Angle <= 45)
  310.    {
  311.        y = circle_y + Length * cos((PI * Angle) / 30- PI);
  312.    }
  313.    else
  314.    {
  315.        y = circle_y - Length * sin((PI * Angle) / 30 - ((3 * PI) / 2));
  316.    }
  317.    return y;                       
  318. }

  319. void zhizhen(uchar x0,uchar y0,struct clock aa,uchar color)
  320. {
  321.         line(x0,y0,timex(x0,x0-5,aa.second),timey(y0,y0-5,aa.second),color);
  322.         line(x0,y0,timex(x0,x0-9,aa.minute),timey(y0,y0-9,aa.minute),color);
  323.         line(x0,y0,timex(x0,x0-14,aa.minute/10+5*(aa.hour%12)),timey(y0,y0-14,aa.minute/10+5*(aa.hour%12)),color);
  324. }

  325. void timer0()
  326. {
  327.         TMOD = 0X01;
  328.         TH0 = 0X4c;
  329.         TL0 = 0X00;
  330.         TR0 = 1;
  331.         ET0 = 1;
  332.         EA = 1;
  333.         }

  334. void display(uchar x0,uchar y0)
  335. {
  336.         if(point_time[0].second != point_time[1].second)
  337.         {
  338.                 line(x0,y0,timex(x0,x0-5,point_time[0].second),timey(y0,y0-5,point_time[0].second),0);
  339.         }
  340.         if(point_time[0].minute != point_time[1].minute)
  341.         {
  342.                 line(x0,y0,timex(x0,x0-9,point_time[0].minute),timey(y0,y0-9,point_time[0].minute),0);
  343.         }
  344.         if((point_time[0].minute/10+5*(point_time[0].hour%12)) != (point_time[1].minute/10+5*(point_time[1].hour%12)))
  345.         {
  346.                 line(x0,y0,timex(x0,x0-14,point_time[0].minute/10+5*(point_time[0].hour%12)),timey(y0,y0-14,point_time[0].minute/10+5*(point_time[0].hour%12)),0);
  347.         }
  348.         zhizhen(x0,y0,point_time[1],1);
  349.         point_time[0] = point_time[1];
  350. }

  351. void main()
  352. {        
  353.                 uchar j;
  354.                 lcd_init();
  355.                 screen_clear();
  356.                 delay(1);
  357.                 screen_clear();
  358.                 delay(1);
  359.                 write_com(0x01);
  360.                 delay(1);

  361.                 for(j=0;j<60;j++)
  362.   {
  363.                 if((j%5)==0)      //画刻度
  364.     {
  365.                         line(timex(31,30,j),timey(32,30,j),timex(31,27,j),timey(32,27,j),1);                                       
  366.     }
  367.   }
  368.   point_time[1].second = 50;
  369.         point_time[1].minute = 29;
  370.         point_time[1].hour = 7;
  371.         point_time[0] = point_time[1];        
  372.          circle(31,32,31);
  373.          timer0();
  374.        while(1);
  375. }


  376. void zhongduan() interrupt 1
  377. {        
  378.         uint num=0;
  379.         lay++;

  380.         
  381. if(num==0)
  382. {
  383.         if(lay == 20)
  384.         {
  385.                 lay = 0;
  386.                 point_time[1].second += 1;
  387.                 if(point_time[1].second == 60)
  388.                 {
  389.                         point_time[1].second = 0;
  390.                         point_time[1].minute += 1;
  391.                         if(point_time[1].minute == 60)
  392.                         {
  393.                                 point_time[1].minute = 0;
  394.                                 point_time[1].hour += 1;
  395.                                 if(point_time[1].hour == 12)
  396.                                 {
  397.                                         point_time[1].hour = 0;
  398.                                 }
  399.                         }
  400.                 }

  401.                
  402.                 display(31,32);
  403.         }
  404. }
  405. }
复制代码

回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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