Skip to content

Commit d1bc2f9

Browse files
committed
Add Usart functions
Add Usart set int function Add Usart set str function
1 parent f729891 commit d1bc2f9

File tree

2 files changed

+40
-6
lines changed

2 files changed

+40
-6
lines changed

uno_def.c

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
*/
77
#include "uno_def.h"
88
#include <avr/io.h>
9+
#include <limits.h>
910

1011
void usart_std_init()
1112
{
@@ -30,14 +31,14 @@ void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _sto
3031

3132
switch(_charsize)
3233
{
33-
default:
3434
case BIT5:
3535
break;
3636
case BIT6:
3737
_ON(UCSR0C, UCSZ00);
3838
break;
3939
case BIT9:
4040
_ON(UCSR0C, UCSZ02);
41+
default:
4142
case BIT8:
4243
_ON(UCSR0C, UCSZ00);
4344
case BIT7:
@@ -56,9 +57,6 @@ void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _sto
5657
_ON(UCSR0C, UPM01);
5758
break;
5859
}
59-
60-
_ON(UCSR0C, UCSZ01);
61-
_ON(UCSR0C, UCSZ00);
6260
}
6361

6462
char usart_getc()
@@ -86,3 +84,26 @@ void usart_setc_ifready(char c)
8684
if(!USART_READY)
8785
UDR0 = c;
8886
}
87+
88+
void usart_setstr(char *str)
89+
{
90+
while(*str)
91+
usart_setc(*str++);
92+
}
93+
94+
void usart_setint(int i, BASE base)
95+
{
96+
int size = 0;
97+
int rev = 0;
98+
while (i != 0) {
99+
rev = rev * base + i % base;
100+
i /= base;
101+
++size;
102+
}
103+
104+
for(;size>0;--size)
105+
{
106+
usart_setc((rev%base)+'0');
107+
rev/=base;
108+
}
109+
}

uno_def.h

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,15 +112,17 @@
112112

113113
#define USART_READY _GET(UCSR0A, UDRE0)
114114
#define USART_RECIVE_COMPLETE _GET(UCSR0A, RXC0)
115+
#define USART_TRANSMIT_COMPLETE _GET(UCSR0A, TXC0)
116+
115117
#define USART_RECIVE_ENABLE() _ON(UCSR0B, RXEN0)
116118
#define USART_RECIVE_DISABLE() _OFF(UCSR0B, RXEN0)
117-
#define USART_TRANSMIT_COMPLETE _GET(UCSR0A, TXC0)
119+
118120
#define USART_TRANSMIT_ENABLE() _ON(UCSR0B, TXEN0)
119121
#define USART_TRANSMIT_DISABLE() _OFF(UCSR0B, TXEN0)
120122

123+
#define USART_ENABLE_INT_UDR() _ON(UCSR0B, UDRIE0)
121124
#define USART_ENABLE_INT_RX() _ON(UCSR0B, RXCIE0)
122125
#define USART_ENABLE_INT_TX() _ON(UCSR0B, TXCIE0)
123-
#define USART_ENABLE_INT_UDR() _ON(UCSR0B, UDRIE0)
124126

125127
typedef enum
126128
{
@@ -150,11 +152,22 @@ typedef enum
150152
BIT9
151153
} USART_CHAR_SIZE;
152154

155+
typedef enum
156+
{
157+
BINARY = 2,
158+
OCTAL = 7,
159+
DECIMAL = 10,
160+
HEXDECIMAL = 16
161+
} BASE;
162+
153163
void usart_std_init();
154164
void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity);
155165
char usart_getc();
156166
char usart_getc_ifready();
157167
void usart_setc(char c);
158168
void usart_setc_ifready(char c);
159169

170+
void usart_setstr(char *str);
171+
void usart_setint(int i, BASE base);
172+
160173
#endif /* UNO_DEF_H_ */

0 commit comments

Comments
 (0)