Jump to content

Recommended Posts

Hello, I´m a newie. I started with the proyects posted in section for beginners and I have the following problem:

 

I tested the proyect "binary progression" and all works ok in simulation, but when I wired in a proyect board,

the delays programmed run faster than should be. I measured about 200ms with my oscilloscope and the programed value was 500ms , its a big difference.

 

Later I changed delay_ms(500) for delay_s(2). In this case it works ok, ie programmed values are very similar to measured ones.

 

Is there a range values that could be used inside delays functions like delay_ms ??

 

any help?

 

thanks

Jorge

Link to post
Share on other sites

delay_ms only takes an unsigned char value therefore should be in the range 1 - 255. Page 69 of manual.

 

To get 500ms call the delay_ms(250) twice.

 

Cheers

 

Reynard

Link to post
Share on other sites

Thanks Reynard, I suspected it!.

So there are in the Forum at least 3 or 4 wrong examples using delay_ms(500).

The corious thing is that they work fine with the simulation software without any warning or error message.

 

bye.

 

Jorge

Link to post
Share on other sites
... So there are in the Forum at least 3 or 4 wrong examples using delay_ms(500).

The corious thing is that they work fine with the simulation software without any warning or error message...

 

You'll get a warning if you use -W2 compiler command line option (all warnings). And for simulator there is nothing to report because by the time code gets executed under sim it already uses a converted value.

 

Regards,

Pavel

Link to post
Share on other sites

Hi Jorge,

 

As Pavel says, if you turn on 'All Warnings' you would have gotten a warning informing of loss of data.

 

You can use the command line setting or in the IDE use 'Settings -> Options...' then click All Warnings.

 

The compiler demoted your value of 500 by truncating to the last 8 bits leaving you with 0xF4 or 244ms as you got.

 

All Warnings is good for beginners as it can pick out simple unintended errors of loss of data etc. It is also useful for experienced programmers to run their code through the All Warnings occasionally. We all have senior moments.

 

Cheers

 

Reynard

Link to post
Share on other sites

Most ( all? ) times you use a variable that is too small for a value the value will "roll over" 255 + 1 = 0, etc. You can use a bigger variable type, but if the function does not

honor it you will still not get what you want. Sticking to unsigned char gives the fastest code, but the most limited.

 

 

 

good, I did it and now I can see the warning.

Thanks for your time.

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...