Trouble with pwm outputs

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?

	pwm02 = (unsigned char)Get_Analog_Value(rc_ana_in01)>>2;
",  pwm02);
	// output: 10496

Thanks in advance,
Jon B (:


",  (int) pwm02);

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.

It wasn’t clear from your response if you tried my suggestion or not. I went through similar machinations trying to get a printf to work, and casting within the printf was the only way that would work for me. (BTW, I eventually tried it that way because I noticed that that was the way the default program did it.)

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.

i found that

",  (int) pwm02);

i think the 3 has something to do with the digits displayed if i am correct. i used the existing code as a basis for the assumption.

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.

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:

", &pwm02);

Notice the ampherstand (&). This will tell printf to read the value of pwn02 rather than the memory address of pwn02 (this is why 10000 or so seemed so out of range; memory addresses range all the way up to the maximum RAM).

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

float f = 34.67;
printf("%2.1f", &f);

The output would be 34.7, rather than 34.670000.
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.

Correct me if I’m wrong but from my knowledge of C syntax and printf() the & would cause printf() to read the address of the variable, not the value. If you are working with a pointer (which you are not in this case) you would use * to access the value, not &. Unless in a function declaration (or in a binary/logic statement) & always references the address of a variable.

Still, I could be wrong with the C18-based printf() library, you never know…

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.

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:

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")

Is that so? Most of my experience with the printf standards has been the c++ libraries’ implementation of sprintf(). With sprintf() you send the value of the variable, rather than the address. It’s odd that printf() would require an address, any reasoning behind it?

So that’s why printf hasn’t worked for me the past 8 years, I should have tried adding an ‘&’ and would have all been good :wink:

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 ‘&’.

int a;
a = 5;
printf("%d %d
", a, &a);

will print

5 2293484

Notice that the first one printed the value of a and the second printed the address of a (address will vary).

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:

", (int)a);

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.

Also not correct.

int a;
a = 5;
", a, a);

will print out:


The zeros will only be printed out if you have a ‘0’ before the number.

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??

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.

If I’m not mistaken, doesn’t the rewritten printf function not support the %f format for floating point. If I am right. How would I display a floating point number in the terminal window?

depending on the size, you might just multiply it by 1000 to give you an integer (if the number is -32.767 < x < 32.767) or some other power of 10 (i.e. if you have values -320.767 - 320.767, mult. by 100)