Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A/D On 18F1320

Status
Not open for further replies.

AtomSoft

Well-Known Member
This is so confusing this is basically my setup but its on RB1

**broken link removed**

This is the code im trying to use or well trying to correct. Can someone help. Im using a junebug (you prob know by now) as my Test Circuit and connecting RB1 from side pins to my switch.

Code:
list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	b'00000001'
	MOVWF	ADCON1	
	MOVLW	b'11010101'
	MOVWF	ADCON0	
	MOVLW	b'00010011'
	MOVWF	ADCON2	
	MOVLW	b'11001111'
	MOVWF	TRISB	
	MOVLW	b'11111000'
	MOVWF	INTCON
	MOVLW   b'01110000'
	MOVWF	INTCON2
	MOVLW	b'00011000'
	MOVWF	INTCON3
	MOVLW   b'01000000'
	MOVWF	PIE1
LED1	LED	b'00000001',b'10111110'
LED2	LED	b'01000000',b'10111110'
LED3	LED	b'01000000',b'00111111'
LED4	LED	b'10000000',b'00111111'
LED5	LED	b'10000000',b'01111110'
LED6	LED	b'00000001',b'01111110'
	goto	LED1		;loop forever

Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return

ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	BSF ADCON0, 1
Loop1	BTFSC ADCON0, 1
	goto Loop1
	call	Delay
	call	Delay
	retfie
	END
 
Well i understand a small portion but i have 3 multiplexed switches how can i get the value from it on 1 pin? This confused me. My inpu tto the switches is 5v my steady output is 3.23 volts

When button 1 pressed
4.86V
When button 2 pressed
.09v
When button 3 pressed
2.43v

Why is it all mixed? And how would i set it so default value is not a interrupt and if it changes then interrupt?
 
i redid the buttons and now in the watch
when nothing is pressed i get

ADRESH = FF
ADRESL = C0

When Btn 1 pressed
ADRESH = 00
ADRESL = 80

When Btn 2 pressed
ADRESH = FF
ADRESL = C0

why is it the same when bt2 is pressed as no button?

**broken link removed**
 
ok now i get

when nothing is pressed i get

ADRESH = FF
ADRESL = 80

When Btn 1 pressed
ADRESH = 00
ADRESL = 80

When Btn 2 pressed
ADRESH = 80
ADRESL = 80

Would these be good results?
 
Ok sort of got it working
Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x10        ;b'00010001'	; Set RB0
	MOVWF	ADCON1		;digital I/O pin
	MOVLW	b'11001111' ;0xCF		;Value used to initialize data direction
	MOVWF	TRISB		;Set RB<3:0> as inputs RB<5:4> as outputs RB<7:6> as inputs
	MOVLW	b'11111000' ;b'11011000'
	MOVWF	INTCON
	MOVLW   b'01110000' ;b'11110101'
	MOVWF	INTCON2
	MOVLW	b'00011000' ;b'00001000'
	MOVWF	INTCON3
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
LED1	LED	b'00000001',b'10111110'
LED2	LED	b'01000000',b'10111110'
LED3	LED	b'01000000',b'00111111'
LED4	LED	b'10000000',b'00111111'
	bsf	ADCON0,GO	;go do a/d conversion
adloop	btfsc	ADCON0,DONE
	goto	adloop
	;movff	ADRESH,advtemp
	MOVLW	0x80
NoChange	CPFSGT	ADRESH
	goto	Check1
	goto	LED1		;loop forever
Check1	MOVLW	0x80
	CPFSEQ	ADRESH
	goto Check2
	goto LED6
Check2	MOVLW	0x00
	CPFSEQ	ADRESH
	goto LED1
	goto LED5
LED5	LED	b'10000000',b'01111110'
	call Delay
	call Delay
	goto LED1
LED6	LED	b'00000001',b'01111110'
	call Delay
	call Delay
	goto	LED1		;loop forever
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return

ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	call	Delay
	call	Delay
	retfie
	END

only works when holding button down i assume because its not on a interrupt?
 
