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


Groups > sci.electronics.design > #380452 > unrolled thread

OT: programming problem

Started byRobert Baer <robertbaer@localnet.com>
First post2015-10-05 23:09 -0700
Last post2015-10-07 19:33 -0700
Articles 17 — 6 participants

Back to article view | Back to sci.electronics.design


Contents

  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

#380452 — OT: programming problem

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-05 23:09 -0700
SubjectOT: programming problem
Message-ID<dAJQx.52004$P21.14655@fx09.iad>
   Yes, the same old PIC16F648A..

DELAY   MOVWF   sW     ; save W
; following gives Warning: Argument out of range.  Least significant 
bits used.
         MOVF    sSTATUS,STATUS  ;assy: d=1;sSTATUS@25 == save STATUS
         CLRF    COUNT1
         CLRF    COUNT2
         MOVLW   d'16'  ; fudge for about 1 Sec delay
         MOVWF   COUNT3

LOOPIE  DECFSZ  COUNT3
         GOTO    LUPOTR
         GOTO    DEXIT

LUPOTR  DECFSZ  COUNT2
         GOTO    LUPINR  ; COUNTed once,do inner loop
         GOTO    LOOPIE

LUPINR  INCFSZ  COUNT1  ; race inside inner loop
         GOTO    LUPINR  ; high speed count
         GOTO    LUPOTR  ; go for another outer count

DEXIT:  MOVF    sW      ; restore W
; following gives Warning: Argument out of range.  Least significant 
bits used.
         MOVF    STATUS,sSTATUS  ;assy: d=0;STATUS@03  == restore STATUS
         RETURN
   Using this shows that W is NOT restored, that it set to the delay value.
   Therefore, i can assume ditto for STATUS.
   Comments in listing derived from compiled code; "d" stands for the 
direction bit.

   Help?

[toc] | [next] | [standalone]


#380478

Frompiglet <erichpwagner@hotmail.com>
Date2015-10-06 09:48 +0100
Message-ID<mv01og$cr7$1@dont-email.me>
In reply to#380452
On 06/10/2015 07:09, Robert Baer wrote:

>          MOVF    sSTATUS,STATUS  ;assy: d=1;sSTATUS@25 == save STATUS
 > ....
>          MOVF    STATUS,sSTATUS  ;assy: d=0;STATUS@03  == restore STATUS
>          RETURN
>    Using this shows that W is NOT restored, that it set to the delay value.

You cannot do register to (different) register moves. MOVF will either 
move specified file register to W or to itself (the utility of which is 
that it updates flags, e.g. for a Z flag test later on).

When you wrote movf sSTATUS,STATUS the compiler saw the second parameter 
(STATUS) is non-zero and so set direction d=1 when coding.

So in your example above do this to save status word:

movf STATUS,W
movwf sSTATUS
...
...
movf sSTATUS,W
movwf STATUS

It is all in the chip datasheet.

piglet

[toc] | [prev] | [next] | [standalone]


#380521

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-06 09:58 -0700
Message-ID<G4TQx.371$q%3.108@fx24.iad>
In reply to#380478
piglet wrote:
> On 06/10/2015 07:09, Robert Baer wrote:
>
>> MOVF sSTATUS,STATUS ;assy: d=1;sSTATUS@25 == save STATUS
>  > ....
>> MOVF STATUS,sSTATUS ;assy: d=0;STATUS@03 == restore STATUS
>> RETURN
>> Using this shows that W is NOT restored, that it set to the delay value.
>
> You cannot do register to (different) register moves. MOVF will either
> move specified file register to W or to itself (the utility of which is
> that it updates flags, e.g. for a Z flag test later on).
>
> When you wrote movf sSTATUS,STATUS the compiler saw the second parameter
> (STATUS) is non-zero and so set direction d=1 when coding.
>
> So in your example above do this to save status word:
>
> movf STATUS,W
> movwf sSTATUS
> ...
> ...
> movf sSTATUS,W
> movwf STATUS
>
> It is all in the chip datasheet.
>
> piglet
   Thanks lots!

[toc] | [prev] | [next] | [standalone]


#380481

