Small applications for PIC microcontroler, each in it's own directory

root / placaIntrerupatoare / placaMansarda.asm

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
; Copiright (C) 2018 Eduard Timotei Budulea
; This program is free software: you can redistribute it and/or modify
; it under the terms of the GNU General Public License as published by
; the Free Software Foundation, either version 3 of the License, or
; (at your option) any later version.
; 
; This program is distributed in the hope that it will be useful,
; but WITHOUT ANY WARRANTY; without even the implied warranty of
; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
; GNU General Public License for more details.
; 
; You should have received a copy of the GNU General Public License
; along with this program.  If not, see <http://www.gnu.org/licenses/>.


;Legaturile cu placa.
;        PIC16F88
;         ____________________ 
;     -->| 1  RA2     RA1  18 |<--
;     -->| 2  RA3     RA0  17 |<--
;     -->| 3  RA4     SDO  16 |<--
;     -->| 4  /MCLR   SCL  15 |<--
;     ---| 5  VSS     VDD  14 |---
;     -->| 6  RB0     RB7  13 |<--
;     -->| 7  RB1     RB6  12 |<--
;     -->| 8  RX       TX  11 |>--
;     --<| 9  RB3     RB4  10 |>--
;        |____________________|
;
;Placa:
;Avem 11 senzori de curent si 22 de comenzi de releu.
;
;Cele 11 intrari digitale sunt legate la RA[0-4] si RB[0,1,3,4,6,7], in aceasta ordine.
;Cele 22 de iesiri digitale sunt multiplexate pe un SPI care controleaza 3*CD4094.
;    Cel mai semnificativ bit si cel mai putin semnificativ bit sunt 1.

; Protocolul:
; Se primesc 6 octeti:
; 1. trebuie sa fie 0xB8 (11 inputuri, adresa este 8)
; 2. nu conteaza
; 3. nu conteaza primii 2 biti, apoi sunt valorile pentru iesiri.
; 4 si 5. valori de iesire.
; 6. CRC8 peste cei 5 octeti anteriori.
; Se raspunde cu 6 octeti:
; 1. 0xC8
; 2. valorile de intrarilor.
; 3. primii 3 biti sunt valori de intrare, apoi sunt copie a octetului 3 primit.
; 4 si 5. valori copie la octetii primiti.
; 6. CRC8 peste cei 5 octeti anteriori.

; Dupa aproape 200ms de la ultima comanda se trec toate iesirile in 0.

list p=16f88
include p16f88.inc

    __CONFIG _CONFIG1, _FOSC_INTOSCIO & _WDTE_OFF & _PWRTE_ON & _LVP_OFF
    __CONFIG _CONFIG2, _FCMEN_OFF & _IESO_OFF

errorlevel -302

; Adresa device-ului.
devAddr equ 8
; Locatii de memorie folosite.
timerInactiv equ 0x70
vectorOcteti equ 0x7A ; vectorul are 6 octeti.

scrie macro bitOctet, bitInSine
    bcf PORTA,RA7
    btfsc bitOctet,bitInSine
    bsf PORTA,RA7
    bsf PORTA,RA6
    bcf PORTA,RA6
 endm

    org 0
    bsf STATUS,RP0
    movlw b'00000111'
    movwf OPTION_REG
    movlw 0x50
    movwf OSCCON
    clrf ANSEL
    movlw b'11011111'
    movwf TRISB
    movlw b'00111111'
    movwf TRISA
    movlw 0xC
    movwf SPBRG
    bsf TXSTA,BRGH
    bsf TXSTA,TXEN
    bcf STATUS,RP0
    bsf RCSTA,CREN
    bsf RCSTA,SPEN
    clrf INTCON
    movlw 4
    movwf PCLATH
    movlw 0xFE
    movwf TMR0
    clrf timerInactiv

octetNecitit:
    btfss INTCON,TMR0IF
    goto sariReset
    btfsc timerInactiv,0
    goto sariReset
    scrie INTCON,TMR0IF
    movlw 0x16
buclaScrie0:
    scrie timerInactiv,0
    addlw 0xFF
    btfss STATUS,Z
    goto buclaScrie0
    scrie INTCON,TMR0IF
    decf timerInactiv,F
    call resetOutput

