Uart H
Uart H
***********************************************************************************
****************
ExploreEmbedded
***********************************************************************************
*****************
* File: uart.c (AVR controllers)
* Version: 16.0
* Author: ExploreEmbedded
* Website: http://www.exploreembedded.com/wiki
* Description: File contains the Library routines for UART
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************************
***************/
/
***********************************************************************************
***************
Revision History
***********************************************************************************
*****************
15.0: Initial version
16.0: Updated the Tx Number function to support bin,dec,hex in one function.
***********************************************************************************
****************/
#include <stdarg.h>
#include "uart.h"
/
***********************************************************************************
****************
void UART_Init(uint32_t v_baudRate_u32)
***********************************************************************************
*****************
* I/P Arguments: uint32_t : Baudrate to be configured.
* Return value : none
/
***********************************************************************************
****************
void UART_SetBaudRate(uint32_t v_baudRate_u32)
***********************************************************************************
****************
* I/P Arguments: uint32_t : v_baudRate_u32 to be configured.
* Return value : none
UBRRL = util_ExtractByte0to8(RegValue);
UBRRH = util_ExtractByte8to16(RegValue);
}
/
***********************************************************************************
****************
char UART_RxChar()
***********************************************************************************
****************
* I/P Arguments: none.
* Return value : char: Ascii value of the character received
/
***********************************************************************************
****************
void UART_TxChar(char v_uartData_u8)
***********************************************************************************
*****************
* I/P Arguments: char--> Ascii value of the character to be transmitted.
* Return value : none.
/
***********************************************************************************
****************
void UART_TxString(char *ptr_string)
***********************************************************************************
*****************
* I/P Arguments: String(Address of the string) to be transmitted.
* Return value : none
/
***********************************************************************************
****************
uint8_t UART_RxString(char *ptr_string)
***********************************************************************************
*****************
* I/P Arguments: char *: Address of the string/buffer where the received data
needs to be stored
* Return value : uint8_t: Number of chars received.
* description :
1.This function is used to receive a ASCII string through UART till
the carriage_return/New_line
2.The stream of data is copied to the buffer till
carriage_return/New_line is encountered.
3. Once the Carriage_return/New_Line is received the string
will be null terminated.
*****NOTE*******:
1.The received char is ECHOED back,
if not required then comment UART_TxChar(ch) in the code.
2.BackSlash is not taken care.
***********************************************************************************
****************/
#if (Enable_UART_RxString==1)
uint8_t UART_RxString(char *ptr_string)
{
char ch;
uint8_t len = 0;
while(1)
{
ch=UART_RxChar(); //Receive a char
UART_TxChar(ch); //Echo back the received char
/
***********************************************************************************
****************
void UART_TxNumber(uint8_t v_numericSystem_u8, uint32_t v_number_u32, uint8_t
v_numOfDigitsToTransmit_u8)
***********************************************************************************
****************
* I/P Arguments:
uint8_t : specifies type of number
C_BINARY_U8(2),C_DECIMAL_U8(10), C_HEX_U8(16)
uint32_t: Number to be transmitted on UART.
uint8_t : Number of digits to be transmitted
Decimal
4.(12345,4) then 4-digits ie. 2345 will be transmitted
5.(12345,6) then 6-digits ie. 012345 will be transmitted
6.(12345,C_DefaultDigitsToTransmit_U8) then 12345 will be
transmitted.
Hex:
7.(0x12AB,3) then 3-digits ie. 2AB will be transmitted
8.(0x12AB,6) then 6-digits ie. 0012AB will be transmitted
9.(0x12AB,C_DefaultDigitsToTransmit_U8) then 12AB will be
transmitted.
***********************************************************************************
****************/
#if ((Enable_UART_TxNumber==1) || (Enable_UART_TxFloatNumber==1) ||
(Enable_UART_Printf == 1))
void UART_TxNumber(uint8_t v_numericSystem_u8, uint32_t v_number_u32, uint8_t
v_numOfDigitsToTransmit_u8)
{
uint8_t i=0,a[10];
if(C_BINARY_U8 == v_numericSystem_u8)
{
while(v_numOfDigitsToTransmit_u8!=0)
{
/* Start Extracting the bits from the specified bit positions.
Get the Acsii values of the bits and transmit */
i = util_GetBitStatus(v_number_u32,(v_numOfDigitsToTransmit_u8-1));
UART_TxChar(util_Dec2Ascii(i));
v_numOfDigitsToTransmit_u8--;
}
}
else if(v_number_u32==0)
{
/* If the number is zero then update the array with the same for
transmitting */
for(i=0;((i<v_numOfDigitsToTransmit_u8) &&
(i<C_MaxDigitsToTransmit_U8)) ;i++)
UART_TxChar('0');
}
else
{
for(i=0;i<v_numOfDigitsToTransmit_u8;i++)
{
/* Continue extracting the digits from right side
till the Specified v_numOfDigitsToTransmit_u8 */
if(v_number_u32!=0)
{
/* Extract the digits from the number till it becomes zero.
First get the remainder and divide the number by 10 each time.
while(i)
{
/* Finally get the ascii values of the digits and transmit*/
UART_TxChar(util_Hex2Ascii(a[i-1]));
i--;
}
}
}
#endif
/
***********************************************************************************
****************
void UART_TxFloatNumber(float v_floatNumber_f32)
***********************************************************************************
****************
* Function name: UART_TxFloatNumber()
* I/P Arguments: float: float Number to be transmitted on UART.
* Return value : none
***********************************************************************************
****************/
#if (Enable_UART_TxFloatNumber==1)
void UART_TxFloatNumber(float v_floatNumber_f32)
{
uint32_t v_tempNumber_u32;
/* Dirty hack to support the floating point by extracting the integer and
fractional part.
1.Type cast the number to int to get the integer part.
2.transmit the extracted integer part followed by a decimal point(.).
3.Later the integer part is made zero by subtracting with the extracted
integer value.
4.Finally the fractional part is multiplied by 100000 to support 6-digit
precision */
UART_TxChar('.');
/
***********************************************************************************
****************
void UART_Printf(const char *argList, ...)
***********************************************************************************
****************
* Function name: UART_Printf()
* I/P Arguments: variable length arguments similar to printf
Note: By default all the functions will be disabled. The required functions can
be enabled by
setting the respective compiler switch to 1 in uart.h file.
Ex: setting Enable_UART_TxDecimalNumber to 1 will enable %d
setting Enable_UART_TxHexNumber to 1 will enable %x
#####: In case of printing the variables(8-bit) its recommended to type cast and
promote them to uint16_t.
uint8_t v_Num_u8;
UART_Printf("num1:%u",(uint16_t)v_Num_u8);
***********************************************************************************
****************/
#if ( Enable_UART_Printf == 1 )
void UART_Printf(const char *argList, ...)
{
const char *ptr;
va_list argp;
sint16_t v_num_s16;
sint32_t v_num_s32;
uint16_t v_num_u16;
uint32_t v_num_u32;
char *str;
char ch;
uint8_t v_numOfDigitsToTransmit_u8;
#if (Enable_UART_TxFloatNumber==1)
double v_floatNum_f32;
#endif
va_start(argp, argList);
ch= *ptr;
if(ch == '%') /*Check for '%' as there will be format specifier
after it */
{
ptr++;
ch = *ptr;
if((ch>=0x30) && (ch<=0x39))
{
v_numOfDigitsToTransmit_u8 = 0;
while((ch>=0x30) && (ch<=0x39))
{
v_numOfDigitsToTransmit_u8 = (v_numOfDigitsToTransmit_u8
* 10) + (ch-0x30);
ptr++;
ch = *ptr;
}
}
else
{
v_numOfDigitsToTransmit_u8 =
C_MaxDigitsToTransmitUsingPrintf_U8;
}
UART_TxNumber(C_DECIMAL_U8,v_num_s16,v_numOfDigitsToTransmit_u8);
break;
UART_TxNumber(C_DECIMAL_U8,v_num_s32,v_numOfDigitsToTransmit_u8);
break;
UART_TxNumber(C_DECIMAL_U8,v_num_u16,v_numOfDigitsToTransmit_u8);
break;
UART_TxNumber(C_DECIMAL_U8,v_num_u32,v_numOfDigitsToTransmit_u8);
break;
UART_TxNumber(C_HEX_U8,v_num_u16,v_numOfDigitsToTransmit_u8);
break;
UART_TxNumber(C_HEX_U8,v_num_u32,v_numOfDigitsToTransmit_u8);
break;
if(v_numOfDigitsToTransmit_u8 ==
C_MaxDigitsToTransmitUsingPrintf_U8)
v_numOfDigitsToTransmit_u8 = 16;
UART_TxNumber(C_BINARY_U8,v_num_u16,v_numOfDigitsToTransmit_u8);
break;
if(v_numOfDigitsToTransmit_u8 ==
C_MaxDigitsToTransmitUsingPrintf_U8)
v_numOfDigitsToTransmit_u8 = 32;
UART_TxNumber(C_BINARY_U8,v_num_u32,v_numOfDigitsToTransmit_u8);
break;
case 'F':
case 'f': /* Argument type is of float, hence read double data
from the argp */
#if (Enable_UART_TxFloatNumber==1)
v_floatNum_f32 = va_arg(argp, double);
UART_TxFloatNumber(v_floatNum_f32);
#endif
break;
case 'S':
case 's': /* Argument type is of string, hence get the pointer to
sting passed */
str = va_arg(argp, char *);
UART_TxString(str);
break;
case '%':
UART_TxChar('%');
break;
}
}
else
{
/* As '%' is not detected transmit the char passed */
UART_TxChar(ch);
}
}
va_end(argp);
}
#endif