Uart Example
Uart Example
h"
#include "rcc_clock.h"
#include "delay.h"
#include "uart_driver.h"
/******************************************************************************/
/* */
/* Helper functions */
/* */
/******************************************************************************/
/**
*Enable the given USART peripheral
*Paramter: *uartx : base address of the USART or UART peripheral
*return: None
*/
void USART_Enable(USART_TypeDef *uartx)
{
/**
*Disable the given USART peripheral
*Parameter: *uartx : base address of the USART or UART peripheral
*Return: None
*/
void USART_Disable(USART_TypeDef *uartx)
{
uartx->CR1 &= ~USART_CR1_UE;
}
/**
*Enable/Disable the Transmit block of the given USART peripheral
*Parameter *uartx : base address of the USART or UART peripheral
*Return: None
*/
void USART_EnableTx(USART_TypeDef *uartx)
{
/**
*Enable/Disable the Receive block of the given USART peripheral
*Parameter :*uartx : base address of the USART or UART peripheral
*Return: None
*/
void USART_EnableRx(USART_TypeDef *uartx)
{
/**********************************************************/
/*********Drivers******************************************/
else
return 0;
////////////////////////////////////////////
///////Similar to Tiva C Course(on edX)
//------------UART_InChar------------
// Wait for new serial port input
// Input: none
// Output: ASCII code for key typed
char USART_InChar(USART_TypeDef *USARTx){
//------------UART_OutChar------------
// Output 8-bit to serial port
// Input: letter is an 8-bit ASCII character to be transferred
// Output: none
void USART_OutChar(USART_TypeDef *USARTx,char data){
/* Transmit Data */
USARTx->DR = ((uint16_t)data & (uint16_t)0x01FF);
while((USARTx->SR & USART_SR_TC) == 0); //Changed from SR_TXE to SR_TC
}
//------------UART_InUDec------------
// InUDec accepts ASCII input in unsigned decimal format
// and converts to a 32-bit unsigned number
// valid range is 0 to 4294967295 (2^32-1)
// Input: none
// Output: 32-bit unsigned number
// If you enter a number above 4294967295, it will return an incorrect value
// Backspace will remove last digit typed
uint32_t USART_InUDec(USART_TypeDef *USARTx){
uint32_t number=0, length=0;
char character;
character = USART_InChar(USARTx);
while(character != CR){ // accepts until <enter> is typed
// The next line checks that the input is a digit, 0-9.
// If the character is not 0-9, it is ignored and not echoed
if((character>='0') && (character<='9')) {
number = 10*number+(character-'0'); // this line overflows if above
4294967295
length++;
USART_OutChar(USARTx,character); //echo to terminal
}
// If the input is a backspace, then the return number is
// changed and a backspace is outputted to the screen
else if((character==BS) && length){
number /= 10;
length--;
USART_OutChar(USARTx,character);
}
character = USART_InChar(USARTx);
}
return number;
//-----------------------UART_OutUDec-----------------------
// Output a 32-bit number in unsigned decimal format
// Input: 32-bit number to be transferred
// Output: none
// Variable format 1-10 digits with no space before or after
void USART_OutUDec(USART_TypeDef *USARTx,uint32_t n){
// This function uses recursion to convert decimal number
// of unspecified length as an ASCII string
if(n >= 10){
USART_OutUDec(USARTx,n/10);
n = n%10;
}
USART_OutChar(USARTx,n+'0'); /* n is between 0 and 9 */
}
case USART_IT_TC:
USARTx->SR &= ~USART_SR_TC;
break;
case USART_IT_RXNE:
USARTx->SR &= ~USART_SR_RXNE;
break;
default :
break;
//////////////////////
///////////
//IDLE, PE and TXE interrupts are cleared using two step prosess:
// (a read from the status register followed by a read or write
access to the
// USART_DR data register)
// see manual
}
uint32_t status;
status=USARTx->SR;
return status;