News:

Let's find out together what makes a PIC Tick!

Main Menu

Select-Case / If-then oddity ?

Started by CPR, Jul 09, 2021, 11:12 AM

Previous topic - Next topic

CPR

Hi all,

Attached a snippet of code described below, which is not working as I'd expect it to.
Has anyone any idea why the first version fails but the second version works correctly?


'************************************************************************
'*  Name    : Select-case / If-then oddity ?                            *
'*  Author  :                                                           *
'*  Notice  :                                                           *
'*          :                                                           *
'*  Date    :                                                           *
'*  Version : Code Produced by the Positron8 Compiler. Version 4.0.0.4  *
'*  Notes   : Code SNIPPET with problem                                 *
'*          :                                                           *
'************************************************************************
    Device = 18F1220                '// or 18F1320
    Declare Xtal = 8                '// 8Mhz base clock
    Declare PLL_Req = On            '// Device running at 32Mhz using PLL
    Declare All_Digital = 1   
                                    '// Declare Optimiser_Level = 3 (makes no difference)
   
    TRISB=%00000011                 '// all output except B.0 & B.1
    Symbol LED_G = LATB.3           '// GREEN LED
   
    Dim curve As Byte
    Dim linear_curve As Dword
    Dim rpmtime As Dword

'// ------------------------ NON WORKING -----------------------------
                       
                        curve = 2
                        LED_G = 0
                        'rpmtime = 14999
                        rpmtime = 15001
                       
                        Select curve                                        '// LED_G permanently lit
                        Case  2                       
                            If rpmtime >= 15000 Then                        '// This block appears to be skipped over
                            linear_curve = 544*rpmtime                     
                            linear_curve = linear_curve/1000000
                            linear_curve = (linear_curve + tab) - 36
                            LED_G = 0                                       '// doesn't extinguish with rpmtime >= 15000
                                Else                                        '// Execution appears to jump straight here
                                linear_curve = 0
                                LED_G = 1                                   '// should light with rpmtime < 15000
                            EndIf
                        End Select
                       
'// ------------------------ WORKING -----------------------------

                        curve = 2
                        LED_G = 0
                        'rpmtime = 14999
                        rpmtime = 15001
                       
                        If curve = 2 Then                                   '// LED_G works as expected
                            If rpmtime >= 15000 Then
                            linear_curve = 544*rpmtime                     
                            linear_curve = linear_curve/1000000
                            linear_curve = (linear_curve + tab) - 36
                            LED_G = 0                                       '// does extinguish with rpmtime >= 15000
                                Else
                                linear_curve = 0
                                LED_G = 1                                   '// does light with rpmtime < 15000
                            EndIf
                        EndIf


Pepe

Is select case curve not select curve

CPR

Hi Pepe, I just tried Select Case curve and the compiler accepted that but it's still not working.

Dompie

According to the manual it is EndSelect and not with a space between End and Select. I'd get that straight before you look any further.

Johan

top204

#4
With the Positron8 compiler, the EndSelect and End Select are exactly the same. I made EndSelect mandatory for Positron16 because it was a new syntax, but I had to leave it so it recognised the previous syntax for backward compatability for the 8-bit compiler, as well as recognising the new "recommended" syntax.

I've just run a test with both If-Then and the Select-EndSelect and they both operate exactly the same in the simulator using your code snippet. When the variable curve is holding a value of 2, the block of code inside the Case operates. However, if the variable curve is not holding the value of 2, the block of code does not operate. The same with the If-Endif block

The If and Select directives also both use the same assembler mnemonics as well. For example:

F1_000042 equ $ ; in [TEST_18F25K20.BAS] Select curve
F1_000043 equ $ ; in [TEST_18F25K20.BAS] Case  2
    movlw 2
    cpfseq curve,0
    bra _lbl__3

F1_000061 equ $ ; in [TEST_18F25K20.BAS] if curve = 2 Then
    movlw 2
    cpfseq curve,0
    bra _lbl__9

The Bra mnemonic's label will be different because they are jumping to different locations within the code where the EndSelect or Endif are placed, but they both use the cpfseq mnemonic, so if one comparison directive works, so does the other.

There seems to be something else going on within your code somewhere, but it does not seem to be the comparisons,

trastikata

Quote from: CPR on Jul 09, 2021, 11:12 AMHas anyone any idea why the first version fails but the second version works correctly?

Hi,

Select ends by "EndSelect" and not "End Select". "End Select" are actually two separate commands - End of program (continuous loop) and the beginning of new Select comparison. Same goes for If - EndIf and not "End If"

top204

See the beginning section of my previous post:

QuoteWith the Positron8 compiler, the EndSelect and End Select are exactly the same. I made EndSelect mandatory for Positron16 because it was a new syntax, but I had to leave it so it recognised the previous syntax for backward compatability for the 8-bit compiler, as well as recognising the new "recommended" syntax.

If the compiler did not recognise the directive or the command, it would create a syntax error, not just do it differently!

trastikata

Quote from: top204 on Jul 09, 2021, 02:54 PMSee the beginning section of my previous post...

Thank you for the clarification Les.

CPR

All, thanks for the hints and tips and Les for checking the code :-)

Les, a quick question:
I'm guessing the results of integer division are truncated? i.e a result of 2.9 is returned as 2, rather than 3.
Is this correct?

CPR


trastikata

Quote from: CPR on Jul 10, 2021, 05:02 PMI'll rephrase that 29 / 10 = 2

Yes, that's how it works.

CPR