Subversion Repositories Code-Repo

Rev

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

Rev 270 Rev 273
Line 1... Line 1...
1
#include "defines.h"
1
#include "defines.h"
2
#include "MCP23009.h"
2
#include "MCP23009.h"
3
#include "I2C2.h"
3
#include "I2C2.h"
4
 
4
 
-
 
5
static BTN_STATUS *status_ptr;
5
 
6
 
6
void MCP23009_Init(void) {
7
void MCP23009_Init(BTN_STATUS *status) {
-
 
8
    status_ptr = status;
-
 
9
    
7
    uint8_t buffer[8];
10
    uint8_t buffer[8];
8
 
11
 
9
    buffer[0] = 0x00;   // Starting register address
12
    buffer[0] = 0x00;   // Starting register address
10
    buffer[1] = 0xFF;   // Set all pins as inputs
13
    buffer[1] = 0xFF;   // IODIR, Set all pins as inputs
11
    buffer[2] = 0xFF;   // Reported values are inverted
14
    buffer[2] = 0xFF;   // IPOL, Reported values are inverted
12
    buffer[3] = 0x00;   // Disable interrupt-on-change
15
    buffer[3] = 0xFF;   // GPINTEN, Enable interrupt-on-change
13
    buffer[4] = 0x00;   // IOC default values
16
    buffer[4] = 0x00;   // DEFVAL, IOC default values
14
    buffer[5] = 0x00;   // IOC compare to previous pin value
17
    buffer[5] = 0x00;   // INTCON, IOC compare to previous value
15
    buffer[6] = 0x00;   // Config settings
18
    buffer[6] = 0x00;   // IOCON, Incrementing address, active-low interrupt cleared on GPIO read
16
    buffer[7] = 0xFF;   // Enable pull-ups on all pins
19
    buffer[7] = 0xFF;   // GPPU, Enable pull-ups on all pins
17
 
20
 
18
    I2C2_Master_Send(MCP23009_ADDR, 8, buffer);
21
    I2C2_Master_Send(MCP23009_ADDR, 8, buffer);
19
    uint8_t result;
22
    uint8_t result;
20
    do {
23
    do {
21
        result = I2C2_Get_Status();
24
        result = I2C2_Get_Status();
22
    } while (!result);
25
    } while (!result);
23
}
26
}
24
 
27
 
25
void MCP23009_Query(BTN_STATUS *status) {
28
void MCP23009_Query(void) {
26
    uint8_t buffer[2] = {MCP23009_GPIOA};
29
    uint8_t buffer[2];
27
 
30
 
28
    I2C2_Master_Send(MCP23009_ADDR, 1, buffer);
-
 
29
    uint8_t result;
-
 
30
    do {
-
 
31
        result = I2C2_Get_Status();
-
 
32
    } while (!result);
-
 
33
 
-
 
34
    I2C2_Master_Recv(MCP23009_ADDR, 1);
31
    I2C2_Master_Restart(MCP23009_ADDR, MCP23009_GPIOA, 1);
35
    uint8_t result;
32
    uint8_t result;
36
    do {
33
    do {
37
        result = I2C2_Get_Status();
34
        result = I2C2_Get_Status();
38
    } while (!result);
35
    } while (!result);
39
    I2C2_Read_Buffer(buffer);
36
    I2C2_Read_Buffer(buffer);
40
 
37
 
41
    status->w = buffer[0];
-
 
42
}
-
 
43
38
    status_ptr->w = buffer[0];
-
 
39
}
-
 
40