Subversion Repositories Code-Repo

Rev

Rev 237 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

Rev 237 Rev 276
Line 27... Line 27...
27
 
27
 
28
    // Enable the I2C module and set the clock stretch enable bit
28
    // Enable the I2C module and set the clock stretch enable bit
29
    // Note: Automatically overrides any other pin settings
29
    // Note: Automatically overrides any other pin settings
30
    I2C1CONSET = 0x00008040;
30
    I2C1CONSET = 0x00008040;
31
    I2C1ADD = address;
31
    I2C1ADD = address;
-
 
32
    if (speed == 0x01) {
-
 
33
        I2C1BRG = 0x0BE;    // Operate at 200kHZ (80MHz)
-
 
34
        I2C1CONbits.DISSLW = 0; // Slew rate control enabled
-
 
35
    } else if (speed == 0x02) {
32
    if (!speed) I2C1BRG = 0x05A;    // Operate at 400kHZ (80MHz)
36
        I2C1BRG = 0x05A;    // Operate at 400kHZ (80MHz)
-
 
37
        I2C1CONbits.DISSLW = 1; // Slew rate control disabled
-
 
38
    } else if (speed == 0x03) {
-
 
39
        I2C1BRG = 0x020; // Operate at 1MHz (80MHz)
-
 
40
        I2C1CONbits.DISSLW = 1; // Slew rate control disabled
-
 
41
    } else {
33
    else        I2C1BRG = 0x186;    // Operate at 100kHZ (80MHz)
42
        I2C1BRG = 0x186;    // Operate at 100kHZ (80MHz)
-
 
43
        I2C1CONbits.DISSLW = 0; // Slew rate control enabled
-
 
44
    }
34
    IFS0CLR = 0xE0000000;   // Clear any existing events
45
    IFS0CLR = 0xE0000000;   // Clear any existing events
35
    IPC6CLR = 0x00001F00;   // Reset priority levels
46
    IPC6CLR = 0x00001F00;   // Reset priority levels
36
    IPC6SET = 0x00001500;   // Set IPL=5, Subpriority 1
47
    IPC6SET = 0x00001600;   // Set IPL=6, Subpriority 2
37
    IEC0SET = 0xE0000000;   // Enable I2C1 interrupts
48
    IEC0SET = 0xE0000000;   // Enable I2C1 interrupts
38
 
49
 
39
    INTEnableInterrupts();
50
    INTEnableInterrupts();
40
}
51
}
41
 
52
 
Line 247... Line 258...
247
                break;
258
                break;
248
            case I2C1_CHECK_ACK_SEND:
259
            case I2C1_CHECK_ACK_SEND:
249
                // Check if ACK is received or not
260
                // Check if ACK is received or not
250
                if (!I2C1STATbits.ACKSTAT) {
261
                if (!I2C1STATbits.ACKSTAT) {
251
                    // If an ACK is received, send first byte of data
262
                    // If an ACK is received, send first byte of data
252
                    I2C1TRN = i2c_data_p->buffer_in[0];
263
                    uint8_t to_send = i2c_data_p->buffer_in[0];
-
 
264
                    I2C1TRN = to_send;
253
                    i2c_data_p->operating_state = I2C1_CHECK_ACK_RESTART;
265
                    i2c_data_p->operating_state = I2C1_CHECK_ACK_RESTART;
254
                } else {
266
                } else {
255
                    // If a NACK is received, stop transmission and send error
267
                    // If a NACK is received, stop transmission and send error
256
                    i2c_data_p->operating_state = I2C1_STOPPED;
268
                    i2c_data_p->operating_state = I2C1_STOPPED;
257
                    I2C1CONbits.PEN = 1;
269
                    I2C1CONbits.PEN = 1;