Got it! When button 1 is pressed it scrolls 1 time when button 2 is pressed it scrolls 2 times
Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	L2T
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x10        ;b'00010001'	; Set RB0
	MOVWF	ADCON1		;digital I/O pin
	MOVLW	b'11001111' ;0xCF		;Value used to initialize data direction
	MOVWF	TRISB		;Set RB<3:0> as inputs RB<5:4> as outputs RB<7:6> as inputs
	MOVLW	b'11111000' ;b'11011000'
	MOVWF	INTCON
	MOVLW   b'01110000' ;b'11110101'
	MOVWF	INTCON2
	MOVLW	b'00011000' ;b'00001000'
	MOVWF	INTCON3
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
StartUp 	bsf	ADCON0,GO	;go do a/d conversion
adloop	btfsc	ADCON0,DONE
	goto	adloop
	;movff	ADRESH,advtemp
;	MOVLW	0x80
;NoChange	CPFSGT	ADRESH
	;goto	Check1
	;goto	adloop		;loop forever
	MOVLW	0x02
	MOVWF	L2T
Check1	MOVLW	0x80
	CPFSEQ	ADRESH
	goto Check2
	goto LED2T
Check2	MOVLW	0x00
	CPFSEQ	ADRESH
	goto StartUp
	goto LED1T
LED1T	
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	goto StartUp
LED2T
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	DECFSZ	L2T
	goto	LED2T
	goto	StartUp		;loop forever
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return

ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	retfie
	END
 
Got it it had to do with INTCONx again:

Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	L2T
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x10        ;b'00010001'	; Set RB0
	MOVWF	ADCON1		;digital I/O pin
	MOVLW	b'11001111' ;0xCF		;Value used to initialize data direction
	MOVWF	TRISB		;Set RB<3:0> as inputs RB<5:4> as outputs RB<7:6> as inputs
	MOVLW	b'11001000' ;b'01111000' ;b'11011000'
	MOVWF	INTCON
	MOVLW   b'11110101'  ;b'01110000' ;b'11110101'
	MOVWF	INTCON2
	MOVLW	b'11011000'  ;b'00011000' ;b'00001000'
	MOVWF	INTCON3
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
StartUp 	bsf	ADCON0,GO	;go do a/d conversion
adloop	btfsc	ADCON0,DONE
	goto	adloop
	;movff	ADRESH,advtemp
;	MOVLW	0x80
;NoChange	CPFSGT	ADRESH
	;goto	Check1
	;goto	adloop		;loop forever
	MOVLW	0x02
	MOVWF	L2T
Check1	MOVLW	0x80
	CPFSEQ	ADRESH
	goto Check2
	goto LED2T
Check2	MOVLW	0x00
	CPFSEQ	ADRESH
	goto StartUp
	goto LED1T
LED1T	
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	goto StartUp
LED2T
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	DECFSZ	L2T
	goto	LED2T
	goto	StartUp		;loop forever
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return

ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	retfie
	END

A little smaller
Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	L2T
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x10        ;b'00010001'	; Set RB0
	MOVWF	ADCON1		;digital I/O pin
	MOVLW	b'11001111' ;0xCF		;Value used to initialize data direction
	MOVWF	TRISB		;Set RB<3:0> as inputs RB<5:4> as outputs RB<7:6> as inputs
	MOVLW	b'11001000' ;b'01111000' ;b'11011000'
	MOVWF	INTCON
	MOVLW   b'11110101'  ;b'01110000' ;b'11110101'
	MOVWF	INTCON2
	MOVLW	b'11011000'  ;b'00011000' ;b'00001000'
	MOVWF	INTCON3
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
StartUp 	bsf	ADCON0,GO	;go do a/d conversion
adloop	btfsc	ADCON0,DONE
	goto	adloop
	;movff	ADRESH,advtemp
;	MOVLW	0x80
;NoChange	CPFSGT	ADRESH
	;goto	Check1
	;goto	adloop		;loop forever
	MOVLW	0x02
	MOVWF	L2T
Check1	MOVLW	0x80
	CPFSEQ	ADRESH
	goto Check2
	goto LED2T
