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

Discussion in 'Programmer's Corner' started by Liam Porter, Jan 11, 2015.

  1. Liam Porter

    Thread Starter New Member

    Jan 11, 2015
    2
    0
    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 (Text):
    1.     LIST    P=16f84A, R=Dec
    2.     __FUSES    _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_ON
    3.     include "P16f84A.inc"
    4.  
    5.     CBLOCK    0x20
    6.         W_TEMP
    7.         STATUS_TEMP
    8.         COUNTER
    9.         FLAGS
    10.         FILTR1
    11.         FILTR2
    12.     ENDC
    13.  
    14.  
    15.    
    16.    
    17. #DEFINE    ST_BT1    FLAGS,0  
    18. #DEFINE    ST_BT2    FLAGS,1
    19.    
    20.  
    21.    
    22.    
    23. MIN        EQU    .0 ;minimum counter value
    24. MAX        EQU    .15 ;maximum counter value
    25. T_FILTR    EQU    .255  
    26.    
    27.    
    28. CMCON  
    29.     movlw 0x07
    30.     movwf CMCON
    31.    
    32.    
    33.    
    34.    
    35.    
    36.     ;reset
    37.     ORG 0x00
    38.     GOTO     start
    39.  
    40.    
    41.    
    42.    
    43.    
    44.  
    45.    
    46.     ORG    0x04
    47.     RETFIE
    48.    
    49.    
    50.    
    51.    
    52.    
    53.  
    54.  
    55. DISP7
    56.     MOVF    COUNTER,W
    57.     ANDLW    B'0001111'
    58.    
    59.     ADDWF    PCL,FLAGS
    60.  
    61. ;
    62.     RETLW    b'11101110' ;digit 0
    63.     RETLW    b'00101000' ;digit 1
    64.     RETLW    b'11001101' ;digit 2
    65.     RETLW    b'01101101' ;digit 3
    66.     RETLW    b'00101011' ;digit 4
    67.     RETLW    b'01100111'    ;digit 5
    68.     RETLW    b'11100111'    ;digit 6
    69.     RETLW    b'00101100'    ;digit 7
    70.     RETLW    b'11101111'    ;digit 8
    71.     RETLW    b'00101111'    ;digit 9
    72.     RETLW    b'10101111' ;A
    73.     RETLW    b'11100011'    ;B
    74.     RETLW    b'11000110'    ;C
    75.     RETLW    b'11101001'    ;D
    76.     RETLW    b'11000111'    ;E
    77.     RETLW    b'10000111'    ;F
    78.    
    79.    
    80.    
    81.    
    82.    
    83. start      
    84.     BCF        STATUS,RP0
    85.     MOVLW    B'00000111'
    86.     MOVWF    CMCON  
    87.    
    88.     BSF        STATUS,RP0
    89.     MOVLW    B'00000110'
    90.     MOVWF    TRISA
    91.    
    92.     MOVLW    B'00000000'
    93.     MOVWF    TRISB
    94.     MOVLW    B'10000000'
    95.     MOVWF    OPTION_REG
    96.    
    97.    
    98.     MOVLW    B'00000000'
    99.     MOVWF    INTCON
    100.     BCF        STATUS,RP0
    101.    
    102.    
    103.    
    104.    
    105.    
    106.     CLRF    PORTA
    107.     CLRF    PORTB
    108.     CLRF    FLAGS
    109.     MOVLW    MIN
    110.     MOVWF    COUNTER
    111.     GOTO    DISPUP
    112.    
    113.    
    114.    
    115.    
    116. MAIN
    117.     MOVLW    T_FILTR
    118.     MOVWF    FILTR1
    119.     MOVWF    FILTR2
    120.    
    121. CHECK_BUTTON1
    122.     BTFSC    PORTA,1 ;is button 1 pressed
    123.     GOTO    BUTTON1_RELEASED
    124.                     ;yes
    125.     DECFSZ    FILTR1,F
    126.    
    127.     GOTO    CHECK_BUTTON1
    128.    
    129.     BTFSS    ST_BT1
    130.     GOTO    DEC
    131.     GOTO    CHECK_BUTTON2
    132.    
    133. BUTTON1_RELEASED
    134.     BCF        ST_BT1
    135.  
    136. CHECK_BUTTON2
    137.     BTFSC    PORTA,2
    138.     GOTO    BUTTON2_RELEASED
    139.    
    140.     DECFSZ    FILTR2,F
    141.    
    142.     GOTO    CHECK_BUTTON2
    143.  
    144.     BTFSS    ST_BT2
    145.     GOTO    INC
    146.     GOTO    MAIN
    147.    
    148. BUTTON2_RELEASED
    149.     BCF    ST_BT2
    150.     GOTO    MAIN
    151.  
    152. DEC
    153.     BSF    ST_BT1
    154.     MOVF    COUNTER,W
    155.     XORLW    MIN
    156.    
    157.    
    158.     BTFSC    STATUS,Z
    159.     GOTO    MAIN
    160.    
    161.     DECF    COUNTER,F
    162.     GOTO    DISPUP
    163.  
    164. INC
    165.     BSF    ST_BT2
    166.     MOVF    COUNTER,W
    167.     XORLW    MAX
    168.    
    169.    
    170.     BTFSC    STATUS,Z
    171.     GOTO    MAIN
    172.    
    173.     INCF    COUNTER,F
    174.     GOTO    DISPUP
    175.    
    176. DISPUP
    177.     CALL    DISP7
    178.    
    179.     MOVWF    PORTB
    180.    
    181.    
    182.     GOTO    MAIN
    183.    
    184.    
    185.    
    186.     END
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,345
    1,028
    Sure and welcome to AAC!
    The 'overwriting' error is occurring because of the code in the snippet below:
    Code (Text):
    1. CMCON
    2.     movlw 0x07       ;;; These 2 lines generate code at the implied program address 0000h
    3.     movwf CMCON;;; and 0001h
    4.    ;reset
    5.     ORG 0x00   ;; this resets the address counter to 0000h
    6.     GOTO     start ;; this OVERWRITES the previous program word generated above (the movlw 0x07)
    7.  
    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: Jan 11, 2015
  3. Liam Porter

    Thread Starter New Member

    Jan 11, 2015
    2
    0
    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 (Text):
    1.     LIST    P=16f628A, R=Dec
    2.     __FUSES    _XT_OSC & _WDT_OFF & _CP_OFF & _PWRTE_ON
    3.     include "P16f628A.inc"
    4.  
    5.     CBLOCK    0x20
    6.         W_TEMP
    7.         STATUS_TEMP
    8.         COUNTER
    9.         FLAGS
    10.         FILTR1
    11.         FILTR2
    12.     ENDC
    13.  
    14.  
    15.    
    16.    
    17. #DEFINE    ST_BT1    FLAGS,0  
    18. #DEFINE    ST_BT2    FLAGS,1
    19.    
    20.  
    21.    
    22.    
    23. MIN        EQU    .0 ;minimum counter value
    24. MAX        EQU    .15 ;maximum counter value
    25. T_FILTR    EQU    .255  
    26.    
    27.    
    28.    
    29.    
    30.    
    31.    
    32. CMCON  
    33.     movlw 0x07
    34.     movwf CMCON
    35.    
    36.    
    37.    
    38.    
    39.    
    40.     ;reset
    41.     ORG 0x00
    42.     GOTO     start
    43.  
    44.    
    45.    
    46.    
    47.    
    48.  
    49.    
    50.     ORG    0x04
    51.     CLRF    INTCON
    52.     RETURN
    53.    
    54.    
    55.    
    56.    
    57.    
    58.  
    59.  
    60. DISP7
    61.     MOVF    COUNTER,W
    62.     ANDLW    B'0001111'
    63.    
    64.     ADDWF    PCL,F
    65.  
    66. ;
    67.     RETLW    b'11101110' ;digit 0
    68.     RETLW    b'00101000' ;digit 1
    69.     RETLW    b'11001101' ;digit 2
    70.     RETLW    b'01101101' ;digit 3
    71.     RETLW    b'00101011' ;digit 4
    72.     RETLW    b'01100111'    ;digit 5
    73.     RETLW    b'11100111'    ;digit 6
    74.     RETLW    b'00101100'    ;digit 7
    75.     RETLW    b'11101111'    ;digit 8
    76.     RETLW    b'00101111'    ;digit 9
    77.     RETLW    b'10101111' ;A
    78.     RETLW    b'11100011'    ;B
    79.     RETLW    b'11000110'    ;C
    80.     RETLW    b'11101001'    ;D
    81.     RETLW    b'11000111'    ;E
    82.     RETLW    b'10000111'    ;F
    83.    
    84.    
    85.    
    86.    
    87.    
    88. start      
    89.     BCF        STATUS,RP0
    90.     MOVLW    B'00000111'
    91.     MOVWF    CMCON  
    92.    
    93.     BSF        STATUS,RP0
    94.     MOVLW    B'00000110'
    95.     MOVWF    TRISA
    96.    
    97.     MOVLW    B'00000000'
    98.     MOVWF    TRISB
    99.     MOVLW    B'10000000'
    100.     MOVWF    OPTION_REG
    101.    
    102.    
    103.     MOVLW    B'00000000'
    104.     MOVWF    INTCON
    105.     BCF        STATUS,RP0
    106.    
    107.    
    108.    
    109.    
    110.    
    111.     CLRF    PORTA
    112.     CLRF    PORTB
    113.     CLRF    FLAGS
    114.     MOVLW    MIN
    115.     MOVWF    COUNTER
    116.     GOTO    DISPUP
    117.    
    118.    
    119.    
    120.    
    121. MAIN
    122.     MOVLW    T_FILTR
    123.     MOVWF    FILTR1
    124.     MOVWF    FILTR2
    125.    
    126. CHECK_BUTTON1
    127.     BTFSC    PORTA,1 ;is button 1 pressed
    128.     GOTO    BUTTON1_RELEASED
    129.                     ;yes
    130.     DECFSZ    FILTR1,F
    131.    
    132.     GOTO    CHECK_BUTTON1
    133.    
    134.     BTFSS    ST_BT1
    135.     GOTO    DEC
    136.     GOTO    CHECK_BUTTON2
    137.    
    138. BUTTON1_RELEASED
    139.     BCF        ST_BT1
    140.  
    141. CHECK_BUTTON2
    142.     BTFSC    PORTA,2
    143.     GOTO    BUTTON2_RELEASED
    144.    
    145.     DECFSZ    FILTR2,F
    146.    
    147.     GOTO    CHECK_BUTTON2
    148.  
    149.     BTFSS    ST_BT2
    150.     GOTO    INC
    151.     GOTO    MAIN
    152.    
    153. BUTTON2_RELEASED
    154.     BCF    ST_BT2
    155.     GOTO    MAIN
    156.  
    157. DEC
    158.     BSF    ST_BT1
    159.     MOVF    COUNTER,W
    160.     XORLW    MIN
    161.    
    162.    
    163.     BTFSC    STATUS,Z
    164.     GOTO    MAIN
    165.    
    166.     DECF    COUNTER,F
    167.     GOTO    DISPUP
    168.  
    169. INC
    170.     BSF    ST_BT2
    171.     MOVF    COUNTER,W
    172.     XORLW    MAX
    173.    
    174.    
    175.     BTFSC    STATUS,Z
    176.     GOTO    MAIN
    177.    
    178.     INCF    COUNTER,F
    179.     GOTO    DISPUP
    180.    
    181. DISPUP
    182.     CALL    DISP7
    183.    
    184.     MOVWF    PORTB
    185.    
    186.    
    187.     GOTO    MAIN
    188.    
    189.    
    190.    
    191.     END
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,387
    1,605
    Shortly after the two #DEFINE statements you use CMCON as a line lable.

    Simon sez don't do that. CMCON should be a register name only.
     
    ke5nnt and JohnInTX like this.
Loading...