Skip to content

Commit f729891

Browse files
committed
Add USART functions
Add USART functions Add _delay_s makro
1 parent e456547 commit f729891

File tree

4 files changed

+148
-3
lines changed

4 files changed

+148
-3
lines changed

glob_def.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,18 @@
1010
#define GLOB_DEF_H_
1111

1212
#include <stdbool.h>
13+
#include <stddef.h>
1314

1415
#ifndef F_CPU
1516
#define F_CPU 16000000UL // 16 MHZ
1617
#endif /* F_CPU */
1718

19+
#ifndef BAUD
20+
#define BAUD 9600UL
21+
#endif /* BAUD */
22+
23+
#define _delay_s(__s) _delay_ms((__s)*1000)
24+
1825
#define BIT(x) (1 << (x))
1926

2027
#define _ON(x, y) (x |= BIT(y))

shield_def.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
* Author: LD
66
*/
77

8-
#include "uno_def.h"
9-
#include "glob_def.h"
10-
118
#ifndef SHIELD_DEF_H_
129
#define SHIELD_DEF_H_
1310

11+
#include "glob_def.h"
12+
#include "uno_def.h"
13+
1414
// LEDs
1515
#define D1 P13
1616
#define D2 P12

uno_def.c

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* uno_def.c
3+
*
4+
* Created: 18.12.2021 11:56:46
5+
* Author: LD
6+
*/
7+
#include "uno_def.h"
8+
#include <avr/io.h>
9+
10+
void usart_std_init()
11+
{
12+
UBRR0 = UBRR_VALUE;
13+
14+
USART_TRANSMIT_ENABLE();
15+
USART_RECIVE_ENABLE();
16+
17+
_ON(UCSR0C, UCSZ01);
18+
_ON(UCSR0C, UCSZ00);
19+
}
20+
21+
void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity)
22+
{
23+
UBRR0 = UBRR_VALUE;
24+
25+
USART_TRANSMIT_ENABLE();
26+
USART_RECIVE_ENABLE();
27+
28+
_SET(_mode,UCSR0C, UMSEL00);
29+
_SET(_stopbit,UCSR0C, USBS0);
30+
31+
switch(_charsize)
32+
{
33+
default:
34+
case BIT5:
35+
break;
36+
case BIT6:
37+
_ON(UCSR0C, UCSZ00);
38+
break;
39+
case BIT9:
40+
_ON(UCSR0C, UCSZ02);
41+
case BIT8:
42+
_ON(UCSR0C, UCSZ00);
43+
case BIT7:
44+
_ON(UCSR0C, UCSZ01);
45+
break;
46+
}
47+
48+
switch(_parity)
49+
{
50+
default:
51+
case DISABLED:
52+
break;
53+
case EVEN:
54+
_ON(UCSR0C, UPM00);
55+
case ODD:
56+
_ON(UCSR0C, UPM01);
57+
break;
58+
}
59+
60+
_ON(UCSR0C, UCSZ01);
61+
_ON(UCSR0C, UCSZ00);
62+
}
63+
64+
char usart_getc()
65+
{
66+
while(USART_RECIVE_COMPLETE);
67+
return UDR0;
68+
}
69+
70+
char usart_getc_ifready()
71+
{
72+
if(!USART_RECIVE_COMPLETE)
73+
return UDR0;
74+
else
75+
return 0;
76+
}
77+
78+
void usart_setc(char c)
79+
{
80+
while(USART_READY);
81+
UDR0 = c;
82+
}
83+
84+
void usart_setc_ifready(char c)
85+
{
86+
if(!USART_READY)
87+
UDR0 = c;
88+
}

uno_def.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
#ifndef UNO_DEF_H_
1010
#define UNO_DEF_H_
1111

12+
#include "glob_def.h"
13+
#include <util/setbaud.h>
14+
1215
// LED
1316
#define LED P13
1417
#define _LED _P13
@@ -107,4 +110,51 @@
107110
#define _P1_ DDRD
108111
#define _P0_ DDRD
109112

113+
#define USART_READY _GET(UCSR0A, UDRE0)
114+
#define USART_RECIVE_COMPLETE _GET(UCSR0A, RXC0)
115+
#define USART_RECIVE_ENABLE() _ON(UCSR0B, RXEN0)
116+
#define USART_RECIVE_DISABLE() _OFF(UCSR0B, RXEN0)
117+
#define USART_TRANSMIT_COMPLETE _GET(UCSR0A, TXC0)
118+
#define USART_TRANSMIT_ENABLE() _ON(UCSR0B, TXEN0)
119+
#define USART_TRANSMIT_DISABLE() _OFF(UCSR0B, TXEN0)
120+
121+
#define USART_ENABLE_INT_RX() _ON(UCSR0B, RXCIE0)
122+
#define USART_ENABLE_INT_TX() _ON(UCSR0B, TXCIE0)
123+
#define USART_ENABLE_INT_UDR() _ON(UCSR0B, UDRIE0)
124+
125+
typedef enum
126+
{
127+
DISABLED,
128+
EVEN,
129+
ODD
130+
} USART_PARITY;
131+
132+
typedef enum
133+
{
134+
ONE = 0,
135+
TWO = 1
136+
} USART_STOP_BIT;
137+
138+
typedef enum
139+
{
140+
ASYNC = 0,
141+
SYNC = 1
142+
} USART_MODE;
143+
144+
typedef enum
145+
{
146+
BIT5,
147+
BIT6,
148+
BIT7,
149+
BIT8,
150+
BIT9
151+
} USART_CHAR_SIZE;
152+
153+
void usart_std_init();
154+
void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity);
155+
char usart_getc();
156+
char usart_getc_ifready();
157+
void usart_setc(char c);
158+
void usart_setc_ifready(char c);
159+
110160
#endif /* UNO_DEF_H_ */

0 commit comments

Comments
 (0)