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.
I've moved a couple things around and added OSCCON

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

    cblock    0x00
    Count
    advtemp
    L2T
    endc    
    
    org 0x00            ; reset vector

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

    goto Main
    org 0x08            ;  ISR Low vector
    goto ISR
    org 0x18            ;  ISR Low vector
    goto ISR
Main      movlw     0x72         ; 8MHz clock select
      movwf     OSCCON
    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
 
Dude i cant seem to get the delay right. I used the Delay Code Generator to make the below and it doesnt work or maybe im doing something wrong?

Code:
; Delay = 0.2 seconds
; Clock frequency = 8 MHz

; Actual delay = 0.2 seconds = 400000 cycles
; Error = 0 %

	cblock
	d1
	d2
	d3
	endc

Delay
			;399992 cycles
	movlw	0x35
	movwf	d1
	movlw	0xE0
	movwf	d2
	movlw	0x01
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;4 cycles
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	return

; Generated by http://www.piclist.com/cgi-bin/delay.exe (December 7, 2005 version)
; Fri Mar 14 23:43:27 2008 GMT

; See also various delay routines at http://www.piclist.com/techref/microchip/delays.htm

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

    cblock    0x00
    Count
    advtemp
    L2T
	d1
	d2
	d3
    endc    
    
    org 0x00            ; reset vector

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

    goto Main
    org 0x08            ;  ISR Low vector
    goto ISR
    org 0x18            ;  ISR Low vector
    goto ISR
Main 
	movlw    0x72         ; 8MHz clock select
    movwf    OSCCON
    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
Delay
			;399992 cycles
	movlw	0x35
	movwf	d1
	movlw	0xE0
	movwf	d2
	movlw	0x01
	movwf	d3
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	$+2
	decfsz	d3, f
	goto	Delay_0

			;4 cycles
	goto	$+1
	goto	$+1

			;4 cycles (including call)
	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

But not working
 
On the bright side this is my final 4 button pad design. Since its for testing on various projects i will not doll it up with a case and such.

**broken link removed**

I made the connector from some old parts off an old motherboard i had around.
 
AtomSoft said:
Dude i cant seem to get the delay right. I used the Delay Code Generator to make the below and it doesnt work or maybe im doing something wrong?

And no wonder. You can't use "goto $+2" on an 18F. You must change those to "goto $+6". The delay calculator was made for 16F's. So of course your delays are completely screwed up. :D

Like this:
Code:
Delay	movlw	0x23			;2 second delay @8MHz
	movwf	d1
	movlw	0xb9
	movwf	d2
	movlw	0x09
	movwf	d3
Delay_0	decfsz	d1,F
	goto	$+6
	decfsz	d2,F
	goto	$+6
	decfsz	d3,F
	goto	Delay_0
	return

