zakir_cool
Junior Member level 3
hi...i am trying a c code for rtc using pic...almost i have done,i have done all function in i2c....can any one tell that,after sending a data or address, we have to call ack funtion or else..it will call automatically..after sending..below is my code..can any body suggest me....
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 unsigned char read_ds1307(unsigned char address) { unsigned char data; I2CStart(); I2CSend(0xd0); delay_ms(10); I2CSend(address); delay_ms(4); I2CRestart(); delay_ms(10); I2CSend(0xd1); delay_ms(4); data=I2CRead(); delay_ms(10); I2CStop(); delay_ms(10); return(data); } void write_ds1307(unsigned char address,unsigned char w_data) { I2CStart(); // issue I2C start signal //address 0x68 followed by direction bit (0 for write, 1 for read) 0x68 followed by 0 --> 0xD0 I2CSend(0xD0); // send byte via I2C (device address + W) delay_ms(10); I2CSend(address); // send byte (address of DS1307 location) delay_ms(10); I2CSend(w_data); // send data (data to be written) delay_ms(10); I2CStop(); // issue I2C stop signal delay_ms(10); } void I2CInit() { TRISC3 = 1; /* SDA and SCL as input pin */ TRISC4 = 1; /* these pins can be configured either i/p or o/p */ SSPSTAT = 0x80; /* Slew rate disabled */ SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */ SSPADD = 0x28; /* 100Khz @ 4Mhz Fosc */ } /* Function: I2CStart Return: Arguments: Description: Send a start condition on I2C Bus */ void I2CStart() { SEN = 1; /* Start condition enabled */ while(SEN); /* automatically cleared by hardware */ /* wait for start condition to finish */ } /* Function: I2CStop Return: Arguments: Description: Send a stop condition on I2C Bus */ void I2CStop(){ PEN = 1; /* Stop condition enabled */ while(PEN); /* Wait for stop condition to finish */ /* PEN automatically cleared by hardware */ } /* Function: I2CRestart Return: Arguments: Description: Sends a repeated start condition on I2C Bus */ void I2CRestart(){ RSEN = 1; /* Repeated start enabled */ while(RSEN); /* wait for condition to finish */ } /* Function: I2CAck Return: Arguments: Description: Generates acknowledge for a transfer */ void I2CAck(){ ACKDT = 0; /* Acknowledge data bit, 0 = ACK */ ACKEN = 1; /* Ack data enabled */ while(ACKEN); /* wait for ack data to send on bus */ } /* Function: I2CNck Return: Arguments: Description: Generates Not-acknowledge for a transfer */ void I2CNak(){ ACKDT = 1; /* Acknowledge data bit, 1 = NAK */ ACKEN = 1; /* Ack data enabled */ while(ACKEN); /* wait for ack data to send on bus */ } /* Function: I2CWait Return: Arguments: Description: wait for transfer to finish */ void I2CWait(){ while ( ( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ) ); /* wait for any pending transfer */ } /* Function: I2CSend Return: Arguments: dat - 8-bit data to be sent on bus data can be either address/data byte Description: Send 8-bit data on I2C bus */ void I2CSend(unsigned char dat){ SSPBUF = dat; /* Move data to SSPBUF */ while(BF); /* wait till complete data is sent from buffer */ BF = 0; I2CWait(); /* wait for any pending transfer */ } /* Function: I2CRead Return: 8-bit data read from I2C bus Arguments: Description: read 8-bit data from I2C bus */ unsigned char I2CRead(void){ unsigned char temp; /* Reception works if transfer is initiated in read mode */ RCEN = 1; /* Enable data reception */ while(!BF); /* wait for buffer full */ BF = 1; temp = SSPBUF; /* Read serial buffer and store in temp register */ I2CWait(); /* wait to check any pending transfer */ return temp; /* Return the read data from bus */ }
Last edited by a moderator: