MachaniGoutham
Newbie level 5
- Joined
- Nov 10, 2011
- Messages
- 9
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 1,281
- Activity points
- 1,458
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 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 void main() { unsigned char sec,min,hour,day,month,year; /* Initilize the Uart before Transmiting/Reaceiving any data */ UART_Init(); /* Initilize the RTC(ds1307) before reading or writing time/date */ DS1307_Init(); /*Set the time and Date only once */ DS1307_SetTime(0x23,0x30,0x20); /* 05:30:20 am changed to 23 */ DS1307_SetDate(0x38,0x12,0x12); /* 18th DEC 2012 */ while(1){ /* Display the Time and Date continously on UART*/ /* Read the Time from RTC(ds1307) is done by call by reference*/ UART_TxChar(0x0A);UART_TxChar(0x0B);delay_ms(100); DS1307_GetTime(&hour,&min,&sec); /* Display the time */ UART_TxChar(0x0A);UART_TxChar(0x0D);delay_ms(100); /* conversion of HEX value hours,min,sec in to Decimal values */ DisplayRtcTime(hour,min,sec); /* Read the Date from RTC(ds1307) in Decimal */ DS1307_GetDate(&day,&month,&year); /* Display the Date */ UART_TxChar(0x0A);UART_TxChar(0x0D);delay_ms(100); DisplayRtcDate(day,month,year); } } /*-------------------------------------------------------------------------------- ---------------------------------------------------------------------------------*/ #include "ds1307.h" #include "i2c.h" #include "delay.h" /* Below values are fixed and should not be changed. Refer Ds1307 DataSheet for more info*/ #define DS1307_ID 0xD0 // DS1307 ID #define SEC_ADDRESS 0x00 // Address to access Ds1307 SEC register #define DATE_ADDRESS 0x04 // Address to access Ds1307 DATE register #define CONTROL 0x07 // Address to access Ds1307 CONTROL register /*--------------------------------------------------------------------------------- void DS1307_Init() ---------------------------------------------------------------------------------- * I/P Arguments: none. * Return value : none * description :This function is used to initialize the Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, write 0x00 into Control register of Ds1307 ------------------------------------------------------------------------------------*/ void DS1307_Init() { I2C_Start(); // Start I2C communication DS1307_Write(DS1307_ID); // Connect to DS1307 by sending its ID on I2c Bus DS1307_Write(CONTROL); // Select the Ds1307 ControlRegister to configure Ds1307 DS1307_Write(0x00); // Write 0x00 to Control register to disable SQW-Out I2C_Stop(); // Stop I2C communication after initilizing DS1307 } /*--------------------------------------------------------------------------------- void DS1307_Write(unsigned char dat) ---------------------------------------------------------------------------------- * I/P Arguments: char-> Data to be written into DS1307. * Return value : none * description :This function is used to initialize the Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, write 0x00 into Control register of Ds1307 ------------------------------------------------------------------------------------*/ void DS1307_Write(unsigned char dat) { I2C_Write(dat); // Connect to DS1307 by sending its ID on I2c Bus I2C_Clock(); } /*--------------------------------------------------------------------------------- unsigned char DS1307_Read() ---------------------------------------------------------------------------------- * I/P Arguments: char-> Data to be written into DS1307. * Return value : none * description :This function is used to initialize the Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, write 0x00 into Control register of Ds1307 ------------------------------------------------------------------------------------*/ unsigned char DS1307_Read() { unsigned char dat; dat = I2C_Read(); // Connect to DS1307 by sending its ID on I2c Bus return(dat); } /*---------------------------------------------------------------------------------- void DS1307_SetTime(unsigned char hh, unsigned char mm, unsigned char ss) ----------------------------------------------------------------------------------- * I/P Arguments: char,char,char-->hh,mm,ss to initilize the time into DS1307. * Return value : none * description :This function is used to set Time(hh,mm,ss) into the Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, select the RAM address 0x00 to point to sec Initilze Sec, MIN, Hour one after the other. Stop the I2c communication. -----------------------------------------------------------------------------------*/ void DS1307_SetTime(unsigned char hh, unsigned char mm, unsigned char ss) { I2C_Start(); // Start I2C communication DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus DS1307_Write(SEC_ADDRESS); // Select the SEC RAM address DS1307_Write(ss); // Write sec on RAM address 00H DS1307_Write(mm); // Write min on RAM address 01H DS1307_Write(hh); // Write hour on RAM address 02H I2C_Stop(); // Stop I2C communication after Setting the Time } /*--------------------------------------------------------------------------------- void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy) ---------------------------------------------------------------------------------- * I/P Arguments: char,char,char-->day,month,year to initilize the Date into DS1307. * Return value : none * description :This function is used to set Date(dd,mm,yy) into the Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, select the RAM address 0x04 to point to day Initilze Day,Month and Year one after the other. Stop the I2c communication. ----------------------------------------------------------------------------------*/ void DS1307_SetDate(unsigned char dd, unsigned char mm, unsigned char yy) { I2C_Start(); // Start I2C communication DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H DS1307_Write(dd); // Write date on RAM address 04H DS1307_Write(mm); // Write month on RAM address 05H DS1307_Write(yy); // Write year on RAM address 06h I2C_Stop(); // Stop I2C communication after Setting the Date } /*---------------------------------------------------------------------------------- void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr) ----------------------------------------------------------------------------------- * I/P Arguments: char *,char *,char *-->pointers to get the hh,mm,ss. * Return value : none * description :This function is used to get the Time(hh,mm,ss) from Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, select the RAM address 0x00 to point to sec Get Sec, MIN, Hour one after the other. Stop the I2c communication. -----------------------------------------------------------------------------------*/ void DS1307_GetTime(unsigned char *h_ptr,unsigned char *m_ptr,unsigned char *s_ptr) { I2C_Start(); // Start I2C communication DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus DS1307_Write(SEC_ADDRESS); // Request Sec RAM address at 00H I2C_Stop(); // Stop I2C communication after selecting Sec Register I2C_Start(); // Start I2C communication DS1307_Write(0xD1); // connect to DS1307( under Read mode) //by sending its ID on I2c Bus *s_ptr = DS1307_Read(); I2C_Ack(); // read second and return Positive ACK *m_ptr = DS1307_Read(); I2C_Ack(); // read minute and return Positive ACK *h_ptr = DS1307_Read(); I2C_NoAck(); // read hour and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Time } /*---------------------------------------------------------------------------------- void DS1307_GetDate(unsigned char *y_ptr,unsigned char *m_ptr,unsigned char *d_ptr) ----------------------------------------------------------------------------------- * I/P Arguments: char *,char *,char *-->pointers to get the y,m,d. * Return value : none * description :This function is used to get the Date(y,m,d) from Ds1307 RTC. Ds1307 ic is enabled by sending the DS1307 id on the I2C bus. After selecting DS1307, select the RAM address 0x00 to point to DAY Get Day, Month, Year one after the other. Stop the I2c communication. -----------------------------------------------------------------------------------*/ void DS1307_GetDate(unsigned char *d_ptr,unsigned char *m_ptr,unsigned char *y_ptr) { I2C_Start(); // Start I2C communication DS1307_Write(DS1307_ID); // connect to DS1307 by sending its ID on I2c Bus DS1307_Write(DATE_ADDRESS); // Request DAY RAM address at 04H I2C_Stop(); // Stop I2C communication after selecting DAY Register I2C_Start(); // Start I2C communication DS1307_Write(0xD1); // connect to DS1307( under Read mode) // by sending its ID on I2c Bus *d_ptr = DS1307_Read(); I2C_Ack(); // read Day and return Positive ACK *m_ptr = DS1307_Read(); I2C_Ack(); // read Month and return Positive ACK *y_ptr = DS1307_Read(); I2C_NoAck(); // read Year and return Negative/No ACK I2C_Stop(); // Stop I2C communication after reading the Time }
Last edited by a moderator: