ADC__Multiplex.asm


1
;
2
;    Author: 
3
;    
4
;
5
6
      LIST P=18F452        ; directive to define processor
7
      #include <P18F452.INC>    ; processor specific variable definitions
8
9
    
10
      CONFIG OSC = HS              ; HS oscillator
11
      CONFIG CP0 = OFF      ; code protect disabled
12
      CONFIG WDT = OFF      ; Watch Dog Timer disabled
13
      CONFIG LVP = OFF      ; Single-Supply ICSP disabled
14
15
      wait_reg  EQU  0x20    ; holds starting value for waiting counter
16
              
17
18
      ORG 0x0000        ; reset vector
19
      GOTO init
20
21
      ORG 0x0008        ; HP interrupt vector
22
      RETFIE          ; return from ISR
23
24
      ORG 0x0018        ; LP interrupt vector
25
      RETFIE          ; return from ISR
26
27
28
init     CLRF  PORTD, 0
29
      CLRF   TRISD, 0    ; PORTD all outputs
30
      
31
      CLRF  PORTE, 0
32
      SETF  TRISE, 0
33
      BCF    TRISE, 0, 0
34
      BCF    TRISE, 1, 0    ; only RE0 and RE1 are outputs 
35
      
36
      CLRF    PORTB, 0
37
      BSF    TRISB,0,0    ; only RB0 is input
38
      
39
      CLRF    ADCON1
40
      MOVLW   0x80      ; VSS and VDD are reference voltages for ADC and...
41
      MOVWF   ADCON1,0    ; only AN0 (RA0) is analog
42
      
43
44
start    BTFSC PORTB,0,0
45
      GOTO init_AN1
46
      GOTO init_AN0    
47
48
      
49
init_AN0  
50
      CLRF  ADCON0      ; select AN0 (RA0), ADC off (for now)  
51
      MOVLW   0x41
52
      MOVWF   ADCON0,0  
53
      
54
      BSF ADCON0,2,0       ; start operation
55
56
retry0    BTFSC  ADCON0,2,0  ; poll for GO/DONE bit to be cleared
57
      GOTO  retry0      ; if not cleared, poll again
58
59
      GOTO output
60
61
init_AN1    
62
63
      CLRF  ADCON0      ; select AN0 (RA0), ADC off (for now)  
64
      MOVLW   0x49
65
      MOVWF   ADCON0,0  
66
      
67
      BSF ADCON0,2,0       ; start operation
68
69
retry1    BTFSC  ADCON0,2,0  ; poll for GO/DONE bit to be cleared
70
      GOTO  retry1      ; if not cleared, poll again
71
    
72
    
73
      GOTO output
74
75
76
output    MOVFF  ADRESH, PORTE  ; Bits 9 and 8 of result to PORTE<1:0>
77
      MOVFF  ADRESL, PORTD  ; Bits 7 to 0 of result to PORTD<7:0>  
78
79
;check     GOTO check      
80
81
wait    SETF wait_reg,0
82
      NOP
83
      NOP
84
      NOP
85
      NOP
86
      NOP
87
      NOP
88
      NOP
89
      NOP
90
      DECFSZ wait_reg,0
91
      GOTO wait
92
      GOTO start
93
94
      
95
  END