Check2	MOVLW	0x00
	CPFSEQ	ADRESH
	goto StartUp
	goto LED1T
LED1T	
	call ScrollMe
	goto StartUp
LED2T
	call ScrollMe
	DECFSZ	L2T
	goto	LED2T
	goto	StartUp		;loop forever
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return
ScrollMe
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	return
ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	retfie
	END
 
Last edited:
OMG im so glad (for myself lol) i got it with 4 buttons.

Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	L2T
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
;	bsf	ADCON1,0	    ;make RA0 digital
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x10        ;b'00010001'	; Set RB0
	MOVWF	ADCON1		;digital I/O pin
	MOVLW	b'11001111' ;0xCF		;Value used to initialize data direction
	MOVWF	TRISB		;Set RB<3:0> as inputs RB<5:4> as outputs RB<7:6> as inputs
	MOVLW	b'11001000' ;b'01111000' ;b'11011000'
	MOVWF	INTCON
	MOVLW   b'11110101'  ;b'01110000' ;b'11110101'
	MOVWF	INTCON2
	MOVLW	b'11011000'  ;b'00011000' ;b'00001000'
	MOVWF	INTCON3
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
StartUp 	bsf	ADCON0,GO	;go do a/d conversion
adloop	btfsc	ADCON0,DONE
	goto	adloop
	;movff	ADRESH,advtemp
;	MOVLW	0x80
;NoChange	CPFSGT	ADRESH
	;goto	Check1
	;goto	adloop		;loop forever
	MOVLW	0xEE
	CPFSLT	ADRESH
	goto	StartUp
	MOVLW	0x02
	MOVWF	L2T
Check1	MOVLW	0xBF
	CPFSGT	ADRESH
	goto Check2
	MOVLW	0x04
	MOVWF	L2T
	goto LEDXT
Check2	MOVLW	0x7F
	CPFSGT	ADRESH
	goto Check3
	MOVLW	0x03
	MOVWF	L2T
	goto LEDXT
Check3	MOVLW	0x3F
	CPFSGT	ADRESH
	goto Check4
	MOVLW	0x02
	MOVWF	L2T
	goto LEDXT
Check4	MOVLW	0x00
	CPFSEQ	ADRESH
	goto StartUp
	goto LED1T
LED1T	
	call ScrollMe
	goto StartUp
LEDXT
	call ScrollMe
	DECFSZ	L2T
	goto	LEDXT
	goto	StartUp		;loop forever
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return
ScrollMe
	LED	b'00000001',b'10111110'
	LED	b'01000000',b'10111110'
	LED	b'01000000',b'00111111'
	LED	b'10000000',b'00111111'
	LED	b'10000000',b'01111110'
	LED	b'00000001',b'01111110'
	return
ISR	BCF INTCON3, 0
	BCF INTCON3, 1
	BCF INTCON, 0
	BCF INTCON, 1
	BCF INTCON, 2
	call Delay
	LED	b'00000001',b'01111110'
	call	Delay
	retfie
	END
 
Here is it commented for those who get stuck:
Code:
	list p=18F1320
	include <p18F1320.inc>
	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

	cblock	0x00
	advtemp
	L2T
	endc

LED	macro	x,y	     	;MACRO LED <PORTA>, <TRISA>
	movlw	x
	movwf	LATA		;LATA = x
	movlw	y
	movwf	TRISA		;TRISA = y
	call	Delay		;call the Delay subroutine
	endm		    	;end macro
Count	equ	0	    	;delay loop counter
	org 0x00            ; reset vector
	goto Main
	org 0x08            ;  ISR Low vector
	goto ISR
	org 0x18            ;  ISR Low vector
	goto ISR
