我改了一下DS1302的驱动函数。你的main函数都写成mian.我仿真一直没反应。我还以为是程序问题呢。
你ds1302的读写都保存在数组time-dat中,根本就没有用到结构体time.
发改过的程序给你你下载看看还有什么问题没。因为我不会12864,因此也不能完全仿真。有问题再上来问。
- #include
- #include
- #define uchar unsigned char
- #define uint unsigned int
- ***it lcdrs=P0^7;
- ***it lcdrw=P0^6;
- ***it lcde=P0^5;
- ***it lcdp***=P0^4;
- ***it rst=P1^0;
- ***it sclk=P1^2;
- ***it io=P1^1;
- uchar writedat[]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80};
- uchar read_dat[]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81};
- uchar time_dat[7]={14,1,4,21,11,58,30}; //年周月日时分秒
- uchar code table[]="0123456789";
- uchar code table1[]="一二三四五六日";
- uchar str0[16]="2014/04/21";
- uchar str1[16]="09:46:30 星期一";
- uchar str2[16]="fffff";
- uchar str3[16]="dddd";
- void write1302_byte(uchar dat);//写数据
- void write1302(uchar add,uchar dat);//写地址和写数据
- read1302(uchar add);//读数据
- void set_rtc(void);//初始化
- void lcd12864display(uchar *p,uchar line);
- void lcd12864display(uchar *p,uchar line)
- {
- uchar add;
- uchar i;
- if(line==1)
- {add=0x80;}
- if(line==2)
- {add=0x90;}
- if(line==3)
- {add=0x88;}
- if(line==4)
- {add=0x98;}
- i=*p+line;
- // write_com(i);
- }
- void delayms(uint x)
- {
- uchar i,j;
- for(j=0;j
- for(i=0;i<110;i++); //延时x秒
- }
- void write_com(uchar com) //写指令
- {
- lcdrs=0;
- lcdrw=0;
- lcde=0;
- P2=com;
- delayms(5);
- lcde=1;
- delayms(5);
- lcde=0;
- }
- void write_dat(uchar dat) //写数据
- {
- lcdrs=1;
- lcdrw=0;
- lcde=0;
- P2=dat;
- delayms(5);
- lcde=1;
- delayms(5);
- lcde=0;
- }
- void lcd_init() //初始化
- {
- lcdp***=1;
- write_com(0x30);
- delayms(5);
- write_com(0x0c);
- delayms(5);
- write_com(0x01);
- delayms(5);
- }
- void write1302_byte(uchar dat)//写数据
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- sclk=0;
- io=dat&0x01;
- dat=dat>>1;
- sclk=1;
- }
- }
- void write1302(uchar add,uchar dat)
- {
- rst=0;
- _nop_();
- sclk=0;
- _nop_();
- rst=1;
- _nop_();
- write1302_byte(add);//写地址
- write1302_byte(dat);//写数据
- rst=0;
- }
- read1302(uchar add)
- {
- uchar i,value;
- rst=0;
- _nop_();
- sclk=0;
- _nop_();
- rst=1;
- _nop_();
- write1302_byte(add);
- for(i=0;i<8;i++)
- {
- value=value>>1;
- sclk=0;
- if(io)
- value=value|0x80;
- sclk=1;
- _nop_();
- }
- rst=0;
- _nop_();
- sclk=0;
- _nop_();
- sclk=1;
- _nop_();
- io=0;
- _nop_();
- return value;//返回
- }
- void set_rtc() //初始化
- {
- uchar i,j;
- for(i=0;i<7;i++)
- {
- j=time_dat[i]/10;
- time_dat[i]=time_dat[i]%10;
- time_dat[i]=time_dat[i]+j*16;
- }
- write1302(0x8e,0x00); //去除写保护
- for(i=0;i<7;i++)
- {
- write1302(writedat[i],time_dat[i]);
- }
- write1302(0x8e,0x80);//写保护
- }
- void read_str(void)
- {
- uchar i;
- for(i=0;i<7;i++)
- {
- time_dat[i]=read1302(read_dat[i]);
- }
- }
- void main()
- {
- set_rtc();
- delayms(50);
- lcd_init();
- delayms(50);
- while(1)
- {
- read_str();
- str0[2]=table[time_dat[0]/10];
- str0[3]=table[time_dat[0]%10];
- str0[6]=table[time_dat[2]/10];
- str0[7]=table[time_dat[2]%10];
- str0[9]=table[time_dat[3]/10];
- str0[10]=table[time_dat[3]%10];
- str1[1]=table[time_dat[4]/10];
- str1[2]=table[time_dat[4]%10];
- str1[4]=table[time_dat[5]/10];
- str1[5]=table[time_dat[5]%10];
- str1[7]=table[time_dat[6]/10];
- str1[8]=table[time_dat[6]%10];
- str1[14]=table1[(time_dat[1]-1)*2-1];
- str1[15]=table1[(time_dat[1]-1)*2] ;
- lcd12864display(str0,1);
- lcd12864display(str1,2);
- lcd12864display(str2,3);
- lcd12864display(str3,4);
- }
-
- }
复制代码
|