Ya, unless I need precision I always strip the excess clutter (those little fine-tuning goto's and nops) out of delays made by the piclist generator. Even if I do need precision I still clean it up. The comments are splattered all over the place and the labels are on the line before the code. They can almost always be on same line as code (depends on length of label).
 
Last edited:
Ok got it at 100ms heres the delay:
Code:
Delay
			;199993 cycles
	movlw	0x3E
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+6
	decfsz	d2, f
	goto	Delay_0

			;3 cycles
	goto	$+2
	nop

			;4 cycles (including call)
	return

Thanks a bunch i didnt know that :D
 
AtomSoft said:
Ok got it at 100ms heres the delay:
Code:
Delay
			;199993 cycles
	movlw	0x3E
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0	decfsz	d1, f
	goto	$+6
	decfsz	d2, f
	goto	Delay_0

			;3 cycles
	goto	$+2
	nop

			;4 cycles (including call)
	return
Ya, but clean up the delay like this:
Code:
Delay	movlw	0x3E		;100ms
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+6
	decfsz	d2, f
	goto	Delay_0
	goto	$+2		;can be deleted if you don't need precision
	nop			;can be deleted if you don't need precision
	return
The generator builds nasty, cluttered looking code.
 
Last edited:
AtomSoft said:
1 silly question

will comments be in code? Like do they take up space? if not can i just comment out the code?
Comments take no space in the PIC. They are ignored by the assembler. If you want code left in your source for later use but not assembled then by all means just comment it out. Standard practice.
 
Last edited:
Thanks Then The Final Code Is:
Code:
    list p=18F1320
    include <p18F1320.inc>
    CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

    cblock    0x00
    Count
    advtemp
    L2T
	d1
	d2
	d3
    endc    
    
    org 0x00            ; reset vector

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
    ;goto Main
    ;org 0x08            ;  ISR Low vector
    ;goto ISR
    ;org 0x18            ;  ISR Low vector
    ;goto ISR
Main 
	movlw    0x72         ; 8MHz clock select
    movwf    OSCCON
    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
Delay
	movlw	0x3E
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+6
	decfsz	d2, f
	goto	Delay_0
	goto	$+2
	nop
	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
 
Last edited:
blueroomelectronics said:
Instead of goto (long branch) try using bra +4 instead of goto +6

what would be the difference?... just asking because i never really understood why use bra instead of goto whats the advantage?
 
AtomSoft said:
what would be the difference?... just asking because i never really understood why use bra instead of goto whats the advantage?
I haven't looked this up, but if the 1320 is anything like other CPUs I've worked with there are short jumps and long jumps. Short jumps (bra) are quicker and/or smaller but have limited range. A short jump typically jumps 8-bit relative to the PC. Long jumps (goto) can jump anywhere in memory (absolute - not relative to the PC) but are a bit slower and/or take more space. They're typically a 16-bit absolute jump (maybe 12 or 14 bits on PICs - PICs are weird :D ).

EDIT: Looked it up and I'm correct except in the details. Bra is a 10 bit PC relative jump and is 1 word long. Goto is a 20 bit absolute jump and is 2 words long. Both take 2 cycles.
 
Last edited:
ok FINAL code lol
Code:
    list p=18F1320
    include <p18F1320.inc>
    CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

    cblock    0x00
    Count
    advtemp
    L2T
	d1
	d2
	d3
    endc    
    
    org 0x00            ; reset vector

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
    ;goto Main
    ;org 0x08            ;  ISR Low vector
    ;goto ISR
    ;org 0x18            ;  ISR Low vector
    ;goto ISR
Main 
	movlw    0x72         ; 8MHz clock select
    movwf    OSCCON
    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
Delay
	movlw	0x3E
	movwf	d1
	movlw	0x9D
	movwf	d2
[b]Delay_0
	decfsz	d1, f
	bra		$+4
	decfsz	d2, f
	;goto	Delay_0
	bra		$-6
	bra		$+2
	nop
	return[/b]
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:
ok FINAL code lol
Code:
Main 
	movlw    0x72         ; 8MHz clock select
    movwf    OSCCON
    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
I just realized why your code always looks like a messed up jumble when you post it. The fix is to change your tab size in MPLAB to match the 8 space tabs used by this web-site. Go to Edit/Properties and an Editor Options window will open. Switch to the 'ASM' File Types tab and change tab size from 4 to 8. Leave Keep tabs selected. Hit OK.

Now all your source code will be tabbed wrong. Every time you edit an existing source file, clean up all the excess tabs. There will be too many. On new files you'll use a single tab where you were using two.

And best of all, when you post here your code will look great instead of hideous! :D Good looking code gets you more and better help. It's hard enough reading through unfamiliar code without it being sloppy looking because the site's Code tags moved everything around.
 
Last edited:
Lets see if this is better:
Code:
	list p=18F1320
	include <p18F1320.inc>
    	CONFIG OSC = INTIO2, WDT = OFF, LVP = OFF

    	cblock    0x00
    	Count
    	advtemp
    	L2T
	d1
	d2
	d3
    	endc    
    
    	org 0x00 		; reset vector

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
    	;goto Main	
	;org 0x08           	;  ISR High vector
	;goto ISR
    	;org 0x18        	;  ISR Low vector
    	;goto ISR
Main 
	movlw    0x72 		; 8MHz clock select
    	movwf    OSCCON
    	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
Delay
	movlw	0x3E
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	bra	$+4
	decfsz	d2, f
	;goto	Delay_0
	bra	$-6
	bra	$+2
	nop
	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
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top