-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathuno_def.c
206 lines (171 loc) · 2.87 KB
/
uno_def.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* uno_def.c
*
* Created: 18.12.2021 11:56:46
* Author: LD
*/
#include "uno_def.h"
#include <avr/io.h>
#include <limits.h>
void usart_std_init()
{
UBRR0 = UBRR_VALUE;
USART_TRANSMIT_ENABLE();
USART_RECIVE_ENABLE();
_ON(UCSR0C, UCSZ01);
_ON(UCSR0C, UCSZ00);
}
void usart_init(USART_MODE _mode, USART_CHAR_SIZE _charsize, USART_STOP_BIT _stopbit, USART_PARITY _parity)
{
UBRR0 = UBRR_VALUE;
USART_TRANSMIT_ENABLE();
USART_RECIVE_ENABLE();
_SET(_mode,UCSR0C, UMSEL00);
_SET(_stopbit,UCSR0C, USBS0);
switch(_charsize)
{
case BIT5:
break;
case BIT6:
_ON(UCSR0C, UCSZ00);
break;
case BIT9:
_ON(UCSR0C, UCSZ02);
default:
case BIT8:
_ON(UCSR0C, UCSZ00);
case BIT7:
_ON(UCSR0C, UCSZ01);
break;
}
switch(_parity)
{
default:
case DISABLED:
break;
case EVEN:
_ON(UCSR0C, UPM00);
case ODD:
_ON(UCSR0C, UPM01);
break;
}
}
char usart_getc()
{
while(USART_RECIVE_COMPLETE);
return UDR0;
}
char usart_getc_ifready()
{
if(!USART_RECIVE_COMPLETE)
return UDR0;
else
return 0;
}
void usart_setc(char c)
{
while(USART_READY);
UDR0 = c;
}
void usart_setc_ifready(char c)
{
if(!USART_READY)
UDR0 = c;
}
void usart_setstr(char *str)
{
while(*str)
usart_setc(*str++);
}
void usart_setint(int i, BASE base)
{
int size = 0;
int rev = 0;
while (i != 0) {
rev = rev * base + i % base;
i /= base;
++size;
}
for(;size>0;--size)
{
usart_setc((rev%base)+'0');
rev/=base;
}
}
void usart_win_cursor(bool on)
{
if(on)
usart_setstr("\e[?25h");
else
usart_setstr("\e[?25l");
}
void i2c_init(unsigned long clock, bool pullup)
{
TWBR = ((F_CPU/clock) - 16)/2;
INPUT(A5);
INPUT(A4);
if(pullup)
{
ON(A5);
ON(A4);
}
else
{
OFF(A5);
OFF(A4);
}
}
void i2c_twi_start()
{
TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (!(TWCR & (1<<TWINT)));
}
void i2c_twi_tx(uint8_t data, I2C_ACK ack)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
if (ack) TWCR |= (1<<TWEA);
while (!(TWCR & (1<<TWINT)));
}
void i2c_twi_tx16(uint16_t data, I2C_ACK ack)
{
i2c_twi_tx(data>>8, NACK);
i2c_twi_tx((uint8_t)data, NACK);
}
uint8_t i2c_twi_rx(I2C_ACK ack)
{
TWCR = (1<<TWINT)|(1<<TWEN);
if (ack) TWCR |= (1<<TWEA);
while (!(TWCR & (1<<TWINT)));
return TWDR;
}
void i2c_twi_stop()
{
TWCR = (1<<TWINT)|(1<<TWSTO)|(1<<TWEN);
}
void i2c_tx(uint8_t address, uint8_t data, I2C_ACK ack)
{
i2c_twi_start();
//address &= 0b00000001 & WRITE;
i2c_twi_tx(address, ACK);
i2c_twi_tx(data, ack);
i2c_twi_stop();
}
uint8_t i2c_rx(uint8_t address, I2C_ACK ack)
{
uint8_t data = 0;
i2c_twi_start();
//address &= 0b00000001 & READ;
i2c_twi_tx(address, ACK);
data = i2c_twi_rx(ack);
i2c_twi_stop();
return data;
}
void i2c_tx16(uint8_t address, uint16_t data, I2C_ACK ack)
{
i2c_twi_start();
//address &= 0b00000001 & WRITE;
i2c_twi_tx(data>>8, NACK);
i2c_twi_tx((uint8_t)data, NACK);
i2c_twi_stop();
}