-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path214505927P4.asm
959 lines (811 loc) · 24.7 KB
/
214505927P4.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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
;*******************************************************************************
; *
; Filename: Project 4 *
; Date: 25 September 2016 *
; File Version: 1 *
; Author: Muhammad Vaid *
; Company: University of Kwa-Zulu Natal *
; Description: Digital Thermometer with user set bounds *
; *
;*******************************************************************************
; Processor Inclusion
#include "p16F690.inc"
;*******************************************************************************
; CONFIG
; __config 0xFFFF
__CONFIG _FOSC_EXTRCCLK & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_ON & _IESO_ON & _FCMEN_ON
;*******************************************************************************
; Variable Definitions
;*******************************************************************************
;---------------------------------
;Delay
d1 equ 0x31 ;Used in Delay
d2 equ 0x32 ;Used in Delay
d3 equ 0x33
;---------------------------------
;Timer2
flag equ 0x34 ;Used to generate PWM
alarmOn equ 0x56
;---------------------------------
;Timer1
isFour equ 0x35
tempFlag equ 0x36
timer equ 0x37
dutyFlag equ 0x60 ;toggle alarm on and off every second
;---------------------------------
;Display
Display equ 0x38 ;Current count value
Check equ 0x39 ;Used to switch between SSDs - Only one SSD is lit per cycle
Units equ 0x40 ;Store Units value
Tens equ 0x41 ;Store Tens value
Quot equ 0x42
temp1 equ 0x43 ;Temporary register
flickSpeed equ 0x55
;---------------------------------
;Main
MODE0 equ 0x44
MODE1 equ 0x45
MODE2 equ 0x46
temp equ 0x47
buttonClicked equ 0x48
threeSec equ 0x59
;---------------------------------
;Context Saving
W_TEMP equ 0x49
STATUS_TEMP equ 0x50
;---------------------------------
;Temperature
temperature equ 0x51 ;Temperature Reading from ADC
tempLow equ 0x52 ;Lower Bound for temperature
tempHigh equ 0x53 ;Upper bound for temperature
mode equ 0x54 ;Run MODE(0), SETUP LOW MODE(1), SETUP HIGH MODE(2)
;EEPROM Variables
;----------------------
HIGH_EE_ADDR equ 0x57
LOW_EE_ADDR equ 0x58
DATA_EE_ADDR equ 0x75
DATA_EE_DATA equ 0x76
;----------------------
ORG 0x2100 ; data EEPROM location
DE 13,23 ; define first four EEPROM locations as 1, 2, 3, and 4
;23 --> 35 degrees
;13 --> 19 degrees
;5,46 --> 5 and 70
;0x60
;*******************************************************************************
;Reset Vector
;*******************************************************************************
RES_VECT CODE 0x0000 ; processor reset vector
GOTO SETUP ; go to beginning of program
;*******************************************************************************
; Interrupt Service Routines
ISR CODE 0x0004 ;interrupt vector location
;------------CONTEXT SAVING-----------------
MOVWF W_TEMP
SWAPF STATUS,W
CLRF STATUS
MOVWF STATUS_TEMP
;--------------------------------------------
Inter
BTFSS INTCON,T0IF ;Check for Timer0 Interrupt
goto CHECK_T1
goto ISR_TIMER0
CHECK_T1
BTFSS PIR1,TMR1IF ;Check for Timer1 Interrupt
goto CHECK_T2
goto TIMER1_OVERFLOW
CHECK_T2
BTFSS PIR1,TMR2IF ;Check for Timer2 Interrupt
RETFIE
goto ISR_TIMER2
;*******************************************************************************
; SETUP
;*******************************************************************************
SETUP
movlw 0x01 ;Store HIGH EEPROM address
movwf HIGH_EE_ADDR
movlw 0x00 ;Store LOW EEPROM address
movwf LOW_EE_ADDR
movlw d'30'
movwf tempHigh
movlw d'15'
movwf tempLow
BANKSEL TRISA
BCF TRISB,4 ;Tens BJT - output
BCF TRISB,5 ;Units BJT - output
BCF TRISA,2 ;Units BJT - output
BSF TRISB,7 ;Up Switch - input
BSF TRISB,6 ;Down Switch - input
MOVLW 0x00
MOVWF TRISC ;7-Segment Display - output
BANKSEL ANSEL
clrf ANSEL
clrf ANSELH
BSF ANSEL,1 ;Analog Select for Vref
BSF ANSEL,0 ;Analog select for pin RA0
BANKSEL PORTA
;--------------------------------------------------------------
;Timer 0 Interrupt
;--------------------------------------------------------------
BSF STATUS,5
MOVLW b'00000111'
; -----111 prescalar 1:256
; ----0--- prescaler assigned to timer0
; ---0---- Edge Select
; --0----- Used as a timer
; -0------ Set external interrupt off (for now)
; 0------- Disable pull-up resistors
MOVWF OPTION_REG
BCF STATUS,5
MOVLW d'215' ;REQUIRED NUMBER OF STEPS:67 (~60Hz)
MOVWF TMR0 ;INITIAL STEP VALUE
;--------------------------------------------------------------
;Timer 1 Interrupt
;--------------------------------------------------------------
BANKSEL T1CON
MOVLW b'00110001'
; -------1 Enables Timer1
; ------0- Internal Clock
; -----0-- Bit Ignored
; ----0--- Bit Ignored
; --11---- Prescalar 8
; -0------ Timer1 is on
; 0------- Bit Ignored
MOVWF T1CON
movlw 0xDC ; Start timer at 0BDCh
movwf TMR1L ; which allows 62,500
movlw 0x0B ; clocks until the
movwf TMR1H ; timer overflows.
;--------------------------------------------------------------
BANKSEL ADCON1
MOVLW b'01010000'
MOVWF ADCON1
BANKSEL ADCON0
MOVLW b'01000001'
; -------1 ADC Enabled
; ------0- ADC not in progress
; --0000-- AN0 - Channel Select
; -1------ External Vref
; 0------- Left Justified
MOVWF ADCON0
BANKSEL PORTA
;*******************************************************************************
;INITIALISE
;*******************************************************************************
BANKSEL PIE1 ;Enable Interrupt
BSF PIE1,TMR1IE
BANKSEL PIR1 ;CLEAR FLAG
BCF PIR1,TMR1IF
BANKSEL INTCON
BSF INTCON,T0IE ;Timer 0 Interrupt set on
BCF INTCON,T0IF ;clear interrupt flag
BSF INTCON,GIE ;Set Global Interrupts on
BANKSEL PORTA
;clear ports
bcf PORTA,2
bcf PORTB,5
bcf PORTB,6
bcf PORTB,7
bcf PORTB,4
;clear registers
CLRW
CLRF Check
CLRF Display
CLRF Units
CLRF Tens
CLRF Quot
CLRF temp1
CLRF isFour
CLRF tempFlag
CLRF temperature
CLRF flickSpeed
CLRF dutyFlag
;--------------------------------------------------------------
;Set registers
movlw d'220'
movwf flickSpeed
movf HIGH_EE_ADDR,w ;Read HIGH bound
call READ
movwf tempHigh
movf LOW_EE_ADDR,w ;Read Low Bound
call READ
movwf tempLow
movlw d'0'
movwf mode
movwf MODE0
movlw d'1'
movwf MODE1
movlw d'2'
movwf MODE2
movlw d'3'
movwf threeSec
clrw
;*******************************************************************************
;MACROS
;*******************************************************************************
BEQ MACRO REG1, REG2, DEST ;branch if REG1 == REG2
movf REG2, W ;W <- REG2
subwf REG1, W ;W <- REG1 - REG2
btfsc STATUS, Z ;if result was nonzero: skip out
goto DEST ;otherwise jump
ENDM
BNEQ MACRO REG1, REG2, DEST ;branch if REG1 == REG2
movf REG2, W ;W <- REG2
subwf REG1, W ;W <- REG1 - REG2
btfss STATUS, Z ;if result was nonzero: skip out
goto DEST ;otherwise jump
ENDM
BGTE MACRO REG1, REG2, DEST ;branch if REG1 >= REG2
movf REG2, W ;W <- REG2
subwf REG1, W ;W <- REG1 - REG2
btfsc STATUS, C
GOTO DEST
ENDM
BLT MACRO REG1, REG2, DEST ;branch if REG1 < REG2
movf REG2, W ;W <- REG2
subwf REG1, W ;W <- REG1 - REG2
btfss STATUS, C
GOTO DEST
ENDM
;*******************************************************************************
;MAIN PROGRAM
;*******************************************************************************
MAIN_PROG CODE
START
clrf PCLATH
BEQ mode,MODE0,RUN_MODE
BEQ mode,MODE1,Setup_LOW
goto Setup_HIGH
;************************************************
;RUN_MODE
;************************************************
RUN_MODE
movlw d'0' ;Main mode, display temp and poll buttons
movwf mode
movlw d'220'
movwf flickSpeed
call Delay05s
call Delay05s
MOVFW temperature
MOVWF Display
movlw d'10'
movwf temp
BGTE timer, temp,resetTimer
goto ALARM
resetTimer
clrf timer
clrf isFour
;----------------Alarm----------------
ALARM
movfw tempHigh
subwf temperature,w
BTFSS STATUS,C ;will skip if temp > high
GOTO CheckLow
call START_ALARM
goto UP0
CheckLow
movfw tempLow
subwf temperature,w
BTFSC STATUS,C ;will skip if temp < low
GOTO AlarmOff
call START_ALARM
GOTO UP0
AlarmOff
call STOP_ALARM
;----------------Poll Buttons----------------
UP0
BTFSS PORTB,7 ;check if Up button is pressed
goto DOWN0 ;if not check Down button
call DELAY_10ms
BTFSS PORTB,7
goto DOWN0
;UP clicked
clrf timer
clrf isFour
bsf buttonClicked,0
Check_ReleaseUP
BTFSS buttonClicked,0
GOTO JUMPUP0
bcf buttonClicked,0 ;if set - reset timer
clrf timer
clrf isFour
JUMPUP0
BGTE timer,MODE2,High_Mode ;if held up for 2s go to setup high
BTFSC PORTB,7
GOTO Check_ReleaseUP
Goto Continue0
DOWN0
BTFSS PORTB,6 ;check if Down button is pressed
goto Continue0 ;if not, go back to start of loop
call DELAY_10ms
BTFSS PORTB,6
goto Continue0
;DOWN clicked
clrf timer
clrf isFour
bsf buttonClicked,0
Check_ReleaseDOWN
BTFSS buttonClicked,0
GOTO JUMPDOWN0
bcf buttonClicked,0 ;if set - reset timer
clrf timer
clrf isFour
JUMPDOWN0
BGTE timer,MODE2,Low_Mode ;if held down for 2s go to setup LOW
BTFSC PORTB,6
GOTO Check_ReleaseDOWN
Continue0
clrf timer
clrf isFour
bcf buttonClicked,0
GOTO START
;************************************************
;Low_Mode
;************************************************
Low_Mode
call STOP_ALARM
clrf timer
clrf isFour
movlw d'1' ;change mode to 1
movwf mode
movlw d'160'
movwf flickSpeed
incf tempLow
Setup_LOW
movfw tempLow ;move tempLow to Display
movwf Display
BGTE timer,threeSec,Write ;if timer reaches 3sec, write to EEPROM and go back to run
UP1
BTFSS PORTB,7 ;check if Up button is pressed
goto DOWN1 ;if not check Down button
call DELAY_10ms
BTFSS PORTB,7
goto DOWN1
;UP clicked - reset timer and inc temp_low
clrf timer
clrf isFour
incf tempLow,1
;--------------------------------------------------------------
;If count overflows, reset W and keep temp @ 99
;--------------------------------------------------------------
movf tempLow,w
sublw d'100'
BTFSS STATUS,Z ;At 100, zero flag will be set - skip the next line and keep tempLow at 99
GOTO Continue1
movlw d'0' ;set tempLow to 99
movwf tempLow
Goto Continue1
DOWN1
BTFSS PORTB,6 ;check if Down button is pressed
goto START ;if not, go back to start of loop
call DELAY_10ms
BTFSS PORTB,6
goto START
;DOWN clicked - reset timer and dec tempLow
clrf timer
clrf isFour
decf tempLow,1
;--------------------------------------------------------------
;Test MSB - Will be set if underflow occurred (negative value)
;--------------------------------------------------------------
BTFSS tempLow,7 ;If tempLow is negative, MSB will be set - skip the next line and keep tempLow value at 0
Goto Continue1
movlw d'99' ;set tempLow to 99
movwf tempLow
Goto Continue1
Continue1
MOVFW tempLow ;move updated tempLow to Display
movwf Display
Check_Release1
clrf timer
clrf isFour
BTFSC PORTB,7
GOTO Check_Release1
BTFSC PORTB,6
GOTO Check_Release1
goto START
;************************************************
;High_Mode
;************************************************
High_Mode
call STOP_ALARM
clrf timer
clrf isFour
movlw d'2' ;change mode to 2
movwf mode
movlw d'160'
movwf flickSpeed
decf tempHigh
Setup_HIGH
movfw tempHigh ;move tempHigh to Display
movwf Display
BGTE timer,threeSec,Write
UP2
BTFSS PORTB,7 ;check if Up button is pressed
goto DOWN2 ;if not check Down button
call DELAY_10ms
BTFSS PORTB,7
goto DOWN2
;UP clicked - reset timer and inc tempHigh
clrf timer
clrf isFour
incf tempHigh,1
;--------------------------------------------------------------
;If count overflows, reset W and keep temp @ 99
;--------------------------------------------------------------
movf tempHigh,w
sublw d'100'
BTFSS STATUS,Z ;At 100, zero flag will be set - skip the next line and keep tempHigh at 99
GOTO Continue2
movlw d'0' ;keep tempHigh at 99
movwf tempHigh
Goto Continue2
DOWN2
BTFSS PORTB,6 ;check if Down button is pressed
goto START ;if not, go back to start of loop
call DELAY_10ms
BTFSS PORTB,6
goto START
clrf timer
clrf isFour
decf tempHigh,1
;--------------------------------------------------------------
;Test MSB - Will be set if underflow occurred (negative value)
;--------------------------------------------------------------
BTFSS tempHigh,7 ;If tempHigh is negative, MSB will be set - skip the next line and keep tempHigh value at 0
Goto Continue2
movlw d'99' ;set tempLow to 99
movwf tempLow
Goto Continue2
;DOWN clicked - reset timer and dec temp_high
Continue2
MOVFW tempHigh ;move updated tempHigh to Display
movwf Display
Check_Release2
clrf timer
clrf isFour
BTFSC PORTB,7
GOTO Check_Release2
BTFSC PORTB,6
GOTO Check_Release2
goto START
;************************************************
;Write to EEPROM
;************************************************
Write ;Write to EEPROM, clear timer and return to RUN_MODE
call WRITE
clrf timer
clrf isFour
goto RUN_MODE
;*******************************************************************************
;Interrupt Service Routine - Timer1 - Seconds counter
;*******************************************************************************
TIMER1_OVERFLOW
bcf T1CON,0 ;Turn the timer off.
BCF PIR1,TMR1IF
movlw 0xDC ;Start timer at 0BDCh
movwf TMR1L ;which allows 62,500
movlw 0x0B ;clocks until the
movwf TMR1H ;timer overflows.
BTFSC tempFlag,0 ;Toggle alarm on and off
goto SET_IT
BSF tempFlag,0
goto DONE1
SET_IT
BTFSC dutyFlag,0
goto SetDutyFlag
BSF dutyFlag,0
goto DONE1
SetDutyFlag
BCF dutyFlag,0
BCF tempFlag,0
DONE1
incf isFour
movfw isFour
sublw d'2'
btfss STATUS,Z ;if isFOur = 2, Count++ - Every 2 interrupts one sec passes
goto NotFour
clrf isFour
incf timer
NotFour
bsf T1CON,0 ;Turn the timer back on.
;-----------Context Saving-----------------
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
;-------------------------------------------
goto CHECK_T2
;*******************************************************************************
;Interrupt Service Routine - TIMER2 - PWM
;*******************************************************************************
ISR_TIMER2
BTFSC flag,0 ;Used to toggle pin using flag - bit-banging
goto SET_IT2
BCF PORTA,2
BSF flag,0
goto DONE2
SET_IT2
BSF PORTA,2
BCF flag,0
DONE2
BANKSEL PIR1
BCF PIR1,TMR2IF ;Clear Flag
BANKSEL PORTA
;-----------Context Saving-----------------
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
;-------------------------------------------
RETFIE
;*******************************************************************************
;Interrupt Service Routine - Timer0 - Multiplexing & Display
;*******************************************************************************
ISR_TIMER0
BTFSC mode,0
goto Cont
BTFSC mode,1
goto Cont
BSF ADCON0,GO
BTFSC ADCON0,GO ;Poll for end of conversion
GOTO $-1
MOVF ADRESH,W
MOVWF temperature ;Store value
Cont
MOVFW Display ;move temp value to Display
Call Binary2BCD ;call method to convert binary to BCD
;Tens and Units are switched on by sending a signal to the base of the corresponding BJT
;If Check = 0 - Turn Tens On
;If Check = 1 - Turn Units On
BTFSC Check,0
GOTO DispUnits
DispTens
BCF PORTB,4 ;Units OFF
movlw HIGH SSD_Output ;Avoid PCL Rollover
movwf PCLATH
MOVF Tens,w ;move tens value into w
call SSD_Output ;Get SSD output value
MOVWF PORTC ;output Tens to SSD
BSF Check,0
BSF PORTB,5 ;Tens ON
GOTO SET_TIMER ;skip DispUnits
DispUnits
BCF PORTB,5 ;Tens OFF
movlw HIGH SSD_Output ;Avoid PCL rollover
movwf PCLATH
MOVF Units,w ;move units value into w
call SSD_Output ;Get SSD output value
MOVWF PORTC
BCF Check,0
BSF PORTB,4 ;Units ON
SET_TIMER
;-------------RESET TIMER0------------------
MOVFW flickSpeed
MOVWF TMR0 ;Reset step value
BCF INTCON,T0IF ;Clear Interrupt Flag
;-------------------------------------------
;-----------Context Saving-----------------
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
;-------------------------------------------
goto CHECK_T1
;*******************************************************************************
;ALARM
;*******************************************************************************
START_ALARM
;----Duty Cycle----
BTFSC dutyFlag,0
goto STOP_ALARM
;------------CONTEXT SAVING-----------------
MOVWF W_TEMP
SWAPF STATUS,W
CLRF STATUS
MOVWF STATUS_TEMP
;--------------------------------------------
movlw d'1'
movwf temp
BEQ alarmOn, temp, DONT_START ;Flag is set? --> ALARM already on --> skip
BANKSEL TRISB
BCF TRISA,2
BANKSEL PIE1
BSF PIE1,TMR2IE
BANKSEL T2CON
MOVLW b'00000101' ;Configure TMR2 @ 440Hz
MOVWF T2CON
BANKSEL PIR1
BCF PIR1,TMR2IF
BANKSEL INTCON
BSF INTCON,PEIE
BANKSEL PORTA
BSF alarmOn,0
DONT_START
;-----------Context Saving-----------------
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
;-------------------------------------------
return
STOP_ALARM
BANKSEL TRISB ;Disable pin
BSF TRISA,2
BANKSEL PIE1 ;Disable Interrupt
BCF PIE1,TMR2IE
BANKSEL PIR1 ;Clear Flag
BCF PIR1,TMR2IF
BANKSEL PORTA
BCF alarmOn,0
;-----------Context Saving-----------------
SWAPF STATUS_TEMP,W
MOVWF STATUS
SWAPF W_TEMP,F
SWAPF W_TEMP,W
;-------------------------------------------
return
;*******************************************************************************
;0.5S DELAY
;*******************************************************************************
DELAY_0.5s
;499994 cycles
movlw 0x08
movwf d1
movlw 0x2F
movwf d2
movlw 0x03
movwf d3
Delay_01
decfsz d1, f
goto $+2
decfsz d2, f
goto $+2
decfsz d3, f
goto Delay_01
;6 cycles
goto $+1
goto $+1
goto $+1
return
;*******************************************************************************
;10MS DELAY
;*******************************************************************************
DELAY_10ms
;9993 cycles
movlw 0xE7
movwf d1
movlw 0x04
movwf d2
Delay
decfsz d1, f
goto Delay
decfsz d2, f
goto Delay
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;*******************************************************************************
;SSD Binary corresponding to PORTC configuration
;*******************************************************************************
;Common Cathode SSD
SSD_Output
addwf PCL
retlw b'01011111' ;0
retlw b'01000100' ;1
retlw b'01101011' ;2
retlw b'01101110' ;3
retlw b'01110100' ;4
retlw b'00111110' ;5
retlw b'00111111' ;6
retlw b'01001100' ;7
retlw b'01111111' ;8
retlw b'01111110' ;9
;*******************************************************************************
;BINARY TO BCD CONVERSION
;*******************************************************************************
Binary2BCD
movwf temp1 ;store input
bcf STATUS, C ;If Input>99, exit subroutine
sublw d'99' ;if number is less than 99 then carry flag will be set
btfss STATUS, C ;Only if carry flag set, continue with conversion
goto EndConv
Begin
movlw d'0' ;initialise: quotient = 0
movwf Quot
movlw d'10' ;store 10 in W register
Divide ;keep subtracting until value reaches/passes zero
incf Quot,1 ;quotient += 1
subwf temp1,1 ;temp1 -= 10
;check if result is positive
btfsc STATUS, C ;if carry flag not set, jump back to divide
goto Divide
btfsc STATUS, Z ;if zero flag not set, jump back to divide
goto Divide
EndDivide ;subtracted one time too many. Dec quotient and add 10 back to value
decf Quot,1 ;quotient -= 1
addwf temp1,1 ;value += 10
MOVFW Quot ;Copy quotient value to Tens register
MOVWF Tens
MOVFW temp1 ;Copy remainder value to Units register
MOVWF Units
EndConv
return
;*******************************************************************************
;0.5MS DELAY
;*******************************************************************************
Delay05s
;99993 cycles
movlw 0x1E
movwf d1
movlw 0x4F
movwf d2
Delay05s_0
decfsz d1, f
goto $+2
decfsz d2, f
goto Delay05s_0
;3 cycles
goto $+1
nop
;4 cycles (including call)
return
;*******************************************************************************
;Read from EEPROM
;*******************************************************************************
READ
BANKSEL EEADR
MOVWF EEADR ;Data Memory
BANKSEL EECON1 ;Address to read BANKSEL EECON1
BCF EECON1, EEPGD ;Point to DATA memory
BSF EECON1, RD ;EE Read
BANKSEL EEDAT
MOVF EEDAT, W ;W=EDAT
BCF STATUS, RP1 ;Bank 0
return
;*******************************************************************************
;Write to EEPROM
;*******************************************************************************
WRITE
MOVLW d'1'
MOVWF temp
BNEQ mode,temp,WRITE_HIGH ;Check which mode value need to be written
WRITE_LOW
MOVF LOW_EE_ADDR, W
MOVWF DATA_EE_ADDR
MOVF tempLow, W
MOVWF DATA_EE_DATA
GOTO CONTINUE_WRITE
WRITE_HIGH
MOVF HIGH_EE_ADDR, W
MOVWF DATA_EE_ADDR
MOVF tempHigh, W
MOVWF DATA_EE_DATA
GOTO CONTINUE_WRITE
CONTINUE_WRITE
BANKSEL EEADR
MOVF DATA_EE_ADDR, W
MOVWF EEADR ;Data Memory Address to write
MOVF DATA_EE_DATA, W
MOVWF EEDAT ;Data Memory Value to write
BANKSEL EECON1
BCF EECON1, EEPGD ;Point to DATA memory
BSF EECON1, WREN ;Enable writes
BCF INTCON, GIE ;Disable INTs.
BTFSC INTCON, GIE
GOTO $-2
MOVLW 0x55
MOVWF EECON2 ;Write 55h
MOVLW 0xAA
MOVWF EECON2 ;Write AAh
BSF EECON1, WR ;Set WR bit to begin write
BSF INTCON, GIE ;Enable INTs.
BCF EECON1, WREN ;Disable writes
BANKSEL 0x00 ;Bank 0
END_WRITE
return
;*******************************************************************************
END
;*******************************************************************************