Flipklok
 
Loading...
Searching...
No Matches
uart.c
Go to the documentation of this file.
1/*
2 * uart.c
3 *
4 * Created on: 2 apr. 2025
5 */
10
11#include "uart.h"
12#include "dcfReceive.h"
13
18void UART_Init(void)
19{
20 #ifdef UART_ENABLED
21 P1SEL |= BIT1 + BIT2; // Set P1.1 (RX) and P1.2 (TX) for UART function
22 P1SEL2 |= BIT1 + BIT2;
23
24 UCA0CTL1 |= UCSSEL_2; // Use SMCLK
25 UCA0BR0 = 13; // 2400 baud rate
26 UCA0BR1 = 0;
27 UCA0MCTL = UCBRS0; // Modulation UCBRSx = 1
28 UCA0CTL1 &= ~UCSWRST; // Initialize USCI state machine
29 // IE2 |= UCA0RXIE; // Enable RX interrupt
30 #endif
31}
32
37void UART_SendChar(char c)
38{
39 #ifdef UART_ENABLED
40 while (!(IFG2 & UCA0TXIFG)); // Wait until buffer is empty
41 UCA0TXBUF = c; // Send character
42 #endif
43}
44
49void UART_SendString(char *str)
50{
51 #ifdef UART_ENABLED
52 while (*str)
53 {
54 UART_SendChar(*str++);
55 }
56 #endif
57}
58
63void UART_SendInt(int32_t num)
64{
65 #ifdef UART_ENABLED
66 char buffer[12]; // Enough for "-2147483648\0"
67 int i = 0;
68 uint8_t isNegative = 0;
69
70 if (num < 0)
71 {
72 isNegative = 1;
73 num = -num;
74 }
75
76 // Convert integer to string (reverse order)
77 do
78 {
79 buffer[i++] = (num % 10) + '0';
80 num /= 10;
81 } while (num > 0);
82
83 if (isNegative)
84 {
85 buffer[i++] = '-';
86 }
87
88 buffer[i] = '\0';
89
90 // Reverse the string and send
91 int j;
92 for (j = i - 1; j >= 0; j--)
93 {
94 UART_SendChar(buffer[j]);
95 }
96 #endif
97}
98
103void UART_SendUint32(uint32_t num)
104{
105 #ifdef UART_ENABLED
106 char buffer[11]; // Enough for "4294967295\0"
107 int i = 0;
108
109 // Convert integer to string (reverse order)
110 do
111 {
112 buffer[i++] = (num % 10) + '0';
113 num /= 10;
114 } while (num > 0);
115
116 buffer[i] = '\0';
117
118 // Reverse the string and send
119 int j;
120 for (j = i - 1; j >= 0; j--)
121 {
122 UART_SendChar(buffer[j]);
123 }
124 #endif
125}
126
131void UART_SendTime(uint32_t seconds)
132{
133 #ifdef UART_ENABLED
134 int hours = (seconds / 3600) % 24; // Get hours (mod 24 to avoid overflow)
135 int minutes = (seconds % 3600) / 60; // Get minutes
136
137 // UART_SendString("time: ");
138 UART_SendInt(hours);
139 UART_SendString(":");
140 if (minutes <= 9)
141 {
142 UART_SendString("0"); // send a zero first other wise it prints xx:y instead of xx:yy
143 }
144 UART_SendInt(minutes);
145 UART_SendString("\r\n");
146 #endif
147}
148
154void UART_Bitstream(const bool* arr, unsigned int length)
155{
156 #ifdef UART_ENABLED
157 unsigned int i;
158 for (i = 0; i < length; i++)
159 {
160 char c = arr[i] ? '1' : '0'; // convert to ASCII
161 UART_SendChar(c);
162 }
163 UART_SendString("\r\n");
164 #endif
165}
166
void UART_SendChar(char c)
send a single character via UART to host system
Definition uart.c:37
void UART_SendInt(int32_t num)
send a 16 bit integer to host system
Definition uart.c:63
void UART_Bitstream(const bool *arr, unsigned int length)
Sends a the received DCF bitstream to host system.
Definition uart.c:154
void UART_SendTime(uint32_t seconds)
Sends a time formated in hours and minutes to host system.
Definition uart.c:131
void UART_Init(void)
Setup UART communication, gets called from main()
Definition uart.c:18
void UART_SendUint32(uint32_t num)
send a 32 bit integer to host system
Definition uart.c:103
void UART_SendString(char *str)
send a full string to host system
Definition uart.c:49