Teja.p
Member level 2
sir,
i trying to interface the DS1307 with PIC16F877A using I2C.
RTC running 10sec slow per MINUTE .. please help mee..
i have WRITE the RTC with some defaults ...please find attached code.
i trying to interface the DS1307 with PIC16F877A using I2C.
RTC running 10sec slow per MINUTE .. please help mee..
i have WRITE the RTC with some defaults ...please find attached code.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 #define _XTAL_FREQ 20000000 //We are running on 20MHz crystal /*Set the current value of date and time below*/ int sec = 00; int min = 04; int hour = 05; int date = 00; int month = 00; int year = 00; /*Set the current value of date and time below*/ unsigned int sec1 = 0; unsigned int min1 = 0; unsigned int hour1 = 0; unsigned int date1 = 0; unsigned int month1 = 0; unsigned int year1 = 0; /*Time and Date Set*/ void I2C_Initialize(const unsigned long feq_K) //Begin IIC as master { TRISC3_bit = 1; TRISC4_bit = 1; //Set SDA and SCL pins as input pins SSPCON = 0b00101000; SSPCON2 = 0b00000000; SSPADD = (_XTAL_FREQ/(4*feq_K*100))-1; //Setting Clock Speed pg99/234 SSPSTAT = 0b10000000; } //I2C_Hold() function checks the status of the bus. It checks either I2C bus is busy or free. is It completed the last operation successfully? void I2C_Hold() { while((SSPCON2 & 0b00011111)||(SSPSTAT & 0b00000100)) ; //check the bis on registers to make sure the IIC is not in progress } void I2C_start() { I2C_Hold(); //Hold the program is I2C is busy SEN_bit = 1; //Begin IIC pg85/234 } //input parameter to this function can be a byte ( either address or data) unsigned char I2C_Write(unsigned char Data) { I2C_Hold(); // wait untill I2C_Bus of PIC18F4550 microcontroller becomes free SSPBUF = Data; // store data inside SSPBUF register of PIC18F4550 microcontroller return ACKSTAT; //return status of data or address transmission } unsigned char I2C_Read_byte(unsigned short ack) { unsigned short incoming; //I2C_Hold(); RCEN_bit = 1; I2C_Hold(); incoming = SSPBUF; //get the data saved in SSPBUF I2C_Hold(); ACKDT_bit = (ack)?0:1; //check if ack bit received ACKEN_bit = 1; //pg 85/234 return incoming ; } void write_ds1307() { I2C_start(); I2C_Write(0xD0); I2C_Write(0); I2C_Write(DEC_2_BCD (sec)); //update sec I2C_Write(DEC_2_BCD (min)); //update min I2C_Write(DEC_2_BCD (hour)); //update hour I2C_Write(1); //ignore updating day I2C_Write(date); //update date I2C_Write(month); //update month I2C_Write(year); //update year I2C_Write(0x13); //update year I2C_Stop(); } void Update_Current_Date_Time() { //START to Read I2C_start(); I2C_Write(0xD0); I2C_Write(0); I2C_Stop(); //READ I2C_start(); I2C_Write(0xD1); // Initialize data read sec1 = BCD_2_DEC(I2C_Read_Byte(1)); min1 = BCD_2_DEC(I2C_Read_Byte(1)); // Read sec from register hour1 = BCD_2_DEC(I2C_Read_Byte(1)); I2C_Read_Byte(1); date1 = BCD_2_DEC(I2C_Read_Byte(1)); month1 = BCD_2_DEC(I2C_Read_Byte(1)); year1 = BCD_2_DEC(I2C_Read_Byte(1)); I2C_Stop(); //END Reading I2C_start(); I2C_Write(0xD1); // Initialize data read I2C_Read_Byte(1); I2C_Stop(); } int BCD_2_DEC(int convert) { char r=0; r=((convert >> 4) * 10 + (convert & 0x0F)); return r; } int DEC_2_BCD (int to_convert) { return ((to_convert / 10) << 4) + (to_convert % 10); } void main() { TRISB=0X00; TRISD=0X00; PORTB=0X00; PORTD=0X00; PORTC=0X00; TRISC0_bit = 0; TRISC1_bit =0; //Set rs and en pins as o/p pins for lcd I2C_Initialize(100); I2C_start(); write_ds1307(); while(1) { Update_Current_Date_Time(); // Bcd2Dec lcdcmd(0xc0); //Lcd_dec1(hour1); //lcdcmd(0xc5); // Lcd_dec1((min1)); // lcdcmd(0x0c9); Lcd_dec1((sec1)); } }