Menu

millis() wrong result

2022-01-24
2022-01-28
1 2 > >> (Page 1 of 2)
  • stan cartwright

    stan cartwright - 2022-01-24

    I tried this code and it should run at around 50Hz but usb scope says 60 odd kHz.
    If I scope the 1 KHz calibrate probe pin it says 1 KHz.

    #chip mega328p,16
    #option Explicit
    #include <millis.h>
    dim elapsed as word
    dir portb.1 out
    elapsed=millis()
    do
      if millis()-elapsed>=20 then
        elapsed=millis()
        portb.1=!portb.1
      end if
    loop
    
     

    Last edit: stan cartwright 2022-01-24
  • William Roth

    William Roth - 2022-01-25

    Looks like you have it coded incorrectly. Try this instead ( Adapted from Demo) This should set B.1 on for 10 ms then off for 10ms giving a frequency of ~50Hz (Untested)

    #chip mega328p,16
    #option Explicit
    #include <millis.h>
    Dim CurrentMs as WORD
    Dim PreviousMs as WORD
    Dir PortB.1 out
    
    CurrentMs = 0
    PreviousMs = 0
    
    Do
        CurrentMs = Millis() 
        If CurrentMs - PreviousMs >= 10 then 
            PortB.1 = !PortB.1 
            PreviousMs = CurrentMs
        End if
    Loop
    
     

    Last edit: William Roth 2022-01-25
  • stan cartwright

    stan cartwright - 2022-01-25

    Thanks. I saw Chris Ropers demo
    I don't see my code gave so high a frequency. and why

     if millis()-elapsed>=20 then
        elapsed=millis()
    

    Didn't work. I shouldgiver 25Hz.

     
  • Chris Roper

    Chris Roper - 2022-01-25

    NO - That should give 50Hz

    20mS = 20E-3 = 0.020
    1/0.02 = 50

    if you want 25Hz then:
    1/25 = 0.04 = 40E-3 or 40mS

    so

    if millis()-elapsed>=40 then ' will generate a 25Hz signal

     

    Last edit: Chris Roper 2022-01-25
    • stan cartwright

      stan cartwright - 2022-01-25

      CurrentMs - PreviousMs >= 20 gives 25Hz
      CurrentMs - PreviousMs >= 10 gives 50Hz

       
  • William Roth

    William Roth - 2022-01-26

    Sure. The pin is on for 10 ms then off for 10ms giving a period of 20ms. 1000ms / 20 ms = 50Hz

     
  • stan cartwright

    stan cartwright - 2022-01-26

    Don't buy a new usb scope. Get a second hand hardware scope with real knobs.
    Modern ones are all software dso though. I wouldn't buy an old scope, too much component wear.
    Scopes are very useful. I use a hantek 2022 with official and open source software, only 16M samples/S and not suitable for all ucontroller testing. Better than nothing I guess.

     
  • mkstevo

    mkstevo - 2022-01-26

    If anybody wants a USB 'scope, I could let one go for a £10 (which I will make a donation to The British Hedgehog Preservation Society) and the cost of postage. I think it cost me something like £50? It's not great, but does work. 2 Channels, good for about 10MHz. Model number is Cleqee OSC482.

    I've replaced that with a Fnirsi 1014D. Claimed to be good for 100MHz but realistically 25-30MHz. It is however a fabulous little 'scope. Much better than the one I bought at work (OWON) that cost well over twice the price. Cost just under £150 from AliExpress.

    I would really like an ana-digi type "real" 'scope with a CRT but a used Hameg will often fetch significantly more than the amount I paid for a newer and much lighter LCD one.

     
  • stan cartwright

    stan cartwright - 2022-01-26

    Mkstevo Right on and all. looks tidy. How much? Postage 4u a few quid ukmail to wales,cardiff.
    Typical dso in a box , nice.

     
    • mkstevo

      mkstevo - 2022-01-26

      Just to be clear, the picture above is NOT the one I'm selling, it is my new one.

      This is the one I'm selling.

       

      Last edit: mkstevo 2022-01-26
  • stan cartwright

    stan cartwright - 2022-01-26

    Oh dear. I'll stick with mine until I can get a real scope, not with a crt and the faults but a lcd.... lots o money for a new one from flea bay though.

     
  • William Roth

    William Roth - 2022-01-27

    I purchased a new Siglent 1202X-E about 6 months ago to replace my old TEK with a dim screen. So far I am very pleased. I use this scope almost daily with no problems. More than good enough for microcontroller stuff.

     

    Last edit: William Roth 2022-01-28
  • stan cartwright

    stan cartwright - 2022-01-27

    Any problems with it ? There's a similar hantek screen,same price. Get what you pay for I suppose. Using a pc and a usb box is poor. £150 seems ok for a scope.

     

    Last edit: stan cartwright 2022-01-27
    • mkstevo

      mkstevo - 2022-01-27

      The Hantek is more expensive, and Amazon UK have the Yeapook in stock for next day delivery.

      I prefer the layout of the FNIRSI/Yeapook screen. With the measurements shown on the right hand side, there is more vertical space for the traces. They can run from a USB power bank making it (semi) portable too.

       
  • stan cartwright

    stan cartwright - 2022-01-27

    Thanks all. I'll see what's reviewed for £150 scopes. I never got x,y mode to work with my hantek usb.

     
  • Chris Roper

    Chris Roper - 2022-01-27

    I am getting a bit lost here Stan.
    Are you saying that the code works and the USB Scope was giving incorrect results?
    Or have we gone off on a tangent discussing Scopes and lost the focus?

    If Millis() is not working in the new compilers I need to fix it so please advise.

     
  • William Roth

    William Roth - 2022-01-28

    Stan's original code worked ok for 65 seconds, then start toggling as fast as the loop would allow. Seems that after 65 seconds the "if" condition is always true causing an a very fast toggle.

    Nothing wrong with the scope.

     

    Last edit: William Roth 2022-01-28
  • Anobium

    Anobium - 2022-01-28

    So, no issue with millis() ? Stan's code just is not right.

     
  • stan cartwright

    stan cartwright - 2022-01-28

    The point was strange results on scope but scope gets the 1KHz probe trimmer signal correct.
    I dug out some 50Hz timer 1 & 0 interrupts that worked once but gave strange results.
    I tried using millis() instead. I think millis() is a long and I'm using word vars.
    If I change elapsed to a long it seems to work when if millis()-elapsed>=10.
    Probably scope triggers on rising edge which is every other 10ms so gives 50Hz.

     
  • stan cartwright

    stan cartwright - 2022-01-28

    The point was strange results on scope but scope gets the 1KHz probe trimmer signal correct.
    I dug out some 50Hz timer 1 & 0 interrupts that worked once but gave strange results.
    I tried using millis() instead. I think millis() is a long and I'm using word vars.
    If I change elapsed to a long it seems to work when if millis()-elapsed>=10.
    Probably scope triggers on rising edge which is every other 10ms so gives 50Hz.
    I guess if millis()-elapsed>=10 will only work as both long vars or if millis() >65535 then it won't using a word var for elapsed. Testing a new gcb release.

     

    Last edit: stan cartwright 2022-01-28
  • Anobium

    Anobium - 2022-01-28

    This is Bill's method. Note the assignment of the CurrentMs variable. Why is this important? This 'If CurrentMs - PreviousMs >= 20 then' will work as expected and the variable states will be caches when the millis interrupt fires.

    CurrentMs = 0
    PreviousMs = 0
    
    Do
        CurrentMs = Millis()
        If CurrentMs - PreviousMs >= 20 then
            PortB.5 = !PortB.5
            PreviousMs = CurrentMs
        End if
    Loop
    

    This may not work as expected ' if millis()-elapsed>=20 then' Why? The interrupt can fire durin g calc of millis and elapsed.... yes. Millis is four bytes and those four bytes can change messing up any calculation. This is NOT an implementation issue of millis() this is a usage issue. The way to stop this... use Bills method.

     
    • stan cartwright

      stan cartwright - 2022-01-28

      It has CurrentMs = Millis() in the loop, CurrentMs will overflow after time.
      Your point about a millis event happening during the main program is true.
      Use int off, do a comparison to millis(), then int on?

       
1 2 > >> (Page 1 of 2)

Log in to post a comment.