FromMartin Brown <|||newspam|||@nezumi.demon.co.uk>
Date2015-10-06 10:51 +0100
Message-ID<mv05ie$66b$1@speranza.aioe.org>
In reply to#380452
On 06/10/2015 07:09, Robert Baer wrote:
 >    Yes, the same old PIC16F648A..
 >
 > DELAY   MOVWF   sW     ; save W
 > ; following gives Warning: Argument out of range.  Least significant
 > bits used.
 >          MOVF    sSTATUS,STATUS  ;assy: d=1;sSTATUS@25 == save STATUS
 >          CLRF    COUNT1
 >          CLRF    COUNT2
 >          MOVLW   d'16'  ; fudge for about 1 Sec delay
 >          MOVWF   COUNT3
 >
 > LOOPIE  DECFSZ  COUNT3
 >          GOTO    LUPOTR
 >          GOTO    DEXIT
 >
 > LUPOTR  DECFSZ  COUNT2
 >          GOTO    LUPINR  ; COUNTed once,do inner loop
 >          GOTO    LOOPIE
 >
 > LUPINR  INCFSZ  COUNT1  ; race inside inner loop
 >          GOTO    LUPINR  ; high speed count
 >          GOTO    LUPOTR  ; go for another outer count
 >
 > DEXIT:  MOVF    sW      ; restore W
 > ; following gives Warning: Argument out of range.  Least significant
 > bits used.
 >          MOVF    STATUS,sSTATUS  ;assy: d=0;STATUS@03  == restore STATUS
 >          RETURN
 >    Using this shows that W is NOT restored, that it set to the delay 
value.
 >    Therefore, i can assume ditto for STATUS.
 >    Comments in listing derived from compiled code; "d" stands for the
 > direction bit.
 >
 >    Help?

Learning how to program in PIC assembler might help. RTFM

http://tutor.al-williams.com/pic-inst.html

MOVF sW,W

MOVF sW,F merely tests if sW==0

Is this some sort of competition to maximise the number of GOTOs and 
random flow of control? Heaven help the maintenance programmers.

If you reorder the code sensibly half of the GOTOs vanish.
COUNT3 needs to be one less due to dropping through the loop code.

LUPINR INCFSZ COUNT1
        GOTO   LUPINR
        DECFSZ COUNT2
        GOTO   LUPINR
        DECFSZ COUNT3
        GOTO   LUPINR
DEXIT

I prefer to have a single delay routine that accepts W on input as the 
exact number of cycles to delay by as a separate routine. It makes doing 
variable precision delays in software so much easier. YMMV

-- 
Regards,
Martin Brown

[toc] | [prev] | [next] | [standalone]


#380522

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-06 10:11 -0700
Message-ID<RgTQx.75969$up4.24745@fx16.iad>
In reply to#380481
Martin Brown wrote:
> On 06/10/2015 07:09, Robert Baer wrote:
>  > Yes, the same old PIC16F648A..
>  >
>  > DELAY MOVWF sW ; save W
>  > ; following gives Warning: Argument out of range. Least significant
>  > bits used.
>  > MOVF sSTATUS,STATUS ;assy: d=1;sSTATUS@25 == save STATUS
>  > CLRF COUNT1
>  > CLRF COUNT2
>  > MOVLW d'16' ; fudge for about 1 Sec delay
>  > MOVWF COUNT3
>  >
>  > LOOPIE DECFSZ COUNT3
>  > GOTO LUPOTR
>  > GOTO DEXIT
>  >
>  > LUPOTR DECFSZ COUNT2
>  > GOTO LUPINR ; COUNTed once,do inner loop
>  > GOTO LOOPIE
>  >
>  > LUPINR INCFSZ COUNT1 ; race inside inner loop
>  > GOTO LUPINR ; high speed count
>  > GOTO LUPOTR ; go for another outer count
>  >
>  > DEXIT: MOVF sW ; restore W
>  > ; following gives Warning: Argument out of range. Least significant
>  > bits used.
>  > MOVF STATUS,sSTATUS ;assy: d=0;STATUS@03 == restore STATUS
>  > RETURN
>  > Using this shows that W is NOT restored, that it set to the delay value.
>  > Therefore, i can assume ditto for STATUS.
>  > Comments in listing derived from compiled code; "d" stands for the
>  > direction bit.
>  >
>  > Help?
>
> Learning how to program in PIC assembler might help. RTFM
>
> http://tutor.al-williams.com/pic-inst.html
>
> MOVF sW,W
>
> MOVF sW,F merely tests if sW==0
>
> Is this some sort of competition to maximise the number of GOTOs and
> random flow of control? Heaven help the maintenance programmers.
>
> If you reorder the code sensibly half of the GOTOs vanish.
> COUNT3 needs to be one less due to dropping through the loop code.
>
> LUPINR INCFSZ COUNT1
> GOTO LUPINR
> DECFSZ COUNT2
> GOTO LUPINR
> DECFSZ COUNT3
> GOTO LUPINR
> DEXIT
>
> I prefer to have a single delay routine that accepts W on input as the
> exact number of cycles to delay by as a separate routine. It makes doing
> variable precision delays in software so much easier. YMMV
>
   Thanks again.

