1. 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.
    Dismiss Notice

lookup table Code works in proteus sim, but not IRL

Discussion in 'Microcontrollers' started by Roger_NO, Aug 24, 2017.

  1. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo
    Hi,
    Just learnd how to use loockup tables and SPI interface with a digital pot. When adding a switch to my project the code halts. It does not get the table data it seems. The gode works fine without the "BTFSS PORTA,1"

    Code (text):

         LIST      p=16F88              ; list directive to define processor
         #INCLUDE <p16f88.inc>          ; processor specific variable definitions

    ;------------------------------------------------------------------------------
    ;
    ; CONFIGURATION WORD SETUP
    ;
    ; The 'CONFIG' directive is used to embed the configuration word within the
    ; .asm file. The lables following the directive are located in the respective
    ; .inc file.  See the data sheet for additional information on configuration
    ; word settings.
    ;
    ;------------------------------------------------------------------------------

         __CONFIG    _CONFIG1, _CP_OFF & _CCP1_RB0 & _DEBUG_OFF & _WRT_PROTECT_OFF & _CPD_OFF & _LVP_OFF & _BODEN_OFF & _MCLR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_IO
         __CONFIG    _CONFIG2, _IESO_OFF & _FCMEN_OFF
        errorlevel -302


     cblock    0x20
            d1
            d2
            d3
            count1
            count2
     endc


    ;******************** EQUATES ************************************************************


    CS         EQU H'00'     ;CHIP SELECT
    COMMAND EQU H'13'     ;VARIABLE FOR THE COMMAND BYTE
    R_VALUE EQU d'20'     ;VARIABLE FOR THE RESISTANCE VALUE
    DIM        EQU    H'00'


    ;******************** PROGRAM ORIGIN *****************************************************

     ORG     0X00

    ;-------------------- PORTB AND SPI SETTING ----------------------------------------------

     BSF     STATUS, RP0 ;SPECIFY BANK 1
     MOVLW    B'01101100'
     MOVWF    OSCCON
     MOVLW     H'00'
     MOVWF     TRISB         ;SET PORTB AS AN OUTPUT
     CLRF     PORTA
     MOVLW     0x00
     MOVWF     ANSEL
     MOVLW     0xFF
     MOVWF     TRISA
     BCF     STATUS, RP0 ;SPECIFY BANK 0
     CLRF     PCLATH         ;ENSURE PCLATH BIT 3 IS CLEARED
     CLRF     INTCON         ;ENSURE ALL INTERRUPTS ARE DISABLED
     MOVLW     0x30         ;
     MOVWF     SSPCON         ;SET SYNC SERIAL PORT CONTROL REGISTER

    ;-------------------- PROGRAM ROUTINE ----------------------------------------------------

    LOOP1
     btfss    PORTA,1        ;start?
     goto    LOOP1

    START  
     BCF     PORTA, CS     ;SELECT THE POT
     CLRF    DIM
    LOOP
     MOVLW     COMMAND     ;LOAD THE COMMAND BYTE IN THE ACCUMULATOR
     CALL     TRANSMIT    ;TRANSMIT THE COMMAND BYTE
     MOVF    DIM,W
     CALL    TABLE
     CALL    TRANSMIT
     INCF    DIM,1
     BTFSS    DIM,4
     GOTO    LOOP
     BSF     PORTA, CS     ;UNSELECT THE POT
     GOTO     START        ;FINISH

    ;-------------------- TRANSMISSION SUBROUTINE --------------------------------------------

    TRANSMIT
     BCF     STATUS, RP0 ;SPECIFY BANK 0
     MOVWF     SSPBUF         ;PLACE DATA IN BUFFER TO SEND
     BSF     STATUS, RP0 ;SPECIFY BANK 1
    LOOP3
     BTFSS     SSPSTAT, BF ;CHECK IF TRANSMISSION IS COMPLETE
     GOTO     LOOP3 ;
     BCF     STATUS, RP0 ;SPECIFY BANK 0
     RETURN             ;RETURN FROM SUBROUTINE

    ;*******************************************************************************************
    TABLE
        ADDWF    PCL,1
        retlw    d'220'
        retlw    d'221'
        retlw    d'222'
        retlw    d'223'
        retlw    d'224'
        retlw    d'225'
        retlw    d'226'
        retlw    d'227'
        retlw    d'228'
        retlw    d'229'
        retlw    d'230'
        retlw    d'231'
        retlw    d'232'
        retlw    d'233'
        retlw    d'234'
        retlw    d'235'


    DELAY                    ;100ms
                ;99993 cycles
        movlw    0x1E
        movwf    d1
        movlw    0x4F
        movwf    d2
    DELAY_0
        decfsz    d1, f
        goto    $+2
        decfsz    d2, f
        goto    DELAY_0

                ;3 cycles
        goto    $+1
        nop

                ;4 cycles (including call)
        return

     END
    ;******************** END OF PROGRAM *****************************************************
     
     
  2. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    955
    Likes:
    43
    Location:
    England
    How have you got your switch wired to PortA,1 and what resistor value are you using, pulled Up or Down ?

    Using BSF STATUS , RP0 can be a real headache, much easier to use the Directive ' banksel OSCCON ' which selects the respective bank.
    You can use banksel 0 to return to Bank 0, all other banks need a register specifying .

    You are using CLRF PORTA ( bank0) but you are still in Bank 1, though that should not cause your problem.

    Are you using Mplab ide 8.92 or Mplab X and which programmer are you using Pk2 /Pk3 ?

    The 16F88 has in inbuilt debugger so you can use that with the Pk2 etc and single step though your code and watch the registers etc change and hopefully see where your 'bug' is.


    000382.jpg
     
    • Like Like x 1
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Your problem is you set DIM to zero, addwf PCL,w will be an infinite loop with W set to zero.

    Mike.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo

    Thanx for your reply!

    I use a Capacitive Touch Switch module, so no pull-up/down necessary? It's based on the TTP223.

    I'm using bouth;) but i tryed installing X and step trough while watching registers, and it seemed fine. I'm using a QL200 DEV board from pic16.

    Mike
    I set DIM to zero so it returns with the first value in my table. Am i doing it wrong?
     
    Last edited: Aug 24, 2017
  6. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,007
    Likes:
    316
    Location:
    Brisbane Australia
    Whoops, my mistake. It's a long time since I did lookups in assembly.

    Edit, are you sure your table resides in one bank?

    Mike.
     
    • Like Like x 1
  7. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    955
    Likes:
    43
    Location:
    England
    Have not used a digital pot, so wonder how exactly you are measuring/using its output to see if its receiving the data from the Pic ?

    Which digital pot are you using ?

    Are you saying if you run your code on a continuous basis without this code it works ok ?

    LOOP1
    btfss PORTA,1 ;start?
    goto LOOP1


    Again do not know which touch switch you are , but some have pads to select High or Low outputs and Momentary or Latched outputs .
     
    • Like Like x 1
  8. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo
    Im using MCP4151 from micrchip. The pot has 3 pins where i connect one end to VDD and the other to VSS. The wiper pin voltage tells me if the communication is up.

    If i remove the btfss PORTA,1 from the code the output wiper (from MCP4151) regulates the voltage by the value in my table. So yes. Without this my code works excellent.
    When simulating in proteus 8 the code works well both with and without the btfss instruction. But when testing on my board, it does not.

    The touch toggles from low to high when i touch, then back to low when released.
     
    Last edited: Aug 25, 2017
  9. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo
    No problem;) My long term memory is horrible as well;)

    I am not sure, but when adding a delay between fetching table data, the nr of output changes corresponds with number of variables in the table. (without switch)
     
  10. Wp100

    Wp100 Active Member

    Joined:
    Feb 3, 2009
    Messages:
    955
    Likes:
    43
    Location:
    England
    Cannot see your error, would suggest you replace your touch switch with a standard pull up or down switch to eliminate that.

    If you look at your Disassembly listing it shows the addresses your code occupies, seems ok.

    If you remove your lookup table ; CALL TABLE and let DIM just increment from 0 to 256 in an endless loop then you can be sure its not anything to do with the table.

    That said, Proteus can often run much slower than on an actual chip, try putting CALL DELAY once or even twice before the TX routine, so you can clearly see the increment on the output.
    Running around just 16 values as you have the code with no delays it would be happening very fast eg milliseconds, so doubt a DVM would read it or are use using a scope ?
     

    Attached Files:

  11. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo
    I added 0.5 second delay when i tested i proteus. on my breadboard i used an osilloscope to see and measure the "stepform". i'll try a pull-up resistor to see if it helps when i come home. I'll also try to increment DIM.
    I will tell you how it goes! Thanx for all the suggestions.
     
  12. Roger_NO

    Roger_NO Member

    Joined:
    Oct 11, 2008
    Messages:
    35
    Likes:
    1
    Location:
    Norway, oslo
    took some time, but got it working. Had to clear PORTA! (in the rigt bank as you said Wp100) did not think that clearing A would be the main issue! I have skipped this in many other projects, but first time usin f88;)
     

Share This Page