Difference between revisions of "UART"
(→UART_Printf) |
|||
Line 6: | Line 6: | ||
==UART_Init== | ==UART_Init== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination || void UART_Init(uint32_t v_baudRate_u32) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments ||*uint32_t : Baudrate to be configured. |
− | + | ||
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
|- | |- | ||
− | | Description ||This function is used to initialize the UART at specified baud rate.If the requested baud rate is not within the supported range then the default baud rate of 9600 is set. | + | | Description ||This function is used to initialize the UART at specified baud rate.<br />If the requested baud rate is not within the supported range then the default baud rate of 9600 is set. |
|- | |- | ||
− | | Usage || | + | | Usage ||UART_Init(9600); //Initialize the uart module for 9600 baud rate; |
|} | |} | ||
+ | |||
+ | |||
+ | |||
+ | |||
+ | ==UART_SetBaudRate== | ||
+ | {|{{Widget:LibCol}} | ||
+ | {{#Widget:LibTable}} | ||
+ | |- | ||
+ | |Defination ||void UART_SetBaudRate(uint32_t v_baudRate_u32) | ||
+ | |- | ||
+ | | Input Arguments ||*uint32_t : Baudrate to be configured. | ||
+ | |- | ||
+ | | Return Value|| none | ||
+ | |- | ||
+ | | Description ||This function is used to Set/Change the baudrate on the fly.<br />If the requested baud rate is not within the supported range then the default baud rate of 9600 is set. | ||
+ | |- | ||
+ | | Usage ||UART_SetBaudRate(9600); //Configures the uart module for 9600 baud rate; | ||
+ | |} | ||
+ | |||
+ | |||
+ | |||
==UART_TxChar== | ==UART_TxChar== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination ||void UART_TxChar(char v_uartData_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || char:Ascii value of the character to be transmitted. |
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
Line 33: | Line 53: | ||
| Description ||This function is used to transmit a char through UART module. | | Description ||This function is used to transmit a char through UART module. | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | char ch='A'; | ||
+ | UART_TxChar(ch); //Transmits the Char stored in ch | ||
+ | UART_TxChar('A'); //Transmits the Char 'A' | ||
+ | UART_TxChar(65); //Transmits the Char 'A' | ||
+ | UART_TxChar(0x41); //Transmits the Char 'A' | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_RxChar== | ==UART_RxChar== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination || char UART_RxChar() |
|- | |- | ||
| Input Arguments || none. | | Input Arguments || none. | ||
|- | |- | ||
− | | Return Value|| | + | | Return Value|| char: Ascii value of the character received |
|- | |- | ||
− | | Description ||This function is used to receive a char from UART module. It waits till a char is received and returns | + | | Description ||This function is used to receive a char from UART module. It waits till a char is received and returns its acsii value after reception. |
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | char ch; | ||
+ | ch = UART_RxChar(); //Receives a Ascii char and copies into ch | ||
|} | |} | ||
+ | |||
+ | |||
==UART_TxString== | ==UART_TxString== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination || void UART_TxString(char *p_stringPointer_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || char *:String(Address of the string) to be transmitted. |
|- | |- | ||
| Return Value||none | | Return Value||none | ||
|- | |- | ||
− | | Description ||This function is used to transmit | + | | Description ||This function is used to transmit a NULL terminated string through UART. |
− | + | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | char myString[]={"hello, world"}; | ||
+ | UART_RxString(myString); | ||
+ | UART_RxString("\n\rgood morning"); | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_RxString== | ==UART_RxString== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination || void UART_RxString(char *p_stringPointer_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || char *: Address of the string/Buffer where the received data needs to be stored |
− | Address of the string where the received data needs to be stored | + | |
|- | |- | ||
| Return Value||none | | Return Value||none | ||
|- | |- | ||
− | | Description || | + | | Description ||<br /> |
− | + | *This function is used to receive a ASCII string through UART till the carriage_return/New_line<br /> | |
− | *The stream of data is copied to the buffer till carriage_return/New_line is encountered. | + | *The stream of data is copied to the buffer till carriage_return/New_line is encountered.<br /> |
* Once the Carriage_return/New_Line is received the string will be null terminated. | * Once the Carriage_return/New_Line is received the string will be null terminated. | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | char buffer[50]; | ||
+ | UART_RxString(buffer); | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_TxDecimalNumber== | ==UART_TxDecimalNumber== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination ||void UART_TxDecimalNumber(uint32_t v_decNumber_u32, uint8_t v_numOfDigitsToTransmit_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments ||<br /> |
− | + | *uint32_t: Number to be transmitted on UART.<br /> | |
+ | *uint8_t : Number of digits to be transmitted | ||
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
Line 104: | Line 146: | ||
*(12345,C_DefaultDigitsToTransmit) then 12345 will be transmitted. | *(12345,C_DefaultDigitsToTransmit) then 12345 will be transmitted. | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | uint32_t decNumber = 123456; | ||
+ | UART_TxDecimalNumber(decNumber,5); //Transmit last 5-digits of Number | ||
+ | UART_TxDecimalNumber(12345,4); //Transmit last 4-digits of number | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_TxHexNumber== | ==UART_TxHexNumber== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination ||void UART_TxHexNumber(uint32_t v_hexNumber_u32, uint8_t v_numOfDigitsToTransmit_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || uint32_t: Hexadecimal Number to be transmitted on UART. |
− | + | uint8_t : Number of digits to be transmitted | |
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
|- | |- | ||
− | | Description ||This function is used to transmit a max of 10digit hex number.2nd parameter specifies the number of digits from the right side to be transmitted . | + | | Description ||This function is used to transmit a max of 10digit hex number.<br /> |
+ | 2nd parameter specifies the number of digits from the right side to be transmitted . | ||
The output for the input combinations is as below . | The output for the input combinations is as below . | ||
*(0x12AB,3) then 3-digits ie. 2AB will be transmitted | *(0x12AB,3) then 3-digits ie. 2AB will be transmitted | ||
Line 124: | Line 173: | ||
*(0x12AB,C_DefaultDigitsToTransmit) then 12AB will be transmitted. | *(0x12AB,C_DefaultDigitsToTransmit) then 12AB will be transmitted. | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | uint32_t hexNumber = 0x123456; | ||
+ | UART_TxHexNumber(hexNumber,5); //Transmit last 5-digits of hexNumber | ||
+ | UART_TxHexNumber(0xABCDEF,4); //Transmit last 4-digits of number | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_TxBinaryNumber== | ==UART_TxBinaryNumber== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination ||void UART_TxBinaryNumber(uint32_t v_binNumber_u32, uint8_t v_numOfBitsToTransmit_u8) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || uint32_t: Hexadecimal Number to be transmitted on UART. |
− | + | uint8_t : Number of bits to be transmitted | |
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
|- | |- | ||
− | | Description ||This function is used to transmit the binary equivalent of the given number. 2nd parameter specifies the number of LSB to be transmitted . | + | | Description ||This function is used to transmit the binary equivalent of the given number.<br /> |
+ | 2nd parameter specifies the number of LSB to be transmitted . | ||
The output for the input combinations is as below . | The output for the input combinations is as below . | ||
*(10,4) then 4-LSB will be transmitted ie. 1010 | *(10,4) then 4-LSB will be transmitted ie. 1010 | ||
Line 144: | Line 200: | ||
*(10,2) then 2-LSB will be transmitted ie. 10 | *(10,2) then 2-LSB will be transmitted ie. 10 | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | uint32_t Number = 0xABCD; | ||
+ | UART_TxDecimalNumber(Number,16); //Transmit last 16-bits of Number | ||
+ | UART_TxDecimalNumber(0x1234,8); //Transmit last 8-bits of number | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_TxFloatNumber== | ==UART_TxFloatNumber== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination ||void UART_TxFloatNumber(float v_floatNumber_f32) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || float: float Number to be transmitted on UART. |
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
|- | |- | ||
− | | Description ||This function is used to transmit a floating point number . | + | | Description ||This function is used to transmit a floating point number. It supports 6-digits of precision. <br /> |
− | + | *Note :Float will be disabled by default as it takes huge controller resources .It can be enabled by changing value of Enable_UART_TxFloatNumber to 1 in uart.h | |
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | float Number = 1234.5678; | ||
+ | UART_TxFloatNumber(Number); | ||
+ | UART_TxFloatNumber(3.33333); | ||
|} | |} | ||
+ | |||
+ | |||
+ | |||
==UART_Printf== | ==UART_Printf== | ||
− | {| | + | {|{{Widget:LibCol}} |
{{#Widget:LibTable}} | {{#Widget:LibTable}} | ||
|- | |- | ||
− | |Defination || | + | |Defination || void UART_Printf(const char *argList, ...) |
|- | |- | ||
− | | Input Arguments || | + | | Input Arguments || variable length arguments similar to printf |
|- | |- | ||
| Return Value|| none | | Return Value|| none | ||
|- | |- | ||
− | | Description ||This function is similar to printf function in C. It takes the arguments with specified format and | + | | Description ||This function is similar to printf function in C. It takes the arguments with specified format and transmits accordingly . |
The supported format specifiers are as below. | The supported format specifiers are as below. | ||
*%c: character | *%c: character | ||
− | *%d: decimal | + | *%d: decimal 16-Bit number |
+ | *%l: decimal 32-Bit number | ||
*%b: binary | *%b: binary | ||
*%s: string | *%s: string | ||
Line 183: | Line 252: | ||
*%x: hexadecimal | *%x: hexadecimal | ||
|- | |- | ||
− | | Usage || | + | | Usage || |
+ | |||
+ | void main() | ||
+ | { | ||
+ | uint16_t decNum=1234;//16-bit | ||
+ | uint32_t longDec=12345678;//32-bit | ||
+ | float floatNum=1234.5678; | ||
+ | uint16_t hexNum=0xABCD; | ||
+ | char myString[]={"hello, world}; | ||
+ | |||
+ | UART_Printf("%d %l %f %x %b %s",decNum,longDec,floatNum,hexNum,hexNum,myString); | ||
+ | UART_Printf("D:%d,L:%l,F=%f,H:%x,Str:%s",1234,12345678,123.456,0xAB,"hello"); | ||
+ | } | ||
|} | |} | ||
+ | |||
+ | |||
+ | ==Usage guide== | ||
+ | #include"uart.h" | ||
+ | int main() | ||
+ | { | ||
+ | char a_TxMsg_u8[]={"Good Morning"}; | ||
+ | char v_uartData_u8='A'; | ||
+ | uint16_t v_num_u16= 1234u; | ||
+ | char a_readString_u8[50]; | ||
+ | char ch; | ||
+ | |||
+ | |||
+ | UART_Init(9600); | ||
+ | UART_TxString("\n\rProgram to illustrate the UART library usage "); | ||
+ | |||
+ | UART_TxString(a_TxMsg_u8); /* Transmit the message stored in a string */ | ||
+ | UART_TxString("\n\rChar:"); | ||
+ | UART_TxChar('X'); /* Transmit the specified Ascii character */ | ||
+ | UART_TxChar(v_uartData_u8); /* Transmit the char stored in a variable */ | ||
+ | |||
+ | UART_TxString("\n\rDec:"); | ||
+ | UART_TxDecimalNumber(1234,5); /* Transmit the specified digits of a number */ | ||
+ | UART_TxString(" Hex:"); | ||
+ | UART_TxHexNumber(0xABCD,5); /* Note here the number of digits to transmit=5 */ | ||
+ | UART_TxString(" Bin:"); /* The number Transmitted on UART will be as below*/ | ||
+ | UART_TxBinaryNumber(0xABCD,16);/* dec=01234, hex=0ABCD, bin=1010101111001101*/ | ||
+ | |||
+ | |||
+ | UART_Printf("\n\rDec=%d Hex=%x Bin:%b str=%s",v_num_u16,v_num_u16,v_num_u16,a_TxMsg_u8); | ||
+ | UART_Printf("\n\rDec=%d Hex=%x Bin:%b str=%s",123u,0xABCDu,0x55u,"hello,world"); | ||
+ | |||
+ | UART_Printf("\n\rEnter a String: "); | ||
+ | UART_RxString(a_readString_u8); | ||
+ | UART_Printf("\n\rEntered string is: %s",a_readString_u8); | ||
+ | |||
+ | do | ||
+ | { | ||
+ | UART_Printf("\n\rEnter 'E' to exit: "); | ||
+ | ch = UART_RxChar(); | ||
+ | UART_Printf("\n\rYou entered: %c",ch); | ||
+ | }while((ch!='E') && (ch!='e')); | ||
+ | |||
+ | |||
+ | UART_Printf("\n\rEnjoy Embedded Programming"); | ||
+ | while(1); | ||
+ | |||
+ | return 0; | ||
+ | } |
Revision as of 16:16, 28 December 2014
Contents
UART
UART_Init
Defination | void UART_Init(uint32_t v_baudRate_u32) |
Input Arguments | *uint32_t : Baudrate to be configured. |
Return Value | none |
Description | This function is used to initialize the UART at specified baud rate. If the requested baud rate is not within the supported range then the default baud rate of 9600 is set. |
Usage | UART_Init(9600); //Initialize the uart module for 9600 baud rate; |
UART_SetBaudRate
Defination | void UART_SetBaudRate(uint32_t v_baudRate_u32) |
Input Arguments | *uint32_t : Baudrate to be configured. |
Return Value | none |
Description | This function is used to Set/Change the baudrate on the fly. If the requested baud rate is not within the supported range then the default baud rate of 9600 is set. |
Usage | UART_SetBaudRate(9600); //Configures the uart module for 9600 baud rate; |
UART_TxChar
Defination | void UART_TxChar(char v_uartData_u8) |
Input Arguments | char:Ascii value of the character to be transmitted. |
Return Value | none |
Description | This function is used to transmit a char through UART module. |
Usage |
char ch='A'; UART_TxChar(ch); //Transmits the Char stored in ch UART_TxChar('A'); //Transmits the Char 'A' UART_TxChar(65); //Transmits the Char 'A' UART_TxChar(0x41); //Transmits the Char 'A' |
UART_RxChar
Defination | char UART_RxChar() |
Input Arguments | none. |
Return Value | char: Ascii value of the character received |
Description | This function is used to receive a char from UART module. It waits till a char is received and returns its acsii value after reception. |
Usage |
char ch; ch = UART_RxChar(); //Receives a Ascii char and copies into ch |
UART_TxString
Defination | void UART_TxString(char *p_stringPointer_u8) |
Input Arguments | char *:String(Address of the string) to be transmitted. |
Return Value | none |
Description | This function is used to transmit a NULL terminated string through UART. |
Usage |
char myString[]={"hello, world"}; UART_RxString(myString); UART_RxString("\n\rgood morning"); |
UART_RxString
Defination | void UART_RxString(char *p_stringPointer_u8) |
Input Arguments | char *: Address of the string/Buffer where the received data needs to be stored |
Return Value | none |
Description |
|
Usage |
char buffer[50]; UART_RxString(buffer); |
UART_TxDecimalNumber
Defination | void UART_TxDecimalNumber(uint32_t v_decNumber_u32, uint8_t v_numOfDigitsToTransmit_u8) |
Input Arguments |
|
Return Value | none |
Description | This function is used to transmit a max of 10digit decimal number.2nd parameter specifies the number of digits from the right side to be transmitted .
The output for the input combinations is as below.
|
Usage |
uint32_t decNumber = 123456; UART_TxDecimalNumber(decNumber,5); //Transmit last 5-digits of Number UART_TxDecimalNumber(12345,4); //Transmit last 4-digits of number |
UART_TxHexNumber
Defination | void UART_TxHexNumber(uint32_t v_hexNumber_u32, uint8_t v_numOfDigitsToTransmit_u8) |
Input Arguments | uint32_t: Hexadecimal Number to be transmitted on UART.
uint8_t : Number of digits to be transmitted |
Return Value | none |
Description | This function is used to transmit a max of 10digit hex number. 2nd parameter specifies the number of digits from the right side to be transmitted . The output for the input combinations is as below .
|
Usage |
uint32_t hexNumber = 0x123456; UART_TxHexNumber(hexNumber,5); //Transmit last 5-digits of hexNumber UART_TxHexNumber(0xABCDEF,4); //Transmit last 4-digits of number |
UART_TxBinaryNumber
Defination | void UART_TxBinaryNumber(uint32_t v_binNumber_u32, uint8_t v_numOfBitsToTransmit_u8) |
Input Arguments | uint32_t: Hexadecimal Number to be transmitted on UART.
uint8_t : Number of bits to be transmitted |
Return Value | none |
Description | This function is used to transmit the binary equivalent of the given number. 2nd parameter specifies the number of LSB to be transmitted . The output for the input combinations is as below .
|
Usage |
uint32_t Number = 0xABCD; UART_TxDecimalNumber(Number,16); //Transmit last 16-bits of Number UART_TxDecimalNumber(0x1234,8); //Transmit last 8-bits of number |
UART_TxFloatNumber
Defination | void UART_TxFloatNumber(float v_floatNumber_f32) |
Input Arguments | float: float Number to be transmitted on UART. |
Return Value | none |
Description | This function is used to transmit a floating point number. It supports 6-digits of precision.
|
Usage |
float Number = 1234.5678; UART_TxFloatNumber(Number); UART_TxFloatNumber(3.33333); |
UART_Printf
Defination | void UART_Printf(const char *argList, ...) |
Input Arguments | variable length arguments similar to printf |
Return Value | none |
Description | This function is similar to printf function in C. It takes the arguments with specified format and transmits accordingly .
The supported format specifiers are as below.
|
Usage |
void main() { uint16_t decNum=1234;//16-bit uint32_t longDec=12345678;//32-bit float floatNum=1234.5678; uint16_t hexNum=0xABCD; char myString[]={"hello, world}; UART_Printf("%d %l %f %x %b %s",decNum,longDec,floatNum,hexNum,hexNum,myString); UART_Printf("D:%d,L:%l,F=%f,H:%x,Str:%s",1234,12345678,123.456,0xAB,"hello"); } |
Usage guide
- include"uart.h"
int main() {
char a_TxMsg_u8[]={"Good Morning"}; char v_uartData_u8='A'; uint16_t v_num_u16= 1234u; char a_readString_u8[50]; char ch; UART_Init(9600); UART_TxString("\n\rProgram to illustrate the UART library usage "); UART_TxString(a_TxMsg_u8); /* Transmit the message stored in a string */ UART_TxString("\n\rChar:"); UART_TxChar('X'); /* Transmit the specified Ascii character */ UART_TxChar(v_uartData_u8); /* Transmit the char stored in a variable */ UART_TxString("\n\rDec:"); UART_TxDecimalNumber(1234,5); /* Transmit the specified digits of a number */ UART_TxString(" Hex:"); UART_TxHexNumber(0xABCD,5); /* Note here the number of digits to transmit=5 */ UART_TxString(" Bin:"); /* The number Transmitted on UART will be as below*/ UART_TxBinaryNumber(0xABCD,16);/* dec=01234, hex=0ABCD, bin=1010101111001101*/
UART_Printf("\n\rDec=%d Hex=%x Bin:%b str=%s",v_num_u16,v_num_u16,v_num_u16,a_TxMsg_u8); UART_Printf("\n\rDec=%d Hex=%x Bin:%b str=%s",123u,0xABCDu,0x55u,"hello,world"); UART_Printf("\n\rEnter a String: "); UART_RxString(a_readString_u8); UART_Printf("\n\rEntered string is: %s",a_readString_u8); do { UART_Printf("\n\rEnter 'E' to exit: "); ch = UART_RxChar(); UART_Printf("\n\rYou entered: %c",ch); }while((ch!='E') && (ch!='e'));
UART_Printf("\n\rEnjoy Embedded Programming"); while(1);
return 0;
}