PIC Programming Help. Creating a counter but I have some errors in my code

Thread Starter

Liam Porter

Joined Jan 11, 2015
2
I am attempting to create a simple counter on a PIC16F628A board that will have two inputs, one to increase value, one to decrease and the values will be displayed on a 7 segment display. I have written my code but when I attempt to compile I get these error codes:

Counter.asm:42:Error[118] Overwriting previous address contents: 0x0000 Counter.asm:63:Warning[202] Argument out of range. Least significant bits used. 35 <0x23>

Can anyone help identify where the problem is?
Code:
    LIST    P=16f84A, R=Dec
    __FUSES    _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_ON
    include "P16f84A.inc"

    CBLOCK    0x20
        W_TEMP
        STATUS_TEMP
        COUNTER
        FLAGS
        FILTR1
        FILTR2
    ENDC


   
   
#DEFINE    ST_BT1    FLAGS,0   
#DEFINE    ST_BT2    FLAGS,1
   

   
   
MIN        EQU    .0 ;minimum counter value
MAX        EQU    .15 ;maximum counter value
T_FILTR    EQU    .255   
   
   
CMCON   
    movlw 0x07
    movwf CMCON
   
   
   
   
   
    ;reset
    ORG 0x00
    GOTO     start

   
   
   
   

   
    ORG    0x04
    RETFIE
   
   
   
   
   


DISP7
    MOVF    COUNTER,W
    ANDLW    B'0001111'
   
    ADDWF    PCL,FLAGS

;
    RETLW    b'11101110' ;digit 0
    RETLW    b'00101000' ;digit 1
    RETLW    b'11001101' ;digit 2
    RETLW    b'01101101' ;digit 3
    RETLW    b'00101011' ;digit 4
    RETLW    b'01100111'    ;digit 5
    RETLW    b'11100111'    ;digit 6
    RETLW    b'00101100'    ;digit 7
    RETLW    b'11101111'    ;digit 8
    RETLW    b'00101111'    ;digit 9
    RETLW    b'10101111' ;A
    RETLW    b'11100011'    ;B
    RETLW    b'11000110'    ;C
    RETLW    b'11101001'    ;D
    RETLW    b'11000111'    ;E
    RETLW    b'10000111'    ;F
   
   
   
   
   
start       
    BCF        STATUS,RP0
    MOVLW    B'00000111'
    MOVWF    CMCON   
   
    BSF        STATUS,RP0
    MOVLW    B'00000110'
    MOVWF    TRISA
   
    MOVLW    B'00000000'
    MOVWF    TRISB
    MOVLW    B'10000000'
    MOVWF    OPTION_REG
   
   
    MOVLW    B'00000000'
    MOVWF    INTCON
    BCF        STATUS,RP0
   
   
   
   
   
    CLRF    PORTA
    CLRF    PORTB
    CLRF    FLAGS
    MOVLW    MIN
    MOVWF    COUNTER
    GOTO    DISPUP
   
   
   
   
MAIN
    MOVLW    T_FILTR
    MOVWF    FILTR1
    MOVWF    FILTR2
   
CHECK_BUTTON1
    BTFSC    PORTA,1 ;is button 1 pressed
    GOTO    BUTTON1_RELEASED
                    ;yes
    DECFSZ    FILTR1,F
   
    GOTO    CHECK_BUTTON1
   
    BTFSS    ST_BT1
    GOTO    DEC
    GOTO    CHECK_BUTTON2
   
BUTTON1_RELEASED
    BCF        ST_BT1

CHECK_BUTTON2
    BTFSC    PORTA,2
    GOTO    BUTTON2_RELEASED
   
    DECFSZ    FILTR2,F
   
    GOTO    CHECK_BUTTON2

    BTFSS    ST_BT2
    GOTO    INC
    GOTO    MAIN
   
BUTTON2_RELEASED
    BCF    ST_BT2
    GOTO    MAIN

DEC
    BSF    ST_BT1
    MOVF    COUNTER,W
    XORLW    MIN
   
   
    BTFSC    STATUS,Z
    GOTO    MAIN
   
    DECF    COUNTER,F
    GOTO    DISPUP

INC
    BSF    ST_BT2
    MOVF    COUNTER,W
    XORLW    MAX
   
   
    BTFSC    STATUS,Z
    GOTO    MAIN
   
    INCF    COUNTER,F
    GOTO    DISPUP
   
DISPUP
    CALL    DISP7
   
    MOVWF    PORTB
   
   
    GOTO    MAIN
   
   
   
    END
 

JohnInTX

Joined Jun 26, 2012
4,787
Sure and welcome to AAC!
The 'overwriting' error is occurring because of the code in the snippet below:
Code:
CMCON
    movlw 0x07       ;;; These 2 lines generate code at the implied program address 0000h
    movwf CMCON;;; and 0001h
   ;reset
    ORG 0x00   ;; this resets the address counter to 0000h
    GOTO     start ;; this OVERWRITES the previous program word generated above (the movlw 0x07)
Warning [202] means that an operand has evaluated to something that won't fit in the destination. For example movlw .1000 would generate it since 1000 won't fit in 8 bits. I don't know exactly where its occurring in your code (no line numbers). I'm guessing it might be the ADDWF PCL,FLAGS - the value after the comma must evaluate to 0 or 1 (one bit) to specify the destination i.e. W or F. F is what you want here.

