-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuart_test.c
90 lines (77 loc) · 2.33 KB
/
uart_test.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
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
* UART_Test.c
*
* Created: 06-Mar-19 5:04:35 PM
* Author : TEP SOVICHEA
* Description: In this tutorial you will learn how to use UART interface
* to communicate between microcontroller and PC by sending command such as:
* - led on: to turn on LED on pin 13
* - led off: to turn off LED on pin 13
* - led blink: to blink LED every 100ms
*/
#include "uart.h"
#include <avr/io.h>
#include <util/delay.h>
int main(void)
{
// initialize UART with baud rate 250kbps
uart_init(250000);
char rx_buf[RX_BUFSIZE];
int8_t mode = -1;
cli_reset();
puts("Initializing UART tutorial...");
// set pin 13 on Arduino board to output
DDRB |= (1 << PORTB5);
puts("------------------- Application Started -------------------");
while (1)
{
// print ">>" only once when console is accepting command
cli_print();
if (uart_available())
{
// read character by character until '\n' is found
if (gets_nb(rx_buf) != 0)
{
// initialize argument placeholder
char arg[10] = "";
// read argument from command line
sscanf(rx_buf, "led %s", arg);
if (strcmp(arg, "on") == 0)
{
puts(BG "Turning on LED" RESET);
mode = 0;
}
else if (strcmp(arg, "off") == 0)
{
puts(BG "Turning off LED" RESET);
mode = 1;
}
else if (strcmp(arg, "blink") == 0)
{
puts(BG "Blinking LED" RESET);
mode = 2;
}
else
{
puts(BR "error: command not found" RESET);
}
// reset console so that it can accept new command
cli_done();
}
}
switch (mode)
{
case 0:
PORTB |= (1 << PORTB5);
break;
case 1:
PORTB &= ~(1 << PORTB5);
break;
case 2:
PORTB ^= (1 << PORTB5);
_delay_ms(100);
break;
default: break;
}
}
}