1 | -- procedure to write a register
|
2 | procedure mcp23008_register_write(byte in opcode,byte in reg, byte in data) is
|
3 | var bit r
|
4 |
|
5 | i2c_start()
|
6 | r = i2c_transmit_byte(opcode) -- i2c Device Opcode for write
|
7 | r = i2c_transmit_byte(reg) -- register
|
8 | r = i2c_transmit_byte(data) -- hex
|
9 | i2c_stop()
|
10 |
|
11 | end procedure
|
12 |
|
13 | -- procedure to read a register
|
14 | procedure mcp23008_register_read(byte in opcode,byte in reg, byte out data) is
|
15 | var bit r
|
16 | i2c_start()
|
17 | r = i2c_transmit_byte(opcode) -- i2c Device Opcode for write
|
18 | r = i2c_transmit_byte(reg) -- device register address
|
19 | i2c_restart()
|
20 | r = i2c_transmit_byte(opcode + 1) -- i2c device opcode for read
|
21 | data = i2c_receive_byte(false)
|
22 | i2c_stop()
|
23 |
|
24 | end procedure
|
25 |
|
26 | -- setup mcp23008 as required
|
27 | procedure mcp23008_initialize(byte in opcode) is
|
28 |
|
29 | mcp23008_register_write(opcode,0x0A,0b0000_0000) -- OLAT O/P (0000_0000 no latching on all pins)
|
30 | mcp23008_register_write(opcode,0x06,0b1110_0000) -- GPPU High bytes pull up low bytes no pull up
|
31 | mcp23008_register_write(opcode,0x00,0b1110_0000) -- IODIR High bytes i/p low bytes o/p
|
32 | mcp23008_register_write(opcode,0x05,0x34) -- IOCON b5 seq read off b4 slew rate dis-able
|
33 | -- b2 int pin open
|
34 | end procedure
|
35 |
|
36 | -- setup mcp23008 as required all ouputs
|
37 | procedure mcp23008_lcd2_init(byte in opcode) is
|
38 |
|
39 | mcp23008_register_write(opcode,0x0A,0b0000_0000) -- OLAT O/P (0000_0000 no latching on all pins)
|
40 | mcp23008_register_write(opcode,0x06,0b0000_0000) -- GPPU High bytes pull up low bytes no pull up
|
41 | mcp23008_register_write(opcode,0x00,0b0000_0000) -- IODIR High bytes i/p low bytes o/p
|
42 | mcp23008_register_write(opcode,0x05,0x34) -- IOCON b5 seq read off b4 slew rate dis-able
|
43 | -- b2 int pin open
|
44 | end procedure
|