-
Notifications
You must be signed in to change notification settings - Fork 36
/
example(single usart).c
77 lines (58 loc) · 2.15 KB
/
example(single usart).c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/*!
* \brief
*
* \author Jan Oleksiewicz <jnk0le@hotmail.com>
* \license SPDX-License-Identifier: MIT
*/
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include <stdio.h>
#include <string.h>
#include "usart.h"
#define BUFF_SIZE 25
const char foo_string[] PROGMEM = "Unluckily gcc string polling doesn't work for PROGMEM/PSTR() strings";
int main(void)
{
//uart_set_FrameFormat(USART_8BIT_DATA|USART_1STOP_BIT|USART_NO_PARITY|USART_ASYNC_MODE); // default settings
uart_init(BAUD_CALC(115200)); // 8n1 transmission is set as default
stdout = &uart0_io; // attach uart stream to stdout & stdin
stdin = &uart0_io; // uart0_in and uart0_out are only available if NO_USART_RX or NO_USART_TX is defined
sei(); // enable interrupts, library wouldn't work without this
uart_puts("hello from usart 0\r\n"); // write const string to usart buffer // C++ restriction, in C its the same as uart_putstr()
// if you do not have enough SRAM memory space to keep all strings, try to use puts_P instead
uart_puts_P("hello from flashed, usart\r\n"); // write string to usart buffer from flash memory // string is parsed by PSTR() macro
uart_puts_p(foo_string);
uart_puts_p(PSTR("we can also do like this\r\n"));
printf("hello from printf\n");
char buffer[BUFF_SIZE];
uart_gets(buffer, BUFF_SIZE); // read at most 24 bytes from buffer (CR,LF will not be cut)
int a;
uart_puts("gimmie a number: ");
a = uart_getint();
uart_puts("numba a: ");
uart_putint(a);
uart_puts("\r\n");
while(1)
{
uart_puts("bytes waiting in receiver buffer : ");
uart_putint(uart_AvailableBytes()); // ask for bytes waiting in receiver buffer
uart_getln(buffer, BUFF_SIZE); // read 24 bytes or one line from usart buffer
if (!strcmp(buffer, "people who annoy you"))
{
uart_putc('>');
_delay_ms(5000);
uart_puts_P(" Googles");
}
uart_puts("\r\n");
uart_putfloat(0.1337f);
uart_puts("\r\n");
uart_putstr(buffer); // write array string to usart buffer
uart_puts("\r\n");
printf("Say my name: ");
scanf("%s", buffer);
printf("So it's %s, You are damn' right.\n", buffer);
_delay_ms(5000);
}
}