このファイル: /home/web6047/www/cgi-bin/prj/20190815-book_asm_picprj/20200812-P93.X/newmain - snapshot 20200812.c
1
2
3
#include "pic.h"
4
#include "stdio.h"
5
6
7
8
9
#pragma config WDTE = OFF
10
11
12
13
#pragma config LVP = OFF
14
15
16
17
18
19
20
21
22
23
24
#define I2C_SCL RB4
25
#define I2C_SDA RB1
26
#define I2C_SDA_TRIS TRISB1
27
28
29
#define LCD_ADDR 0x7C
30
31
32
static void I2C_start(void);
33
static void I2C_send(unsigned char);
34
static unsigned char I2C_ackchk(void);
35
static void I2C_acksnd(void);
36
static void I2C_nacksnd(void);
37
static unsigned char I2C_rcv(void);
38
static unsigned char I2C_rcv_char(unsigned char EE_P0 ,unsigned char addr1 ,unsigned char addr2);
39
static void I2C_stop();
40
41
static void wait_50us(void);
42
static void wait_1ms(void);
43
static void wait_ms(unsigned int);
44
45
46
47
48
49
static void I2C_snd_char2( unsigned char databyte) {
50
I2C_start();
51
52
I2C_send(LCD_ADDR);
53
if (I2C_ackchk()) {
54
}
55
56
57
I2C_send( 0b00000000 );
58
if (I2C_ackchk()) {
59
}
60
61
62
I2C_send(databyte);
63
if (I2C_ackchk()) {
64
}
65
66
I2C_stop();
67
68
wait_ms(5);
69
return;
70
}
71
72
73
74
75
void lcdClr() {
76
I2C_snd_char2( 0x01 );
77
}
78
79
80
81
82
void lcdStr( char *str ) {
83
I2C_start();
84
I2C_send(LCD_ADDR);
85
if (I2C_ackchk()) {
86
}
87
88
while( *str != '\0' ) {
89
90
if( *( str + 1 ) == '\0' ) {
91
I2C_send( 0b01000000 );
92
} else {
93
I2C_send( 0b11000000 );
94
}
95
96
if (I2C_ackchk()) {
97
}
98
99
100
I2C_send( *str );
101
102
103
if (I2C_ackchk()) {
104
}
105
106
107
str++;
108
}
109
I2C_stop();
110
}
111
112
113
114
void init( void ) {
115
OSCCON = 0x70;
116
ANSEL = 0x00;
117
TRISA = 0xF0;
118
TRISB = 0xE3;
119
PORTA = 0x00;
120
PORTB = 0x12;
121
122
123
124
125
wait_ms( 500 );
126
127
128
I2C_snd_char2( 0x38 );
129
I2C_snd_char2( 0x3d );
130
I2C_snd_char2( 0x1d );
131
I2C_snd_char2( 0x52 );
132
I2C_snd_char2( 0x6b );
133
I2C_snd_char2( 0x70 );
134
I2C_snd_char2( 0x39 );
135
I2C_snd_char2( 0x0f );
136
lcdClr();
137
}
138
139
140
141
142
143
144
145
146
147
148
149
volatile persistent signed char GR0;
150
volatile persistent signed char GR1;
151
volatile persistent signed char GR2;
152
volatile persistent signed char GR3;
153
volatile persistent signed char X;
154
volatile persistent signed char Y;
155
volatile persistent unsigned char CONST0;
156
volatile persistent unsigned char CONST1;
157
volatile persistent char GR4;
158
159
int main() {
160
init();
161
162
lcdStr( "Asm P93" );
163
I2C_snd_char2( 0xc0 );
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
X = 4;
189
Y = 3;
190
CONST0 = 0;
191
CONST1 = 1;
192
#asm
193
;課題 2つの整数x,yがメモリに保存されているとき、x * y をGR2に求める。
194
; ただし、yは負でない整数とする。
195
movf _X, 0 ;2
196
movwf _GR0 ;2
197
movf _Y, 0 ;3
198
movwf _GR1 ;3
199
movf _CONST0, 0 ;4
200
movwf _GR2 ;4
201
movwf _GR3 ;5
202
LOOP: movf _GR1, 0 ;6,7 GR1==GR3判断
203
subwf _GR3, 0 ;6,7 GR1==GR3判断 計算結果代入してしまうがCPAと同等
204
btfsc STATUS, 2 ;6,7 GR1==GR3判断 下の行と合わせてJZEと同等
205
goto LABEL1 ;6,7 GR1==GR3です。
206
movf _GR0, 0 ;8
207
addwf _GR2, 1 ;8
208
movf _CONST1, 0 ;9
209
addwf _GR3, 1 ;9
210
goto LOOP ;10
211
LABEL1: ;11
212
#endasm
213
214
215
char buf[15];
216
sprintf( buf, "%d", GR2 );
217
lcdStr( buf );
218
219
220
221
222
while(1) {
223
}
224
}
225
226
227
228
229
230
static void I2C_start() {
231
232
I2C_SDA = 1;
233
I2C_SDA_TRIS = 0;
234
I2C_SCL = 1;
235
NOP();
236
NOP();
237
NOP();
238
NOP();
239
I2C_SDA = 0;
240
NOP();
241
NOP();
242
NOP();
243
NOP();
244
I2C_SCL = 0;
245
246
return;
247
}
248
249
250
251
252
253
static void I2C_stop() {
254
255
I2C_SDA = 0;
256
I2C_SDA_TRIS = 0;
257
I2C_SCL = 1;
258
NOP();
259
NOP();
260
NOP();
261
NOP();
262
I2C_SDA = 1;
263
264
return;
265
}
266
267
268
269
270
271
static void I2C_send(unsigned char send_data) {
272
unsigned char i2c_data,i;
273
274
I2C_SDA_TRIS = 0;
275
276
i2c_data = send_data;
277
for (i=8 ; i>0 ; i--) {
278
279
if (i2c_data & 0x80) {
280
I2C_SDA = 1;
281
} else {
282
I2C_SDA = 0;
283
}
284
i2c_data = i2c_data << 1;
285
286
I2C_SCL = 1;
287
NOP();
288
NOP();
289
NOP();
290
NOP();
291
I2C_SCL = 0;
292
}
293
I2C_SDA_TRIS = 1;
294
295
return;
296
}
297
298
299
300
301
302
static unsigned char I2C_ackchk() {
303
304
unsigned char i2c_data;
305
306
I2C_SCL = 1;
307
NOP();
308
NOP();
309
I2C_SCL = 0;
310
NOP();
311
NOP();
312
i2c_data = PORTB & 0x02;
313
314
return(i2c_data);
315
}
316
317
318
319
320
321
static void wait_ms(unsigned int ms) {
322
unsigned int c;
323
for (c=ms ; c>0 ; c--) {
324
wait_1ms();
325
}
326
return;
327
}
328
329
330
331
332
333
static unsigned char I2C_rcv() {
334
unsigned char i2c_data,i;
335
336
I2C_SDA_TRIS = 1;
337
for (i=8 ; i>0 ; i--) {
338
NOP();
339
I2C_SCL = 1;
340
i2c_data = i2c_data <<1;
341
if (PORTB & 0x02) {
342
i2c_data = i2c_data | 0x01;
343
} else {
344
i2c_data = i2c_data & 0xFE;
345
}
346
I2C_SCL = 0;
347
}
348
349
return(i2c_data);
350
}
351
352
353
354
static unsigned char I2C_rcv_char(unsigned char ee_p0 ,unsigned char addr1 ,unsigned char addr2) {
355
unsigned char c;
356
357
I2C_start();
358
I2C_send(LCD_ADDR | ee_p0);
359
if (I2C_ackchk()) {
360
361
}
362
I2C_send(addr1);
363
if (I2C_ackchk()) {
364
365
}
366
I2C_send(addr2);
367
if (I2C_ackchk()) {
368
369
}
370
371
I2C_start();
372
I2C_send(LCD_ADDR | ee_p0 | 0x01);
373
if (I2C_ackchk()) {
374
375
}
376
c = I2C_rcv();
377
I2C_nacksnd();
378
379
I2C_stop();
380
381
return(c);
382
}
383
384
385
386
static void I2C_acksnd() {
387
I2C_SDA = 0;
388
I2C_SDA_TRIS = 0;
389
NOP();
390
I2C_SCL = 1;
391
NOP();
392
NOP();
393
NOP();
394
NOP();
395
I2C_SCL = 0;
396
I2C_SDA_TRIS = 1;
397
398
return;
399
}
400
401
402
static void I2C_nacksnd() {
403
I2C_SDA = 1;
404
I2C_SDA_TRIS = 0;
405
I2C_SCL = 1;
406
NOP();
407
I2C_SCL = 0;
408
NOP();
409
I2C_SDA_TRIS = 1;
410
411
return;
412
}
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
static void wait_50us() {
431
unsigned char i;
432
433
for (i=32 ; i>0 ; i--) {}
434
return;
435
}
436
437
438
static void wait_1ms() {
439
unsigned char i;
440
441
for (i=179 ; i>0 ; i--) {
442
NOP();
443
NOP();
444
NOP();
445
}
446
return;
447
}
448
449