Download AVR GCC Tutorial (WinAVR)

Transcript
// putc for AVR with a UART (e.g. AT90S8515)
int uart_putc(unsigned char c)
{
while(!(USR & (1 << UDRE))) /* wait, to UDR ready */
{
}
UDR = c;
return 0;
/* send to indications */
}
/** OR **/
// with newer AVRs other name for the status registers, here ATmega16:
int uart_putc(unsigned char c)
{
while (!(UCSRA & (1<<UDRE))) /* waits for possible sending */
{
}
UDR = c;
return 0;
/* send to indications */
}
/* PUTs is independently of the type of microcontroller */
void uart_puts (char *s)
{
while (*s)
{
/* so long *s! = “\ 0” thus unequally the “stringer end-character”*/
uart_putc(*s);
s++;
}
}
Waiting loops are to that extent somewhat critical, since during sending a stringer no more will not react to
other events can. The use of FIFO (first in first out) is more universal - buffers, in which and/or the received
indications/byte buffered and by means of interrupt routines at the U (S) KIND which can be sent are passed on
and/or selected. In addition finished components (libraries, Libraries) exist, which one can integrate quite
simply into own developments. It is advisable to use these components and not to invent the wheel again.
Write of variables' contents
If contents of variable (integers, floating point) in “more human being-readably form” are to be sent, a
transformation is necessary in indication (“ASCII”) before the transfer. With only one number this
transformation is relatively simple: one adds the ASCII value from zero to the number and can this value
directly send.
///...
// here uart_putc (s.o.)
// expenditure of 0123456789
char c;
for (uint8_t i=0; i<=9; ++i) {
c = i + '0';
uart_putc( c );
// shortens: uart_putc (i + “0”);
}