|
|
|
![]() |
|
|||||||
|
||||||||
![]() |
| Thread Tools | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
We're having a hard time with pwm outputs in the default code.
We assign an 8-bit value to, say, pwm02, and then printf pwm02. The output looked like gibberish, but when we printf-ed it in binary format, the 8-bit value was in the high byte of the value. We've tried various methods of casting, shifting, and other wierd things, all to no avail. (see some examples below) Can anyone help? Code:
pwm02 = (unsigned char)Get_Analog_Value(rc_ana_in01)>>2;
printf("%d\n", pwm02);
// output: 10496
Thanks in advance, Jon B (: |
|
#2
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
Try
Code:
printf("%d\n", (int) pwm02);
|
|
#3
|
||||
|
||||
|
Re: Trouble with pwm outputs
I don't think it was a problem with the printf() function. We were trying to cast an int as an unsigned char, in order to be able to properly work with the pwm outputs. The variable pwm02 is #define'd to txdata.rc_pwm02, which is an unsigned char.
|
|
#4
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
Quote:
|
|
#5
|
||||
|
||||
|
Re: Trouble with pwm outputs
Okay, we finally figured some things out. PWM outputs require the 7.2V battery to be present for logic and supply. It should have been obvious to us when the red pin was associated with "7.2V," but apparently it wasn't. Whatever.
|
|
#6
|
||||
|
||||
|
Re: Trouble with pwm outputs
Quote:
Code:
printf("%3d\n", (int) pwm02);
|
|
#7
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
The 3 in %3d causes printf() to blank fill the data to three characters. %03d would zero fill it. (This is the way most C compilers implement printf, anyway.)
As FotoPlasma mentioned, though, their problem was that their backup battery was not connected. It turns out it was not a display problem. |
|
#8
|
|||
|
|||
|
Re: Trouble with pwm outputs
If for some reason you get the power set up once more and it still fails, try this, it will help clear up any future printf statements:
Code:
printf("%d\n", &pwm02);
As for the digits between the % and the 'd', lpramo55 was right in saying the number controls the number of digits output. If you write a 3, it will display 3 digits, adding zeroes as necessary. This also works with decimal places. For example: If you wrote Code:
float f = 34.67;
printf("%2.1f", &f);
The number between the '%' and the 'f' in this case represents 2 digits, plus a decimal point, plus 1 additional digit. Hope this helps! NOTE: Do not use floats with the robot programming because it has no floating point processor. Instead, stick to ints and unsigned chars, using "%i", and "%d" as needed. Last edited by Just3D : 15-01-2004 at 00:22. Reason: Clarify information. |
|
#9
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
Quote:
Still, I could be wrong with the C18-based printf() library, you never know... |
|
#10
|
|||
|
|||
|
Re: Trouble with pwm outputs
You are correct. The & symbol denotes the address of a variable, passing this information to the printf function. Printf requires a pointer, which is why the & is needed.
However, I realized my mistake: This is the behavior of the standard C printf function, and I believe the default code comes with a new re-written printf function, in which case the & might not be necessary at all. I just got started with this specific compiler, so I'm still getting accustomed to the new conventions. Thanks for the insight. |
|
#11
|
||||
|
||||
|
Re: Trouble with pwm outputs
also, if you are required to pass a value, but you have a pointer-based variable, the asterisk (*) will get you what you need.
quick pointers tutorial: Code:
char *ptr; //declares pointer to a char ptr = whatever; //assigns "whatever" as the *address* where ptr is pointing (usually for assigning the address of one pointer to another) *ptr = whatever; //assigns "whatever" as the value stored where ptr is pointing (the normal "i want to assign a value") ptr = &whatever; //assigns the address of "whatever" to the ptr (essentially, makes ptr point at "whatever") |
|
#12
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
Quote:
|
|
#13
|
||||
|
||||
|
Re: Trouble with pwm outputs
Quote:
![]() Please do NOT answer questions if you do not know the answer. Giving people an authoritative answer when you don't really know what you're talking about is bad and is one of the huge downfalls of CD. printf does not require the address of the variable unless you want to print out the address. It takes the variable itself. Do not use an '&'. Code:
int a;
a = 5;
printf("%d %d\n", a, &a);
Code:
5 2293484 Also, as gwross (someone you should always listen to) said, you need to cast your variables to an integer to get them to print correctly with this implementation of printf: Code:
printf("%d\n", (int)a);
Quote:
Code:
int a;
a = 5;
printf("%3d\n%03d\n", a, a);
Code:
5 005 |
|
#14
|
|||||
|
|||||
|
Re: Trouble with pwm outputs
Thanks for the clarification Mike! I was really worried for a while that all the work I had done for years only worked due to an error in my string libraries!! I thought it was pretty strange that a print function would ask for pointers...
I've always done my work with printf() using values but I haven't worked with the C18 stuff yet this year so I wasn't sure if maybe they changed the library for the controller. I'm glad somebody knows the answer! And as for Just3D, 2 questions: 1) Which team number are you? 2) Have you been using & in your printf() statments all along?? |
|
#15
|
||||
|
||||
|
Re: Trouble with pwm outputs
the only situation in which I could think printf would use a pointer is in printing a string, as that is a character array and thus must be passed by reference.
as Mike said, you have to have the 0 in the % code to tell printf to pad with zeros - otherwise it pads with spaces. |
![]() |
| Thread Tools | |
| Display Modes | Rate This Thread |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Tether for new EduBot Controller | gburlison | Programming | 9 | 18-12-2003 17:53 |
| pwm 13-15 | wayne 05 | Programming | 2 | 04-10-2003 12:08 |
| PWM and burning out motors | patrickrd | Technical Discussion | 7 | 19-06-2003 15:30 |
| Hints For Rookie Programmers | Noah | Programming | 30 | 20-02-2003 20:07 |
| relay and pwm outputs | nick_champ_2 | Electrical | 1 | 31-01-2003 13:08 |