sariReset:
    btfss PIR1,RCIF
    goto octetNecitit

    movfw vectorOcteti + 1
    movwf vectorOcteti
    movfw vectorOcteti + 2
    movwf vectorOcteti + 1
    movfw vectorOcteti + 3
    movwf vectorOcteti + 2
    movfw vectorOcteti + 4
    movwf vectorOcteti + 3
    movfw vectorOcteti + 5
    movwf vectorOcteti + 4
    movfw RCREG
    movwf vectorOcteti + 5

    movlw 0xB0 + devAddr
    xorwf vectorOcteti,W
    btfss STATUS,Z
    goto octetNecitit

    movfw vectorOcteti
    call crcTable
    xorwf vectorOcteti + 1,W
    call crcTable
    xorwf vectorOcteti + 2,W
    call crcTable
    xorwf vectorOcteti + 3,W
    call crcTable
    xorwf vectorOcteti + 4,W
    call crcTable
    xorwf vectorOcteti + 5,W
    btfss STATUS,Z
    goto octetNecitit

    scrie STATUS,Z
    scrie vectorOcteti + 2,2
    scrie vectorOcteti + 2,3
    scrie vectorOcteti + 2,4
    scrie vectorOcteti + 2,5
    scrie vectorOcteti + 2,6
    scrie vectorOcteti + 2,7
    scrie vectorOcteti + 3,0
    scrie vectorOcteti + 3,1
    scrie vectorOcteti + 3,2
    scrie vectorOcteti + 3,3
    scrie vectorOcteti + 3,4
    scrie vectorOcteti + 3,5
    scrie vectorOcteti + 3,6
    scrie vectorOcteti + 3,7
    scrie vectorOcteti + 4,0
    scrie vectorOcteti + 4,1
    scrie vectorOcteti + 4,2
    scrie vectorOcteti + 4,3
    scrie vectorOcteti + 4,4
    scrie vectorOcteti + 4,5
    scrie vectorOcteti + 4,6
    scrie vectorOcteti + 4,7
    scrie STATUS,Z

    call resetOutput

    clrf vectorOcteti + 1
    movlw 0x7
    andwf vectorOcteti + 2,F
    btfsc PORTA,RA0
    bsf vectorOcteti + 1,0
    btfsc PORTA,RA1
    bsf vectorOcteti + 1,1
    btfsc PORTA,RA2
    bsf vectorOcteti + 1,2
    btfsc PORTA,RA3
    bsf vectorOcteti + 1,3
    btfsc PORTA,RA4
    bsf vectorOcteti + 1,4
    btfsc PORTB,RB0
    bsf vectorOcteti + 1,5
    btfsc PORTB,RB1
    bsf vectorOcteti + 1,6
    btfsc PORTB,RB3
    bsf vectorOcteti + 1,7
    btfsc PORTB,RB4
    bsf vectorOcteti + 2,0
    btfsc PORTB,RB6
    bsf vectorOcteti + 2,1
    btfsc PORTB,RB7
    bsf vectorOcteti + 2,2

    movfw vectorOcteti
    call crcTable
    xorwf vectorOcteti + 1,W
    call crcTable
    xorwf vectorOcteti + 2,W
    call crcTable
    xorwf vectorOcteti + 3,W
    call crcTable
    xorwf vectorOcteti + 4,W
    movwf vectorOcteti + 5

    movlw vectorOcteti + 0x80
    movwf FSR
scrieUART:
    btfss PIR1,TXIF
    goto scrieUART
    movfw INDF
    movwf TXREG
    incfsz FSR,F
    goto scrieUART

    clrf TMR0
    clrf timerInactiv
    bcf INTCON,TMR0IF
    goto octetNecitit

resetOutput:
    movlw 0x18
    bcf PORTA,RA7
trageApa:
    bsf PORTA,RA6
    bcf PORTA,RA6
    addlw -1
    btfss STATUS,Z
    goto trageApa
    return

