-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4_chan_vend.asm
1966 lines (1808 loc) · 53.9 KB
/
4_chan_vend.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
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;******************************************************************************
; *
;******************************************************************************
; *
; Filename: 4_chan_vend.ASM *
; Date: 08/05/2016 *
; File Version: 0.1 *
; *
; Author: Dave Plater *
; Copyright: Dave Plater 17 March 2017 *
; *
;******************************************************************************
; *
; Files Required: P18F4520.INC 4_chan_vend.inc *
; *
;******************************************************************************
list c=200
LIST P=18F4520 ;directive to define processor
#include "4_chan_vend.inc"
#include "p18f4520.inc" ;processor specific variable definitions
errorlevel 0
;******************************************************************************
; PIC18F4520 Configuration Bit Settings
; ASM source line config statements
; CONFIG1H
CONFIG OSC = XT ; Oscillator Selection bits (XT oscillator)
CONFIG FCMEN = OFF ; Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
CONFIG IESO = OFF ; Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
; CONFIG2L
CONFIG PWRT = ON ; Power-up Timer Enable bit (PWRT enabled)
CONFIG BOREN = ON ; Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
CONFIG BORV = 3 ; Brown Out Reset Voltage bits (Minimum setting)
; CONFIG2H
CONFIG WDT = OFF ; Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
CONFIG WDTPS = 32768 ; Watchdog Timer Postscale Select bits (1:32768)
; CONFIG3H
CONFIG CCP2MX = PORTC ; CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
CONFIG PBADEN = OFF ; PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
CONFIG LPT1OSC = OFF ; Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
CONFIG MCLRE = ON ; MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
; CONFIG4L
CONFIG STVREN = ON ; Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
CONFIG LVP = OFF ; Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
CONFIG XINST = OFF ; Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
; CONFIG5L
CONFIG CP0 = OFF ; Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
CONFIG CP1 = OFF ; Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
CONFIG CP2 = OFF ; Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
CONFIG CP3 = OFF ; Code Protection bit (Block 3 (006000-007FFFh) not code-protected)
; CONFIG5H
CONFIG CPB = OFF ; Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
CONFIG CPD = OFF ; Data EEPROM Code Protection bit (Data EEPROM not code-protected)
; CONFIG6L
CONFIG WRT0 = OFF ; Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
CONFIG WRT1 = OFF ; Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
CONFIG WRT2 = OFF ; Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
CONFIG WRT3 = OFF ; Write Protection bit (Block 3 (006000-007FFFh) not write-protected)
; CONFIG6H
CONFIG WRTC = OFF ; Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
CONFIG WRTB = OFF ; Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
CONFIG WRTD = OFF ; Data EEPROM Write Protection bit (Data EEPROM not write-protected)
; CONFIG7L
CONFIG EBTR0 = OFF ; Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
CONFIG EBTR1 = OFF ; Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
CONFIG EBTR2 = OFF ; Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
CONFIG EBTR3 = OFF ; Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
; CONFIG7H
CONFIG EBTRB = OFF ; Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)
;
;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here
ORG 0xf00000
data 0x1E1E ;Price2 - Price1
data 0x3232 ;Price4 - Price3
data 0x0105 ;Coin pulse - Hopper coin value
data 0x0000 ;Coins in byte 0 - Venderr
data 0x0000 ;Coins in byte 2 - Coins in byte 1
data 0x0000 ;Coinsr in byte 1 - Coinsr in byte 0
data 0x0000 ;Coins out1 - Coins out0
data 0x0000 ;vends2 - vends1
data 0x0000 ;vends4 - vends3
data 0x0000
;Configuration Memory 0x200000 to 0x200007
data "4_Chan01"
;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used.
; More variables may be needed to store other special function registers used
; in the interrupt routines.
CBLOCK 0x000
price1pv ;Initialized price1
price2pv ;Initialized price2
price3pv ;Initialized price3
price4pv ;Initialized price4
hopvaluepv ;Subtract this value for every coin out.
coinvaluepv ;Add this value for every coin pulse.
venderrpv ;Bit0=mot1 bit1=mot2 bit2=mot3 bit3=mot4 bit4=hopper
coinsinpv ;Non volatile coinsin low byte
coinsin1pv ;Non volatile coinsin middle byte
coinsin2pv ;Non volatile coinsin high byte
coinsinvpv ;Volatile coinsin low byte
coinsinv1pv ;Volatile coinsin high byte
coinsoutpv ;Non volatile coinsin low byte
coinsout1pv ;Non volatile coinsin middle byte
credit ;Credit in R1 increments
creditt ;Temporary credit for display and hopper
vendenable ;Bit0=mot1 bit1=mot2 bit2=mot3 bit3=mot4 set means vend
vendcost ;Store cost of current vend
vendflags ;bit 0=still credit bit 1=error bit 2=display incoin bit 3=nochange
;bit4=credisplay bit5=Buy another?
sensor ;Digital value of sensor volts
sensflags ;bit 0=1 sensor interupt bit 1=1 timer end
timcount ;Timer interupt counter
timcounth
dspaddr ;Display digit address holder
WREG_TEMP ;variable used for context saving
STATUS_TEMP ;variable used for context saving
minprice ;Lowest vend price
maxprice ;Turn off coin mech when this is reached
winterupt ;Bit 0 = display service Bit 1 = eeprom service
dispbyte ;Display data to be written, msnibble first ls second, clear after write
dispwait ;Display data read, bit 7 = busy flag, high = busy
mcstatus ;bit0=inscoin, bit1=maxprice bit2=maxcred bit3=coindisabled
;bit4=no change
togd ;Toggle display counter
hexdisp ;Least significant hex display byte
hexdisp1 ;middle hex display byte
hexdisp2 ;Most significant hex display byte
buttons ;Button decoder
motors ;Vend motor enable bits
loopr
loopv ;Loop storage
busy ;Busy flag storage
nibble1 ;lowest nibble for bcd display
nibble2
nibble3 ;msn for credit display
nibble4
nibble5 ;For 5th display digit
nibble6
nibble7
nibble8 ;Highest nibbles used only for total cash
loopnop ;Display delay loop counter
loopwsv ;Save WREG here for Display delay loop counter
wait0 ;counter registers
wait1
wait2
servswitch ;Service switch bits bit 0 = 1 service switch pressed
DATA_EE_DATA
DATA_EE_ADDR
ENDC
;_______________________________________________________________________________
;Macro definitions
pushwr macro
movff WREG, WREG_TEMP ;Save wreg in wreg_temp
endm
popwr macro
movff WREG_TEMP, WREG ;Restore wreg
endm
pushst macro
movff WREG, WREG_TEMP ;Save wreg in wreg_temp
endm
popst macro
movff WREG_TEMP, WREG ;Restore wreg
endm
clnib3 macro
clrf nibble1
clrf nibble2
clrf nibble3
endm
clnib5 macro
clrf nibble1
clrf nibble2
clrf nibble3
clrf nibble4
clrf nibble5
endm
clnib8 macro
clrf nibble1
clrf nibble2
clrf nibble3
clrf nibble4
clrf nibble5
clrf nibble6
clrf nibble7
clrf nibble8
endm
clrhex macro
clrf hexdisp
clrf hexdisp1
clrf hexdisp2
endm
clrsnc macro
clrf sensorcount
movlw 0xEE
movwf sensorcount1
movlw 0x86
movwf sensorcount2
endm
presnc macro
movlw 0xEE
movwf sensorcount1
endm
; clrf sensorcount3
; clrf sensorcount4 ;7A 12 00
;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.
ORG 0x0000
goto Main ;go to start of main code
;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.
ORG 0x0008
goto HighInt ;go to high priority interrupt routine
;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.
ORG 0x0018
goto HighInt ;go to high priority interrupt routine
;******************************************************************************
;High priority interrupt routine
;winterupt Bit 0 = display service Bit 1 = eeprom service
HighInt: bsf sensflags,1
highint1: retfie FAST
;******************************************************************************
;Write to display contents of displayd address in displaya
;******************************************************************************
;Start of main program
; The main program code is placed here.
Main: Call setports
Call initdisplay
nop
call initram
call setmaxprice
bsf vendflags,2 ;Display insert coin
call timer2s
Maincoine: call pricecheck
Maincoin1: call togdisplay ;Alternate between nochange and insert coin
Maincoin: btfsc vendflags,2 ;Bit 2 set = display insert coin
call displinsert
btfsc vendflags,3 ;Vendflags bit 3 = display no change
call displnochange
btfss PORTA, 1
call setup
call pricedisplay
call chancheck
call senserd
btfsc sensflags,0 ;If sensor interupted stop
call sensorblock ;Sensor blocked, shut down machine
bsf LATC,7 ;Turn on coin mech
bcf LATA,2 ;Turn on note reader
btfss PORTA, 4 ;Check for coin in
call coinin ;Check for coins
btfss PORTA, 5 ;Check for note in
call notein ;Check for notes
tstfsz credit ;No credit then loop
bra vendloop
btfsc venderrpv,4 ;Check if hopper is shut down
bra Maincoin1
bra Maincoin
vendloop: call pricecheck ;See if there's cash for vend.
tstfsz vendenable ;Are the vend flags set
call vend ;If button pushed vend channel
btfsc vendflags,0 ;Bit 0=1 if vend complete and credit left
call refund ;Start the change loop
btfss PORTA, 1
call setup
tstfsz credit
bra venloop
bsf vendflags,2
bra Maincoine
venloop: movff credit,WREG
call maxpriceck
btfss PORTA, 5 ;Check for coin in
call notein ;Check for notes
call togdisplayv ;Alternate between nochange and credit display
btfsc vendflags,3 ;bit 3=nochange
call displnochange
btfss vendflags,4 ;More credit display it
bra vendloop
call credisplay ;Update the display for fresh credit
bra vendloop
maxpriceck: subwf maxprice,0 ;Compare credit to maximum price
bc onmech
offmech: bcf LATC,7 ;Turn off coin & note reader
bsf LATA,2
return
onmech: bsf LATC,7 ;Turn on coin & note reader
bcf LATA,2
return
togdisplayv: btfss INTCON,2 ;If bit 2 = 1 timer finished
return
call timer1.6s ;Reset timer
btfss venderrpv,4 ;Check if hopper empty
bra togdispv2
incf togd
btfss togd,0
bra togdispv1 ;Display credit
bcf vendflags,4 ;Alternate display bit 4 = credit display
bsf vendflags,3 ;Bit 3 high = display no change
return ;Display "No Change"
togdispv1: bsf vendflags,4 ;Alternate display bit 4 = credit display
bcf vendflags,3 ;If bit 3 high = display no change
return
togdispv2: bcf vendflags,3
return
togdisplay: btfss INTCON,2 ;If bit 2 = 1 timer finished
return
call timer1.6s ;Reset timer
incf togd
btfss venderrpv,4
return
btfsc togd,0
bra togdisp1 ;Display "Insert Coin"
bcf vendflags,2 ;Alternate display bit 2 = insert coin
bsf vendflags,3 ;Bit 3 high = display no change
return ;Display "No Change"
togdisp1: bsf vendflags,2 ;Alternate display bit 2 = insert coin
bcf vendflags,3 ;Bit 3 high = display no change
return
chancheck: movlw 0xF0 ;Check if any channels still work
iorwf venderrpv,0 ;Or in venderrpv to WREG
comf WREG
bz chancheck1
return
chancheck1: call displempty ;All channels empty.
chancheck2: btfss PORTA, 1
call setup
bra chancheck2
pricecheck: clrf vendenable
movff price1pv,WREG
cpfslt credit ;Skip if credit less than price
bsf vendenable,0 ;Set the enable flag
movff price2pv,WREG
cpfslt credit
bsf vendenable,1
movff price3pv,WREG
cpfslt credit
bsf vendenable,2
movff price4pv,WREG
cpfslt credit
bsf vendenable,3
movff venderrpv,WREG
comf WREG
andwf vendenable, 1 ;Unset buttons with error
btfss vendenable,0
bcf LATD,0
btfss vendenable,1
bcf LATD,1
btfss vendenable,2
bcf LATD,4
btfss vendenable,3
bcf LATD,5
return
vend: clrf motors
btfsc vendenable,0
call svend1
btfsc vendenable,1
call svend2
btfsc vendenable,2
call svend3
btfsc vendenable,3
call svend4
return
svend1: bsf LATD,0 ;Button light one on RD0
movff price1pv,vendcost
btfsc PORTD, 2 ;Button one on RD2 - motor RC2
return
clrf LATD
bsf LATD,0 ;Button light one on RD0
bsf motors,0 ;Turn on motor flag
bra startvend
svend2: bsf LATD,1 ;Button light two on RD1
movff price2pv,vendcost
btfsc PORTD, 3 ;Button two on RD3 - motor RC3
return
clrf LATD
bsf LATD,1 ;Button light two on RD1
bsf motors,1
bra startvend
svend3: bsf LATD,4 ;Button light three on RD4
movff price3pv,vendcost
btfsc PORTC, 4 ;Button three on RC4 - motor RC0
return
clrf LATD
bsf LATD,4 ;Button light three on RD4
bsf motors,2
bra startvend
svend4: bsf LATD,5 ;Button light one on RD5
movff price4pv,vendcost
btfsc PORTC, 5 ;Button four on RC5 - motor RC1
return
clrf LATD
bsf LATD,5 ;Button light one on RD5
bsf motors,3
bra startvend
sensorerr: clrf vendcost ;Sensor error shut down machine
movlw 0x0F
iorwf venderrpv ;Set disable all channels
bsf vendflags,1 ;error set flag bit 1 100% refund
errorvend1: movlw venderr
movwf EEADR ;Address of venderr storage
movff venderrpv,EEDATA ;Write error flags
call eewrite ;Write updated value to eeprom
bsf vendflags,0 ;credit leftover set vendflags bit 0
bsf vendflags,4 ;More credit display it
clrf LATD
clrf togd
return
errorvend: movff motors,WREG
iorwf venderrpv ;Combine error into volatile venderr flags
bra errorvend1
startvend: bcf LATC,7 ;Turn off coin & note reader
bsf LATA,2
call displwait ;Display "Wait"
call storecoins ;Update eeprom coins in
clrf vendflags ;Reset vendflags at start of vend.
bsf vendflags,4 ;More credit display it
bcf vendflags,3 ;Don't display No Change
call senserd ;Read optic sensor
btfsc sensflags,0 ;If bit 0 = 0 continue with vend
bra sensorerr ;Sensor blocked
movff motors, PORTC ;Start to vend
call delay.1s ;Wait for 100mS for motor start
call timer2s ;Start the timer, monitor sensflags bit 1
startvend1: call senserd ;Read optic sensor
btfsc sensflags,0 ;Bit 0 = 1 when beam is broken
bra vendend
btfsc INTCON,2 ;If bit 2 set in INTCON timer finished
bra motorsoff ;Turn off motor wait for drop
bra startvend1 ;Loop until beam broken or timer finished
motorsoff: clrf LATD ;Switch off button light
tstfsz LATC ;If motor is off and nothing dropped we have a problem
bra motoroff
bra errorvend
motoroff: clrf LATC ;Switch motors off
call timer2s ;Start the timer again
bra startvend1 ;Wait for product to drop
vendend: clrf LATC ;Turn off motor at end of sucessfull vend
movff vendcost,WREG
subwf credit
bc vendend1
clrf credit ;Negative result clear credit
vendend1: tstfsz credit
bsf vendflags,0 ;credit leftover set vendflags bit 0
clrf vendenable ;Reset all vendenable flags
call updatevend ;Update vend counters
clrf togd
return
btfsc venderrpv,4 ;If bit 4 is set hopper error
refund: return
; btfsc vendflags,1 ;Bit 1 set = error vend 100% refund
bra refundall
clrf vendflags ;Reset vendflags
call pricecheck ;Check if enough funds
tstfsz vendenable ;Is there enough cash to vend?
bra refund0 ;Wait for fresh vend alternate display
bra refundall ;Just pay the money
refund0: movlw 0x09
movwf loopv ;Alternate display 9 times
refund1: call timer1s ;Start 1 second timer
call credisplay ;Display credits
refund2: call vend ;Wait for fresh vend
tstfsz vendflags ;Check for successful vends
bra refund ;Start the whole process again
btfsc INTCON,2 ;If bit 1 = 1 timer finished
bra step1
bra refund2 ;Loop until timer finished
step1: decf loopv ;Decrement counter
bz refundall ;Just pay the money
call timer1s ;Start 1 second timer
call displpushbutton ;Display "Push Button"
refund3: call vend ;Wait for fresh vend
tstfsz vendflags
bra refund ;Start the whole process again
btfsc INTCON,2 ;If bit 1 = 1 timer finished
bra step2
bra refund3 ;Loop until timer finished
step2: decf loopv ;Decrement counter
bz refundall ;Just pay the money
call timer1s ;Start 1 second timer
call displbuymore ;Display "Buy Again?"
refund4: call vend ;Wait for fresh vend
tstfsz vendflags ;If vendflags are set a vend has taken place
bra refund ;Start the whole process again
btfsc INTCON,2 ;If bit 1 = 1 timer finished
bra step3
bra refund4 ;Loop until timer finished
step3: decf loopv
bnz refund1
refundall: movff hopvaluepv,WREG ;Value of hopper coin
clrf vendflags ;Reset vendflags
bsf vendflags,4 ;Display credit
clrf creditt ;Creditt = number of coins to vend
cpfslt credit ;Not enough credit left for one coin
bra payout ;credit is => hopper coin.
return ;credit is < hopper coin
payout: incf creditt ;Payout at least one coin
movff hopvaluepv,WREG ;Value of hopper coin
subwf credit ;Subtract one coin from credit
bz payout0 ;Pay out time
cpfslt credit ;Not enough credit left for one more coin
bra payout
payout0: ;btfsc PORTA,3 ;Test if hopper switch works
; bra hoperror ;Hopper switch problem
bsf PORTC,6 ;Hopper motor on RC6 on
btfsc PORTA,3 ;Test if coin in switch
bra payout3 ;Hopper switch open coin going through
payout1: call delay.1s ;Debounce
call timer1.6s ;1.6 second time out
payout2: btfsc PORTA,3 ;Test if hopper switch open
bra payout3 ;Hopper switch open coin going through
btfsc INTCON,2 ;If bit 2 set in INTCON timer finished
bra hoperror ;Hopper timeout
bra payout2 ;Carry on looping wait for coin
payout3: call delay.1s ;debounce
payout4: call timer1s ;Allow 1 Second for coin to pass
payout5: btfss PORTA,3 ;Test if hopper switch closed
bra coinpast ;Coin exit
btfsc INTCON,2 ;If bit 2 set in INTCON timer finished
bra hoperror ;Hopper timeout coin jam
bra payout5 ;Carry on looping
coinpast: infsnz coinsoutpv ;Increment coins out counter
incf coinsout1pv ;Increment high byte
decfsz creditt ;Paying finished if creditt = 0
bra payout1
bcf PORTC,6 ;Hopper motor on RC6 off
movlw coinsout ;Address of coins out storage
movwf EEADR ;Address of coins out storage
movff coinsoutpv,EEDATA ;coins out lower byte
call eewrite ;Write updated value to eeprom
incf EEADR
movff coinsout1pv,EEDATA ;Coins out upper byte
call eewrite
clrf togd
tstfsz credit
bsf vendflags,4 ;More credit display it
return
hoperror: bsf venderrpv,4 ;Bit 4 = hopper shut down
bcf PORTC,6 ;Hopper motor on RC6 off
movff hopvaluepv,WREG
tstfsz creditt
bra hoperrora ;add coins to credit
hoperrore: movlw venderr
movwf EEADR
movff venderrpv,EEDATA ;Shut down hopper for ever
call eewrite
clrf togd
return
hoperrora: addwf credit
decfsz creditt
bra hoperrora
bra hoperrore
updatevend: movlw vends1 ;Address of vends3 storage
btfsc motors,0 ;Motors 0 = vends3
bra updatevend1
movlw vends2 ;Address of vends4 storage
btfsc motors,1 ;Motors 1 = vends4
bra updatevend1
movlw vends3 ;Address of vends1 storage
btfsc motors,2 ;Motors 2 = vends1
bra updatevend1
movlw vends4 ;Address of vends2 storage
updatevend1: movwf EEADR ;Address of vends storage
call eeread
incf EEDATA ;Increment number of vends
call eewrite ;Write updated value to eeprom
return
storecoins: movlw coinsin ;Update non volatile eeprom coinsin
movwf EEADR
movff coinsinpv,EEDATA
call eewrite
movlw coinsin1
movwf EEADR
movff coinsin1pv,EEDATA
call eewrite
movlw coinsin2
movwf EEADR
movff coinsin2pv,EEDATA
call eewrite
movlw coinsinv ;Update volatile eeprom coinsin
movwf EEADR
movff coinsinvpv,EEDATA
call eewrite
movlw coinsinv1
movwf EEADR
movff coinsinv1pv,EEDATA
call eewrite
return
servin: clrf servswitch
btfsc PORTA, 1
return ;Service switch not pressed
bsf servswitch,0
servin1: btfss PORTA, 1 ;Debounce and make service switch usable
bra servin1
call delay.1s
btfss PORTA, 1
bra servin1
return
setup: btfss PORTA, 1
bra setup
bcf LATC,7 ;Turn off coin & note mechs
movlw venderr
call clearee
call displauditmode
call delay1s
setup1: call butin
clrf WREG
btfsc buttons, 0
call priceset
btfsc WREG,0
bra setup
btfsc buttons,1
call sensdisplay
btfsc WREG,1
bra setup
btfsc buttons,2
call auditmode
btfsc WREG,2
bra setup
btfsc buttons,3
call sethop
btfsc WREG,5
bra setup
call servin
btfss servswitch,0
bra setup1
reset
sethop: call displhop
clrhex
movlw hopvalue
call eeread
movff EEDATA,hexdisp
call disphex
sethop1: call butin
clrf WREG
btfsc buttons,0
call dechop
btfsc buttons,1
call inchop
tstfsz WREG
bra sethop
btfss buttons,2
bra sethop1
call delay1s
retlw 0x20
dechop: call delay.2s
call butin
tstfsz buttons
bra dechop
decf EEDATA
call eewrite
retlw 0x01
inchop: call delay.2s
call butin
tstfsz buttons
bra inchop
incf EEDATA
call eewrite
retlw 0x02
auditmode: call displaudit
call delay1s
auditmode1: call butin
clrf WREG
btfsc buttons,0
call displaytot ;Display clearable totals
btfsc WREG,0
bra auditmode
btfsc buttons,1
call displaytotc ;Display clearable totals
btfsc WREG,1
bra auditmode
btfsc buttons,2
call displayvends ;Display vend totals
btfsc WREG,2
bra auditmode
btfss buttons,3
bra auditmode1
auditmodee: call butin
call delay1s
retlw 0x04
displaytot: clrhex
movff coinsin2pv,hexdisp2
movff coinsin1pv,hexdisp1
movff coinsinpv,hexdisp
call displcash
call disphex
displaytot1: call butinf
tstfsz buttons
bra displaytot1
call delay1s
retlw 0x01
displaytotc: call displtotals
call delay.5s
displaytotc1: call butin
clrf WREG
btfsc buttons,0
call cleartot
btfsc WREG,2
bra displaytotc
btfsc buttons,1
call totadisp
btfsc WREG,0
bra displaytotc
btfsc buttons,2
call coutdisp
btfsc WREG,3 ;If WREG not zero refresh display
bra displaytotc
btfss buttons,3 ;Button 4 exit
bra displaytotc1
call delay1s
clrf buttons
retlw 0x02
totadisp: clrf hexdisp2
movlw coinsinv
call eeread
movff EEDATA,hexdisp
movlw coinsinv1
call eeread
movff EEDATA,hexdisp1
call displcash
call disphex
totadispe: call butinf
call delay.5s
tstfsz buttons
bra totadispe
call delay1s
retlw 0x01
coutdisp: call displcoinsout
clrf hexdisp2
movlw coinsout
call eeread
movff EEDATA,hexdisp
movlw coinsout1
call eeread
movff EEDATA,hexdisp1
call disphex
coutdispe: call butinf
call delay.5s
tstfsz buttons
bra coutdispe
call delay1s
retlw 0x08
displayvends: call displvends
call delay.5s
displayvends1: call butinf
displayvends2: movlw "1"
movwf dispbyte
movlw vends1
btfsc buttons,0
bra vendisplay
movlw "2"
movwf dispbyte
movlw vends2
btfsc buttons,1
bra vendisplay
movlw "3"
movwf dispbyte
movlw vends3
btfsc buttons,2
bra vendisplay
movlw "4"
movwf dispbyte
movlw vends4
btfsc buttons,3
bra vendisplay
call servin
btfss servswitch,0
bra displayvends1
call delay1s
retlw 0x04
vendisplay: movwf EEADR
call eeread
clrhex
movff EEDATA,hexdisp
movff dispbyte,WREG
call charwrite
movlw " "
call charwrite
call disphex
vendispexit: call butinf
tstfsz buttons
bra vendispexit
call delay.1s
bra displayvends
cleartot: call displcleartot
cleartot1: call butin
btfsc buttons,2
retlw 0x04
btfss buttons,1
bra cleartot1
movlw coinsinv
call clearee
movlw coinsinv1
call clearee
movlw coinsinv
call clearee
movlw coinsout
call clearee
movlw coinsout1
call clearee
movlw vends1
call clearee
movlw vends2
call clearee
movlw vends3
call clearee
movlw vends4
call clearee
call delay1s
retlw 0x04
clearee: movwf EEADR
clrf EEDATA
call eewrite
return
priceset: call displsetp
clrf vendcost
priceset1: call servin
btfsc servswitch,0
retlw 0x01
call butin
tstfsz buttons
bra priceset1
priceset2: call delay.1s
call butin
btfsc buttons,0
bra setp1
btfsc buttons,1
bra setp2
btfsc buttons,2
bra setp3
btfss buttons,3
bra priceset1
setp4: movlw price4 ;eeprom = 0xf00000
bra setprice
setp3: movlw price3
bra setprice
setp2: movlw price2
bra setprice
setp1: movlw price1
setprice: movwf credit
movwf EEADR
clrf vendcost
; movff EEDATA,vendcost
call delay.2s
call displayprice
movff credit,EEADR
setprice1: clrf credit
call butin
btfsc buttons,3 ;Check button 4
bra setpexit
btfsc PORTA, 5 ;Check for note in
bra setprice1
call notein
movf vendcost,0
movf credit,0
addwf vendcost,1 ;Increment credit store.
movf vendcost,0
movff vendcost,EEDATA
movf vendcost,0
call decprice
call butin
btfss buttons,3
bra setprice1
call delay.5s
setpexit: call butin
btfsc buttons,3
bra setpexit
call delay.2s
bra priceset
decprice: call delay.2s
call eewrite
movff EEDATA,vendcost
call displayprice
return
butinf: clrf buttons ;Fast butin
btfss PORTD, 2 ;Check button 1
bsf buttons, 0 ;Set 1st button bit
btfss PORTD, 3 ;Check button 2
bsf buttons, 1 ;Set 2nd button bit
btfss PORTC, 4 ;Check button 3
bsf buttons, 2 ;Set 3rd button bit
btfss PORTC, 5 ;Check button 4
bsf buttons, 3 ;Set 4th button bit
return
butin: clrf buttons
btfss PORTD, 2 ;Check button 1
bsf buttons, 0 ;Set 1st button bit
btfss PORTD, 3 ;Check button 2
bsf buttons, 1 ;Set 2nd button bit
btfss PORTC, 4 ;Check button 3
bsf buttons, 2 ;Set 3rd button bit
btfss PORTC, 5 ;Check button 4
bsf buttons, 3 ;Set 4th button bit
tstfsz buttons
bra butin1
retlw 0x00 ;Nothing pressed
butin1: movff PORTD,WREG ;store in WREG
andlw 0x0C ;Mask off bits 2 & 3
iorwf PORTC,0 ;combine PORTC
comf WREG ;Invert bits
andlw 0x3C ;Mask off bits 2,3,4 & 5
bnz butin1
retlw 0x00
pricedisplay: call butinf
tstfsz buttons
bra pdspgo
return
pdspgo: btfsc buttons,0
bra price1dsp
btfsc buttons,1
bra price2dsp
btfsc buttons,2
bra price3dsp
btfsc buttons,3
bra price4dsp
return ;False alarm
price1dsp: movff price1pv,vendcost
btfss venderrpv,0
bra displayprice