做的一个12864指针时钟程序,但是指针显示有问题,上一秒指针转到下一秒的位置时,有痕迹没有擦除,那位大佬可以帮给我看一下怎么回事,帮忙改一下。
- #include <reg52.h>
- #include"math.h"
- #define PI 3.1415926
- #define uchar unsigned char
- #define uint unsigned int
- #define basic 0x30 //基本指令集
- #define extend 0x34 //扩充指令集
- #define drawon 0x36 //绘图显示关
- #define drawoff 0x34
- sbit LCD_RS = P0^7; //寄存器选择输入
- sbit LCD_RW = P0^6; //液晶读/写控制
- sbit LCD_EN = P0^5; //液晶使能控制
- sbit LCD_PSB = P0^4;
- uint lay=0;
- struct clock
- {
- uchar hour;
- uchar minute;
- uchar second;
- }point_time[2];
- void delay(uint x) //延时函数
- { int i,j;
- for(i=x;i>0;i--)
- for(j=11;j>0;j--);
- }
- void write_com(uchar com) //写命令
- {
- LCD_RS = 0;
- LCD_RW = 0;
- LCD_EN = 0;
- P2 = com;
- delay(1);
- LCD_EN = 1;
- delay(1);
- LCD_EN = 0;
- }
- void write_dat(uchar dat) //写数据
- {
- LCD_RS = 1;
- LCD_RW = 0;
- LCD_EN = 0;
- P2 = dat;
- delay(1);
- LCD_EN = 1;
- delay(1);
- LCD_EN = 0;
- }
- uchar read_date(void)
- { uchar temp;
- P2=0xff;
- LCD_RS = 1;
- LCD_RW = 1;
- LCD_EN = 1;
- delay(1);
- temp=P2;
- LCD_EN = 0;
- return temp;
- }
- void lcd_init() //初始化
- {
- LCD_PSB = 1; //并口方式
- write_com(0x34); //扩充指令
- delay(10);
- write_com(0x30); //基本指令操作
- delay(10);
- write_com(0x0C); //显示开,关光标
- delay(10);
- write_com(0x01); //清除LCD的显示内容
- delay(10);
- }
- void screen_clear() //清屏
- {
- uchar i,j,k;
- write_com(0x34);
- for(i=0;i<2;i++)
- { for(j=0;j<32;j++)
- { write_com(0x80+j);
- delay(1);
- if(i==0)
- { write_com(0x80);
- delay(1);
- }
- else
- {
- write_com(0x88);
- delay(1);
- }
- for(k=0;k<16;k++)
- {
- write_dat(0x00);
- write_dat(0x00);
- delay(1);
- }
- }
- }
- write_com(0x36);
- write_com(0x30);
- }
- void point(uchar x,uchar y,uchar color) //打点函数
- {
- uchar row,collum,cbite;
- uchar tempH,tempL;
- write_com(0x34);
- write_com(0x36);
- collum=x>>4;
- cbite=x&0x0f;
- if(y<32)row=y;
- else
- {
- row=y-32;
- collum+=8;
- }
- write_com(0x80+row);
- write_com(0x80+collum);
- read_date();
- tempH=read_date();
- tempL=read_date();
- write_com(0x80+row);
- write_com(0x80+collum);
- if(color)
- {
- if(cbite<8)
- {
- tempH|=(1<<(7-cbite));
- }
- else
- {
- tempL|=(1<<(15-cbite));
- }
- }
- else
- {
- if(cbite<8)
- {
- tempH&=(1<<(7-cbite));
- }
- else
- {
- tempL&=~(1<<(15-cbite));
- }
- }
- write_dat(tempH);
- write_dat(tempL);
- }
- void xline(uchar x0,uchar x1,uchar y,uchar color)
- {
- uchar temp;
- if(x0>x1)
- {
- temp = x1;
- x1 = x0;
- x0 = temp;
- }
- do
- {
- point(x0,y,color);
- x0++;
- }while(x1>=x0);
- }
- void yline(uchar x,uchar y0,uchar y1,uchar color)
- {
- uchar temp;
- if(y0>y1)
- {
- temp = y1;
- y1 = y0;
- y0 = temp;
- }
- do
- {
- point(x,y0,color);
- y0++;
- }while(y1>=y0);
- }
- void line(uchar x0,uchar y0,uchar x1,uchar y1,uchar color)
- {
- char dx,dy;
- char xsym,ysym;
- char xx2,yx2;
- char di;
- dx = x1-x0;
- dy = y1-y0;
- if(dx>0)
- xsym = 1;
- else
- {
- if(dx<0)
- xsym = -1;
- else
- {
- yline(x0,y0,y1,color);
- return ;
- }
- }
- if(dy>0)
- ysym = 1;
- else
- {
- if(dy<0)
- ysym = -1;
- else
- {
- xline(x0,x1,y0,color);
- return ;
- }
- }
- dx = xsym*dx; //取绝对值
- dy = ysym*dy;
-
- xx2 = dx*1;
- yx2 = dy*1;
- if(dx>=dy)
- {
- di = yx2 - dx;
- while(x0 != x1)
- {
- point(x0,y0,color);
- x0 += xsym;
- if(di<0)
- di += yx2;
- else
- {
- di += yx2 - xx2;
- y0 += ysym;
- }
- }
- point(x0,y0,color);
- }
- else
- {
- di = xx2 - dy;
- while(y0 != y1)
- {
- point(x0,y0,color);
- y0 += ysym;
- if(di<0)
- di += xx2;
- else
- {
- di += xx2 - yx2;
- x0 += xsym;
- }
- }
- point(x0,y0,color);
- }
- }
- void circle(uchar x0,uchar y0,uchar r)
- {
- char a,b;
- char di;
- if(r>31||r==0)
- return ;
- a = 0;
- b = r;
- di = 3-2*r;
- while(a<=b)
- {
- point(x0+a,y0-b,1); //第一象限上半
- point(x0+b,y0-a,1); //第一象限下半
- point(x0-b,y0-a,1); //第二象限下半
- point(x0-a,y0-b,1); //第二象限上半
- point(x0-b,y0+a,1); //第三象限上半
- point(x0-a,y0+b,1); //第三象限下半
- point(x0+b,y0+a,1); //第四象限上半
- point(x0+a,y0+b,1); //第四象限下半
- delay(100);
- a++;
- if(di<0)
- di += 4*a+6;
- else
- {
- di += 10+4*(a-b);
- b--;
- }
- }
- }
- uchar timex(uchar circle_x,uchar Length,uchar Angle)
- {
- uchar x;
- if((Angle>0) && (Angle<=15))
- {
- x = circle_x + Length * (sin(PI * Angle / 30));
- }
- else if(Angle > 15 && Angle <= 30)
- {
- x = circle_x + Length * cos((PI * Angle) / 30 - (PI / 2 ));
- }
- else if(Angle > 30 && Angle <= 45)
- {
- x = circle_x - Length * sin((PI * Angle) / 30- PI);
- }
- else
- {
- x = circle_x-Length * cos((PI * Angle) / 30 - ((3 * PI) / 2));
- }
- return x;
- }
- uchar timey(uchar circle_y,uchar Length,uchar Angle)
- {
- uchar y;
- if((Angle>0) && (Angle<=15))
- {
- y = circle_y - Length * (cos(PI * Angle / 30));
- }
- else if(Angle > 15 && Angle <= 30)
- {
- y = circle_y + Length * sin((PI * Angle) / 30 - (PI / 2 ));
- }
- else if(Angle > 30 && Angle <= 45)
- {
- y = circle_y + Length * cos((PI * Angle) / 30- PI);
- }
- else
- {
- y = circle_y - Length * sin((PI * Angle) / 30 - ((3 * PI) / 2));
- }
- return y;
- }
- void zhizhen(uchar x0,uchar y0,struct clock aa,uchar color)
- {
- line(x0,y0,timex(x0,x0-5,aa.second),timey(y0,y0-5,aa.second),color);
- line(x0,y0,timex(x0,x0-9,aa.minute),timey(y0,y0-9,aa.minute),color);
- 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);
- }
- void timer0()
- {
- TMOD = 0X01;
- TH0 = 0X4c;
- TL0 = 0X00;
- TR0 = 1;
- ET0 = 1;
- EA = 1;
- }
- void display(uchar x0,uchar y0)
- {
- if(point_time[0].second != point_time[1].second)
- {
- line(x0,y0,timex(x0,x0-5,point_time[0].second),timey(y0,y0-5,point_time[0].second),0);
- }
- if(point_time[0].minute != point_time[1].minute)
- {
- line(x0,y0,timex(x0,x0-9,point_time[0].minute),timey(y0,y0-9,point_time[0].minute),0);
- }
- if((point_time[0].minute/10+5*(point_time[0].hour%12)) != (point_time[1].minute/10+5*(point_time[1].hour%12)))
- {
- 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);
- }
- zhizhen(x0,y0,point_time[1],1);
- point_time[0] = point_time[1];
- }
- void main()
- {
- uchar j;
- lcd_init();
- screen_clear();
- delay(1);
- screen_clear();
- delay(1);
- write_com(0x01);
- delay(1);
- for(j=0;j<60;j++)
- {
- if((j%5)==0) //画刻度
- {
- line(timex(31,30,j),timey(32,30,j),timex(31,27,j),timey(32,27,j),1);
- }
- }
- point_time[1].second = 50;
- point_time[1].minute = 29;
- point_time[1].hour = 7;
- point_time[0] = point_time[1];
- circle(31,32,31);
- timer0();
- while(1);
- }
- void zhongduan() interrupt 1
- {
- uint num=0;
- lay++;
-
- if(num==0)
- {
- if(lay == 20)
- {
- lay = 0;
- point_time[1].second += 1;
- if(point_time[1].second == 60)
- {
- point_time[1].second = 0;
- point_time[1].minute += 1;
- if(point_time[1].minute == 60)
- {
- point_time[1].minute = 0;
- point_time[1].hour += 1;
- if(point_time[1].hour == 12)
- {
- point_time[1].hour = 0;
- }
- }
- }
-
- display(31,32);
- }
- }
- }
复制代码
|