Main
	CLRF	PORTB		;Initialize PORTB by clearing output data latches
	CLRF	LATB		;Alternate method to clear output data latches
	MOVLW	0x01        ;A/D Config
	MOVWF	ADCON1		;ADCON1: All analog except AN0
	MOVLW	b'00000010' ;TRISB Config
	MOVWF	TRISB		;Set RB1 as input RB<7:2> and RB0 as outputs
	MOVLW	b'11001000' ;INTCON Config
	MOVWF	INTCON		;Enable GIE/GIEH, PEIE/GIEL, RBIE Disable rest
	MOVLW   b'11110101' ;INTCON2 Config
	MOVWF	INTCON2		;Enable RBPU, Rising Edge Interrupts, High Priority
	MOVLW	b'11001000' ;INTCON3 Config
	MOVWF	INTCON3		;High Priority, Enable INT1 and Clear Flags
	movlw	b'00010101'	;enable A/D, AN5
	movwf	ADCON0
StartUp 	bsf	ADCON0,GO	;Start A/D conversion
adloop	btfsc	ADCON0,DONE ;Check if done. If not goto adloop else skip loop
	goto	adloop
	MOVLW	0xEE 			;Button Pressed Preset
	CPFSLT	ADRESH			;If ADRESH < Skip the goto else start over 
	goto	StartUp			;because no button pressed
Check1	MOVLW	0xBF		;4th button Preset
	CPFSGT	ADRESH			;If ADRESH > BFH then this button was pressed
	goto Check2				;If not then it wasnt pressed skip to next check
	MOVLW	0x04			
	MOVWF	L2T				;Set L2T to 0x04 (MY LOOP COUNTER)
	goto LEDXT				;Goto LEDXT which is the Light X Times where x = 4h for this button
Check2	MOVLW	0x7F		;3rd button preset
	CPFSGT	ADRESH			;If ADRESH > 7FH then this button was pressed
	goto Check3				;If not then it wasnt pressed skip to next check
	MOVLW	0x03			
	MOVWF	L2T				;Set L2T to 0x03 (MY LOOP COUNTER)
	goto LEDXT				;Goto LEDXT which is the Light X Times where x = 3h for th
Check3	MOVLW	0x3F		;Same as others
	CPFSGT	ADRESH
	goto Check4
	MOVLW	0x02
	MOVWF	L2T
	goto LEDXT
Check4	MOVLW	0x00		;Same
	CPFSEQ	ADRESH			;If ADRESH = 0 then goto LED1T because button 1 was hit
	goto StartUp			;if not then goto startup
	goto LED1T				;LED 1 Time
LED1T	
	call ScrollMe			;Scroll LEDs 1 time
	goto StartUp			;Goto Startup
LEDXT 						;LEDXT loop
	call ScrollMe			;Scroll 1 time
	DECFSZ	L2T				;Decrement L2T and skip the next line if 0
	goto	LEDXT			;goto ledxt loop
	goto	StartUp			;Goto StartUp
Delay	decfsz	Count,f	;decrement Count and skip when zero
	goto	$-2	     	;not zero? repeat
	return
ScrollMe
	LED	b'00000001',b'10111110'	; Light LED 1
	LED	b'01000000',b'10111110'	; Light LED 2
	LED	b'01000000',b'00111111'	; Light LED 3
	LED	b'10000000',b'00111111'	; Light LED 4
	LED	b'10000000',b'01111110'	; Light LED 5
	LED	b'00000001',b'01111110'	; Light LED 6
	return 						; Return from where called
ISR	BCF INTCON3, 0 				; Clear INT1
	BCF INTCON, 0				; Clear RBIF: RB Port Change Interrupt Flag bit
	LED	b'00000001',b'01111110' ; Light LED 6
	call	Delay				; 2 delays 
	call	Delay
	retfie						; Return from where called
	END
 
AtomSoft said:
yep thank god but it takes like 30 seconds to reset is that normal?

30sec seems a little long, do you mean download and rerun?
Also make sure the mode Release / Debug is correct on the MPLAB menu bar.
 
Ahh try this at the beginning of your code. Debug speed is based on oscillator speed.
Default is 32Khz (slow!)
this will set it 8MHz

Of course your delays are going to go quite a bit faster!

Code:
  movlw 0x72 ; 8MHz clock select
  movwf OSCCON
 
Last edited:
ok i will check that but what is it supposed to be (debug right) also when i alter code i usually press compile then program then reset so it starts from ORG 0 again but when i press reset it takes anywhere from 15-30 secs
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top