-
Notifications
You must be signed in to change notification settings - Fork 1
/
uart.c
executable file
·41 lines (35 loc) · 1.3 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
#include "uart.h"
void UART_SetBaudRate(uint32_t v_baudRate_u32)
{
uint16_t RegValue;
if((v_baudRate_u32 >= C_MinBaudRate_U32) && (v_baudRate_u32<=C_MaxBaudRate_U32))
{
/* Check if the requested baudate is within range,
If yes then calculate the value to be loaded into baud rate generator. */
RegValue = M_GetBaudRateGeneratorValue(v_baudRate_u32);
}
else
{
/* Invalid baudrate requested, hence set it to default baudrate of 9600 */
RegValue = M_GetBaudRateGeneratorValue(9600);
}
UBRRL = util_ExtractByte0to8(RegValue);
UBRRH = util_ExtractByte8to16(RegValue);
}
void UART_Init(uint32_t v_baudRate_u32)
{
UCSRB= (1<<RXEN) | (1<<TXEN); // Enable Receiver and Transmitter
UCSRC= (1<<URSEL) | (1<<UCSZ1) | (1<<UCSZ0); // Asynchronous mode 8-bit data and 1-stop bit
UCSRA= 0x00; // Clear the UASRT status register
UART_SetBaudRate(v_baudRate_u32);
}
char UART_RxChar(void)
{
while(util_IsBitCleared(UCSRA,RXC)); // Wait till the data is received
return(UDR); // return the received char
}
void UART_TxChar(char v_uartData_u8)
{
while(util_IsBitCleared(UCSRA,UDRE)); // Wait till Transmitter(UDR) register becomes Empty
UDR =v_uartData_u8; // Load the data to be transmitted
}