-
Notifications
You must be signed in to change notification settings - Fork 0
/
uart.c
59 lines (51 loc) · 1.25 KB
/
uart.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
#include "uart.h"
// par -> '0' sem paridade
// '1' paridade par
// '2' paridade ímpar
void uartInit(uint32_t br, uint8_t isMSB, uint8_t par, uint8_t nStops) {
UCA3CTLW0 = UCSWRST;
if(par != 0) {
UCA3CTLW0 |= UCPEN;
if(par == 1) {
UCA3CTLW0 |= UCPAR__EVEN; // par
}
else {
UCA3CTLW0 |= UCPAR__ODD; // impar
}
}
if(isMSB) {
UCA3CTLW0 |= UCMSB;
}
if(nStops == 2) {
UCA3CTLW0 |= UCSPB;
}
UCA3CTLW0 |= UCSSEL__SMCLK | UCSWRST;
//uint32_t brDiv = (1000000 << 3) / br;
//UCA3BRW = brDiv >> 3;
//UCA3MCTLW = (brDiv & 0x07) << 8;
UCA3BRW = 6;
UCA3MCTLW = UCBRF_8 | UCOS16;
P6SEL1 &= ~(BIT0 | BIT1);
P6SEL0 |= (BIT0 | BIT1);
UCA3CTLW0 &= ~UCSWRST;
}
void uartSend(uint8_t byte) {
while(!(UCA3IFG & UCTXIFG));
UCA3TXBUF = byte;
}
int uartGet(uint8_t *data) {
TA2CTL = TASSEL__ACLK | MC__UP | ID__4;
TA2CCR0 = 0xFFFF; // 8 segundos
while(!(UCA3IFG & UCRXIFG) && !(TA2CTL & TAIFG));
if(TA2CTL & TAIFG) {
return -1;
}
*data = UCA3RXBUF;
return 0;
}
int uartSendStr(char string[]) {
uint32_t cont = 0;
while(string[cont] != '\0') {
uartSend(string[cont++]);
}
}