BASIC compiler/interpreter for PIC32MX/MZ-80K
Revisão | e7737e5c3e44fb1055636f016c09e641e0cd7c3f (tree) |
---|---|
Hora | 2019-01-10 07:00:02 |
Autor | Katsumi <kmorimatsu@sour...> |
Commiter | Katsumi |
Class CIOEX for using I/O expander, MCP23017
@@ -0,0 +1,159 @@ | ||
1 | +REM CIOEX.BAS ver 0.1 | |
2 | +REM Class CIOEX for MachiKania Type M | |
3 | +REM using I/O expander MCP23017 | |
4 | + | |
5 | +FIELD PRIVATE RET,ADDR7 | |
6 | +FIELD PRIVATE TRISAV,TRISBV,LATAV,LATBV,CNPUAV,CNPUBV | |
7 | + | |
8 | +REM Constructor | |
9 | +REM 1st argument is 3 bit address | |
10 | +REM (default 0) | |
11 | +REM 2nd argument is clock frequency in kHz | |
12 | +REM (default 400) | |
13 | +METHOD INIT | |
14 | + REM Address setting | |
15 | + if 1<=args(0) then ADDR7=args(1) else ADDR7=0 | |
16 | + ADDR7=0x20 | (ADDR7 and 0x07) | |
17 | + REM Initialize I2C | |
18 | + if 2<=args(0) then I2C args(2) else I2C 400 | |
19 | + REM Initialize MCP23017 | |
20 | + REM Note that IOCON address is either | |
21 | + REM 0x05 (BANK=1) or 0x0A (BANK=0) | |
22 | + I2CWRITE ADDR7,0x05,0x00 | |
23 | + I2CWRITE ADDR7,0x0A,0x00 | |
24 | + if I2CERROR() then | |
25 | + print "MCP23017 is not connected at ";ADDR7 | |
26 | + end | |
27 | + endif | |
28 | + REM Default values | |
29 | + gosub TRISA,0xFF | |
30 | + gosub TRISB,0xFF | |
31 | + gosub CNPUA,0x00 | |
32 | + gosub CNPUB,0x00 | |
33 | + gosub LATA,0x00 | |
34 | + gosub LATB,0x00 | |
35 | + REM All intializations done | |
36 | + return | |
37 | + | |
38 | +REM TRISA(): Read current value | |
39 | +REM TRISA x: Set byte value | |
40 | +REM TRISA x,y: Set bit value | |
41 | +METHOD TRISA | |
42 | + if args(0)=0 then return TRISAV | |
43 | + if args(1)=1 then | |
44 | + SET8 &TRISAV,0x00,args(1) | |
45 | + else | |
46 | + SETBIT &TRISAV,0x00,args(1),args(2) | |
47 | + endif | |
48 | + return | |
49 | + | |
50 | +REM TRISB(): Read current value | |
51 | +REM TRISB x: Set byte value | |
52 | +REM TRISB x,y: Set bit value | |
53 | +METHOD TRISB | |
54 | + if args(0)=0 then return TRISBV | |
55 | + if args(1)=1 then | |
56 | + SET8 &TRISBV,0x01,args(1) | |
57 | + else | |
58 | + SETBIT &TRISBV,0x01,args(1),args(2) | |
59 | + endif | |
60 | + return | |
61 | + | |
62 | +REM LATA(): Read current value | |
63 | +REM LATA x: Set byte value | |
64 | +REM LATA x,y: Set bit value | |
65 | +METHOD LATA | |
66 | + if args(0)=0 then return LATAV | |
67 | + if args(1)=1 then | |
68 | + SET8 &LATAV,0x14,args(1) | |
69 | + else | |
70 | + SETBIT &LATAV,0x14,args(1),args(2) | |
71 | + endif | |
72 | + return | |
73 | + | |
74 | +REM LATB(): Read current value | |
75 | +REM LATB x: Set byte value | |
76 | +REM LATB x,y: Set bit value | |
77 | +METHOD LATB | |
78 | + if args(0)=0 then return LATBV | |
79 | + if args(1)=1 then | |
80 | + SET8 &LATBV,0x15,args(1) | |
81 | + else | |
82 | + SETBIT &LATBV,0x15,args(1),args(2) | |
83 | + endif | |
84 | + return | |
85 | + | |
86 | +REM CNPUA(): Read current value | |
87 | +REM CNPUA x: Set byte value | |
88 | +REM CNPUA x,y: Set bit value | |
89 | +METHOD CNPUA | |
90 | + if args(0)=0 then return CNPUAV | |
91 | + if args(1)=1 then | |
92 | + SET8 &CNPUAV,0x0C,args(1) | |
93 | + else | |
94 | + SETBIT &CNPUAV,0x0C,args(1),args(2) | |
95 | + endif | |
96 | + return | |
97 | + | |
98 | +REM CNPUB(): Read current value | |
99 | +REM CNPUB x: Set byte value | |
100 | +REM CNPUB x,y: Set bit value | |
101 | +METHOD CNPUB | |
102 | + if args(0)=0 then return CNPUBV | |
103 | + if args(1)=1 then | |
104 | + SET8 &CNPUBV,0x0D,args(1) | |
105 | + else | |
106 | + SETBIT &CNPUBV,0x0D,args(1),args(2) | |
107 | + endif | |
108 | + return | |
109 | + | |
110 | +REM PORTA(): Read current input byte value | |
111 | +REM PORTA(x): Read current x-th input bit value | |
112 | +METHOD PORTA | |
113 | + RET=I2CREAD(ADDR7,0x12) | |
114 | + if args(0)<1 then | |
115 | + return RET | |
116 | + elseif RET and (1<<args(1)) then | |
117 | + return 0 | |
118 | + else | |
119 | + return 1 | |
120 | + endif | |
121 | + | |
122 | +REM PORTB(): Read current input byte value | |
123 | +REM PORTB(x): Read current x-th input bit value | |
124 | +METHOD PORTB | |
125 | + RET=I2CREAD(ADDR7,0x13) | |
126 | + if args(0)<1 then | |
127 | + return RET | |
128 | + elseif RET and (1<<args(1)) then | |
129 | + return 0 | |
130 | + else | |
131 | + return 1 | |
132 | + endif | |
133 | + | |
134 | +REM SET8: Set specific setting | |
135 | +REM 1st argument: pointer to val of private field | |
136 | +REM 2nd argument: address in MCP23017 | |
137 | +REM 3rd argument: setting value in byte | |
138 | +LABEL SET8 | |
139 | + poke args(1), args(3) and 0xff | |
140 | + I2CWRITE ADDR7,args(2),args(3) | |
141 | + return | |
142 | + | |
143 | +REM SETBIT: Set specific bit | |
144 | +REM 1st argument: pointer to val of private field | |
145 | +REM 2nd argument: address in MCP23017 | |
146 | +REM 3rd argument: setting bit position | |
147 | +REM 4th argument: setting value (1 or 0) | |
148 | +LABEL SETBIT | |
149 | + var i | |
150 | + i=1<<(args(3) and 0x07) | |
151 | + if args(4) then | |
152 | + i=peek(args(1)) or i | |
153 | + else | |
154 | + i=peek(args(1)) and (i xor 0xff) | |
155 | + endif | |
156 | + poke args(1),i | |
157 | + I2CWRITE ADDR7,args(2),i | |
158 | + return | |
159 | + |
@@ -0,0 +1,110 @@ | ||
1 | +<クラス名およびバージョン> | |
2 | +CIOEX | |
3 | +ver 0.1 | |
4 | + | |
5 | +<ファイル名> | |
6 | +CIOEX.BAS | |
7 | + | |
8 | +<概要> | |
9 | +I/Oエキスパンダー、MCP23017を制御するためのクラス。最大8個まで接続可。 | |
10 | + | |
11 | +<コンストラクター> | |
12 | +第1引数 | |
13 | + 3ビットの接続アドレスを指定。省略した場合は、0。 | |
14 | + | |
15 | +第2引数 | |
16 | + I2Cクロックを、kHz単位で指定。省略した場合は、400。 | |
17 | + | |
18 | +<パブリックフィールド> | |
19 | +なし | |
20 | + | |
21 | +<パブリックメソッド> | |
22 | +TRISA() | |
23 | + 現在のTRISA値を返す。TRIS値は、ポートを入力で使うか出力で使うかの値で、ビッ | |
24 | + トごとに1で入力、0で出力。デフォルトは、0xFF(全て入力)。 | |
25 | + | |
26 | +TRISA x | |
27 | + TRISA値を、8ビットの値、xで指定。 | |
28 | + | |
29 | +TRISA x,y | |
30 | + TRISA値の、下位からxビット目を、yに指定。 | |
31 | + | |
32 | +TRISB() | |
33 | + 現在のTRISB値を返す。TRIS値は、ポートを入力で使うか出力で使うかの値で、ビッ | |
34 | + トごとに1で入力、0で出力。デフォルトは、0xFF(全て入力)。 | |
35 | + | |
36 | +TRISB x | |
37 | + TRISB値を、8ビットの値、xで指定。 | |
38 | + | |
39 | +TRISB x,y | |
40 | + TRISB値の、下位からxビット目を、yに指定。 | |
41 | + | |
42 | +LATA() | |
43 | + 現在のLATA値を返す。LAT値は、ポートを出力で使う場合の出力値で、ビットごと | |
44 | + に設定。デフォルトは、0x00(全て0を出力)。 | |
45 | + | |
46 | +LATA x | |
47 | + LATA値を、8ビットの値、xで指定。 | |
48 | + | |
49 | +LATA x,y | |
50 | + LATA値の、下位からxビット目を、yに指定。 | |
51 | + | |
52 | +LATB() | |
53 | + 現在のLATB値を返す。LAT値は、ポートを出力で使う場合の出力値で、ビットごと | |
54 | + に設定。デフォルトは、0x00(全て0を出力)。 | |
55 | + | |
56 | +LATB x | |
57 | + LATB値を、8ビットの値、xで指定。 | |
58 | + | |
59 | +LATB x,y | |
60 | + LATB値の、下位からxビット目を、yに指定。 | |
61 | + | |
62 | +CNPUA() | |
63 | + 現在のCNPUA値を返す。CNPU値は、ポートを入力で使う場合のプルアップの設定で、 | |
64 | + ビットごとに1で使用、0で不使用。デフォルトは、0x00(全て不使用)。 | |
65 | + | |
66 | +CNPUA x | |
67 | + CNPUA値を、8ビットの値、xで指定。 | |
68 | + | |
69 | +CNPUA x,y | |
70 | + CNPUA値の、下位からxビット目を、yに指定。 | |
71 | + | |
72 | +CNPUB() | |
73 | + 現在のCNPUB値を返す。CNPU値は、ポートを入力で使う場合のプルアップの設定で、 | |
74 | + ビットごとに1で使用、0で不使用。デフォルトは、0x00(全て不使用)。 | |
75 | + | |
76 | +CNPUB x | |
77 | + CNPUB値を、8ビットの値、xで指定。 | |
78 | + | |
79 | +CNPUB x,y | |
80 | + CNPUB値の、下位からxビット目を、yに指定。 | |
81 | + | |
82 | +PORTA() | |
83 | + ポートAから8ビットの値を読み込み、返す。 | |
84 | + | |
85 | +PORTA(x) | |
86 | + ポートAの下位からxビット目を読み込み、返す。 | |
87 | + | |
88 | +PORTB() | |
89 | + ポートBから8ビットの値を読み込み、返す。 | |
90 | + | |
91 | +PORTB(x) | |
92 | + ポートBの下位からxビット目を読み込み、返す。 | |
93 | + | |
94 | +<使用例> | |
95 | + | |
96 | +MCP23017を2つ接続し、一つはアドレス0(A0=0, A1=0, A2=0)で、もう一つはアドレス | |
97 | +1で使う例です。アドレス0の石はPORTAを出力PORTBを入力で、アドレス1の石は | |
98 | +PORTAを入力PORTBを出力で使う設定です。 | |
99 | + | |
100 | +USECLASS CIOEX | |
101 | +USEVAR IO0,IO1 | |
102 | +IO0=NEW(CIOEX,0x00) | |
103 | +IO1=NEW(CIOEX,0x01) | |
104 | +CALL IO0.TRISA(0x00) | |
105 | +CALL IO1.TRISB(0x00) | |
106 | +CALL IO0.LATA(0x55) | |
107 | +CALL IO1.LATB(0xAA) | |
108 | +PRINT HEX$(IO0.PORTB(),2) | |
109 | +PRINT HEX$(IO1.PORTA(),2) | |
110 | + |