[toc] | [prev] | [next] | [standalone]


#380544

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-06 13:40 -0700
Message-ID<KkWQx.4$VR.3@fx01.iad>
In reply to#380522
Robert Baer wrote:
> Martin Brown wrote:
>> On 06/10/2015 07:09, Robert Baer wrote:
>> > Yes, the same old PIC16F648A..
>> >
>> > DELAY MOVWF sW ; save W
>> > ; following gives Warning: Argument out of range. Least significant
>> > bits used.
>> > MOVF sSTATUS,STATUS ;assy: d=1;sSTATUS@25 == save STATUS
>> > CLRF COUNT1
>> > CLRF COUNT2
>> > MOVLW d'16' ; fudge for about 1 Sec delay
>> > MOVWF COUNT3
>> >
>> > LOOPIE DECFSZ COUNT3
>> > GOTO LUPOTR
>> > GOTO DEXIT
>> >
>> > LUPOTR DECFSZ COUNT2
>> > GOTO LUPINR ; COUNTed once,do inner loop
>> > GOTO LOOPIE
>> >
>> > LUPINR INCFSZ COUNT1 ; race inside inner loop
>> > GOTO LUPINR ; high speed count
>> > GOTO LUPOTR ; go for another outer count
>> >
>> > DEXIT: MOVF sW ; restore W
>> > ; following gives Warning: Argument out of range. Least significant
>> > bits used.
>> > MOVF STATUS,sSTATUS ;assy: d=0;STATUS@03 == restore STATUS
>> > RETURN
>> > Using this shows that W is NOT restored, that it set to the delay
>> value.
>> > Therefore, i can assume ditto for STATUS.
>> > Comments in listing derived from compiled code; "d" stands for the
>> > direction bit.
>> >
>> > Help?
>>
>> Learning how to program in PIC assembler might help. RTFM
>>
>> http://tutor.al-williams.com/pic-inst.html
>>
>> MOVF sW,W
>>
>> MOVF sW,F merely tests if sW==0
>>
>> Is this some sort of competition to maximise the number of GOTOs and
>> random flow of control? Heaven help the maintenance programmers.
>>
>> If you reorder the code sensibly half of the GOTOs vanish.
>> COUNT3 needs to be one less due to dropping through the loop code.
>>
>> LUPINR INCFSZ COUNT1
>> GOTO LUPINR
>> DECFSZ COUNT2
>> GOTO LUPINR
>> DECFSZ COUNT3
>> GOTO LUPINR
>> DEXIT
>>
>> I prefer to have a single delay routine that accepts W on input as the
>> exact number of cycles to delay by as a separate routine. It makes doing
>> variable precision delays in software so much easier. YMMV
>>
> Thanks again.
>
   I now have an "improved" version, but still do not have satisfactory 
results.
**
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   0xFF
         MOVWF   6       ; Port B ones
         CALL    DELAY   ; "freeze" LEDs
setup:
        MOVLW    Low(tab1) ; tab1 at org 0x0100
        ADDWF    ptrlo,W   ; add pointer to W [so W=0 now]
        MOVLW    High(tab1) ;                 [so W=1 now]
        BTFSC    STATUS,C
        INCF     PCLATH,F  ; carry to hi
     ;  CALL     tab1      ; first table, first location (=0)
     ;   RETurns here
         MOVWF   6       ; Port B from W   shows 0x01  [OK]
         CALL    DELAY   ; "freeze" LEDs
