初始只有秒位可以顯示。分,時的初始化都不正確,秒計滿分位也沒有變化。奇怪,秒分時的寫入不都是同樣的函數嗎。怎么就初始化不行。秒位從00到59可以顯示,那能說明DS1302正常工作了嗎。 還有個問題,我看有的書上,DS1302與單片機接口的RST腳接了非門,我的沒接,這樣可以不。
//DS1302 RTC drive program //for 51 mcu with max7219 display //designed by zhaoliang //2005-6-16 18:27 #i nclude <reg51.h> #i nclude <intrins.h>
// common PreDefinition #define HIGH 1 #define LOW 0 #define TRUE 1 #define ZERO 0 #define MSB 0x80 #define LSB 0x01 // max7219 PreDefinitiont #define DECODE_MODE 0x09 #define INTENSITY 0x0A #define SCAN_LIMIT 0x0B #define SHUT_DOWN 0x0C #define DISPLAY_TEST 0x0F // ds1302 PreDefinition #define DS1302_WP 0x8E #define DS1302_RESET RST=LOW;SCL=LOW;RST=HIGH #define DS1302_WP_ENABLE Write_Ds1302(DS1302_WP,0X00) #define DS1302_WP_DISENABLE Write_Ds1302(DS1302_WP,0x80)
//pin defined /***********************************************************************/ //change this part at different board sbit LOAD=P1^2; //MAX7219 Load-Data Input: rising edge pin 12 sbit DIN=P1^1; //MAX7219 Serial-Data Input: rising edge pin 1 sbit CLK=P1^0; //MAX7219 Serial-Clock Input: maximum 10MHz pin 13
sbit SCL = P1^6;// DS1302 Serial-Clock Input pin 7 sbit SDA = P1^7;// DS1302 Serial-Data Input pin 6 sbit RST = P1^4;// DS1302 Chip-Seclet Input pin 5
//function define /***********************************************************************/ void Write_Max7219_byte(unsigned char temp);//write max7219 a byte void Write_Max7219(unsigned char address,unsigned char dat);//write max7219 command and data void Init_Max7219(void);//Initize max7219
void Write_Ds1302_byte(unsigned char temp); void Write_Ds1302( unsigned char address,unsigned char dat ); unsigned char Read_Ds1302 ( unsigned char address );
void Read_RTC(void);//read RTC void Set_RTC(void);//set RTC
void Display(void); void Initial(void);
code unsigned char set_rtc_code[3]={0x00,0x23,0x07}; code unsigned char write_rtc_address[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c}; code unsigned char read_rtc_address[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d}; unsigned char read_rtc_code[3]; /****************************************************************************/ /****************************************************************************/ void main (void) { Initial (); while (1) { Read_RTC(); Display (); } } /****************************************************************************/ /****************************************************************************/ void Initial(void) //MAX7219初始化 { Init_Max7219(); DS1302_WP_ENABLE; Set_RTC(); DS1302_WP_DISENABLE; } /****************************************************************************/ /***********************************************************************/ void Write_Max7219_byte(unsigned char temp) { unsigned char i; for (i=0;i<8;i++) { CLK=LOW; DIN=temp&MSB; temp<<=1; CLK=HIGH; } } /***********************************************************************/ void Write_Max7219(unsigned char address,unsigned char dat) { LOAD=LOW; Write_Max7219_byte(address); Write_Max7219_byte(dat); LOAD=HIGH; } /***********************************************************************/ void Init_Max7219(void) { Write_Max7219(SHUT_DOWN, 0x01); //Normal Operation XXXXXXX1 Shutdown Mode XXXXXXXX0 Write_Max7219(DISPLAY_TEST, 0x00); //Normal Operation XXXXXXX0 Display Test Mode XXXXXXXX1 Write_Max7219(DECODE_MODE, 0xdb); //Decode Mode Select D7~D0 1 B decode 0 No decode Write_Max7219(SCAN_LIMIT, 0x07); //SCAN LIMIT 0~7 0xX0~0xX7 Write_Max7219(INTENSITY, 0x04); //Set Intensity 0xX0~0xXf } /****************************************************************************/ /****************************************************************************/ void Write_Ds1302_Byte(unsigned char temp) { unsigned char i; for (i=0;i<8;i++) { SCL=LOW; SDA=temp&LSB; temp>>=1; SCL=HIGH; } } /****************************************************************************/ void Write_Ds1302( unsigned char address,unsigned char dat ) { DS1302_RESET; Write_Ds1302_Byte(address); Write_Ds1302_Byte(dat); RST=LOW; } /****************************************************************************/ unsigned char Read_Ds1302 ( unsigned char address ) { unsigned char i,temp=0x00,temp_temp; DS1302_RESET; Write_Ds1302_Byte(address); for (i=0;i<8;i++) { if(SDA) temp|=0x80; SCL=LOW; temp>>=1; SCL=HIGH; } RST=LOW; temp_temp=temp/16; temp=temp%16; temp=temp+temp_temp*10; return (temp); } /****************************************************************************/ void Read_RTC(void) { unsigned char i,*p; p=read_rtc_address; for(i=0;i<3;i++) { read_rtc_code=Read_Ds1302(*p); p++; } } /***********************************************************************/ void Set_RTC(void) { unsigned char i,*p; p=write_rtc_address; for(i=0;i<3;i++) { Write_Ds1302(*p,set_rtc_code); p++; } } /****************************************************************************/ void Display(void) { Write_Max7219(1,read_rtc_code[2]/10); Write_Max7219(2,read_rtc_code[2]%10); Write_Max7219(4,read_rtc_code[1]/10); Write_Max7219(5,read_rtc_code[1]%10); Write_Max7219(7,read_rtc_code[0]/10); Write_Max7219(8,read_rtc_code[0]%10); Write_Max7219(3,0x01);//no decode mode Write_Max7219(6,0x01);//display '- }
|