-
Notifications
You must be signed in to change notification settings - Fork 0
/
ADC.asm
134 lines (93 loc) · 3.68 KB
/
ADC.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
; Example 1. This sets PORTA as an INPUT (NB 1 means input)
; and PORTB as an OUTPUT (NB 0 means output). The contents of port A (5 least significant bits)are then transferred to port B
; A semi-colon means that everything on the line after the semi-colon is a comment for the benefit of humans and has no
; significance to the PIC.
;*********************************************************
; EQUATES SECTION (Note 'EQU' is a so-called 'pseudo op' meaning that it is an instruction to the assembler program only,
; NOT to the PIC). The purpose of these 'EQU' instructions is to give locations names rather than having to use the
; actual addresses in the program.
STATUS EQU 3H ;means STATUS is file 3.
OPTION_REG EQU 1H
INTCON EQU 0BH
WTEMP EQU 20H
STATEMP EQU 21H
INDEX EQU 22H
PC EQU 2H
PORTA EQU 5H ;means PORTA is file 5.
PORTB EQU 6H ;means PORTB is file 6.
TRISA EQU 85H ;TRISA (the PORTA I/O selection) is file 85H
TRISB EQU 86H ;TRISB (the PORTB I/O selection) is file 86H
ADCON1 EQU 9FH ;ADCON1 is 9FH
ADCON0 EQU 1FH
PIE1 EQU 8CH
PIR1 EQU 0CH
ADRESH EQU 1EH
LIGHTS EQU 18H
;*********************************************************
LIST P=16F819 ; we are using the 16F819. (another pseudo-op so the assembler knows what the target processor is)
;**********************************************************************
; Configuration Bits.
__CONFIG H'3F18' ;selects internal RC oscillator, WDT off, PUT on, Code Protection disabled.
;This is a special instruction to the programmer to set some special bits in the PIC.
ORG 0 ;the start address in memory is 0 ie the assembler will start arranging
;instructions from this location (this is another pseudo-op)
GOTO START ;goto start of program (this is the first 'real' instruction).
START BSF STATUS,5 ;Set accesses to Bank1.
MOVLW B'11111111' ;set PORTA to be inputs
MOVWF TRISA ;(Actually they are set as inputs anyway on reset so this isn't strictly necessary.)
MOVLW B'00000000'
MOVWF TRISB ;set all the bits in PORTB to be outputs
MOVLW B'00000000' ;Set AN0 to AN4 (ie PA0 - PA4) as digital inputs
MOVWF ADCON1
BCF STATUS,5 ;Reset accesses to Bank0 because PORTA and PORTB are in Bank 0
; Select bank 0 for ADCON0
MOVLW B'01000001' ;Activate A/D converter; Using AN0 (Channel 0); Using RC Oscillator
MOVWF ADCON0
;Starting A/D conversion
LOOP BCF PIR1,6 ;Disabling A/D conversion completion bit so conversion can take place
;BSF INTCON,7 ;Global interrupt
BSF STATUS,5 ; Moving to Bank1
;BSF PIE1,6 ;Enables the A/D converter interrupt
NOP ;
NOP
NOP
BCF STATUS,5 ;Returning to Bank0
BSF ADCON0,2 ;Initiate A/D conversion here
NOP
NOP
NOP
MOVF ADRESH,0 ;MSBs of output are moved to PORTB to be displayed on LEDs as binary
MOVWF PORTB
;GOTO LOOP
;---------------------------------------------------------------------
;EXERCISE 2
;---------------------------------------------------------------------
TABLE ADDWF PC ;W-reg assigned to PCL, corresponding to the relevant index in TABLE
;to be outputted through PORTB.
RETLW B'1111110'
RETLW B'0110000'
RETLW B'1101101'
RETLW B'1111001'
RETLW B'0110011'
RETLW B'1011011'
MOVWF LIGHTS
BTFSC LIGHTS,7
GOTO SUBROUTINE1
BTFSC LIGHTS,6
GOTO SUBROUTINE2
BTFSC LIGHTS,5
GOTO SUBROUTINE3
GOTO SUBROUTINE4
SUBROUTINE1 BTFSC LIGHTS,6
MOVLW B'00000101'
BTFSC LIGHTS,6
GOTO TABLE
MOVLW B'00000100'
GOTO TABLE
SUBROUTINE2 MOVLW B'00000011'
GOTO TABLE
SUBROUTINE3 MOVLW B'00000010'
GOTO TABLE
SUBROUTINE4 MOVLW B'00000001'
GOTO TABLE
END