在DS1302上遇到点麻烦,求各位大虾看看程序
[复制链接]
这个是我的DS1302 谁帮我试下,SCLK=P1^0,IO=P1^1,REST=P2^4;数码管段是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=P2^4; 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,0x88,0x83,0xa7,0xa1};//0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x00,0x10}; unsigned char code smg_we[]={0x01,0x02,0x04,0x08};//0x08,0x18,0x28,0x38,0x48,0x58,0x68,0x78}; uchar time_data[]={0,0,0,0};//,11,58,30}; //年周月日时分秒 uchar write_add[7]={0x8c,0x8a,0x88,0x86,0x84,0x82,0x80}; uchar read_add[7]={0x8d,0x8b,0x89,0x87,0x85,0x83,0x81}; uchar disp[4]; 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 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; rst=0; _nop_(); sck=0; _nop_(); rst=1; _nop_(); write_ds1302_byte(add); for(i=0;i<8;i++) { value=value>>1; sck=0; if(io) value=value|0x80; sck=1; } rst=0; _nop_(); sck=0; _nop_(); sck=1; io=1; return value; } void set_rtc(void) { uchar i,j; for(i=0;i<4;i++) { j=time_data/10; time_data=time_data%10; time_data=time_data+j*16; } write_ds1302(0x8e,0x00); //去除写保护 for(i=0;i<4;i++) { write_ds1302(write_add,time_data); } write_ds1302(0x8e,0x80); //加写保护 } void read_rtc(void) { uchar i; for(i=0;i<4;i++) { time_data=read_ds1302(read_add); } } void time_pros(void) { disp[0]=time_data[4];//%16; disp[1]=time_data[4];///16; disp[2]=time_data[3];//%16;//16; disp[3]=time_data[3];///16;//time_data[5]%16; /* disp[4]=time_data[5]/16; disp[5]=16; disp[6]=time_data[4]%16; disp[7]=time_data[4]/16; */ } void display(void) { uchar i; for(i=0;i<4;i++) { P3=smg_du[disp]; P2=smg_we; delay_50us(20); } } void main(void) { // set_rtc(); P0M0=0X00; P0M1=0xff; P1M0=0X00; P1M1=0xff; P2M0=0X00; P2M1=0xff; while(1) { read_rtc(); time_pros(); display(); }
}
|