Menu

'Wait uS' using a variable does not work for AVR

2015-02-02
2015-02-04
  • Frank Steinberg

    Frank Steinberg - 2015-02-02

    Hi,

    this code does not work on an AVR (similar with PulseOut ... uS):

    #chip tiny85, 16
    
    Dim DelayTime As Word
    DelayTime = 6789
    Wait DelayTime uS
    

    This is the 'Wait uS' related asm output:

    ;**************************
    
    Delay_US:
      ret
    
    ;**************************
    

    Everything seems ok, when
    - i use a constant value instead a variable,
    - i use mS instead uS,
    - code is for a PIC.

    Is it a bug or simply not implemented for AVR?

    Regards
    Steini

     
  • Hugh Considine

    Hugh Considine - 2015-02-03

    That wasn't implemented, even though it should have been implemented way back in 2009 when GCBASIC first started supporting AVRs. But hopefully that is now corrected, please try the compiler exe at http://gcbasic.sourceforge.net/newfiles/gcbasic.exe and let us know if it helps!

    It should work on any AVR faster than 3 MHz, and there will be some inaccuracy depending on the exact speed of the chip (about 12 cycles of extra delay). Tested in AVR Studio 4 with an ATmega328p at 8 MHz, a 100 us delay took 102 us and a 1000 us delay took 1003 us.

    There is one specific section of code that handles variable length microsecond delays, which is why everything works on PIC, or when using millisecond delays or constant lengths.

     
  • Frank Steinberg

    Frank Steinberg - 2015-02-03

    Hello Hugh!

    It works now with my tiny85 - thanks for the quick reply. I tested with this code (moving a servo):

    #chip tiny85, 16
    
    #define ServoMin 1025
    #define ServoMax 1791
    #define Servo PORTB.4
    Dim DelayTime as Word
    Dir Servo Out
    
    Do
      'Move servo forward:
      For DelayTime = ServoMin to ServoMax
        PulseOut Servo, DelayTime uS
        Wait 10 ms
      Next
      'Move servo backward:
      For DelayTime = ServoMax to ServoMin Step -1
        PulseOut Servo, DelayTime uS
        Wait 10 ms
      Next
    Loop
    

    There is still one difference with the same code for PIC(16f1509). While the servo connected to the PIC moves smooth the whole way back and forth, the servo on AVR has noticeably 'ticks'. Both are running at 16Mhz with the internal oscillator. The 'ticks' seem to occur, when the asm code jumps to the outer loop of the uS delay (two times per direction with the attached code)??

    Regards
    Steini

     
  • Hugh Considine

    Hugh Considine - 2015-02-04

    Thanks for the code Steini! Ran it here, and saw the same ticks. Looking at the compiler code, I left out one instruction when translating the PIC delay code to AVR, which added 256 us to delays that were a multiple of 256 us.

    I've updated the compiler exe at the link above, please try again and see if it works smoothly now!

     
  • Frank Steinberg

    Frank Steinberg - 2015-02-04

    SMOOTHLY here too; great!

    Steini

     

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.