Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > sci.electronics.design > #380735

Re: OT: programming problem, NASTY

From Martin Brown <|||newspam|||@nezumi.demon.co.uk>
Newsgroups sci.electronics.design
Subject Re: OT: programming problem, NASTY
Date 2015-10-08 12:05 +0100
Organization Aioe.org NNTP Server
Message-ID <mv5il7$f4m$1@speranza.aioe.org> (permalink)
References <dAJQx.52004$P21.14655@fx09.iad> <mv1qpq$ah1$1@dont-email.me> <mv2kcl$j8k$2@speranza.aioe.org> <b9d14db4-8ef2-438f-8029-e19d05df672a@googlegroups.com> <75nRx.61064$YG4.12078@fx14.iad>

Show all headers | View raw


On 08/10/2015 06:23, Robert Baer wrote:
> Lasse Langwadt Christensen wrote:
>> Den onsdag den 7. oktober 2015 kl. 10.16.26 UTC+2 skrev Martin Brown:
>>> On 07/10/2015 02:01, P E Schoen wrote:
>>>> "Robert Baer"  wrote in message news:dAJQx.52004$P21.14655@fx09.iad...
>>>>
>>>>>    Yes, the same old PIC16F648A..
>>>>
>>>>> DELAY   MOVWF   sW     ; save W
>>>> [snip code]
>>>>>    Help?
>>>>
>>>> It is usually better programming practice to implement longer delays by
>>>> using one of the timers with an interrupt and ISR. You can set the PR1
>>>> or PR2 registers, along with pre and post scalers, to get a convenient
>>>> timer tick, such as 1 mSec. Then you can also make a timer register
>>>> increment every 250 ticks for 1/4 second resolution.
>>>
>>> That is fine if you want longer delays and I agree it is a better
>>> solution (although his chances of getting it working are very low).
>>>
>>
>> http://xkcd.com/1586/
>>
>> -Lasse
>    First, program listing as-is:
>         LIST p=P16F648A
>         #include P16F648A.inc
>           ; Was P16F628A
>         __CONFIG       _CP_OFF & _WDT_OFF & _PWRTE_OFF & _HS_OSC &
> _LVP_OFF & _MCLRE_OFF & _BOREN_OFF
>         __IDLOCS       0x0001
>
> ;                  VARIABLES
> ptrlo       EQU         0x20    ; table pointer
> ptrhi       EQU         0x21
> COUNT1      EQU         0x22    ; delay counter
> COUNT2      EQU         0x23
> COUNT3      EQU         0x24
> sSTATUS     EQU         0x25
> sW          EQU         0x26
> ;
>          ORG     0x0000
>          GOTO    start
>          GOTO    start
>          GOTO    start
>          GOTO    start
>          ORG     0x0010
> start:  ;**             setup I/O
>          CLRF    6    ; Set all Port B pins to logic 0
>          BSF     3,5  ; Bank 1 command comes next STATUS RP0 set
>          CLRF    6    ; sel all Port B pins as outputs
>          BCF     3,5  ; Bank 0 command comes next STATUS RP0 clear
>
>          CLRF    ptrlo
>          CLRF    ptrhi
>
>          CLRW
>          MOVWF   6       ; Port B to zero
>          CALL    DELAY   ; "freeze" LEDs
>
>          MOVLW   0xA5    ;FF [changed to see pattern change]]
>          MOVWF   6       ; Port B ones
>          CALL    DELAY   ; "freeze" LEDs
> setup:
>         MOVLW    Low(tab1)
>         ADDWF    ptrlo,W   ; add pointer to W
>         MOVLW    High(tab1)
>         BTFSC    STATUS,C
>         INCF     PCLATH,F  ; carry to hi
>         CALL     tab1      ; first table, first location (=0), W=1
>      ;   RETurns here
>          MOVWF   6       ; Port B from W
>          CALL    DELAY   ; "freeze" LEDs
> repeat:
>          INCF    ptrlo           ; next loc
> ;        BTFSC   STATUS,C
> ;        INCF    ptrhi
>          MOVF    ptrlo,W
>         CALL     tab1    ;;*REMoving this, one sees W counting
>                  ;; otherwise, LEDs show 0x00 0xA5 0xA5 0xA5 0xA5...
>          MOVWF   6       ; Port B from W
>          CALL    DELAY   ; "freeze" LEDs
>          GOTO    repeat
>
> ; ----------------------------------------------
> DELAY   MOVWF   sW
>        ; MOVF    STATUS,W ;[REMoved STATUS stuff; no change]]
>        ; MOVWF   sSTATUS
>          CLRF    COUNT1
>          CLRF    COUNT2
>          MOVLW   d'43'  ; fudge for about 1 Sec delay
>          MOVWF   COUNT3
> LUPINR  DECFSZ  COUNT1
>          GOTO    LUPINR
>          DECFSZ  COUNT2
>          GOTO    LUPINR
>          DECFSZ  COUNT3
>          GOTO    LUPINR
> DEXIT:; MOVF    sSTATUS,W
>        ; MOVWF   STATUS
>          MOVF    sW,W
>          RETURN
> ; ---------------------------------------------- sine
>          ORG             0x0100
> tab1:
>       ;;   RETLW   0x01    ;       [0]     t=0.7500        r=0.0000
>       ;  ADDWF   PCL,F
>          CALL DELAY
>          GOTO repeat
>
>          RETLW   0x01    ;       [1]     t=0.7539        r=0.0240
>       ;;   RETLW   0x01    ;       [2]     t=0.7578        r=0.0964
>       ;;   RETLW   0x01    ;       [3]     t=0.7617        r=0.2173
>       ;;   RETLW   0x01    ;       [4]     t=0.7656        r=0.3865
>       ;;   RETLW   0x02    ;       [5]     t=0.7695        r=0.6040
>          RETLW   0x02    ;       [6]     t=0.7734        r=0.8696
>       ;;   RETLW   0x02    ;       [7]     t=0.7773        r=1.1832
>          RETLW   0x03    ;       [8]     t=0.7812        r=1.5445
>          RETLW   0x03    ;       [9]     t=0.7852        r=1.9534
>
>    WTH is happening?
>    Shouldn't i see the same thing either way?
>    Results are also nutso (exactly the same) if i have "standard"
> tab1:   ADDWF   PCL,F
>          RETLW   0x01    ;       [1]     t=0.7539        r=0.0240
>          RETLW   0x01    ;       [2]     t=0.7578        r=0.0964
>          RETLW   0x01    ;       [3]     t=0.7617        r=0.2173
>
>    Comments?