repeat: INCF    ptrlo           ; next loc
         MOVWF   6       ; Port B from W   shows 0x18  [???]
         CALL    DELAY   ; "freeze" LEDs
         INCF    ptrlo           ; next loc
         MOVWF   6       ; Port B from W   shows 0x18
         CALL    DELAY   ; "freeze" LEDs
         INCF    ptrlo           ; next loc
      ;   BTFSC   STATUS,Z
      ;   INCF    ptrhi
         MOVWF   6       ; Port B from W   shows 0x18
         CALL    DELAY   ; "freeze" LEDs
         GOTO    setup
; ----------------------------------------------
DELAY   MOVWF   sW
         MOVF    STATUS,W
         MOVWF   sSTATUS
         CLRF    COUNT1
         CLRF    COUNT2
         MOVLW   d'93'  ; fudge for about 4 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
         RETURN
**
   So something in the delay routine is setting W to 0x18.
   WTH?

[toc] | [prev] | [next] | [standalone]


#380601

FromMartin Brown <|||newspam|||@nezumi.demon.co.uk>
Date2015-10-07 09:14 +0100
Message-ID<mv2k94$j8k$1@speranza.aioe.org>
In reply to#380544
On 06/10/2015 21:40, Robert Baer wrote:

[snip]

> DEXIT:  MOVF    sSTATUS,W  <<<< last instruction affecting W
>          MOVWF   STATUS
>          MOVF    sW
>          RETURN
> **
>    So something in the delay routine is setting W to 0x18.
>    WTH?

Your attention to detail is worryingly limited.

MOVF sW,W  moves the saved sW into W

Again you have coded a NOP unless sW==0

-- 
Regards,
Martin Brown

[toc] | [prev] | [next] | [standalone]


#380713

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-07 19:50 -0700
Message-ID<ARkRx.113740$5H1.98110@fx23.iad>
In reply to#380601
Martin Brown wrote:
> On 06/10/2015 21:40, Robert Baer wrote:
>
> [snip]
>
>> DEXIT: MOVF sSTATUS,W <<<< last instruction affecting W
>> MOVWF STATUS
>> MOVF sW
>> RETURN
>> **
>> So something in the delay routine is setting W to 0x18.
>> WTH?
>
> Your attention to detail is worryingly limited.
>
> MOVF sW,W moves the saved sW into W
>
> Again you have coded a NOP unless sW==0
>
1) That is exactly what i want; to save sW into W.
2) Compiler issues 0x0826 which interprets into a MOVF direction bit=0 
meaning destination is W.

[toc] | [prev] | [next] | [standalone]


#380565

From"P E Schoen" <paul@pstech-inc.com>
Date2015-10-06 21:01 -0400
Message-ID<mv1qpq$ah1$1@dont-email.me>
In reply to#380452
"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.

Here is a sample of what can be done. I do almost everything in C now, so I 
had to search for something in ASM:

;Timer0.asm
;Reads ADC (AN0, pin 7) and sets a delay accordingly.

    #include    "P12F675.INC"

    #define LED 2    ;GP2 (pin 5)
    #define INIT_COUNT 10    ;

    errorlevel -302        ;suppress "not in bank 0" message

    cblock    0x20
    tick_counter
    temp
    ADC_value_lo
    ADC_value_hi
    endc

    ;reset vector
    org        0
    clrf    PCLATH
    goto    main

    ;interrupt vector
    org        4
    banksel    INTCON
    bcf        INTCON,T0IF        ;clear Timer0 interrupt flag
    movlw    INIT_COUNT        ;re-initialise count
    movwf    TMR0
    decf    tick_counter,f
    retfie

main:
    banksel TRISIO
    movlw    0x00            ;all pins outputs
    movwf    TRISIO
    bsf        TRISIO, GP0        ;AN0 input
    banksel ANSEL
    movlw     0x01             ;AN0 as analog input
    movwf     ANSEL
    banksel    ADCON0
    movlw    0x01            ;ADC clock Fosc/8, channel 0, ADC turned on 
(01000001)
    movwf     ADCON0
    banksel    OPTION_REG
    movlw    b'00000011'        ;prescaler 1/128
    movwf    OPTION_REG        ;
    banksel    TMR0
    movlw    INIT_COUNT        ;initialise timer count value
    clrf    tick_counter
    movwf    TMR0
    bsf        INTCON,GIE        ;enable global interrupt
    bsf        INTCON,T0IE        ;enable Timer0 interrupt
    banksel    0

