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