36 #if (NUM_I2C_MODS >= 1) 54 u16_temp = (
FCY/1000L)/((uint16_t) u16_FkHZ);
55 #if defined(__PIC24E__) || defined(__dsPIC33E__) 57 u16_temp = (u16_temp - 1);
59 u16_temp = u16_temp -
FCY/10000000L - 1;
62 if (u16_temp > 511) u16_temp = 511;
64 I2C1CONbits.I2CEN = 1;
74 u8_wdtState = _SWDTEN;
78 while (I2C1CONbits.SEN);
79 _SWDTEN = u8_wdtState;
90 u8_wdtState = _SWDTEN;
94 while (I2C1CONbits.RSEN);
95 _SWDTEN = u8_wdtState;
106 u8_wdtState = _SWDTEN;
110 while (I2C1CONbits.PEN);
111 _SWDTEN = u8_wdtState;
123 u8_wdtState = _SWDTEN;
126 while (I2C1STATbits.TRSTAT);
127 _SWDTEN = u8_wdtState;
129 if (I2C1STATbits.ACKSTAT != I2C_ACK) {
145 u8_wdtState = _SWDTEN;
148 while (I2C1STATbits.TRSTAT);
149 _SWDTEN = u8_wdtState;
151 return(I2C1STATbits.ACKSTAT);
164 u8_wdtState = _SWDTEN;
166 while (I2C1CON & 0x1F);
167 I2C1CONbits.RCEN = 1;
168 while (!I2C1STATbits.RBF);
172 while (I2C1CON & 0x1F);
173 I2C1CONbits.ACKDT = u8_ack2Send;
174 I2C1CONbits.ACKEN = 1;
175 while (I2C1CONbits.ACKEN);
176 _SWDTEN = u8_wdtState;
215 for (u16_i=0; u16_i < u16_cnt; u16_i++) {
258 for (u16_i=0; u16_i < u16_cnt; u16_i++) {
259 if (u16_i != u16_cnt-1) *pu8_data =
getI2C1(I2C_ACK);
260 else *pu8_data =
getI2C1(I2C_NAK);
266 #endif // #if (NUM_I2C_MODS >= 1) 314 #if (NUM_I2C_MODS >= 2) 329 void configI2C2(uint16_t u16_FkHZ) {
332 u16_temp = (
FCY/1000L)/((uint16_t) u16_FkHZ);
333 #if defined(__PIC24E__) || defined(__dsPIC33E__) 335 u16_temp = (u16_temp - 1);
337 u16_temp = u16_temp -
FCY/10000000L - 1;
340 if (u16_temp > 511) u16_temp = 511;
342 I2C2CONbits.I2CEN = 1;
348 void startI2C2(
void) {
352 u8_wdtState = _SWDTEN;
356 while (I2C2CONbits.SEN);
357 _SWDTEN = u8_wdtState;
364 void rstartI2C2(
void) {
368 u8_wdtState = _SWDTEN;
370 I2C2CONbits.RSEN = 1;
372 while (I2C2CONbits.RSEN);
373 _SWDTEN = u8_wdtState;
380 void stopI2C2(
void) {
384 u8_wdtState = _SWDTEN;
388 while (I2C2CONbits.PEN);
389 _SWDTEN = u8_wdtState;
401 u8_wdtState = _SWDTEN;
404 while (I2C2STATbits.TRSTAT);
405 _SWDTEN = u8_wdtState;
407 if (I2C2STATbits.ACKSTAT != I2C_ACK) {
423 u8_wdtState = _SWDTEN;
426 while (I2C2STATbits.TRSTAT);
427 _SWDTEN = u8_wdtState;
429 return(I2C2STATbits.ACKSTAT);
442 u8_wdtState = _SWDTEN;
444 while (I2C2CON & 0x1F);
445 I2C2CONbits.RCEN = 1;
446 while (!I2C2STATbits.RBF);
450 while (I2C2CON & 0x1F);
451 I2C2CONbits.ACKDT = u8_ack2Send;
452 I2C2CONbits.ACKEN = 1;
453 while (I2C2CONbits.ACKEN);
454 _SWDTEN = u8_wdtState;
465 putI2C2(I2C_WADDR(u8_addr));
477 putI2C2(I2C_WADDR(u8_addr));
489 void writeNI2C2(
uint8_t u8_addr,
uint8_t* pu8_data, uint16_t u16_cnt) {
492 putI2C2(I2C_WADDR(u8_addr));
493 for (u16_i=0; u16_i < u16_cnt; u16_i++) {
507 putI2C2(I2C_RADDR(u8_addr));
508 *pu8_d1 = getI2C2(I2C_NAK);
520 putI2C2(I2C_RADDR(u8_addr));
521 *pu8_d1 = getI2C2(I2C_ACK);
522 *pu8_d2 = getI2C2(I2C_NAK);
532 void readNI2C2(
uint8_t u8_addr,
uint8_t* pu8_data, uint16_t u16_cnt) {
535 putI2C2(I2C_RADDR(u8_addr));
536 for (u16_i=0; u16_i < u16_cnt; u16_i++) {
537 if (u16_i != u16_cnt-1) *pu8_data = getI2C2(I2C_ACK);
538 else *pu8_data = getI2C2(I2C_NAK);
544 #endif // #if (NUM_I2C_MODS >= 2)
void read2I2C1(uint8_t u8_addr, uint8_t *pu8_d1, uint8_t *pu8_d2)
void read1I2C1(uint8_t u8_addr, uint8_t *pu8_d1)
#define CLRWDT()
Clear the watch-dog timer.
void putI2C1(uint8_t u8_val)
uint8_t getI2C1(uint8_t u8_ack2Send)
Configures the system clock.
_PERSISTENT const char * sz_lastTimeoutError
void configI2C1(uint16_t u16_FkHZ)
void write1I2C1(uint8_t u8_addr, uint8_t u8_d1)
void write2I2C1(uint8_t u8_addr, uint8_t u8_d1, uint8_t u8_d2)
unsigned char uint8_t
An abbreviation for an 8-bit unsigned integer.
void reportError(const char *sz_errorMessage)
uint8_t putNoAckCheckI2C1(uint8_t u8_val)
void writeNI2C1(uint8_t u8_addr, uint8_t *pu8_data, uint16_t u16_cnt)
void readNI2C1(uint8_t u8_addr, uint8_t *pu8_data, uint16_t u16_cnt)