mainloop:
    call    read_ADC
    bsf        GPIO,LED
    call    dly
    bcf        GPIO,LED
    call    dly
    goto    mainloop

dly:
    movfw    ADC_value_hi
    movwf    tick_counter
dly1:
    movf    tick_counter,f
    skpz
    goto    dly1
    return

;--------------------------------------------------------------------- 
;read_ADC
;function to read ADC
;returns high and low bytes (10 bits) in ADC_value_hi
;and ADC_value_lo variables. Value is left-justified.
;--------------------------------------------------------------------- 

read_ADC:
    banksel ADCON0
    bsf     ADCON0,GO    ;start conversion
WaitADC:
    btfss     ADCON0,GO    ;GO/DONE clear when conversion is complete
    goto     WaitADC        ;wait for ADC to finish
GetADC1:
    banksel    ADRESH
    movfw     ADRESH        ;get high byte in W
    movwf     ADC_value_hi
    bcf        STATUS,Z
    rrf        ADC_value_hi,f    ;divide by 2
    movlw    .10                ;and add a constant
    addwf    ADC_value_hi,f    ;to give a narrow range of values
    return

    end

=============================================

HTH,

Paul 

[toc] | [prev] | [next] | [standalone]


#380602

FromMartin Brown <|||newspam|||@nezumi.demon.co.uk>
Date2015-10-07 09:16 +0100
Message-ID<mv2kcl$j8k$2@speranza.aioe.org>
In reply to#380565
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).

-- 
Regards,
Martin Brown

[toc] | [prev] | [next] | [standalone]


#380610

FromLasse Langwadt Christensen <langwadt@fonz.dk>
Date2015-10-07 02:00 -0700
Message-ID<b9d14db4-8ef2-438f-8029-e19d05df672a@googlegroups.com>
In reply to#380602
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

[toc] | [prev] | [next] | [standalone]


#380722 — Re: OT: programming problem, NASTY

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-07 22:23 -0700
SubjectRe: OT: programming problem, NASTY
Message-ID<75nRx.61064$YG4.12078@fx14.iad>
In reply to#380610
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?

[toc] | [prev] | [next] | [standalone]


#380735 — Re: OT: programming problem, NASTY

FromMartin Brown <|||newspam|||@nezumi.demon.co.uk>
Date2015-10-08 12:05 +0100
SubjectRe: OT: programming problem, NASTY
Message-ID<mv5il7$f4m$1@speranza.aioe.org>
In reply to#380722
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

[toc] | [prev] | [next] | [standalone]


#380748 — Re: OT: programming problem, NASTY

Frompiglet <erichpwagner@hotmail.com>
Date2015-10-08 16:31 +0100
SubjectRe: OT: programming problem, NASTY
Message-ID<mv624e$hag$1@dont-email.me>
In reply to#380722
On 08/10/2015 06:23, Robert Baer wrote:
>          CALL DELAY
>          GOTO repeat

Follow the flow around those two above instructions (call delay, goto 
repeat), once and then again and again. Then be very grateful the PIC 
has a small return stack!

piglet

[toc] | [prev] | [next] | [standalone]


#380716

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-07 20:10 -0700
Message-ID<B8lRx.4432$q%3.2304@fx24.iad>
In reply to#380602
Martin Brown wrote:
> 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).
>
   Different problem...cut and paste of some sessions:

Erasing...
Erase device complete

Programming...
Programming/Verify complete

Target Removed

Target Detected
Device ID Revision = 00000005

Target Removed

Target Detected
Device ID Revision = 00000005

Erasing...
Erase device complete

Programming...
Programming/Verify complete

Target Removed            ** unplugged PICkit3

Target Detected           ** plugged it back in
Target Device ID (00000000) does not match expected Device
ID (00001100).            ** note error message

Erasing...
Erase device complete

Erasing...
Erase device complete

Programming...
Programming/Verify complete

Target Removed            ** unplugged PICkit3

Target Detected           ** plugged it back in
Device ID Revision = 00000005  ** note OK

   This is a PIC16F648A that had exhibited that problem and "stuck" bad 
