??? 05/11/09 08:25 Read: times |
#165227 - Interfacing HT1380 with 89C51 in C |
Hello guys,
I am presently working on the Holtek serial Timer IC and interfacing it with the AT89C51. I have the circuit hardware. the problem is that there is no reply form the timer IC. What i am doing is that i have connected the MCU to the serial port of the computer. when i switch it on the MCU is suppose to initialize the 1380 and then whatever command i give to the MCU(0x81,0x83,0x85 ie the read addresses of the registers), I should get the data stored on that register serially to my com port. But the problem is that i either get 0x7F or 0x40 on my computer serial port as a reply. I have changed the 32768Hz crystal and the capacitors many times. The link to the datasheet is: http://www.holtek.com/pdf/consumer/1380_1v111.pdf I am insetting the program code as well. I have two extra LED's connected to the P3_6 and P3_7 of the MCU just for the sake of indication and troubleshooting. ************************************************************************************ #include "8051.h" #include "serial.h" #define SCLK P2_0 #define DATA_IO P2_1 #define RESET_ P2_2 #define YELLOW_LED P3_6 #define RED_LED P3_7 unsigned char serial_storage=0x00; unsigned char display; void usec_wait(unsigned char); void msec_wait(unsigned char); void sec_wait(unsigned char); void convert_and_send(unsigned char); unsigned char receive_convert(void); //************************************************************** //DELAY FUNCTIONS - USEC_WAIT ; MSEC_WAIT ; SEC_WAIT //************************************************************** void usec_wait(unsigned char k) { while(k!=0) { k--; } } ////////////////////////////////////////////////////// void msec_wait(unsigned char j) { while(j!=0) { usec_wait(165); j--; } } ////////////////////////////////////////////////////// void sec_wait(unsigned char sec_count) { while(sec_count!=0) { msec_wait(250); msec_wait(250); msec_wait(250); msec_wait(250); sec_count--; } } ////////////////////////////////////////////////////// //************************************************************** //SERIAL INTERRUPT FUNCTION //************************************************************** void serial_interrupt() interrupt 4 { if(TI) TI=0; if(RI) { serial_storage=SBUF; usec_wait(1); RI=0; RED_LED=1; } } //******************************************************************************** //FUNCTION THAT SENDS ADDRESS OR DATA_IO AFTER CONVERTING IT TO BITS TO THE 1380 //******************************************************************************** void convert_and_send(unsigned char hex_data1) { bit sending_bit; unsigned char comparing_byte=0x01,i,storing_byte=0x00; SBUF=hex_data1; msec_wait(10); for(i=0;i<8;i++) { SCLK=0; storing_byte=(hex_data1&comparing_byte); if(storing_byte==0x00) { sending_bit=0; } else { sending_bit=1; } //send bit DATA_IO = sending_bit; //Clock to DATA_IO delay is 250ns (max) comparing_byte=comparing_byte<<1; SCLK=1; //this is kept here to save the time in giving dalay after DATA_IO sent, instead in that time calculations are done } serial_storage=0x00; usec_wait(2); } //************************************************************************************************* //FUNCTION THAT RECEIVES DATA FROM 1380 AND THEN CONVERTS IT TO CHARACTER AND PASSES TO ITS CALLER //************************************************************************************************* unsigned char receive_convert(void) { unsigned char comparing_byte=0x00,received_data=0x00,i=0; bit receiving_bit; YELLOW_LED=0; sec_wait(2); YELLOW_LED=1; sec_wait(1); for(i=0;i<8;i++) { DATA_IO=1; SCLK=1; SCLK=0; usec_wait(1); receiving_bit=DATA_IO; if(receiving_bit==0) { comparing_byte=0x00; } else { comparing_byte=0x80; //comparing byte is 10000000 as the bit is to be placesd in the MSB position } received_data = received_data|comparing_byte; received_data = received_data>>1; } return(received_data); } void main() { initialize_serial(); //INITIALIZES THE SERIAL PORT RED_LED=0; //BOTH LED's GLOW WHEN THE RTC IS BEING SET YELLOW_LED=0; RESET_=1; msec_wait(2); convert_and_send(0x8E); //SETS THE WP BIT AS ZERO msec_wait(1); convert_and_send(0x00); RESET_=0; msec_wait(2); RESET_=1; msec_wait(2); convert_and_send(0x80); //SETS THE CH BIT AS ZERO msec_wait(1); convert_and_send(0x00); usec_wait(1); RESET_=0; RED_LED=1; sec_wait(4); //waiting time given so that oscilator can start up to speed and gets ready to recieve time RED_LED=0; RESET_=1; msec_wait(2); convert_and_send(0x80); //10000000,00010000-sec:10 msec_wait(1); convert_and_send(0x10); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x82); //10000010,00010000-min:10 msec_wait(1); convert_and_send(0x10); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x84); //10000100,00010000-hour:10,24hr mode msec_wait(1); convert_and_send(0x10); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x86); //10000110,00010000-date:10 msec_wait(1); convert_and_send(0x10); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x88); //10001000,00010000-month:05 msec_wait(1); convert_and_send(0x05); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x8A); //10001010,00010000-day of week:friday ie 7 msec_wait(1); convert_and_send(0x07); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x8C); //10001100,00001000-year:08 msec_wait(1); convert_and_send(0x08); RESET_=0; usec_wait(1); RESET_=1; msec_wait(2); convert_and_send(0x8E); //SETS THE WP BIT AS ONE PREVENT DATA_IO FROM BEING WRITTEN ONTO THE REGISTERS msec_wait(1); convert_and_send(0x80); RESET_=0; RED_LED=1; //BOTH LED's TURN OFF WHEN THE RTC TIME HAS BEEN SET YELLOW_LED=1; while(1) //INFINITE LOOP { RED_LED=0; //This shows that only the while loop is running and system is waiting for command(REGISTER ADDRESS) msec_wait(500); if(serial_storage!=0x00) { SCLK = 0; RED_LED=1; //This shows that the serial_storage has the character RESET_=1; convert_and_send(serial_storage); //sending the address of the register that we want to read from msec_wait(10); display=receive_convert(); //putting the read DATA_IO into display RESET_=0; SBUF=display; //sending the read DATA_IO serially to the computer msec_wait(2); display=0x00; } else { RED_LED=1; msec_wait(500); } } } |