在DS1302上遇到点麻烦,求各位大虾看看程序
[复制链接]
这个是我的DS1302 谁帮我试下,SCLK=P1^0,IO=P1^1,REST=P1^2;数码管段是P3口,位是P2^0-P2^3,可是我显示出来的是1100,不知道怎么回事,帮我改下 谢谢 #include<reg51.h> #include <intrins.h> #define uchar unsigned char #define uint unsigned int sbit sck=P1^0; sbit io=P1^1; sbit rst=P1^2; //sbit smgen=P2^3; sfr P0M0=0X93; sfr P0M1=0x94; sfr P1M0=0X91; sfr P1M1=0x92; sfr P2M0=0X95; sfr P2M1=0x96; unsigned char code smg_du[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x00}; unsigned char code smg_we[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; uchar time_data[]={10,6,4,17,11,58,30}; //年周月日时分秒 uchar write_add[]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; uchar read_add[]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; uchar disp[8]; void write_ds1302_byte(uchar dat); void write_ds1302(uchar add,uchar dat); uchar read_ds1302(uchar add); void set_rtc(void); void read_rtc(void); void time_pros(void); void display(void); //************************************************ //延时函数,在12MHz的晶振频率下 //大约50us的延时 //************************************************ void delay_50us(uint t) { uchar j; for(;t>0;t--) for(j=19;j>0;j--); }
//************************************************ //延时函数,在12MHz的晶振频率下 //大约50ms的延时 //************************************************ void delay_50ms(uint t) { uint j; for(;t>0;t--) for(j=6245;j>0;j--); } /*void initt0() { TMOD=0X01; TH0=0XEF; TL0=0XF0; ET0=1; TR0=1; EA=1; } void to() interrupt 1 { TH0=0XF5; TL0=0XE0; } */ void write_ds1302_byte(uchar dat) { uchar i; for(i=0;i<8;i++) { sck=0; io=dat&0x01; dat=dat>>1; sck=1; } }
void write_ds1302(uchar add,uchar dat) { rst=0; _nop_(); sck=0;_nop_(); rst=1; _nop_(); write_ds1302_byte(add); write_ds1302_byte(dat); rst=0; //_nop_(); //io=1; // sck=1; } uchar read_ds1302(uchar add) { uchar i,value=0x00; rst=0; _nop_(); sck=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); for(i=0;i<8;i++) { if(io) value=value|0x80; sck=0; value=value>>1; sck=1; } rst=0; _nop_(); sck=0; _nop_(); sck=1; _nop_(); io=0; _nop_(); io=1; _nop_(); return value; } void set_rtc(void) { uchar i,j; for(i=0;i<7;i++) { j=time_data/10; time_data=time_data%10; time_data=time_data+j*16; } write_ds1302(0x8e,0x00); //去除写保护 for(i=0;i<7;i++) { write_ds1302(write_add,time_data); } write_ds1302(0x8e,0x80); //加写保护 } void read_rtc(void) { uchar i,*p; p=read_add; for(i=0;i<7;i++) { time_data=read_ds1302(*P);//read_add[*p]); p++; } } void time_pros(void) { disp[0]=time_data[6]%10; disp[1]=time_data[6]/10; disp[2]=0; disp[3]=time_data[5]%10; disp[4]=time_data[5]/10; disp[5]=0; disp[6]=time_data[4]%10; disp[7]=time_data[4]/10; } void display(void) { uchar i; for(i=0;i<8;i++) { P3=smg_du[disp]; P2=smg_we; delay_50us(20); } } void main(void) { //set_rtc(); // initt0(); P0M0=0X00; P0M1=0xff; P1M0=0X00; P1M1=0xff; P2M0=0X00; P2M1=0xff; while(1) { //smgen=0; read_rtc(); // smgen=1; time_pros(); display(); }
}
|