Wavebubble 2010 Firmware

serial.c

Go to the documentation of this file.
00001 
00007 #include <avr/io.h>
00008 #include <avr/pgmspace.h>
00009 #include <avr/interrupt.h>
00010 #include <ctype.h>
00011 #include <string.h>
00012 #include "main.h"
00013 #include "serial.h"
00014 
00021 void usart_init(void) {
00022   // Setup USART
00023   UBRR0H = (((F_CPU/BAUDRATE)/16)-1)>>8;      // set baud rate
00024   UBRR0L = (((F_CPU/BAUDRATE)/16)-1);
00025   // enable Rx & Tx
00026   UCSR0B = _BV(RXEN0)|_BV(TXEN0);
00027   // config 8N1
00028   UCSR0C = _BV(UCSZ01)|_BV(UCSZ00);
00029 }
00030 
00031 
00038 int pc_putc(char data) {
00039   // wait for USART to become available
00040   while ( (UCSR0A & _BV(UDRE0)) != _BV(UDRE0));
00041   UDR0 = data;    // send character
00042   return 0;
00043 }
00044 
00045 
00052 void pc_puts(char *s) {
00053   while(*s) pc_putc(*s++);   // send string char by char
00054 }
00055 
00056 
00063 void pc_puts_P(const char *s) {
00064   // send string char by char
00065   while(pgm_read_byte(&*s)) pc_putc(pgm_read_byte(&*s++));
00066 };
00067 
00074 char pc_getc(void)
00075 {
00076   // wait for complete receive
00077   while ( (UCSR0A & _BV(RXC0)) != _BV(RXC0) );
00078   return UDR0;            // return character
00079 }
00080 
00087 void putnum_ud(uint16_t n) {
00088   uint8_t cnt=0, flag=0;
00089 
00090   while (n >= 10000UL) { flag = 1; cnt++; n -= 10000UL; }
00091   if (flag) pc_putc('0'+cnt);
00092   cnt = 0;
00093   while (n >= 1000UL) { flag = 1; cnt++; n -= 1000UL; }
00094   if (flag) pc_putc('0'+cnt);
00095   cnt = 0;
00096   while (n >= 100UL) { flag = 1; cnt++; n -= 100UL; }
00097   if (flag) pc_putc('0'+cnt);
00098   cnt = 0;
00099   while (n >= 10UL) { flag = 1; cnt++; n -= 10UL; }
00100   if (flag) pc_putc('0'+cnt);
00101   cnt = 0;
00102   pc_putc('0'+n);
00103   return;
00104 }
00105 
00110 void print_div(void) {
00111        pc_puts_P(PSTR("---------------------------------\n"));
00112 };
00113 
00120 uint16_t pc_read16(void) {
00121   uint8_t c;
00122   uint16_t t=0;
00123   while ( (c = pc_getc()) != '\n') {
00124     if (c == '\r') break;
00125     if ((c  > '9') || (c < '0'))
00126             continue;
00127     pc_putc(c);
00128     t *= 10;
00129     t += c-'0';
00130   }
00131   pc_putc(c);
00132   return t;
00133 }