|
|
View previous topic :: View next topic |
Author |
Message |
apcaye
Joined: 22 May 2009 Posts: 29 Location: Brazil
|
|
Posted: Mon Mar 29, 2010 6:06 am |
|
|
I don't know if I understood your question correctly, Simbo, but my suggestion is to call "kbd_getc()" three times to get three digits. Call the macro "toint(c)" on each digit and then do this calculation:
PWM = (First digit*100) + (Second digit*10) + Third digit;
Regards,
Adriano. |
|
|
Simbo
Joined: 02 Mar 2008 Posts: 7 Location: UK
|
|
Posted: Wed Mar 31, 2010 2:34 pm |
|
|
hi,
Sorry I was a bit vague with my question but I think you get the jist of it!
I think your suggestion should do it.
Quote: |
PWM = (First digit*100) + (Second digit*10) + Third digit;
|
I'll adapt the code and try it again soon.
Thank you for the reply.
Simbo _________________ <~Simbo~> |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19546
|
|
Posted: Wed Mar 31, 2010 3:37 pm |
|
|
Can I suggest a fractionally different approach.
Assuming characters are available, from a buffer, or using getc, something like:
Code: |
int32 total=0; //start with total=0
int8 chr;
while (isdigit(chr=getc()) { //Get a character - is it a digit?.
total=mult_with10(total)+(chr-'0'); //if so, multiply total*10,
//and add the digit value
}
//total now contains the value typed in....
|
Multiple key things:
stdlib.h, has a very efficient *10, supporting up to an int32.
All you do, is multiply the total 'so far' by ten, and add in the value of the new digit, stopping when something that is not a digit arrives.
Obviously you can use a smaller type (int8, int16 etc.), but as shown this will handle integers of any normal size, quickly and efficiently.
Best Wishes |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|