EDIT:
MPLAB confirms the ADDWF as the problem.
The LIST directive and include file is for a PIC16F84A but you indicate you are using a 628A.
Specifying the PIC type with the LIST directive is discouraged these days (although allowed if it matches the PIC selection in the project).
In the interrupt routine stub add clrf INTCON then RETURN (not RETFIE). If you have a stray interrupt that has no service routine, you want to disable all IRQs and not reenable them with RETFIE. Even though you correctly cleared INTCON in init, that doesn't mean that some stray pointer or unexpected address evaluation won't clobber it. Seen it happen.

I like that you've located the jump table where it doesn't span banks AND bounds-limits W to always fit the table.

Have fun.
 
Last edited:

Thread Starter

Liam Porter

Joined Jan 11, 2015
2
Hi John

Thank you very much for the help! I believe I have implemented the changes you suggested but now when I try to compile I get another error code:
Counter.asm:32:Error[116] Address label duplicated or different in second pass: "CMCON"

Again thanks very much for the help!
Code:
    LIST    P=16f628A, R=Dec
    __FUSES    _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_ON
    include "P16f628A.inc"

    CBLOCK    0x20
        W_TEMP
        STATUS_TEMP
        COUNTER
        FLAGS
        FILTR1
        FILTR2
    ENDC


   
   
#DEFINE    ST_BT1    FLAGS,0   
#DEFINE    ST_BT2    FLAGS,1
   

   
   
MIN        EQU    .0 ;minimum counter value
MAX        EQU    .15 ;maximum counter value
T_FILTR    EQU    .255   
   
   
   
   
   
   
CMCON   
    movlw 0x07
    movwf CMCON
   
   
   
   
   
    ;reset
    ORG 0x00
    GOTO     start

   
   
   
   

   
    ORG    0x04
    CLRF    INTCON
    RETURN
   
   
   
   
   


DISP7
    MOVF    COUNTER,W
    ANDLW    B'0001111'
   
    ADDWF    PCL,F

;
    RETLW    b'11101110' ;digit 0
    RETLW    b'00101000' ;digit 1
    RETLW    b'11001101' ;digit 2
    RETLW    b'01101101' ;digit 3
    RETLW    b'00101011' ;digit 4
    RETLW    b'01100111'    ;digit 5
    RETLW    b'11100111'    ;digit 6
    RETLW    b'00101100'    ;digit 7
    RETLW    b'11101111'    ;digit 8
    RETLW    b'00101111'    ;digit 9
    RETLW    b'10101111' ;A
    RETLW    b'11100011'    ;B
    RETLW    b'11000110'    ;C
    RETLW    b'11101001'    ;D
    RETLW    b'11000111'    ;E
    RETLW    b'10000111'    ;F
   
   
   
   
   
start       
    BCF        STATUS,RP0
    MOVLW    B'00000111'
    MOVWF    CMCON   
   
    BSF        STATUS,RP0
    MOVLW    B'00000110'
    MOVWF    TRISA
   
    MOVLW    B'00000000'
    MOVWF    TRISB
    MOVLW    B'10000000'
    MOVWF    OPTION_REG
   
   
    MOVLW    B'00000000'
    MOVWF    INTCON
    BCF        STATUS,RP0
   
   
   
   
   
    CLRF    PORTA
    CLRF    PORTB
    CLRF    FLAGS
    MOVLW    MIN
    MOVWF    COUNTER
    GOTO    DISPUP
   
   
   
   
MAIN
    MOVLW    T_FILTR
    MOVWF    FILTR1
    MOVWF    FILTR2
   
CHECK_BUTTON1
    BTFSC    PORTA,1 ;is button 1 pressed
    GOTO    BUTTON1_RELEASED
                    ;yes
    DECFSZ    FILTR1,F
   
    GOTO    CHECK_BUTTON1
   
    BTFSS    ST_BT1
    GOTO    DEC
    GOTO    CHECK_BUTTON2
   
BUTTON1_RELEASED
    BCF        ST_BT1

CHECK_BUTTON2
    BTFSC    PORTA,2
    GOTO    BUTTON2_RELEASED
   
    DECFSZ    FILTR2,F
   
    GOTO    CHECK_BUTTON2

    BTFSS    ST_BT2
    GOTO    INC
    GOTO    MAIN
   
BUTTON2_RELEASED
    BCF    ST_BT2
    GOTO    MAIN

DEC
    BSF    ST_BT1
    MOVF    COUNTER,W
    XORLW    MIN
   
   
    BTFSC    STATUS,Z
    GOTO    MAIN
   
    DECF    COUNTER,F
    GOTO    DISPUP

INC
    BSF    ST_BT2
    MOVF    COUNTER,W
    XORLW    MAX
   
   
    BTFSC    STATUS,Z
    GOTO    MAIN
   
    INCF    COUNTER,F
    GOTO    DISPUP
   
DISPUP
    CALL    DISP7
   
    MOVWF    PORTB
   
   
    GOTO    MAIN
   
   
   
    END
 
Top