crcTable:
    movwf PCL

    org 0x400
    retlw 0x00
    retlw 0x5e
    retlw 0xbc
    retlw 0xe2
    retlw 0x61
    retlw 0x3f
    retlw 0xdd
    retlw 0x83
    retlw 0xc2
    retlw 0x9c
    retlw 0x7e
    retlw 0x20
    retlw 0xa3
    retlw 0xfd
    retlw 0x1f
    retlw 0x41
    retlw 0x9d
    retlw 0xc3
    retlw 0x21
    retlw 0x7f
    retlw 0xfc
    retlw 0xa2
    retlw 0x40
    retlw 0x1e
    retlw 0x5f
    retlw 0x01
    retlw 0xe3
    retlw 0xbd
    retlw 0x3e
    retlw 0x60
    retlw 0x82
    retlw 0xdc
    retlw 0x23
    retlw 0x7d
    retlw 0x9f
    retlw 0xc1
    retlw 0x42
    retlw 0x1c
    retlw 0xfe
    retlw 0xa0
    retlw 0xe1
    retlw 0xbf
    retlw 0x5d
    retlw 0x03
    retlw 0x80
    retlw 0xde
    retlw 0x3c
    retlw 0x62
    retlw 0xbe
    retlw 0xe0
    retlw 0x02
    retlw 0x5c
    retlw 0xdf
    retlw 0x81
    retlw 0x63
    retlw 0x3d
    retlw 0x7c
    retlw 0x22
    retlw 0xc0
    retlw 0x9e
    retlw 0x1d
    retlw 0x43
    retlw 0xa1
    retlw 0xff
    retlw 0x46
    retlw 0x18
    retlw 0xfa
    retlw 0xa4
    retlw 0x27
    retlw 0x79
    retlw 0x9b
    retlw 0xc5
    retlw 0x84
    retlw 0xda
    retlw 0x38
    retlw 0x66
    retlw 0xe5
    retlw 0xbb
    retlw 0x59
    retlw 0x07
    retlw 0xdb
    retlw 0x85
    retlw 0x67
    retlw 0x39
    retlw 0xba
    retlw 0xe4
    retlw 0x06
    retlw 0x58
    retlw 0x19
    retlw 0x47
    retlw 0xa5
    retlw 0xfb
    retlw 0x78
    retlw 0x26
    retlw 0xc4
    retlw 0x9a
    retlw 0x65
    retlw 0x3b
    retlw 0xd9
    retlw 0x87
    retlw 0x04
    retlw 0x5a
    retlw 0xb8
    retlw 0xe6
    retlw 0xa7
    retlw 0xf9
    retlw 0x1b
    retlw 0x45
    retlw 0xc6
    retlw 0x98
    retlw 0x7a
    retlw 0x24
    retlw 0xf8
    retlw 0xa6
    retlw 0x44
    retlw 0x1a
    retlw 0x99
    retlw 0xc7
    retlw 0x25
    retlw 0x7b
    retlw 0x3a
    retlw 0x64
    retlw 0x86
    retlw 0xd8
    retlw 0x5b
    retlw 0x05
    retlw 0xe7
    retlw 0xb9
    retlw 0x8c
    retlw 0xd2
    retlw 0x30
    retlw 0x6e
    retlw 0xed
    retlw 0xb3
    retlw 0x51
    retlw 0x0f
    retlw 0x4e
    retlw 0x10
    retlw 0xf2
    retlw 0xac
    retlw 0x2f
    retlw 0x71
    retlw 0x93
    retlw 0xcd
    retlw 0x11
    retlw 0x4f
    retlw 0xad
    retlw 0xf3
    retlw 0x70
    retlw 0x2e
    retlw 0xcc
    retlw 0x92
    retlw 0xd3
    retlw 0x8d
    retlw 0x6f
    retlw 0x31
    retlw 0xb2
    retlw 0xec
    retlw 0x0e
    retlw 0x50
    retlw 0xaf
    retlw 0xf1
    retlw 0x13
    retlw 0x4d
    retlw 0xce
    retlw 0x90
    retlw 0x72
    retlw 0x2c
    retlw 0x6d
    retlw 0x33
    retlw 0xd1
    retlw 0x8f
    retlw 0x0c
    retlw 0x52
    retlw 0xb0
    retlw 0xee
    retlw 0x32
    retlw 0x6c
    retlw 0x8e
    retlw 0xd0
    retlw 0x53
    retlw 0x0d
    retlw 0xef
    retlw 0xb1
    retlw 0xf0
    retlw 0xae
    retlw 0x4c
    retlw 0x12
    retlw 0x91
    retlw 0xcf
    retlw 0x2d
    retlw 0x73
    retlw 0xca
    retlw 0x94
    retlw 0x76
    retlw 0x28
    retlw 0xab
    retlw 0xf5
    retlw 0x17
    retlw 0x49
    retlw 0x08
    retlw 0x56
    retlw 0xb4
    retlw 0xea
    retlw 0x69
    retlw 0x37
    retlw 0xd5
    retlw 0x8b
    retlw 0x57
    retlw 0x09
    retlw 0xeb
    retlw 0xb5
    retlw 0x36
    retlw 0x68
    retlw 0x8a
    retlw 0xd4
    retlw 0x95
    retlw 0xcb
    retlw 0x29
    retlw 0x77
    retlw 0xf4
    retlw 0xaa
    retlw 0x48
    retlw 0x16
    retlw 0xe9
    retlw 0xb7
    retlw 0x55
    retlw 0x0b
    retlw 0x88
    retlw 0xd6
    retlw 0x34
    retlw 0x6a
    retlw 0x2b
    retlw 0x75
    retlw 0x97
    retlw 0xc9
    retlw 0x4a
    retlw 0x14
    retlw 0xf6
    retlw 0xa8
    retlw 0x74
    retlw 0x2a
    retlw 0xc8
    retlw 0x96
    retlw 0x15
    retlw 0x4b
    retlw 0xa9
    retlw 0xf7
    retlw 0xb6
    retlw 0xe8
    retlw 0x0a
    retlw 0x54
    retlw 0xd7
    retlw 0x89
    retlw 0x6b
    retlw 0x35

    end