Debian Codegraph @

extras/ds1307/examples/ds1307.asm

raw
1 ;
2 ; Test reading, writing and dump for external i2c DS1307 module
3 ;
4 ; Copyright (c) 2007 Roy Rankin
5 ; MACROS and some code included the following
6 ;************************************************************************
7 ;* Microchip Technology Inc. 2006
8 ;* 02/15/06
9 ;* Designed to run at 20MHz
10 ;************************************************************************
11 ; gpsim command
12 .command macro x
13 .direct "C", x
14 endm
15
16
17
18 list p=16F876A
19 #include p16f876a.inc
20 include <coff.inc>
21
22
23 __CONFIG _CP_OFF & _DEBUG_OFF & _WRT_OFF & _CPD_OFF & _LVP_OFF & _PWRTE_ON & _BODEN_ON & _WDT_OFF & _HS_OSC
24
25 errorlevel -302
26 radix dec
27
28
29 variables UDATA 0x30
30 time RES 16
31 temp1 RES 1
32 temp2 RES 1
33 status_temp RES 1
34 w_temp RES 1
35
36 global time
37 ;----------------------------------------------------------------------
38 ; ********************* RESET VECTOR LOCATION ********************
39 ;----------------------------------------------------------------------
40 RESET_VECTOR CODE 0x000 ; processor reset vector
41 movlw high start ; load upper byte of 'start' label
42 movwf PCLATH ; initialize PCLATH
43 goto start ; go to beginning of program
44
45 ;------------------------------------------------------------------------
46 ;
47 ; Interrupt Vector
48 ;
49 ;------------------------------------------------------------------------
50
51 INT_VECTOR CODE 0x004 ; interrupt vector location
52
53 movwf w_temp
54 swapf STATUS,W
55 movwf status_temp
56 btfsc INTCON,RBIF
57 goto rb_int
58
59 .assert "\"***FAILED ds1307 unexpected interrupt\""
60 nop
61 rb_int
62 nop
63 bcf INTCON,RBIF
64 exit_int:
65
66 swapf status_temp,w
67 movwf STATUS
68 swapf w_temp,f
69 swapf w_temp,w
70 retfie
71
72
73
74 ;
75 MAIN CODE
76 start
77 .sim "module lib libgpsim_modules"
78 .sim "module lib libgpsim_extras"
79 .sim "module load ds1307 ee"
80 .sim "module load pu pu1"
81 .sim "module load pu pu2"
82 .sim "module load pu pu3"
83 .sim "node n1"
84 .sim "attach n1 portc4 pu1.pin portb4 ee.SDA" ; SDA
85 .sim "node n2"
86 .sim "attach n2 portc3 pu2.pin portb1 ee.SCL" ; SCL
87 .sim "node n3"
88 .sim "attach n3 pu3.pin portb0 ee.SQW"
89 .sim "pu1.xpos = 240."
90 .sim "pu1.ypos = 336."
91 .sim "pu2.xpos = 216."
92 .sim "pu2.ypos = 24."
93 .sim "ee.xpos = 48."
94 .sim "ee.ypos = 180."
95 .sim "p16f876a.xpos = 240."
96 .sim "p16f876a.ypos = 84."
97 .sim "scope.ch0 = 'portc3'"
98 .sim "scope.ch1 = 'portc4'"
99
100
101
102
103 call ProgInit ; Get everything running step-by-step
104 BANKSEL PORTA
105 movlw 0x2F
106 movwf time+2
107 movwf time+5
108 movlw 0x20
109 movwf time+8
110 movlw 0x3A
111 movwf time+0xb
112 movwf time+0xe
113 nop
114 call is_ready
115 call read_ds1307
116 ; Current date and time startting in ram location 30
117 ; sleep
118 nop
119 call I2CSendResult
120 call write_eeprom
121 call is_ready
122 call read_ds1307
123 ; 02/05/10 19:59:12 starting in ram location 30
124 .assert "time == 0x30, \"*** FAILED ds1307 day 10's\""
125 nop
126 BANKSEL time
127 movf time+1,W
128 .assert "W == 0x33, \"*** FAILED ds1307 day 1's\""
129 nop
130 movf time+3,W
131 .assert "W == 0x30, \"*** FAILED ds1307 month 10's\""
132 nop
133 movf time+4,W
134 .assert "W == 0x35, \"*** FAILED ds1307 month 1's\""
135 nop
136 movf time+6,W
137 .assert "W == 0x31, \"*** FAILED ds1307 year 10's\""
138 nop
139 movf time+7,W
140 .assert "W == 0x30, \"*** FAILED ds1307 year 1's\""
141 nop
142 movf time+9,W
143 .assert "W == 0x31, \"*** FAILED ds1307 hour 10's\""
144 nop
145 movf time+10,W
146 .assert "W == 0x39, \"*** FAILED ds1307 hour 1's\""
147 nop
148 movf time+12,W
149 .assert "W == 0x35, \"*** FAILED ds1307 min 10's\""
150 nop
151 movf time+13,W
152 .assert "W == 0x39, \"*** FAILED ds1307 min 1's\""
153 nop
154 movf time+15,W
155 .assert "W == 0x31, \"*** FAILED ds1307 sec 10's\""
156 nop
157 movf time+16,W
158 .assert "W == 0x32, \"*** FAILED ds1307 sec 1's\""
159 nop
160 sleep ; SQW causes wake
161 nop
162
163 .command "dump e ee dump.hex"
164 nop
165
166 call is_ready
167 call read_ds1307
168 BANKSEL time
169 movf time+16,W
170 .assert "W == 0x33, \"*** FAILED ds1307 sec 1's after 1 sec\""
171 nop
172
173 .assert "\"*** PASSED p16f876a I2C DS1307 test\""
174 goto $
175
176
177
178
179
180
181 ;****************************** SUBROUTINES ****************************
182 ;************************************************************************
183
184 START_I2C MACRO
185 banksel SSPCON2 ; Generate I2C start
186 bsf SSPCON2,SEN
187 btfsc SSPCON2,SEN
188 goto $-1
189 banksel PORTA
190 ENDM
191
192 RSTART_I2C MACRO
193 banksel SSPCON2 ; Generate I2C repeat start
194 bsf SSPCON2,RSEN
195 btfsc SSPCON2,RSEN
196 goto $-1
197 banksel PORTA
198 ENDM
199
200 STOP_I2C MACRO
201 banksel SSPCON2 ; Generate I2C stop
202 bsf SSPCON2,PEN
203 btfsc SSPCON2,PEN
204 goto $-1
205 banksel PORTA
206 ENDM
207
208 IDLE_WAIT_I2C MACRO
209 banksel SSPCON2
210 movlw 0x1f
211 andwf SSPCON2,W
212 BNZ $-3
213 btfsc SSPSTAT,R_W
214 goto $-1
215 banksel PORTA
216 ENDM
217
218
219
220
221 ;****************** Initialize Registers and Variables *****************
222 ;************************************************************************
223 ProgInit
224
225 banksel PORTA
226 clrf PORTA ; Set all bits to zero on Port A
227 banksel TRISA
228 clrf TRISA
229
230
231
232 banksel SSPADD
233 movlw 0x0C ; Set I2C baud rate to 385 kHz
234 movwf SSPADD
235 bcf OPTION_REG,INTEDG
236 banksel SSPCON
237 movlw 0x08 ; Set for I2C master mode
238 movwf SSPCON
239 movlw 0x28 ; Enable I2C
240 movwf SSPCON
241 bsf INTCON,INTE
242
243 return
244
245
246
247
248
249 ;************************************************************************
250 I2CSendResult
251 banksel SSPCON2 ; Generate I2C start, bus collision
252 bsf SSPCON2,PEN
253 btfsc SSPCON2,PEN
254 goto $-1
255
256 bsf SSPCON2,SEN
257 bcf TRISB,1
258 IDLE_WAIT_I2C
259 .assert "(pir2 & 0x08) == 0x08, \"FAILED BCLIF for start\""
260 nop
261 banksel TRISB
262 bsf TRISB,1
263 banksel PIR2
264 bcf PIR2,BCLIF
265
266 banksel SSPCON2 ; Generate I2C start
267 bsf SSPCON2,SEN
268 IDLE_WAIT_I2C
269
270 .assert "(portc & 0x18) == 0, \"FAILED Start SCL, SDL low\""
271 nop
272 .assert "(pir1 & 0x08) == 0x08, \"FAILED Start SSPIF set\""
273 nop
274 .assert "(pir2 & 0x08) == 0x00, \"FAILED Start BCLIF clear\""
275 nop
276 .assert "(sspstat & 0x3f) == 0x08, \"FAILED Start S bit set\""
277 nop
278
279 banksel PIR1
280 bcf PIR1,SSPIF
281
282 banksel SSPCON2 ; Generate I2C restart
283 bsf SSPCON2,RSEN
284 btfsc SSPCON2,RSEN
285 goto $-1
286
287 .assert "(portc & 0x18) == 0, \"FAILED RStart SCL, SDL low\""
288 nop
289 .assert "(pir1 & 0x08) == 0x08, \"FAILED RStart SSPIF set\""
290 nop
291 .assert "(pir2 & 0x08) == 0x00, \"FAILED RStart BCLIF clear\""
292 nop
293 .assert "(sspstat & 0x3f) == 0x08, \"FAILED RStart S bit set\""
294 nop
295
296 banksel PIR1
297 bcf PIR1,SSPIF
298
299 banksel SSPCON2
300 bsf SSPCON2,ACKDT
301 bsf SSPCON2,ACKEN
302 btfsc SSPCON2,ACKEN
303 goto $-1
304 .assert "(portc & 0x18) == 0x10, \"FAILED ACKEN SCL low, SDL high\""
305 nop
306 .assert "(pir1 & 0x08) == 0x08, \"FAILED ACKEN SSPIF set\""
307 nop
308 .assert "(pir2 & 0x08) == 0x00, \"FAILED ACKEN BCLIF clear\""
309 nop
310 .assert "(sspstat & 0x3f) == 0x08, \"FAILED ACKEN S bit set\""
311 nop
312 bcf SSPCON2,ACKDT
313
314 bsf SSPCON2,PEN
315 btfsc SSPCON2,PEN
316 goto $-1
317
318 banksel PIR1
319 bcf PIR1,SSPIF
320
321 banksel SSPCON2 ; Generate I2C restart
322 bsf SSPCON2,RSEN
323 btfsc SSPCON2,RSEN
324 goto $-1
325
326 .assert "(portc & 0x18) == 0, \"FAILED RStart SCL, SDL low\""
327 nop
328 .assert "(pir1 & 0x08) == 0x08, \"FAILED RStart SSPIF set\""
329 nop
330 .assert "(pir2 & 0x08) == 0x00, \"FAILED RStart BCLIF clear\""
331 nop
332 .assert "(sspstat & 0x3f) == 0x08, \"FAILED RStart S bit set\""
333 nop
334
335 banksel PIR1
336 bcf PIR1,SSPIF
337
338 banksel SSPCON2 ; Generate I2C stop
339 bsf SSPCON2,PEN
340 btfsc SSPCON2,PEN
341 goto $-1
342
343 .assert "(portc & 0x18) == 0x18, \"FAILED Stop SCL, SDL high\""
344 nop
345 .assert "(pir1 & 0x08) == 0x08, \"FAILED Stop SSPIF set\""
346 nop
347 .assert "(pir2 & 0x08) == 0x00, \"FAILED Stop BCLIF clear\""
348 nop
349 .assert "(sspstat & 0x3f) == 0x10, \"FAILED Stop P bit set\""
350 nop
351 return
352 ;
353 ; repeatedly send command to eeprom until an ACK
354 ; is received back
355 ;
356 ; The call of delay is not required for operation of the code,
357 ; but it speeds up the simulation with the GUI running. -- RRR
358 ;
359 is_ready
360 banksel SSPCON
361 movlw 0x04
362 movwf temp2
363 clrf temp1
364 call delay
365 banksel SSPCON2 ; Generate I2C start
366 bsf SSPCON2,SEN
367 btfsc SSPCON2,SEN
368 goto $-1
369 movlw 0xd0 ; write command to eeprom
370 call I2C_send_w
371 call I2C_stop
372 banksel SSPCON2 ; Generate I2C start
373 btfsc SSPCON2,ACKSTAT
374 goto is_ready
375 banksel PIR2
376 bcf PIR2,BCLIF
377 return
378
379 write_eeprom_address
380 banksel SSPCON2 ; Generate I2C start
381 bsf SSPCON2,SEN
382 btfsc SSPCON2,SEN
383 goto $-1
384 movlw 0xd0 ; write command to eeprom
385 call I2C_send_w
386 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write command to eeprom ACK\""
387 nop
388 .assert "(sspstat & 0x01) == 0x00, \"FAILED write to eeprom BF clear\""
389 nop
390
391 banksel PIR1
392 bcf PIR1,SSPIF
393 movlw 0x00 ; write eeprom address
394 call I2C_send_w
395 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write address to eeprom ACK\""
396 nop
397 return
398
399 write_eeprom
400 call write_eeprom_address
401 banksel PIR1
402 bcf PIR1,SSPIF
403 movlw 0x12 ; write seconds 12
404 call I2C_send_w
405 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write data1 to eeprom ACK\""
406 nop
407
408 banksel PIR1
409 bcf PIR1,SSPIF
410 movlw 0x59 ; write minutes 59
411 call I2C_send_w
412 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write data2 to eeprom ACK\""
413 nop
414 banksel PIR1
415 bcf PIR1,SSPIF
416 movlw 0x19|0x40 ; write hours 19 and 24 hour format
417 call I2C_send_w
418 nop
419 banksel PIR1
420 bcf PIR1,SSPIF
421 movlw 0x01 ; write Day of week (1-7)
422 call I2C_send_w
423 nop
424 banksel PIR1
425 bcf PIR1,SSPIF
426 movlw 0x03 ; write Day of month
427 call I2C_send_w
428 nop
429 banksel PIR1
430 bcf PIR1,SSPIF
431 movlw 0x05 ; write month
432 call I2C_send_w
433 nop
434 banksel PIR1
435 bcf PIR1,SSPIF
436 movlw 0x10 ; write year
437 call I2C_send_w
438 nop
439 banksel PIR1
440 bcf PIR1,SSPIF
441 movlw 0x10 ; control out=1 SQWE off
442 call I2C_send_w
443 nop
444 call I2C_stop
445 return
446
447 clr_eeprom
448 call write_eeprom_address
449 banksel PIR1
450 bcf PIR1,SSPIF
451 movlw 0x00 ; write data1
452 call I2C_send_w
453 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write data1 to eeprom ACK\""
454 nop
455
456 banksel PIR1
457 bcf PIR1,SSPIF
458 movlw 0x00 ; write data2
459 call I2C_send_w
460 .assert "(sspcon2 & 0x40) == 0x00, \"FAILED write data2 to eeprom ACK\""
461 nop
462 nop
463 call I2C_stop
464 return
465
466
467 read_ds1307
468
469 call write_eeprom_address
470 banksel SSPCON2 ; Generate I2C repeated start
471 bsf SSPCON2,RSEN
472 btfsc SSPCON2,RSEN
473 goto $-1
474 movlw 0xd1 ; Send address/read to eeprom
475 banksel SSPBUF
476 movwf SSPBUF
477 banksel SSPCON2 ; wait for idle (ACKEN,RCEN,PEN,RSEN,SEN) == 0
478 movlw 0x1f
479 andwf SSPCON2,W
480 BNZ $-3
481 btfsc SSPSTAT,R_W ; also R_W == 0
482 goto $-1
483
484 .assert "(sspstat & 0x01) == 0x00, \"FAILED address/read to eeprom BF clear\""
485 nop
486
487 banksel PIR1
488 bcf PIR1,SSPIF
489
490 banksel SSPCON2
491 bsf SSPCON2,RCEN ; read seconds from RTC
492 btfsc SSPCON2,RCEN
493 goto $-1
494 .assert "(pir1 & 0x08) == 0x08, \"FAILED RCEN SSPIF set\""
495 nop
496 .assert "(pir2 & 0x08) == 0x00, \"FAILED RCEN BCLIF clear\""
497 nop
498 .assert "(sspstat & 0x01) == 0x01, \"FAILED RCEN BF set\""
499 nop
500 banksel SSPBUF
501 movf SSPBUF,W
502 banksel PORTA
503 movwf time+0x10
504 movwf time+0xf
505 movlw 0x0f
506 andwf time+0x10,f
507 movlw 0x30
508 addwf time+0x10,f
509 movlw 0x70
510 andwf time+0xf,f
511 RRF time+0xf,f
512 RRF time+0xf,f
513 RRF time+0xf,f
514 RRF time+0xf,f
515 movlw 0x30
516 addwf time+0xf,f
517 nop
518
519 banksel PIR1
520 bcf PIR1,SSPIF
521
522 banksel SSPCON2
523 bcf SSPCON2,ACKDT ; send ACK
524 bsf SSPCON2,ACKEN
525 btfsc SSPCON2,ACKEN
526 goto $-1
527
528 banksel PIR1
529 bcf PIR1,SSPIF
530
531 banksel SSPCON2
532 bsf SSPCON2,RCEN ; read minutes from RTC
533 btfsc SSPCON2,RCEN
534 goto $-1
535 banksel SSPBUF
536 movf SSPBUF,W
537 banksel PORTA
538 movwf time+0xd
539 movwf time+0xc
540 movlw 0x0f
541 andwf time+0xd,f
542 movlw 0x30
543 addwf time+0xd,f
544 movlw 0xf0
545 andwf time+0xc,f
546 RRF time+0xc,f
547 RRF time+0xc,f
548 RRF time+0xc,f
549 RRF time+0xc,f
550 movlw 0x30
551 addwf time+0xc,f
552 nop
553
554 banksel PIR1
555 bcf PIR1,SSPIF
556
557 banksel SSPCON2
558 bcf SSPCON2,ACKDT ; send ACK
559 bsf SSPCON2,ACKEN
560 btfsc SSPCON2,ACKEN
561 goto $-1
562
563 banksel PIR1
564 bcf PIR1,SSPIF
565
566 banksel SSPCON2
567 bsf SSPCON2,RCEN ; read Hours from RTC
568 btfsc SSPCON2,RCEN
569 goto $-1
570 banksel SSPBUF
571 movf SSPBUF,W
572
573 banksel PORTA
574 movwf time+0xa
575 movwf time+0x9
576 movlw 0x0f
577 andwf time+0xa,f
578 movlw 0x30
579 addwf time+0xa,f
580 movlw 0x30
581 andwf time+0x9,f
582 RRF time+0x9,f
583 RRF time+0x9,f
584 RRF time+0x9,f
585 RRF time+0x9,f
586 movlw 0x30
587 addwf time+0x9,f
588 nop
589
590 banksel SSPCON2
591 bcf SSPCON2,ACKDT ; send ACK
592 bsf SSPCON2,ACKEN
593 btfsc SSPCON2,ACKEN
594 goto $-1
595
596 banksel PIR1
597 bcf PIR1,SSPIF
598
599 banksel SSPCON2
600 bsf SSPCON2,RCEN ; read day of week
601 btfsc SSPCON2,RCEN
602 goto $-1
603 banksel SSPBUF
604 movf SSPBUF,W
605
606
607 banksel SSPCON2
608 bcf SSPCON2,ACKDT ; send ACK
609 bsf SSPCON2,ACKEN
610 btfsc SSPCON2,ACKEN
611 goto $-1
612
613 banksel PIR1
614 bcf PIR1,SSPIF
615
616 banksel SSPCON2
617 bsf SSPCON2,RCEN ; read Day of month
618 btfsc SSPCON2,RCEN
619 goto $-1
620 banksel SSPBUF
621 movf SSPBUF,W
622
623 banksel PORTA
624 movwf time+0x1
625 movwf time+0x0
626 movlw 0x0f
627 andwf time+0x1,f
628 movlw 0x30
629 addwf time+0x1,f
630 movlw 0x30
631 andwf time+0x0,f
632 RRF time+0x0,f
633 RRF time+0x0,f
634 RRF time+0x0,f
635 RRF time+0x0,f
636 movlw 0x30
637 addwf time+0x0,f
638 nop
639
640 banksel SSPCON2
641 bcf SSPCON2,ACKDT ; send ACK
642 bsf SSPCON2,ACKEN
643 btfsc SSPCON2,ACKEN
644 goto $-1
645
646 banksel PIR1
647 bcf PIR1,SSPIF
648
649 banksel SSPCON2
650 bsf SSPCON2,RCEN ; read month
651 btfsc SSPCON2,RCEN
652 goto $-1
653 banksel SSPBUF
654 movf SSPBUF,W
655
656 banksel PORTA
657 movwf time+0x4
658 movwf time+0x3
659 movlw 0x0f
660 andwf time+0x4,f
661 movlw 0x30
662 addwf time+0x4,f
663 movlw 0x30
664 andwf time+0x3,f
665 RRF time+0x3,f
666 RRF time+0x3,f
667 RRF time+0x3,f
668 RRF time+0x3,f
669 movlw 0x30
670 addwf time+0x3,f
671 nop
672
673 banksel SSPCON2
674 bcf SSPCON2,ACKDT ; send ACK
675 bsf SSPCON2,ACKEN
676 btfsc SSPCON2,ACKEN
677 goto $-1
678
679 banksel PIR1
680 bcf PIR1,SSPIF
681
682 banksel SSPCON2
683 bsf SSPCON2,RCEN ; read Year
684 btfsc SSPCON2,RCEN
685 goto $-1
686 banksel SSPBUF
687 movf SSPBUF,W
688
689 banksel PORTA
690 movwf time+0x7
691 movwf time+0x6
692 movlw 0x0f
693 andwf time+0x7,f
694 movlw 0x30
695 addwf time+0x7,f
696 movlw 0xf0
697 andwf time+0x6,f
698 RRF time+0x6,f
699 RRF time+0x6,f
700 RRF time+0x6,f
701 RRF time+0x6,f
702 movlw 0x30
703 addwf time+0x6,f
704 nop
705
706 banksel PIR1
707 bcf PIR1,SSPIF
708
709 banksel SSPCON2
710 bsf SSPCON2,ACKDT ; send NACK
711 bsf SSPCON2,ACKEN
712 btfsc SSPCON2,ACKEN
713 goto $-1
714
715 return
716
717
718 I2C_stop
719 banksel SSPCON2 ; Generate I2C stop
720 bsf SSPCON2,PEN
721 btfsc SSPCON2,PEN
722 goto $-1
723
724 return
725
726 delay
727 decfsz temp1,f
728 goto $+2
729 decfsz temp2,f
730 goto delay
731 return
732
733 ;********************** Output byte in W via I2C bus ********************
734 ;************************************************************************
735 I2C_send_w
736 banksel SSPBUF ; Second byte of data (middle)
737 movwf SSPBUF
738 banksel SSPSTAT
739 btfsc SSPSTAT,R_W
740 goto $-1
741
742 return
743
744
745
746
747
748 end