BASIC compiler/interpreter for PIC32MX/MZ-80K (suspended)
リビジョン | 197 (tree) |
---|---|
日時 | 2017-03-05 08:33:20 |
作者 | kmorimatsu |
I2C Zoea ver 0.4 trunk
@@ -0,0 +1,270 @@ | ||
1 | +REM I2C test program for MachiKania | |
2 | +REM Zoea and Microchip MCP23017 | |
3 | +REM This is a LED-blinking example. | |
4 | +REM GPA0 (pin #21) is connected to LED | |
5 | +REM through 330 ohm resistor. | |
6 | +REM Pull-up rsistors (2.2 kohm) are | |
7 | +REM connected to I2C lines. | |
8 | +REM PIC32MX170F256B pins #16 and #21 | |
9 | +REM (RB7 and RB10; down and up buttons) | |
10 | +REM are connected to MCP23017 pins #12 | |
11 | +REM and #13, respectively. | |
12 | + | |
13 | +print "Initialize" | |
14 | + | |
15 | +REM Initialize | |
16 | +gosub I2CZIN | |
17 | + | |
18 | +REM Initialize MCP23017 and set GPA0 | |
19 | +REM for output | |
20 | +REM Send address: 0x00 for IODIRA | |
21 | +REM Send data for IODIRA: 0xfe for | |
22 | +REM output at GPA0 | |
23 | +gosub I2CZ,0x40,0x00,0xFE,-1 | |
24 | + | |
25 | +REM Set GPPUB to 0xff | |
26 | +REM Send address: 0x0D for GPPUB | |
27 | +REM Send data for GPPUB for pull up all | |
28 | +gosub I2CZ,0x40,0x0D,0xFF,-1 | |
29 | + | |
30 | +print "Start" | |
31 | + | |
32 | +for i=1 to 10 | |
33 | + | |
34 | + REM Set GPA0 to H | |
35 | + REM Send address: 0x14 for OLATA | |
36 | + REM Send data for OLATA: 0x01 for H | |
37 | + REM at GPA0 | |
38 | + gosub I2CZ,0x40,0x14,0x01,-1 | |
39 | + | |
40 | + wait 30 | |
41 | + | |
42 | + REM Set GPA0 to L | |
43 | + REM Send address: 0x14 for OLATA | |
44 | + REM Send data for OLATA: 0x00 for H | |
45 | + REM at GPA0 | |
46 | + gosub I2CZ,0x40,0x14,0x00,-1 | |
47 | + | |
48 | + wait 30 | |
49 | + | |
50 | + REM Read from GPB | |
51 | + REM Send address: 0x13 for GPIOB | |
52 | + REM Restart and send control byte for | |
53 | + REM read | |
54 | + print hex$(gosub(I2CZ,0x40,0x13,-3,0x41,-2)) | |
55 | + | |
56 | + wait 30 | |
57 | + | |
58 | +next | |
59 | + | |
60 | +REM All done. Reset peripherals. | |
61 | +gosub I2CZEN | |
62 | + | |
63 | +end | |
64 | + | |
65 | +REM I2C routine wrapper for MachiKania | |
66 | +REM Zoea ver 0.4 | |
67 | +REM Written by Katsumi | |
68 | +REM This code is in public domain. | |
69 | +REM | |
70 | +REM This code requres I2C routine for | |
71 | +REM Nauplia. | |
72 | +REM | |
73 | +REM Subroutines: | |
74 | +REM I2CZIN: Initialize I2C peripheral | |
75 | +REM I2CZEN: End using I2C | |
76 | +REM I2CZ : Send data to I2C | |
77 | +REM Parameters must end either | |
78 | +REM -1 or -2 | |
79 | +REM -1: STOP communication | |
80 | +REM -2: Read 1 bytes and | |
81 | +REM STOP communication | |
82 | +REM with NAK | |
83 | +REM -3: Do repeated START | |
84 | +REM These subroutines do not destroy | |
85 | +REM variables. | |
86 | + | |
87 | +end | |
88 | + | |
89 | +REM Initialize I2C peripheral | |
90 | +label I2CZIN | |
91 | +var B,D,E | |
92 | +gosub I2CINI | |
93 | +return | |
94 | + | |
95 | +REM End using I2C | |
96 | +label I2CZEN | |
97 | +var B,D,E | |
98 | +gosub I2CEND | |
99 | +return | |
100 | + | |
101 | +REM Send to I2C peripheral | |
102 | +REM Parameters for calling this | |
103 | +REM subroutine must end with -1 or -2 | |
104 | +REM For reading byte data from | |
105 | +REM peripheral, use -2. | |
106 | +REM For repeated START, use -3 | |
107 | +label I2CZ | |
108 | +var B,D,E,I | |
109 | +gosub I2CS | |
110 | +I=1 | |
111 | +while 1 | |
112 | + D=args(I) | |
113 | + I=I+1 | |
114 | + if D=-1 then | |
115 | + break | |
116 | + elseif D=-2 then | |
117 | + E=gosub(I2C8RN) | |
118 | + break | |
119 | + elseif D=-3 then | |
120 | + gosub I2CS | |
121 | + elseif 0<=D and D<=0xff then | |
122 | + if gosub(I2C8BD)<0 then | |
123 | + print "No ACK" | |
124 | + gosub I2CEND | |
125 | + end | |
126 | + endif | |
127 | + else | |
128 | + print "Wrong I2C parameter: ";D | |
129 | + gosub I2CEND | |
130 | + end | |
131 | + endif | |
132 | +wend | |
133 | +gosub I2CP | |
134 | +return E | |
135 | + | |
136 | +REM I2C routine for MachiKania Nauplia ver 0.4 | |
137 | +REM Written by Katsumi | |
138 | +REM This code is in public domain. | |
139 | +REM | |
140 | +REM Use RB7 (RB: 0x0080) for CLK line | |
141 | +REM Use RB10 (RB: 0x0400) for DAT line | |
142 | +REM Maximum (peak) speed for I2C communication is 100 kHz. | |
143 | +REM Note that actual speed is lower than this, | |
144 | +REM because NTSC video signal construction is needed by CPU. | |
145 | +REM | |
146 | +REM Subroutines: | |
147 | +REM I2CINI: Initialize I2C peripheral | |
148 | +REM I2CEND: End using I2C | |
149 | +REM I2CS : Start signal | |
150 | +REM I2CP : Stop signal | |
151 | +REM I2C8BD: Send 8 bit data in D and reveive in E (-1 if failed) | |
152 | +REM I2C8RA: Read 8 bit in E and send ACK | |
153 | +REM I2C8RN: Read 8 bit in E and send NAK | |
154 | +REM All routines destroy B. I2C8BD, I2CRA, and I2C8RN also destroys E. | |
155 | +REM | |
156 | +REM PIX32MX peripheral: | |
157 | +REM TRISB =0xBF886110 | |
158 | +REM TRISBCLR=0xBF886114 (TRISB+4*1) | |
159 | +REM TRISBSET=0xBF886118 (TRISB+4*2) | |
160 | +REM PORTB =0xBF886120 (TRISB+4*4) | |
161 | +REM LATBCLR =0xBF886134 (TRISB+4*9) | |
162 | + | |
163 | +end | |
164 | + | |
165 | +REM Initialize I2C | |
166 | +REM Both input for RB7 and RB10 | |
167 | +REM LATB7 and LATB10 are both 0 | |
168 | +REM Wait for 5.2 usec before return | |
169 | + | |
170 | +label I2CINI | |
171 | +B=0xBF886110 | |
172 | +B(2)=0x0480 | |
173 | +B(9)=0x0480 | |
174 | +exec 0x34020240,0x1440FFFF,0x2442FFFF | |
175 | +return | |
176 | + | |
177 | +REM End using I2C | |
178 | +REM Both input for RB7 and RB10 | |
179 | + | |
180 | +label I2CEND | |
181 | +B=0xBF886110 | |
182 | +B(2)=0x0480 | |
183 | +return | |
184 | + | |
185 | +REM Start signal | |
186 | +label I2CS | |
187 | +B=0xBF886110 | |
188 | +B(2)=0x0400 :REM SDA=H | |
189 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
190 | +B(2)=0x0080 :REM SCL=H | |
191 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
192 | +B(1)=0x0400 :REM SDA=L | |
193 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
194 | +B(1)=0x0080 :REM SCL=L | |
195 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
196 | +return | |
197 | + | |
198 | +REM Stop signal | |
199 | +label I2CP | |
200 | +B(1)=0x0400 :REM SDA=L | |
201 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
202 | +B(2)=0x0080 :REM SCL=H | |
203 | +exec 0x34020240,0x1440FFFF,0x2442FFFF :REM 5.2 usec | |
204 | +B(2)=0x0400 :REM SDA=H | |
205 | +exec 0x34020240,0x1440FFFF,0x2442FFFF :REM 5.2 usec | |
206 | +return | |
207 | + | |
208 | +REM Send and receive 8 bit data | |
209 | +REM This returns -1 if ACK bit is H | |
210 | +REM Prepare D for sending 8 bit data. | |
211 | +REM Read data will be in E. | |
212 | +REM For reading 8 bits, let D=0x1ff (NAK) or D=0x2ff (ACK) | |
213 | + | |
214 | +label I2C8BD | |
215 | +E=0 | |
216 | +if D and 0x80 then E=E+gosub(I2CWHR)*0x80 else gosub I2CWL | |
217 | +if D and 0x40 then E=E+gosub(I2CWHR)*0x40 else gosub I2CWL | |
218 | +if D and 0x20 then E=E+gosub(I2CWHR)*0x20 else gosub I2CWL | |
219 | +if D and 0x10 then E=E+gosub(I2CWHR)*0x10 else gosub I2CWL | |
220 | +if D and 0x08 then E=E+gosub(I2CWHR)*0x08 else gosub I2CWL | |
221 | +if D and 0x04 then E=E+gosub(I2CWHR)*0x04 else gosub I2CWL | |
222 | +if D and 0x02 then E=E+gosub(I2CWHR)*0x02 else gosub I2CWL | |
223 | +if D and 0x01 then E=E+gosub(I2CWHR)*0x01 else gosub I2CWL | |
224 | +if D and 0x100 then gosub I2CWHR: return E | |
225 | +if D and 0x200 then gosub I2CWL: return E | |
226 | +if gosub(I2CWHR) then E=-1 | |
227 | +return E | |
228 | + | |
229 | +REM Receive 8 bit data with ACK | |
230 | + | |
231 | +label I2C8RA | |
232 | +D=0x2ff | |
233 | +goto I2C8BD | |
234 | + | |
235 | +REM Receive 8 bit data with NAK | |
236 | + | |
237 | +label I2C8RN | |
238 | +D=0x1ff | |
239 | +goto I2C8BD | |
240 | + | |
241 | +REM 1 bit write/read routines, I2CWHR and I2CWL | |
242 | +REM To read, use I2CWHR. | |
243 | + | |
244 | +label I2CWHR | |
245 | +B=0xBF886110 | |
246 | +B(2)=0x0400 :REM SDA=H | |
247 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
248 | +B(2)=0x0080 :REM SCL=H | |
249 | +exec 0x34020240,0x1440FFFF,0x2442FFFF :REM 5.2 usec | |
250 | +if B(4) and 0x0400 then I2CWH2 | |
251 | + REM read SDA as L | |
252 | + B(1)=0x0400 :REM SDA=L | |
253 | + B(1)=0x0080 :REM SCL=L | |
254 | + exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
255 | + return 0 | |
256 | +label I2CWH2 | |
257 | + REM read SDA as H | |
258 | + B(1)=0x0080 :REM SCL=L | |
259 | + exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
260 | + return 1 | |
261 | + | |
262 | +label I2CWL | |
263 | +B=0xBF886110 | |
264 | +B(1)=0x0400 :REM SDA=L | |
265 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
266 | +B(2)=0x0080 :REM SCL=H | |
267 | +exec 0x34020240,0x1440FFFF,0x2442FFFF :REM 5.2 usec | |
268 | +B(1)=0x0080 :REM SCL=L | |
269 | +exec 0x34020090,0x1440FFFF,0x2442FFFF :REM 2.6 usec | |
270 | +return 0 |