Snip the code down to the smallest segment that demonstrates the fault 
and describe what you see happen and what you expected to see.

How do you expect to tell the difference if all paths return value 1?

Run it in the emulator and/or single step it and watch the registers 
carefully. This has to be user error. PICs really do work!

Here are a couple of canonical concrete examples with no safety features 
for crossing page boundaries so be careful where you put them:

DIG2SEG:			; enter with 0 <= W <= F
	ANDLW $0F		; (make sure)
	ADDWF  PCL,1		
;
;  7 SEGMENT DIGIT CODE LUT

DIG_0  RETLW $3F		
DIG_1  RETLW $6		
DIG_2  RETLW $5B		
DIG_3  RETLW $4F		
DIG_4  RETLW $66		
DIG_5  RETLW $6D		
DIG_6  RETLW $7D		
DIG_7  RETLW $7		
DIG_8  RETLW $7F		
DIG_9  RETLW $67		
DIG_A  RETLW $77
DIG_B  RETLW $7C
DIG_C  RETLW $39
DIG_D  RETLW $5E
DIG_E  RETLW $79
DIG_F  RETLW $71


BITEXP2:
	 ANDLW $07	; RETURNS 2^W  (0 < W < 7)
          ADDWF PCL,1
	 RETLW $01
	 RETLW $02
	 RETLW $04
	 RETLW $08
	 RETLW $10
	 RETLW $20
	 RETLW $40
	 RETLW $80

Suggest you try and do one of your own to return 2*i+1 or something 
similar that can easily be checked.

A similar trick can be used to do fast multiplies by smallish fixed 
constants with a LUT for lsb and msb of the result.

-- 
Regards,
Martin Brown

Back to sci.electronics.design | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-05 23:09 -0700
  Re: OT: programming problem piglet <erichpwagner@hotmail.com> - 2015-10-06 09:48 +0100
    Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-06 09:58 -0700
  Re: OT: programming problem Martin Brown <|||newspam|||@nezumi.demon.co.uk> - 2015-10-06 10:51 +0100
    Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-06 10:11 -0700
      Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-06 13:40 -0700
        Re: OT: programming problem Martin Brown <|||newspam|||@nezumi.demon.co.uk> - 2015-10-07 09:14 +0100
          Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-07 19:50 -0700
  Re: OT: programming problem "P E Schoen" <paul@pstech-inc.com> - 2015-10-06 21:01 -0400
    Re: OT: programming problem Martin Brown <|||newspam|||@nezumi.demon.co.uk> - 2015-10-07 09:16 +0100
      Re: OT: programming problem Lasse Langwadt Christensen <langwadt@fonz.dk> - 2015-10-07 02:00 -0700
        Re: OT: programming problem, NASTY Robert Baer <robertbaer@localnet.com> - 2015-10-07 22:23 -0700
          Re: OT: programming problem, NASTY Martin Brown <|||newspam|||@nezumi.demon.co.uk> - 2015-10-08 12:05 +0100
          Re: OT: programming problem, NASTY piglet <erichpwagner@hotmail.com> - 2015-10-08 16:31 +0100
      Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-07 20:10 -0700
        Re: OT: programming problem new_ibm <randy.day@sasktel.netx> - 2015-10-08 08:33 -0600
    Re: OT: programming problem Robert Baer <robertbaer@localnet.com> - 2015-10-07 19:33 -0700

csiph-web