News:

SMF for DIYStompboxes.com!

Main Menu

PIC16F628 RETLW lookup table

Started by demonstar, November 04, 2008, 05:31:32 PM

Previous topic - Next topic

demonstar

I'm not sure if this is really suitable for this forum but I know there are lots of very clever people here who I suspect will be able to help me out so I hope Aron doesn't mind this post.  :icon_redface:

I have been writing simple programs for the PIC16F628. I'm gradually building them up in complexity. The one I'm working on currently is a seven segment display counting from zero to 9 then going back to zero. The seven segment display is on PORTB.

Here's the issue... I had the program working without the lookup table (just counting in binary on the sevensegment display). When I introduced a look up table I just could not get it working (No signs of life at all). If I remove the table and the call table line it works by counting in binary but soon as I add it in it won't work again. THe program does work when simulated which makes the situation even more confusing. This has been causing me trouble for around a week now so if anyone could give me and tips I'd very much appreciate it. The code is below...

;**********************************************************************
;                                                                     *
;    Filename:        LED Count.asm                                   *
;    Date:            4/10/08                                         *
;    File Version:    1.00                                            *
;                                                                     *
;    Author:          R. Moody                                        *
;    Company:         -                                               *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required: P16F628.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;                                                                     *
;                                                                     *
;                                                                     *
;**********************************************************************


    list      p=16f628            ; list directive to define processor
    #include <p16f628.inc>        ; processor specific variable definitions
   
;VARIABLE DEFINITIONS**************************************************

d1   EQU   H'20'
d2   EQU   H'21'
d3   EQU   H'22'
d4   EQU   H'23'
Count1   EQU   H'24'

;INITIALIZATION********************************************************

setup   BSF   STATUS,RP0   ;SELECT BANK 1
   BCF   STATUS,RP1
      
   MOVLW   B'00000000'               ;SET PORTB AS OUTPUTS
   MOVWF   TRISB

   CLRF   Count1      ;CLEAR Count1

   BCF   STATUS,RP0   ;SELECT BANK 0
   BCF   STATUS,RP1

   GOTO   main

;DELAY******************************************************************
; Delay = 0.5 seconds
; Clock frequency = 4 MHz

; Actual delay = 0.5 seconds = 500000 cycles
; Error = 0 %

delay   ;499994 cycles
   MOVLW   H'03'
   MOVWF   d1
   MOVLW   H'18'
   MOVWF   d2
   MOVLW   H'02'
   MOVWF   d3

Delay_0   DECFSZ   d1, f
   GOTO   $+2
   DECFSZ   d2, f
   GOTO   $+2
   DECFSZ   d3, f
   GOTO   Delay_0
   
   ;6 cycles
   GOTO   $+1
   GOTO   $+1
   GOTO   $+1

   RETURN

; Generated by http://www.golovchenko.org/cgi-bin/delay (December 7, 2005 version)
; Tue Oct 21 18:37:41 2008 GMT

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

;SUBROUTINES************************************************************
;7 SEG DISPLAY TABLE****************************************************

table   ADDWF   PCL
   RETLW   B'00111111'   ;#0
   RETLW   B'00000110'   ;#1
   RETLW   B'01011011'   ;#2
   RETLW   B'01001111'   ;#3
   RETLW   B'01000110'   ;#4
   RETLW   B'01101101'   ;#5
   RETLW   B'01111101'   ;#6
   RETLW   B'00000111'   ;#7
   RETLW   B'01111111'   ;#8
   RETLW   B'01100111'   ;#9

;MAIN CODE*************************************************************

main   MOVFW   Count1                   ;PUT Count1 INTO 'W'
   CALL   table
   MOVWF   PORTB      ;DISPLAY Count1 ON LEDS
   CALL   delay
      
   INCF   Count1      ;INCREASE Count1
   MOVLW   H'10'      ;PUT H'10' INTO 'W'
   SUBWF   Count1,W                   ;SUBTRACT 'W' FROM COUNT, ANSWER IN 'W'
   BTFSC   STATUS,Z                   ;TEST IF ZERO BIT IS SET.
   CLRF   Count1      ;IF SET CLEAR Count1

   GOTO   main      ;LOOP THROUGH 'MAIN CODE'

   END

Thanks!



   
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut"  Words of Albert Einstein

David

I'm not adept enough at Microchip assembly language to know the answer off the top of my head, but I got this snippet from Project #3 at Nigel Goodwin's WinPicProg site.  By the way, if you haven't been there, it's like GEO for PICs in a lot of ways.  Although Nigel isn't the teacher that R.G. is, IMNSHO.  Anyway, here's the snippet:


Text addwf PCL, f
retlw 'H'
retlw 'e'
retlw 'l'
retlw 'l'
retlw 'o'
retlw 0x00


I see some definite differences...

demonstar

Thanks David, I've tried tweaking the code slightly in many ways. The more I think about it, it is really odd that in the MPlab SIM all runs perfect but won't work on the chip. I've heard there can be paging issues associated with modifying the program counter while using lookup tables. Could this be the problem I've got?
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut"  Words of Albert Einstein

David

I've never used the simulator because in the past I've burned PICs on our main computer in the living room.  I have then taken them down to my shop to test in the breadboard.  I would advise you to study the code examples Nigel has.  One thing I saw in your code was the addwf statement in your jump table.  I didn't see addwf PCL, f.  I just saw addwf PCL.  Your bank switching logic looks a little unusual as well.

demonstar

Thanks again! I'm looking through Nigel's stuff now. I'm currently following a book through and to be honest I've been managing well until I've hit this problem. I noticed the ADDWF PCL,F statement in Nigel's work you posted and tried changing it to that, but no success. I THINK that having the ,f is not essential. Could you expand on "Your bank switching logic looks a little unusual as well."

I am very grateful of your help!
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut"  Words of Albert Einstein

David

#5
The only thing I can tell you about bank switching is that I've done very little of it.  I had a MIDI bass pedal system largely complete.  The pedal and footswitch logic started off as Nigel's keypad program.  I did what he did when it came to bank switching.  What I do see is that you appear to be setting a bit he doesn't.

I can also tell you that I implemented a jump table based on the code fragment I showed you.  I did use ADDWF PCL, F and it did work.

demonstar

I just wanted to say I got it working so thanks for the help David. I'm not quite sure what made it work. It might not have been working properly because of a mixture of many reasons but anyway it's up and running now.
"If A is success in life, then A equals x plus y plus z. Work is x; y is play; and z is keeping your mouth shut"  Words of Albert Einstein

David

Excellent news, Demonstar!  I suggest a name change to "Angelstar"!   :icon_mrgreen: :icon_lol: ;D ;D 8) :icon_biggrin:
Now, I suggest you find out how to go about getting this into the "code library".  Good work!