
<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
		<id>https://wiki.robotika.sk/robowiki/index.php?action=history&amp;feed=atom&amp;title=AVR_calculator_example_program</id>
		<title>AVR calculator example program - Revision history</title>
		<link rel="self" type="application/atom+xml" href="https://wiki.robotika.sk/robowiki/index.php?action=history&amp;feed=atom&amp;title=AVR_calculator_example_program"/>
		<link rel="alternate" type="text/html" href="https://wiki.robotika.sk/robowiki/index.php?title=AVR_calculator_example_program&amp;action=history"/>
		<updated>2026-05-13T12:00:45Z</updated>
		<subtitle>Revision history for this page on the wiki</subtitle>
		<generator>MediaWiki 1.30.0</generator>

	<entry>
		<id>https://wiki.robotika.sk/robowiki/index.php?title=AVR_calculator_example_program&amp;diff=1671&amp;oldid=prev</id>
		<title>Palo at 17:47, 1 December 2006</title>
		<link rel="alternate" type="text/html" href="https://wiki.robotika.sk/robowiki/index.php?title=AVR_calculator_example_program&amp;diff=1671&amp;oldid=prev"/>
				<updated>2006-12-01T17:47:13Z</updated>
		
		<summary type="html">&lt;p&gt;&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;This program retrieves (waits for) two numbers from the terminal connected to UART1,&lt;br /&gt;
and sends back the result. It uses the uart1SendByte() and uart1GetByte() functions&lt;br /&gt;
from uart2.c that is part of AVRlib. It works with Cerebot and BlueSmirf modules,&lt;br /&gt;
see [[Cerebot &amp;amp; LEGO Mindstorms]].&lt;br /&gt;
&lt;br /&gt;
The whole project can be downloaded here: [[Media:avr_calc.zip | avr_calc.zip]].&lt;br /&gt;
&lt;br /&gt;
A follow-up version that is utilizing getchar implementation with line editor&lt;br /&gt;
from stdiodemo WinAVR example program has more convenient input editing (BACKSPACE, DEL,&lt;br /&gt;
and some more features work). This new version is available here: [[Media:avr_calc_ed.zip | avr_calc_ed.zip]].&lt;br /&gt;
&lt;br /&gt;
 #include &amp;lt;avr/io.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
 #include &amp;lt;util/delay.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 // this is included from AVRlib (we need to link with uart2.c and buffer.c)&lt;br /&gt;
 #include &amp;lt;uart2.h&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
 static int uart1_putchar(char c, FILE *stream);&lt;br /&gt;
 static int uart1_getchar(FILE *stream);&lt;br /&gt;
 static FILE uart_stdinout = FDEV_SETUP_STREAM(uart1_putchar, uart1_getchar, _FDEV_SETUP_RW);&lt;br /&gt;
 &lt;br /&gt;
 //outputs one character to UART1&lt;br /&gt;
 static int uart1_putchar(char c, FILE *stream)&lt;br /&gt;
 {&lt;br /&gt;
   //we live in Windows world now &lt;br /&gt;
   if (c == '\n') uart1_putchar('\r', stream);&lt;br /&gt;
   uart1SendByte(c);&lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 //retrieves one character (or -1) from UART1&lt;br /&gt;
 static int uart1_getchar(FILE *stream)&lt;br /&gt;
 {&lt;br /&gt;
   char c;&lt;br /&gt;
   while ((c = uart1GetByte()) == (char)-1);&lt;br /&gt;
   uart1SendByte(c);&lt;br /&gt;
   return c;&lt;br /&gt;
 }&lt;br /&gt;
 &lt;br /&gt;
 //this function sleeps s seconds (if F_CPU is set to frequency of processor)&lt;br /&gt;
 void sleep(uint8_t s)&lt;br /&gt;
 {&lt;br /&gt;
   uint8_t i;&lt;br /&gt;
   while (s--) for (i = 0; i &amp;lt; 100; i++) _delay_ms(10);&lt;br /&gt;
 } &lt;br /&gt;
 &lt;br /&gt;
 int main(void)&lt;br /&gt;
 {&lt;br /&gt;
   int a, b, c;&lt;br /&gt;
   char ch;&lt;br /&gt;
  &lt;br /&gt;
   //set port E as output so that we can operate with LEDs&lt;br /&gt;
   DDRE = 0xFF;  &lt;br /&gt;
   &lt;br /&gt;
   //first LED shows that program runs&lt;br /&gt;
   PORTE = PINE | 16;&lt;br /&gt;
   &lt;br /&gt;
   //initialize UART1 as 2400/odd parity (that's how our BlueSmirf is configured)&lt;br /&gt;
   uart1Init();&lt;br /&gt;
   uartSetBaudRate(1, 2400);&lt;br /&gt;
   UCSR1C |= 48; &lt;br /&gt;
 &lt;br /&gt;
   //setup character output function for stdout&lt;br /&gt;
   stdout = stdin = &amp;amp;uart_stdinout;  &lt;br /&gt;
 &lt;br /&gt;
   //while ((ch = fgetc(stdin)) != (char)-1) putc(ch, stdout);&lt;br /&gt;
   &lt;br /&gt;
   //wait 3 sec before printing&lt;br /&gt;
   sleep(3);&lt;br /&gt;
   &lt;br /&gt;
   //send string to UART1&lt;br /&gt;
   printf(&amp;quot;AVR calculator.\r\n&amp;quot;);&lt;br /&gt;
   printf(&amp;quot;a=&amp;quot;);&lt;br /&gt;
   scanf(&amp;quot;%d&amp;quot;, &amp;amp;a);&lt;br /&gt;
   printf(&amp;quot;\r\nb=&amp;quot;);&lt;br /&gt;
   scanf(&amp;quot;%d&amp;quot;, &amp;amp;b);&lt;br /&gt;
   c=a+b;&lt;br /&gt;
   printf(&amp;quot;\r\na+b=%d (0x%x)\r\n&amp;quot;, c, c);&lt;br /&gt;
     &lt;br /&gt;
   //turn off first LED to show that program terminates&lt;br /&gt;
   PORTE = PINE &amp;amp; ~16;&lt;br /&gt;
   &lt;br /&gt;
   return 0;&lt;br /&gt;
 }&lt;/div&gt;</summary>
		<author><name>Palo</name></author>	</entry>

	</feed>