and would not erase. Fixed it by baking at 200F for an hour.

   I have the whole project board wrapped in anti-static bubble wrap 
this time.
   The only possible niggle is that the grounds (PICkit3, project board) 
are not exactly the same when disconnected; 
insulation/separation/connection is via the bubble wrap.
   BUT the device now looks OK...

[toc] | [prev] | [next] | [standalone]


#380744

Fromnew_ibm <randy.day@sasktel.netx>
Date2015-10-08 08:33 -0600
Message-ID<MPG.30803abae188173d989680@freenews.netfront.net>
In reply to#380716
In article <B8lRx.4432$q%3.2304@fx24.iad>, robertbaer@localnet.com 
says...

[snip]

>    This is a PIC16F648A that had exhibited that problem and "stuck" bad 
> and would not erase. Fixed it by baking at 200F for an hour.

I'd check for intermittent contacts on the 
pickit3 as well; I had to reolder the 
connector on mine not long after I got it.


--- news://freenews.netfront.net/ - complaints: news@netfront.net ---

[toc] | [prev] | [next] | [standalone]


#380711

FromRobert Baer <robertbaer@localnet.com>
Date2015-10-07 19:33 -0700
Message-ID<DBkRx.2892$yI3.2717@fx31.iad>
In reply to#380565
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.
>
> Here is a sample of what can be done. I do almost everything in C now,
> so I had to search for something in ASM:
>
> ;Timer0.asm
> ;Reads ADC (AN0, pin 7) and sets a delay accordingly.
>
> #include "P12F675.INC"
>
> #define LED 2 ;GP2 (pin 5)
> #define INIT_COUNT 10 ;
>
> errorlevel -302 ;suppress "not in bank 0" message
>
> cblock 0x20
> tick_counter
> temp
> ADC_value_lo
> ADC_value_hi
> endc
>
> ;reset vector
> org 0
> clrf PCLATH
> goto main
>
> ;interrupt vector
> org 4
> banksel INTCON
> bcf INTCON,T0IF ;clear Timer0 interrupt flag
> movlw INIT_COUNT ;re-initialise count
> movwf TMR0
> decf tick_counter,f
> retfie
>
> main:
> banksel TRISIO
> movlw 0x00 ;all pins outputs
> movwf TRISIO
> bsf TRISIO, GP0 ;AN0 input
> banksel ANSEL
> movlw 0x01 ;AN0 as analog input
> movwf ANSEL
> banksel ADCON0
> movlw 0x01 ;ADC clock Fosc/8, channel 0, ADC turned on (01000001)
> movwf ADCON0
> banksel OPTION_REG
> movlw b'00000011' ;prescaler 1/128
> movwf OPTION_REG ;
> banksel TMR0
> movlw INIT_COUNT ;initialise timer count value
> clrf tick_counter
> movwf TMR0
> bsf INTCON,GIE ;enable global interrupt
> bsf INTCON,T0IE ;enable Timer0 interrupt
> banksel 0
>
> mainloop:
> call read_ADC
> bsf GPIO,LED
> call dly
> bcf GPIO,LED
> call dly
> goto mainloop
>
> dly:
> movfw ADC_value_hi
> movwf tick_counter
> dly1:
> movf tick_counter,f
> skpz
> goto dly1
> return
>
> ;---------------------------------------------------------------------
> ;read_ADC
> ;function to read ADC
> ;returns high and low bytes (10 bits) in ADC_value_hi
> ;and ADC_value_lo variables. Value is left-justified.
> ;---------------------------------------------------------------------
> read_ADC:
> banksel ADCON0
> bsf ADCON0,GO ;start conversion
> WaitADC:
> btfss ADCON0,GO ;GO/DONE clear when conversion is complete
> goto WaitADC ;wait for ADC to finish
> GetADC1:
> banksel ADRESH
> movfw ADRESH ;get high byte in W
> movwf ADC_value_hi
> bcf STATUS,Z
> rrf ADC_value_hi,f ;divide by 2
> movlw .10 ;and add a constant
> addwf ADC_value_hi,f ;to give a narrow range of values
> return
>
> end
>
> =============================================
>
> HTH,
>
> Paul
   Thanks.
   Like that un-documented error suppression trick.

[toc] | [prev] | [standalone]


Back to top | Article view | sci.electronics.design


csiph-web