We use floating points. Make sure to link with nutlibcrtf.
00001 00083 #include <cfg/crt.h> /* Floating point configuration. */ 00084 00085 #include <string.h> 00086 #include <stdio.h> 00087 #include <io.h> 00088 00089 #include <dev/board.h> 00090 #include <sys/timer.h> 00091 00092 static char *banner = "\nNut/OS UART Sample\n"; 00093 static prog_char presskey_P[] = "Press any key..."; 00094 static prog_char pgm_ptr[] = "\nHello stranger!\n"; 00095 00096 static char inbuf[128]; 00097 00098 /* 00099 * UART sample. 00100 * 00101 * Some functions do not work with ICCAVR. 00102 */ 00103 int main(void) 00104 { 00105 int got; 00106 int i; 00107 char *cp; 00108 u_long baud = 115200; 00109 FILE *uart; 00110 #ifdef STDIO_FLOATING_POINT 00111 float dval = 0.0; 00112 #endif 00113 00114 /* 00115 * Each device must be registered. We do this by referencing the 00116 * device structure of the driver. The advantage is, that only 00117 * those device drivers are included in our flash code, which we 00118 * really need. 00119 * 00120 * The uart0 device is the first one on the ATmega chip. So it 00121 * has no configurable base address or interrupt and we set both 00122 * parameters to zero. 00123 */ 00124 NutRegisterDevice(&DEV_UART, 0, 0); 00125 00126 /* 00127 * Now, as the device is registered, we can open it. The fopen() 00128 * function returns a pointer to a FILE structure, which we use 00129 * for subsequent reading and writing. 00130 */ 00131 uart = fopen(DEV_UART_NAME, "r+"); 00132 00133 /* 00134 * Before doing the first read or write, we set the baudrate. 00135 * This low level function doesn't know about FILE structures 00136 * and we use _fileno() to get the low level file descriptor 00137 * of the stream. 00138 * 00139 * The short sleep allows the UART to settle after the baudrate 00140 * change. 00141 */ 00142 _ioctl(_fileno(uart), UART_SETSPEED, &baud); 00143 00144 /* 00145 * Stream devices can use low level read and write functions. 00146 * Writing program space data is supported too. 00147 */ 00148 _write(_fileno(uart), banner, strlen(banner)); 00149 { 00150 _write_P(_fileno(uart), presskey_P, sizeof(presskey_P)); 00151 } 00152 00153 /* 00154 * Stream devices do buffered I/O. That means, nothing will be 00155 * passed to the hardware device until either the output buffer 00156 * is full or we do a flush. With stream I/O we typically use 00157 * fflush(), but low level writing a null pointer will also flush 00158 * the output buffer. 00159 */ 00160 _write(_fileno(uart), 0, 0); 00161 00162 /* 00163 * The low level function read() will grab all available bytes 00164 * from the input buffer. If the buffer is empty, the call will 00165 * block until something is available for reading. 00166 */ 00167 got = _read(_fileno(uart), inbuf, sizeof(inbuf)); 00168 _write(_fileno(uart), inbuf, got); 00169 00170 /* 00171 * Nut/OS never expects a thread to return. So we enter an 00172 * endless loop here. 00173 */ 00174 for (i = 0;; i++) { 00175 /* 00176 * A bit more advanced input routine is able to read a string 00177 * up to and including the first newline character or until a 00178 * specified maximum number of characters, whichever comes first. 00179 */ 00180 fputs("\nEnter your name: ", uart); 00181 fflush(uart); 00182 fgets(inbuf, sizeof(inbuf), uart); 00183 00184 /* 00185 * Chop off trailing linefeed. 00186 */ 00187 cp = strchr(inbuf, '\n'); 00188 if (cp) 00189 *cp = 0; 00190 00191 /* 00192 * Streams support formatted output as well as printing strings 00193 * from program space. 00194 */ 00195 if (inbuf[0]) 00196 fprintf(uart, "\nHello %s!\n", inbuf); 00197 else { 00198 fputs_P(pgm_ptr, uart); 00199 } 00200 00201 /* 00202 * Just to demonstrate formatted floating point output. 00203 * In order to use this, we need to link the application 00204 * with nutcrtf instead of nutcrt for pure integer. 00205 */ 00206 #ifdef STDIO_FLOATING_POINT 00207 dval += 1.0125; 00208 fprintf(uart, "FP %f\n", dval); 00209 #endif 00210 } 00211 }