Need Help SOS

Discussion in 'Embedded Systems and Microcontrollers' started by Zahir_s, Dec 23, 2014.

  1. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    Hi guys,
    This is my first time here.
    I'm having a problem simulating a pic asm code in proteus.
    and I'm sure that there is some problem with the code
    I just can't figure it out nor can I ask anyone except you guys.
    please take a look at my code and tell me what's wrong.
    let me learn something.
    Thanks

    Attached the asm file
     
  2. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You should comment your code and tell us what you want it to do. I, for one, am not going to piece together your intention from uncommented code when you've already stated it doesn't do what you want.
     
  3. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    ok, right. I'll do that, thanks
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Yeah, we may seem to know everything but really, you didn't even bother to write out what the code is supposed to do, no less what it is not doing.

    Sorry to seem rude to a new person but we need some sort of input to form an answer to help you.

    And welcome to the forums. :)
     
  5. JohnInTX

    Moderator

    Jun 26, 2012
    2,338
    1,018
    I agree with the others. Commented code and a schematic is a basic starting point.
    But, just looking at the code so far a few things jump out. I've commented the source with some of those things.

    EDIT: Be sure to init ALL of the IO (unused lines to output '0' and don't forget things like CMCON etc that take over the pins. There are no shortcuts to a full init.
    Also, the 44780 requires a delay after power up before you try to initialize it.

    Good luck.
     
    Last edited: Dec 23, 2014
    Zahir_s likes this.
  6. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    Thank you John, I'll try and work things out as you pointed out and study the datasheet and not just the command words of the LCD. I'll get back with the results as soon I have something substantial.
    My guess is you are from Texas?
     
  7. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    Thanks for the welcome Ernie. I'll make it a point to comment all the code I write from now on.
     
  8. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,494
    2,364
    There are Pic tutorials by N.Goodwin that may help.
    http://www.winpicprog.co.uk/pic_tutorial.htm
    Tutorial 3

    Also you can list code with the 'Insert' drop down. e.g.
    Code (Text):
    1. ;LCD 16 bit counter - using LCD Busy line
    2. ;Nigel Goodwin 2002
    3.  
    4.     LIST    p=16f628a        ;tell assembler what chip we are using
    5.     include "P16F628a.inc"        ;include the defaults for the chip
    6.     ERRORLEVEL    0,    -302    ;suppress bank selection messages
    7.     __config 0x3D18            ;sets the configuration settings (oscillator type etc.)
    8.  
    9.  
    10.  
    11.  
    12.         cblock    0x20            ;start of general purpose registers
    13.             count            ;used in looping routines
    14.             count1            ;used in delay routine
    15.             counta            ;used in delay routine
    16.             countb            ;used in delay routine
    17.             tmp1            ;temporary storage
    18.             tmp2
    19.             templcd            ;temp store for 4 bit mode
    20.             templcd2
    21.  
    22.                 NumL            ;Binary inputs for decimal convert routine
    23.                 NumH  
    24.  
    25.                 TenK            ;Decimal outputs from convert routine
    26.                 Thou  
    27.                 Hund  
    28.                 Tens  
    29.                 Ones  
    30.         endc
    31.  
    32. LCD_PORT    Equ    PORTB
    33. LCD_TRIS    Equ    TRISB
    34. LCD_RS        Equ    0x04            ;LCD handshake lines
    35. LCD_RW        Equ    0x06
    36. LCD_E        Equ    0x07
    37.  
    38.         org    0x0000
    39.         goto    Start
    40.  
    41. HEX_Table      ADDWF   PCL       , f
    42.                 RETLW   0x30
    43.                 RETLW   0x31
    44.                 RETLW   0x32
    45.                 RETLW   0x33
    46.                 RETLW   0x34
    47.                 RETLW   0x35
    48.                 RETLW   0x36
    49.                 RETLW   0x37
    50.                 RETLW   0x38
    51.                 RETLW   0x39
    52.                 RETLW   0x41
    53.                 RETLW   0x42
    54.                 RETLW   0x43
    55.                 RETLW   0x44
    56.                 RETLW   0x45
    57.                 RETLW   0x46
    58.  
    59.  
    60. Text        addwf    PCL, f
    61.         retlw    '1'
    62.         retlw    '6'
    63.         retlw    ' '
    64.         retlw    'B'
    65.         retlw    'i'
    66.         retlw    't'
    67.         retlw    ' '
    68.         retlw    'C'
    69.         retlw    'o'
    70.         retlw    'u'
    71.         retlw    'n'
    72.         retlw    't'
    73.         retlw    'e'
    74.         retlw    'r'
    75.         retlw    '.'
    76.         retlw    0x00
    77.  
    78. Start        movlw    0x07
    79.         movwf    CMCON            ;turn comparators off (make it like a 16F84)
    80.  
    81. Initialise:
    82.         clrf    count
    83.         clrf    PORTA
    84.         clrf    PORTB
    85.         clrf    NumL
    86.         clrf    NumH
    87.  
    88.  
    89.  
    90. SetPorts:
    91.         bsf     STATUS,        RP0    ;select bank 1
    92.         movlw    0x00            ;make all pins outputs
    93.         movwf    LCD_TRIS
    94.         movwf    TRISB
    95.         bcf     STATUS,        RP0    ;select bank 0
    96.  
    97.         call    LCD_Init        ;setup LCD
    98.  
    99.  
    100.         clrf    count            ;set counter register to zero
    101. Message:
    102.         movf    count, w        ;put counter value in W
    103.         call    Text            ;get a character from the text table
    104.         xorlw    0x00            ;is it a zero?
    105.         btfsc    STATUS, Z
    106.         goto    NextMessage
    107.         call    LCD_Char
    108.         incf    count, f
    109.         goto    Message
    110.  
    111. NextMessage:
    112.         movlw    d'2'
    113.         call    LCD_Line2W        ;move to 2nd row, third column
    114.  
    115.         call    Convert            ;convert to decimal
    116.         movf    TenK,    w        ;display decimal characters
    117.         call    LCD_CharD        ;using LCD_CharD to convert to ASCII
    118.         movf    Thou,    w
    119.         call    LCD_CharD
    120.         movf    Hund,    w
    121.         call    LCD_CharD      
    122.         movf    Tens,    w
    123.         call    LCD_CharD
    124.         movf    Ones,    w
    125.         call    LCD_CharD
    126.         movlw    ' '            ;display a 'space'
    127.         call    LCD_Char
    128.         movf    NumH,    w        ;and counter in hexadecimal
    129.         call    LCD_HEX
    130.         movf    NumL,    w
    131.         call    LCD_HEX
    132.         incfsz    NumL,     f
    133.         goto    Next
    134.         incf    NumH,    f
    135. Next:
    136.         call    Delay255        ;wait so you can see the digits change
    137.         goto    NextMessage
    138.  
    139.   etc etc
    140.  
     
    Zahir_s likes this.
  9. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    tHANK YOU GUYS!!! SEE ATTACHED FILE, STILL NO DOCUMENTATION BY ME
    SPECIAL THANKS TO JOHN
    NOW i CAN GET ON WITH THE NEXT PHASE...aTO d CONVERSION...
    SO i'LL CLOSE THIS THREAD NOW?
     
  10. JohnInTX

    Moderator

    Jun 26, 2012
    2,338
    1,018
    Leave the thread open in case you have issues with the ADC.

    Before you move to that, take a pass through your code and remove any comments that don't apply any more then add your own to describe each section of code. You will be glad you did that when your program gets bigger.
    A few things to consider to make your source easier to read and maintain:

    Consider mixed-case - lower case for opcodes, UPPER_CASE for user defined, mixed case for labels etc. LCDwrite. Every programmer has his/her own style but usually employ something like that to help identify things when the code is printed.
    Don't use absolute RAM addresses in the code. Use CBLOCK or EQU to declare them with a label then refer to the label.
    Use #define to make your IO definitions
    Use ASCII characters where appropriate
    Use BANKSEL instead of the B?F STATUS,RPx - its easier and less error prone.
    Code (Text):
    1.  
    2. ;---------- Declaring RAM  -----------
    3. cblock 02Bh ; RAM in your delay code starts here
    4.   DelayCountH : 1 ; declare 1 byte named DelayCountH - it will be at 02bh
    5.   DelayCountL : 1 ; declare another byte at 02ch
    6. endc ; ends the cblock
    7.  
    8. ; then, in your code:
    9. DLY0:  
    10.      MOVLW 0XFF
    11.      MOVWF DelayCountH
    12. AGAIN:   DECFSZ DelayCountH,F ; use F or W instead of 1 or 0
    13.      GOTO D1
    14.      RETURN
    15.  
    16. D1:     MOVLW 0XFF
    17.      MOVWF DelayCountL
    18. AGAIN1:   DECFSZ DelayCountL,F
    19.      GOTO AGAIN1
    20.      GOTO AGAIN
    21.  
    22. ;------- Defining IO  -----------
    23. #define LCD_E PORTB,RB2
    24.  
    25. ; then in your code..
    26. ;Strobe LCD_E high then low
    27. bsf LCD_E ; strobe high - the text 'LCD_E' will be replaced by PORTB,RB2 by the assembler - nice.
    28. nop ; give it a little time
    29. bcf LCD_E ; then low
    30.  
    31. ;---------- ASCII for characters  ------------
    32. movlw 'H' ; 48h is ASCII 'H' - tells the reader that you are using characters
    33. call LCD_WRT
    34. movlw 'E'
    35. call LCE_WRT
    36.  
    37. ;---------- BANKSEL for banks  ---------------
    38. banksel PORTB ; generate bsf/bcf STATUS.. code to select the bank containing PORTB(and the other PORTS too)
    39. clrf PORTB
    40. clrf PORTA
    41. banksel TRISB ; generate bsf/bcf STATUS.. code to select the bank containing TRISB (and the other TRIS too)
    42. clrf TRISB
    43. clrf TRISA
    44. ; etc.
    45.  
    Easier to see what's going on and if you move LCD_E to another pin, you only have to change the #define and rebuild.
    There are couple of more things to chew on like how to write strings of characters (like MAX showed but easier maybe?) and explore the dreaded r-m-w problem when writing to PORTs but that can be later.

    All said, you are on your way - WELL DONE.
    Have fun.
     
    Last edited: Dec 24, 2014
  11. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    tHANKS Its lot to take in, but I'll get it in a few days.
    and as you said i do have a problem with the a/d conversion.
    the problem is this:
    after the a/d interrupt routine, the RETFIE instruction returns the pc to the interrupt vector (instead of pc+1) that has the CALL instruction thus looping again back to the routine endlessly and I dont understand why its doing what it is not supposed to be doing...
     
    • ADC.ASM
      File size:
      2.4 KB
      Views:
      19
  12. JohnInTX

    Moderator

    Jun 26, 2012
    2,338
    1,018
    You are not clearing the interrupt flag ADIF in the interrupt routine so as soon as RETFIE re-enables the interrupts, it gets interrupted again immediately. You also have to set GO again to start the next conversion after reading ADRESH/L.

    Also in init, you should
    1) configure the interrupt sources first
    2) clear any associated interrupt flags
    3) enable the interrupt(s)

    in that order.
     
    Last edited: Dec 24, 2014
  13. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    ok, got it.
    I'll get back with results.
    I wish they taught us in-such-detail in school.
    Anyway I'll stop whining and get to work.
    Thank you again
     
  14. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    !!!!!!!!!mERRY cHRISTMAS!!!!!!!!!
    GREETINGS FROM mUMBAI, iNDIA!!!!

    @JohnInTX - i JUST GOT RID OF THE INTERRUPT SCHEME AS IT TURNS OUT THAT THE STACK WAS OVER AND UNDER FLOWING, INSTEAD OF INTERRUPTING, THE PIC JUST WATCHES THE GO/DONE' BIT FOR CLEARING AND THEN JUMPS TO THE AD READ CODE.
    nOW ALL i HAVE TO DO IS TO COMBINE THE LCD AND A/D CIRCUITS USING A SINGLE PIC AND i'LL BE DONE.

    @MaxHeadRoom - THANKS FOR THE 'LOOK UP TABLE' EXAMPLE IT WILL COME VERY HANDY IF I CAN GET THE OTHER THINGS CORRECTLY.

    tHANK YOU GUYS AND AGAIN ENJOY YOUR HOLIDAYS.
     
  15. bertus

    Administrator

    Apr 5, 2008
    15,634
    2,342
    Hello,

    Is your capslock on?
    Why write almost everything in capitals.
    Writing all in capitals, is like shouting.

    Bertus

    PS the same happens in your profile.
     
  16. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    @bertus - Yes my caps were on, sorry.

    Another question, I'm getting into stack overflow and underflows in my pic project, any ideas.
    Again I'll attach the .asm file. hope it doesn't drive you nuts.

    First things last, the project is a Musical Tuner which I'm doing in my own time to learn as I'm not going to school for another 6 months.
    so instead of wasting time i thought I try my hand at some practical stuff. before doing this all i had designed was a power supply on a dotmatrix pcb, which is again not really designing, I just wired the stuff and soldered and all...
    I'm tired...

    ps: I'm using Proteus-Isis to simulate.
     
  17. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    Guys I got it. worry not.
    It'll take me 1 or 2 days to tie up the loose ends but then I'll submit my first SOLO project here.
    godddamit I feel great!!!
     
  18. JohnInTX

    Moderator

    Jun 26, 2012
    2,338
    1,018
    Merry Christmas and Holiday Greetings to you as well.
    Ideas? Always!
    Code (Text):
    1. START:        ;MAIN LOOP BEGINS HERE
    2.         BCF STATUS,RP0    ;BANK0
    3.         CALL DLY_AQ
    4.         BSF ADCON0,2    ;START CONVERSION
    5. WAIT:    BTFSC ADCON0,2    ;LOOK FOR DONE' OR GO BEING CLEARED
    6.         GOTO WAIT
    7.         CALL DLY0        ;WAIT FOR DATA TO SETTLE IN ADRESL
    8.  
    9.  
    10. ********
    11.         GOTO AD_READ
    12.         you are GOTOing a subroutine ending in RETURN - STACK UNDERFLOW HERE
    13.  
    14. ***********
    15.       CALL LUT ;
    16. you don't ensure that W is < A0h, the end of your table.  W >=A0h will cause it to jump off the end of the table, NOP its way to 100h (unprogrammed memory is NOP)
    17. and work its way down to calling the table again - and again. - DATA DEPENDENT STACK OVERFLOW HERE.
    18. Be sure that W is < the number of entries in the table - especially important when the value is driven by the outside world, in this case the ADC reading.
    19. Also, be sure that the table does not span ROM banks.
    20. Since your lookup does not manage PCLATH, it will just wrap around in one 256 word bank.
    21. Qualifying W and ensuring table location is critical when you use ADDWF PCL,F.
    22.  
    23.         CALL LCD_WRT
    24.         GOTO START         ;BEGIN ALL OVER AGAIN
    If Proteus does not allow you to step your code, build it in MPLAB 8.x or MPLABX and use MPSIM to step the code, watch the stack, trace where the table lookups go. You can also load the .COF file from another assembler into MPLAB and still use MPSIM. If you haven't used MPSIM to debug, this will get you started.

    Off to see family down south. Replies will be a bit spotty if any.
    Good luck!​
     
    Last edited: Dec 25, 2014
  19. Zahir_s

    Thread Starter Member

    Dec 23, 2014
    32
    1
    The 'Tuner' works fine on Proteus, now I want to reduce its size and for that I need to use a small PIC and use the Display in the 4-bit mode.
    So the problem is, I am testing the display in the 4-bit mode and it is just throwing up garbage values, for example, in place of 'L' I'm getting a '$' and likewise.
    Any Ideas on how to debug, I tried breakpointing, but I can't figure out the source of this erroneous data.

    Code (Text):
    1. ;====================================================================
    2. ; Main.asm file generated by New Project wizard
    3. ;
    4. ; Created:   Fri Dec 26 2014
    5. ; Processor: PIC16F877
    6. ; Compiler:  MPASM (MPLAB)
    7. ;====================================================================
    8.  
    9. ;====================================================================
    10. ; DEFINITIONS
    11. ;====================================================================
    12.  
    13.     #include p16f877.inc    ; Include register definition file
    14.  
    15.     __CONFIG _WDT_OFF
    16.     ORG 0X0000
    17.     GOTO INIT
    18.     ORG 0X0004
    19.     GOTO START
    20.    
    21.     CBLOCK 0X0020
    22.         TEMP
    23.         T_S
    24.         T_L
    25.         T_L_2
    26.     ENDC
    27.    
    28.     #DEFINE FN 0X24
    29.     #DEFINE DO 0X0F
    30.     #DEFINE EM 0X06
    31.     #DEFINE DS 0X14
    32.     #DEFINE CD 0X01
    33.     #DEFINE CH 0X02
    34.     #DEFINE COUNT_S 0XFF
    35.     #DEFINE COUNT_L 0XFF
    36.     #DEFINE NO_ANLG 0X06
    37.  
    38.     ORG 0X0030
    39. INIT:
    40.         BCF STATUS,RP1
    41.         BSF STATUS,RP0    ;BANK1
    42.         CLRF TRISA
    43.         CLRF TRISB
    44.         CLRF TRISC
    45.         CLRF TRISD
    46.         CLRF TRISE
    47.         BCF STATUS,RP0    ;BANK0
    48.         CLRF PORTA
    49.         CLRF PORTB
    50.         CLRF PORTC
    51.         CLRF PORTD
    52.         CLRF PORTE        ;PIC INITIALIZATION DONE
    53.         CLRF TEMP
    54.         CLRF ADCON0
    55.         BCF STATUS,RP1
    56.         BSF STATUS,RP0
    57.         MOVLW NO_ANLG
    58.         MOVWF ADCON1
    59.         BCF STATUS,RP0    ;BANK0
    60.        
    61.        
    62. START:
    63.         CALL LCD_INIT
    64.         CALL DLY_L
    65.        
    66.         BSF PORTE,0
    67.         BCF PORTE,1        ;R/W=0,RS=1, WRITE DATA TO LCD
    68.        
    69.        
    70.         MOVLW 'L'
    71.        
    72.         CALL LCD_OUT
    73.        
    74.         MOVLW 'C'
    75.        
    76.         CALL LCD_OUT
    77.        
    78.         MOVLW 'D'
    79.        
    80.         CALL LCD_OUT
    81.        
    82.         MOVLW ' '
    83.        
    84.         CALL LCD_OUT
    85.        
    86.         MOVLW '4'
    87.        
    88.         CALL LCD_OUT
    89.        
    90.         MOVLW '-'
    91.        
    92.         CALL LCD_OUT
    93.        
    94.         MOVLW 'B'
    95.        
    96.         CALL LCD_OUT
    97.        
    98.         MOVLW 'i'
    99.        
    100.         CALL LCD_OUT
    101.        
    102.         MOVLW 't'
    103.        
    104.         CALL LCD_OUT
    105.        
    106.         MOVLW 'M'
    107.        
    108.         CALL LCD_OUT
    109.        
    110.         MOVLW 'o'
    111.        
    112.         CALL LCD_OUT
    113.        
    114.         MOVLW 'd'
    115.        
    116.         CALL LCD_OUT
    117.        
    118.         MOVLW 'e'
    119.        
    120.         CALL LCD_OUT
    121.        
    122.        
    123.         GOTO $
    124.        
    125. LCD_OUT
    126.         BCF STATUS,RP0
    127.         BCF STATUS,RP1    ;BANK 0
    128.         MOVWF TEMP         ;SAVE A COPY OF W DATA TEMPORARILY
    129.         SWAPF TEMP,W
    130.         ANDLW 0X0F        ;MASK THE LSB THAT ARE IN MSB POSITION IE MAKE ALL 4 BITS 0
    131.         MOVWF PORTD        ;OUT WITH MSB
    132.         BSF PORTE,2
    133.         BCF PORTE,2        ;STROBE E
    134.         CALL DLY_S
    135.        
    136.         MOVF TEMP,W
    137.         ANDLW 0X0F
    138.        
    139.         MOVWF PORTD    ;OUT WITH LSB
    140.         BSF PORTE,2
    141.         BCF PORTE,2 ;STROBE E
    142.        
    143.         CALL DLY_L
    144.        
    145.         RETURN
    146.        
    147. LCD_INIT
    148.         BCF STATUS,RP0
    149.         BCF STATUS,RP1    ;BANK 0
    150.         BCF PORTE,0
    151.         BCF PORTE,1        ;RS,R/W=0 I.E. COMMAND
    152.         MOVLW FN        ;FUNC SET
    153.        
    154.         CALL LCD_OUT
    155.         BSF PORTE,2
    156.         BCF PORTE,2
    157.         CALL DLY_L
    158.        
    159.         MOVLW DO        ;DISP ON OFF
    160.        
    161.         CALL LCD_OUT
    162.         BSF PORTE,2
    163.         BCF PORTE,2
    164.         CALL DLY_L
    165.        
    166.        
    167.         MOVLW EM        ;ENTRY MODE
    168.        
    169.         CALL LCD_OUT
    170.         BSF PORTE,2
    171.         BCF PORTE,2
    172.         CALL DLY_L
    173.        
    174.         MOVLW DS        ;DISP SHIFT
    175.        
    176.         CALL LCD_OUT
    177.         BSF PORTE,2
    178.         BCF PORTE,2
    179.         CALL DLY_L
    180.        
    181.         MOVLW CD        ;CLEAR DISP
    182.         CALL LCD_OUT
    183.         BSF PORTE,2
    184.         BCF PORTE,2
    185.         CALL DLY_L
    186.        
    187.         MOVLW CH        ;CURSOR HOME
    188.         CALL LCD_OUT
    189.         BSF PORTE,2
    190.         BCF PORTE,2
    191.         CALL DLY_L
    192.         RETURN
    193.        
    194. DLY_S
    195.         BCF STATUS,RP0
    196.         BCF STATUS,RP1
    197.         MOVLW COUNT_S
    198.         MOVWF T_S
    199. AGAIN:    DECFSZ T_S,F
    200.         GOTO AGAIN
    201.         RETURN
    202.        
    203. DLY_L
    204.         BCF STATUS,RP0
    205.         BCF STATUS,RP1
    206.         MOVLW COUNT_L
    207.         MOVWF T_L
    208. REPEAT:    DECFSZ T_L,F
    209.         GOTO NEST1
    210.         RETURN
    211.         MOVLW COUNT_L
    212.         MOVWF T_L_2
    213. NEST1:  DECFSZ T_L_2,F
    214.         GOTO NEST1
    215.         GOTO REPEAT
    216.        
    217.       END
    218.  
     
  20. John P

    AAC Fanatic!

    Oct 14, 2008
    1,632
    224
    ORG 0X0004
    GOTO START???

    0x04 is where the interrupts jump to. I suppose it's OK if you aren't actually using interrupts.

    Is there any pattern to what appears on the LCD versus what you think is being sent to it? Because if there's a predictable effect, it might be a result of having the data wires in the wrong order.
     
Loading...