ramxis
Newbie level 5
hi. below is my code for interfacing ds1307 with pic16f877 i am using hi-tech with mplab .i am having an issue that i always get 00:00:00 on time and 00/00/2000 on date no matter what value i
initialize the time and date to. this code is part of my fyp and i have been strugling with it for quite a while now please read through and help me figure out what i am doing wrong . my suspicion is the i2c protocol i have used is not working.
COde......
initialize the time and date to. this code is part of my fyp and i have been strugling with it for quite a while now please read through and help me figure out what i am doing wrong . my suspicion is the i2c protocol i have used is not working.
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 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 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 #include<pic.h> #define SCL TRISC3//RC3 #define SDA TRISC4 //LCD functions void delay(void); void command(unsigned char com); void display(unsigned char ascii);//for sending display information to lcd void dip(void); //I2c PRotocol for pic void initialize(void);//put initial info on lcd void I2CInit(void);//initialize I2c protocol void Stop(); void Start(); void Restart(); void Ack(); void Nak(); void Wait(); void Send(unsigned char dat); unsigned char Read(void); / unsigned char BCD2UpperCh(unsigned char bcd); unsigned char BCD2LowerCh(unsigned char bcd); //unsigned char init_info[12]={'D','a','t','e',':','8','/','2','7','/','1','1'};//,'0',':','3','5',' ','8','9','5','L'}; //unsigned char second_info[10]={'T','i','m','e',':','0','9',':','5','5'}; unsigned char intensity_info[13]={'i','n','t','e','n','s','i','t','y',':','8','5','9'}; unsigned char pr_info[12]={'P','r','e','v','i','o','u','s',':','8','2','0'}; unsigned short table[8]; unsigned short I2CData[8] = {0x00, 0x10, 0x01, 0x02, 0x05, 0x01, 0x09, 0x10};//14:27:00 17/6/2008 //unsigned short I2CData[8];// = {'0', '0', '0', '1', '1', '1', '9', '0'}; main() { unsigned char test,i; unsigned char j; //************************************************************************* //************************************************************************* // write //************************************************************************* //************************************************************************* // Initialize I2C Port I2CInit(); //Send Start condition Start(); //Send DS1307 slave address with write operation Send(0xD0); // Send subaddress 0x00, we are writing to this location Send(0x00); //Loop to write 8 bytes //unsigned short address=0x00; for(j=0;j<8;j++) { // Send subaddress 0x00, we are writing to this location //Send(address); // send I2C data one by one Send(I2CData[j]); //address++; } //Send a stop condition - as transfer finishes Stop(); dip(); //****************************************************************************** //**************************************read from ds1307************************ //****************************************************************************** //****************************************************************************** //Reading for a memory based device always starts with a dummy write // Send a start condition Start(); // Send slave address with write Send(0xD0); // Send address for dummy write operation //this address is actually where we are going to read from Send(0x00); //Send a repeated start, after a dummy write to start reading Restart(); // send slave address with read bit set Send(0xD1); // Loop to read 8 bytes from I2C slave //int readaddress=0; for(i=8;i>0;i--) { /* read a byte */ //I2CData[i] = Read(); // Send slave address with write /* Send(0xD0); Send(0x00); Restart(); Send(0xD1);*/ table[i]=Read(); //readaddress++; if(i-1) Ack(); // ACK if its not the last byte to read else Nak();// if its the last byte then send a NAK } /* Send stop */ Stop(); //********************************************************************** //******************display calls*************************************** //********************************************************************** //********************************************************************** initialize(); for(i=0;i<2;i++) { for(test=0;test<200;test++) { delay(); } } //dip(); while(1) { } } /************************************************************************** *************************************************************************** MAIN ends here *************************************************************************** **************************************************************************/ //delay functions void delay(void)//of roughly 5msec { int i=0; for(i=0;i<=255;i++) { } } /************************************************************************** *************************************************************************** LCD Functions *************************************************************************** **************************************************************************/ void command(unsigned char com) { delay(); PORTD=com; RC0=0; RC1=0; RC2=1; RC2=0; } void display(unsigned char ascii) { delay(); PORTD=ascii; RC0=1; RC1=0; RC2=1; delay(); RC2=0; } void initialize(void) { unsigned char i,j; TRISC0=0; TRISC1=0; TRISC2=0; TRISD=0; // configure portd as output command(0x38); command(0x0f); command(0x01); command(0x80); command(0x06); //unsigned char i; /*for(i=0;i<12;i++) { display(init_info[i]); }*/ /* for(i=0;i<5;i++) { display(second_info[i]); }*/ //Display time char time[9]; time[0] = BCD2UpperCh(table[2]);//hours time[1] = BCD2LowerCh(table[2]);//hours time[2] = ':'; time[3] = BCD2UpperCh(table[1]); time[4] = BCD2LowerCh(table[1]);//minutes time[5] = ':'; time[6] = BCD2UpperCh(table[0]); time[7] = BCD2LowerCh(table[0]);//seconds time[8] = '\0'; for(i=0;i<9;i++) { //display(second_info[i]); display(time[i]); } //display dow display(BCD2UpperCh(table[3])); display(BCD2LowerCh(table[3])); //Display date command(0xc0); command(0x06); for(j=0;j<11;j++) { char date[11]; date[0] = BCD2UpperCh(table[4]); date[1] = BCD2LowerCh(table[4]);//date date[2] ='/'; date[3] = BCD2UpperCh(table[5]); date[4] = BCD2LowerCh(table[5]);//month date[5] ='/'; date[6] = '2'; date[7] = '0'; date[8] = BCD2UpperCh(table[6]); date[9] = BCD2LowerCh(table[6]);//year date[10] = '\0'; display(date[j]); } } void dip(void) { unsigned char i; TRISC0=0; TRISC1=0; TRISC2=0; TRISD=0; command(0x38); command(0x0F); command(0x01); command(0x80); command(0x06); //unsigned char i; for(i=0;i<13;i++) { display(intensity_info[i]); } //remove later command(0xc0); command(0x06); for(i=0;i<12;i++) { display(pr_info[i]); } } /************************************************************************** *************************************************************************** I2C Functions *************************************************************************** **************************************************************************/ //------------------------------- // Intialize I2C //------------------------------- void I2CInit(void) { SCL = 1; /* SDA and SCL as input pin */ SDA = 1; /* these pins can be configured either i/p or o/p */ SSPSTAT |= 0x80; /* Slew rate disabled | (is bit wise or)*/ SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, */ SSPADD = 0x09;//=0x28 /* 100Khz @ 4Mhz Fosc ,clock = FOSC/(4 * (SSPADD + 1))*/ } //------------------------------------ //Send a start condition on I2C Bus //------------------------------------ void Start() { SEN = 1; // Start condition enabled while(SEN); // automatically cleared by hardware // wait for start condition to finish } //----------------------------------- // Send a stop condition on I2C Bus //------------------------------------ void Stop() { PEN = 1; /* Stop condition enabled */ while(PEN); /* PEN automatically cleared by hardware */ } //tells nopt to release the bus void Restart() { RSEN = 1; /* Repeated start enabled */ while(RSEN); } /* ------------------------------------------------- Generates ACK ------------------------------------------------- */ void Ack() { ACKDT = 0; /* Acknowledge data bit, 0 = ACK */ ACKEN = 1; /* Ack data enabled */ while(ACKEN); /* wait for ack data to send on bus */ } /* ----------------------------------------------------- Generates a Nack ----------------------------------------------------- */ void Nak() { ACKDT = 1; // Acknowledge data bit, 1 = NAK ACKEN = 1; // Ack data enabled while(ACKEN); // wait for ack data to send on bus } /* ----------------------------------------------------- wait ----------------------------------------------------- */ void Wait() { while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); // wait for any pending transfer } /* ----------------------------------------------------- Send ----------------------------------------------------- */ void Send(unsigned char dat) { SSPBUF = dat; // Move data to SSPBUF TRISC6=ACKSTAT; //chechk if ack recieved from slave while(BF); // wait till complete data is sent from buffer //dip(); Wait(); // wait for any pending transfer } /* ----------------------------------------------------- Read ----------------------------------------------------- */ unsigned char Read(void) { unsigned char temp; // Reception works if transfer is initiated in read mode RCEN = 1; // Enable data reception while(!BF); // wait for buffer full temp = SSPBUF; // Read serial buffer and store in temp register Wait(); // wait to check any pending transfer return temp; // Return the read data from bus } //-------------------------------------------------------- // Chararcter conversion functions for displaying on lcd //-------------------------------------------------------- unsigned char BCD2UpperCh(unsigned char bcd) { return ((bcd >> 4) + '0'); } unsigned char BCD2LowerCh(unsigned char bcd) { return ((bcd & 0x0F) + '0'); }
Last edited by a moderator: