Home > other >  51 single-chip microcomputer and DS1302 for help!
51 single-chip microcomputer and DS1302 for help!

Time:11-29

Using DS1302 module in a recent study, meet a problem, is what I need to put the DS1302 time displayed on the LCD, but LCD shows my time is in the program initialization time, rather than the current time, real-time and bosses guidance,

The following is my program:
#include #include //contains _nop_ empty () function instruction of header files # define uchar unsigned char# define uint unsigned intsbit IO=P1 ^ 2;//input/output sbit SCLK P1=^ 1;//the clock sbit RST=P1 ^ 0;//reset sbit RS=P2 ^ 5;//variable sbit RW=P2 ^ 6;//a variable sbit E=P2 ^ 7;////a variable - DS1302 write and read the minutes of address command -////- second anniversary of the time-sharing runs its lowest reading and writing; -- -- -- -- -- -- --//uchar READ_RTC_ADDR [7]={0 x81, 0 x83, 0 x85, 0 x87, 0 x89, 0 x8b, 0 x8d}; Uchar WRITE_RTC_ADDR [7]={x84 0 x80, 0 x82, 0, 0 x86, 0 x88, 0 x8a, 0 x8c}; Initialization//- DS1302 clock on January 1, 2013, 00 00 p.m. on Tuesday,////-- - storage sequence is the second anniversary of the time-sharing runs, storage format is to use BCD -//uchar TIME [6]={0 0, x12, 0 x01, 0 x01, 0 x13}; Void Lcd_Display ();//LCD display function void delay (uint j);//delay function void check_busy (void);//busy sign detection function void write_command (uchar com);//write command function void write_data (uchar dat);//write data function void Lcd_Init (void);//LCD initialization function void Ds1302_Write (uchar addr, uchar dat);//DS1302 write command function uchar Ds1302_Read (uchar addr);//DS1302 read an address of data void Ds1302_Init ();//initialize DS1302void Ds1302_ReadTime ();//read the clock information/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * the main function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void main () {Lcd_Init (); Ds1302_Init (); While (1) Lcd_Display (); }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * 1 ms delay function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void delay (uint j) {uchar I=250; For (; J> 0; J -) {while (- I); I=249; While (I); I=250; }}/* * * * * * * * * * * * * * * * * * * * * * * * * * * * LCD initialization function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void Lcd_Init (void) {write_command x38 (0);//write command 0 x38:8, according to two lines of 5 x 7 dot matrix characters write_command x0c (0);//write command 0 x0c: overall, according to the cursor, no black piece write_command x06 (0);//write command 0 x06, 0 x05 left write_command (0 x01);//write command 0 x01: screen clearing delay (1); }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * write command function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void write_command (uchar com) {check_busy (); E=0; RS=0; RW=0; P0=com; E=1; _nop_ (); E=0; Delay (1); }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * to write data function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void write_data (uchar dat) {check_busy (); E=0; RS=1; RW=0; P0=dat; E=1; _nop_ (); E=0; Delay (1); }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * check busy sign function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void check_busy (void) {uchar dt. Do {dt=0 XFF; E=0; RS=0; RW=1; E=1; Dt=P0; } while (dt& 0 x80); E=0; }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * initializes the DS1302 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void Ds1302_Init () {uchar n; Ds1302_Write (0 x8e, 0 x00);//write protected, is to close the write protect function for (n=0; n<7. N++)//write 7 bytes of the clock signal: when a vehicle runs anniversary {Ds1302_Write (WRITE_RTC_ADDR [n], TIME [n]); } Ds1302_Write (0 x8e, 0 x80);//open the write protect function}/* * * * * * * * * * * * * * * * * * * * * * * * * * * to DS1302 write command + data (address) * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void Ds1302_Write (uchar addr, uchar dat) {uchar n; RST=0; _nop_ (); SCLK=0;//SCLK low level, first _nop_ (); RST=1;//high level then RST (CE), _nop_ (); For (n=0; n<8; Send eight address orders n++)//{IO=addr & amp; 0 x01;//transfer data from low addr & gt;>=1; SCLK=1;//data on the rise along, DS1302 read data _nop_ (); SCLK=0; _nop_ (); } the for (n=0; n<8; N++)//write 8 bits of data {IO=dat & amp; 0 x01; Dat & gt;>=1; SCLK=1;//data on the rise along, DS1302 read data _nop_ (); SCLK=0; _nop_ (); } RST=0;//send data over _nop_ (); }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * read an address of data * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */uchar Ds1302_Read (uchar addr) {uchar n, dat, dat1; RST=0; _nop_ (); SCLK=0;//SCLK low level, first _nop_ (); RST=1;//high level then RST (CE), _nop_ (); For (n=0; n<8; Send eight address orders n++)//{IO=addr & amp; 0 x01;//transfer data from low addr & gt;>=1; SCLK=1;//data on the rise along, DS1302 read data _nop_ (); SCLK=0;//DS1302 falling edge, place data _nop_ (); } _nop_ (); For (n=0; n<8; N++)//read 8 bits of data {dat1=IO;//from the lowest to receive dat=(dat> 1) | (dat1 & lt; <7); SCLK=1; _nop_ (); SCLK=0;//DS1302 falling edge, place data _nop_ (); } RST=0; _nop_ ();//the following for the stability of DS1302 reset time, must, SCLK=1; _nop_ (); IO=0; _nop_ (); IO=1; _nop_ (); Return dat. }/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * read clock information * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void Ds1302_ReadTime () {uchar n; For (n=0; n<6; N++)//read 6 bytes of the clock signal: the second anniversary of the time-sharing runs {TIME [n]=Ds1302_Read (READ_RTC_ADDR [n]); }}/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * LCD display function * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */void Lcd_Display () {Ds1302_ReadTime (); Write_command (0 x80 + 0 x40); Write_data (' 0 '+ TIME [2]/16);//write_data (' 0 '+ (TIME [2] & amp; 0 x0f)); Write_data (' - '); Write_data (' 0 '+ TIME [1]/16);//write_data (' 0 '+ (TIME [1] & amp; 0 x0f)); Write_data (' - '); Write_data (' 0 '+ TIME [0]/16);//SEC write_data (' 0 '+ (TIME [0] & amp; 0 x0f)); Write_command (0 x80); Write_data (' 2 '); Write_data (' 0 '); Write_data (' 0 '+ TIME [5]/16);//year write_data (' 0 '+ (TIME [5] & amp; 0 x0f)); Write_data (' - '); Write_data (' 0 '+ TIME [4]/16);//write_data (' 0 '+ (TIME [4] & amp; 0 x0f)); Write_data (' - '); Write_data (' 0 '+ TIME [3]/16);//day write_data (' 0 '+ (TIME [3] & amp; 0 x0f)); }

CodePudding user response:

Detailed see DS1302 manual, there is a control bit is used to start the RTC clock

CodePudding user response:

Commented DS1302 initialization statement compilation again, don't every time electricity is initialization time,

CodePudding user response:

DS1302 first initialized, go in time to write, then initialization time code commented out recompiling burn again is OK
  • Related