Menu

Code review - fix for incorrect specification of WAIT time units

Anobium
2021-10-10
2021-10-12
  • Anobium

    Anobium - 2021-10-10

    This is a long standing issue where the units of the WAIT command are incorrect. The errors issued by the compiler are meaningless.


    Example 1

    #chip 10f200
    wait 10 10 us  'where this is intended to be 10 10us
    

    Error message is:

    10usBug.gcb (2): Warning: Inaccurate microsecond delay due to use of variable at the current clock speed

    Example 2

    #chip 16f1503
    #define AD_Delay 4 10 us
    ADCVAlue=ReadAD(an0)
    

    Error messages are:

    a-d.h (1682): Warning: Inaccurate microsecond delay due to use of variable at the current clock speed a-d.h (1682): Error: Incorrect parameters in Set, expected: Set variable.bit status

    Example 3

    #chip 16f18855
    'USART settings for USART1
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 0 10 us
    
    HSerPrint ""
    

    Error message is:

    usart.h (682): Error: Invalid variable name: 010


    The root cause is the compiler is not correctly parsing the WAIT value parameter. After a few hours of hacking the code the following change to the compiler could resolve.

            'Check the syntax of the Value... does it have a space or other error?
            If InStr(Value, " ") <> 0 Then  'The Value has spaces but this cound be a calc like 'show_timer / 4 ms'
              For UP = 1 to LEN(Value)
                lValueASC = ASC(MID(Value,UP,1))
                'walk the Value looking for NON numeric values.. if non numeric then we can assume that we have numbers only and numbers can be tested 
                If ( lValueASC < 47 OR  lValueASC > 57 ) and lValueASC <> 32 Then 'this is NOT a numeric constant
                    Goto NoTNumericConstant
                End if
              Next
              'Test a numeric value of Value .. if Value <> Str(Val(Value)) then we have something like '10 10'... error!
              If Value <> Str(Val(Value)) Then
                  LogError Message("IncorrectWaitParameter"), Origin
                  CurrLine = LinkedListDelete(CurrLine)
                  GoTo EndWaitCompile
              End If
            End if
            NoTNumericConstant:
    

    With the change above the following messages are now issued.

    Example 1

    #chip 10f200
    wait 10 10 us  'where this is intended to be 10 10us
    

    Error message is:

    10usBug.gcb (2): Error: Incorrect time unit specified. This can be an indirect error from a subroutine - so, inspect all time based constants

    Example 2

    #chip 16f1503
    #define AD_Delay 4 10 us
    ADCVAlue=ReadAD(an0)
    

    Error messages are:

    10usBug.gcb (2): Error: Incorrect time unit specified. This can be an indirect error from a subroutine - so, inspect all time based constants

    Example 3

    #chip 16f18855
    'USART settings for USART1
    #define USART_BAUD_RATE 9600
    #define USART_TX_BLOCKING
    #define USART_DELAY 0 10 us
    
    HSerPrint ""
    

    Error message is:

    usart.h (936): Error: Incorrect time unit specified. This can be an indirect error from a subroutine - so, inspect all time based constants


    Anyone spot any issues with this solution to the problem?

    Evan

     

    Last edit: Anobium 2021-10-11
  • Anobium

    Anobium - 2021-10-11

    Updated the code to handle Constants.

     'Patch 1041 10/10/2021
        'Check the syntax of the Value... does it have a space or other error?
        ExpandedValue = ReplaceConstantsLine(trim(Value),0)
        print Value, ExpandedValue, Str(Val(ExpandedValue))
        If InStr(ExpandedValue, " ") <> 0 Then  'The Value has spaces but this cound be a calc like 'show_timer / 4 ms'
          For UP = 1 to LEN(ExpandedValue)
            lValueASC = ASC(MID(ExpandedValue,UP,1))
            'walk the Value looking for NON numeric values.. if non numeric then we can assume that we have numbers only and numbers can be tested
            If ( lValueASC < 47 OR  lValueASC > 57 ) and lValueASC <> 32 Then 'this is NOT a numeric constant
                Goto NoTNumericConstant
            End if
          Next
          'Test a numeric value of Value .. if Value <> Str(Val(Value)) then we have something like '10 10'... error!
          If trim(ExpandedValue) <> trim(Str(Val(ExpandedValue))) Then
              LogError Message("IncorrectWaitParameter"), Origin
              CurrLine = LinkedListDelete(CurrLine)
              GoTo EndWaitCompile
          End If
        End if
        NoTNumericConstant:
        'End of Patch 1041
    
     

    Last edit: Anobium 2021-10-11
    • jackjames

      jackjames - 2021-10-11

      Hi Evan.
      Is the modified code available for download?

       
      • Anobium

        Anobium - 2021-10-11

        Build 1042 is uploaded.

        Enjoy

         
  • Anobium

    Anobium - 2021-10-11

    Yes, from the patches in File. If you wait until I post 1042 later today.

     
  • stan cartwright

    stan cartwright - 2021-10-11

    Why have I not seen any problems using wait?

     
    • Anobium

      Anobium - 2021-10-11

      Cus, you have not come across the bug. It is very specific to 10us and 10ms where the program has a space in the time parameter.

      But, if you had seen it.. you would have been scratching head, for a long time, looking for it.

       
      • stan cartwright

        stan cartwright - 2021-10-12

        I used pulsout 1500 10us instead of 150ms with rc servos and no problems but maybe pulseout is different to wait.

         
  • Anobium

    Anobium - 2021-10-11

    Just been told this is also fixed....

    wait 10 10 ms
    

    Again, a space in the time parameter. However, this was a silent failure where you got no warning the generated code was incorrect.

    :-) Fixed.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.