1 | -- I2C hardware implementation pic18f
|
2 | -- at 20 Mhz crystal and 100kHz bus speed master mode
|
3 |
|
4 | procedure _i2c_init is
|
5 |
|
6 | SSPADD = 0x31 -- 0b_0011_0001 -- clock = 20/(4*(12+1)) = 385kHz -- 49 for 100kHz
|
7 | SSPCON1 = 0b_0000_1000 -- master mode, clock=Fosc/(4*(SSPADD+1))
|
8 | SSPCON1_SSPEN = high -- MSSP-Modul enable
|
9 | SSPCON2 = 0b_0010_0000 -- sets default acknowledge bit value
|
10 | -- SSPSTAT = 0b_0000_0000 -- enable slew rate control
|
11 | end procedure
|
12 |
|
13 |
|
14 | -- output a start condition in Master-Mode
|
15 | procedure i2c_start is
|
16 |
|
17 | -- PIR1_SSPIF = low -- SSPIF Bit löschen
|
18 | SSPCON2_SEN = high -- Bus Übernahme anweisen
|
19 | while SSPCON2_SEN == high loop
|
20 | end loop
|
21 | end procedure
|
22 |
|
23 | -- i2c_restart - Sends Restart bit and waits untill finished
|
24 | procedure i2c_restart is
|
25 | SSPCON2_RSEN = high
|
26 | while SSPCON2_RSEN == high loop
|
27 | end loop
|
28 | end procedure
|
29 |
|
30 |
|
31 | -- output stop condition
|
32 | procedure i2c_stop is
|
33 |
|
34 | SSPCON2_PEN = high -- initiate I2C bus stop condition
|
35 | while SSPCON2_PEN == high loop
|
36 | end loop
|
37 |
|
38 | end procedure
|
39 |
|
40 | -- init the I2C
|
41 | _i2c_init
|
42 | i2c_stop
|
43 |
|
44 |
|
45 | -- output an ack condition
|
46 | procedure i2c_put_ack is
|
47 | SSPCON2_ACKDT = low
|
48 | SSPCON2_ACKEN = high -- ACK senden
|
49 | while SSPCON2_ACKEN == high loop
|
50 | end loop
|
51 | SSPCON2_RCEN = high -- generate receive condition
|
52 | end procedure
|
53 |
|
54 | -- output a nack condition
|
55 | procedure i2c_put_nack is
|
56 | SSPCON2_ACKDT = high
|
57 | SSPCON2_ACKEN = high
|
58 | end procedure
|
59 |
|
60 | -- output a nack condition and stop
|
61 | procedure i2c_put_nack_stop is
|
62 | i2c_put_nack
|
63 | i2c_stop
|
64 | end procedure
|
65 |
|
66 | -- output an ack condition and stop
|
67 | procedure i2c_put_ack_stop is
|
68 | i2c_put_ack
|
69 | i2c_stop
|
70 | end procedure
|
71 |
|
72 |
|
73 | -- i2c_transmit_byte
|
74 | function i2c_send_byte(byte in data) return bit is
|
75 |
|
76 | PIR1_SSPIF = low -- clear pending flag
|
77 | SSPBUF = data -- write data
|
78 |
|
79 | -- wait untill write is finished
|
80 | while PIR1_SSPIF == low loop
|
81 | end loop
|
82 |
|
83 | -- get Acknowledge Status Bit _ACKSTAT
|
84 | -- wich indicates that the slave has responded (or not)
|
85 | -- if i2c device send an Aknowledge, then ready and OK
|
86 | if SSPCON2_ACKSTAT == low then
|
87 | return true -- okay
|
88 | else
|
89 | SSPCON1_SSPEN = low
|
90 | SSPCON1_SSPEN = high
|
91 |
|
92 | return false -- no response
|
93 | end if
|
94 | end function
|
95 |
|
96 |
|
97 | -- i2c_receive_byte
|
98 | --
|
99 | -- The ACK flag indicated if the byte read must be acked.
|
100 | -- In general, all bytes read by the master are acked, except the last one.
|
101 | -- Failing to NACK the last byte read will give unpredictable results.
|
102 | --
|
103 | -- start receiving of a byte and waits till finished
|
104 | -- if param ACK is true, the byte is acknowledged and next bytes can be received.
|
105 | -- if param ACK is false, the byte is nacked and a stop *should be* sent.
|
106 | -- note: this behavior is inverted from the line level and orignal library, but
|
107 | -- consistent with the i2c_software library.
|
108 | --
|
109 | function i2c_get_byte(bit in ACK ) return byte is
|
110 | var byte data
|
111 | -- start receive cyclus and wait till full byte received
|
112 | SSPCON2_RCEN = high
|
113 | -- delay_100us(1)
|
114 | while SSPSTAT_BF == low loop
|
115 | end loop
|
116 |
|
117 | -- send Acknowledge (=low) if STOP=true=high
|
118 | -- and wait till finishd
|
119 | -- SSPCON2_ACKDT = ACK
|
120 | SSPCON2_ACKDT = ! ACK
|
121 | SSPCON2_ACKEN = high
|
122 | while SSPCON2_ACKEN == high loop
|
123 | end loop
|
124 |
|
125 | -- get read data
|
126 | data = SSPBUF
|
127 |
|
128 | return data
|
129 | end function
|
130 |
|
131 | -- -----------------------------------------------------------------------------
|
132 | -- start receiving of a byte and waits till finished
|
133 | -- if stop is set, an ACK and STOP wil be generated
|
134 | -- -----------------------------------------------------------------------------
|
135 | procedure I2C_get_raw_byte (byte out data) is
|
136 | -- procedure _I2C_receive_byte (byte out data) is
|
137 | -- start receive cyclus and wait till full byte received
|
138 | SSPCON2_RCEN = high
|
139 | while SSPSTAT_BF == low loop
|
140 | end loop
|
141 |
|
142 | -- send Acknowledge (=low) if STOP=true=high
|
143 | -- and wait till finishd
|
144 | SSPCON2_ACKDT = low
|
145 | SSPCON2_ACKEN = high
|
146 | while SSPCON2_ACKEN == high loop
|
147 | end loop
|
148 | -- get read data
|
149 | data = SSPBUF
|
150 | end procedure
|