On the serial monitor I see the ASCII characters, not the numbers. How can I change it? I want it to show the numbers, not the ASCII.
Your above statements actually contain the clue as to why the output is not what you expected.
Terminal emulation programs like Hyperterminal, Putty, etc, interpret the received bytes of data as ASCII characters, not numerical values.
Therefore to display the received data as numerical data, rather than ASCII characters, you must first convert the numerical value to an ASCII character string.
For example, the numerical value 32535 would need to be converted to a character string of '3''2''5''3''5' or "32535".
There are several ways to achieve this including writing your own routine, which your following statement touches on:
Well, it's working if I change n=0 to n=48 but it still only counts until 9, so it wouldn't be the best solution.
However, as you pointed out above, you must first separate the numerical values digits before converting them to ASCII characters.
If you search the forum you will find numerous methods of achieve this task.
Fortunately, the C Standard Library provides the sprintf() routine which accomplishes this task quite nicely.
int sprintf(char *str, const char *format, ...)
Code:
#include <stdio.h>
#include <math.h>
int main()
{
char str[80];
sprintf(str, "Value of Pi = %f", M_PI);
puts(str);
return(0);
}
The sprintf() routine can consume large amounts of code storage, making its utilization in some cases, particularly with embedded systems, less than ideal.
Which is why many embedded programmers prefer to utilize their own routines to save code storage (Flash) and increase performance.
BigDog