Hi
I'm using device initialization to initialize the KwikStik with uart, and I´ve created two functions with the routines, can somebody tell me what am I doing wrong???
I´m using TWR-SER to engage the communication with my pc
char uart_getchar (void)
{
/* Wait until character has been received */
while ((UART5_S1 & 0x40)==0)
{asm(nop);}
(void) UART5_S1;
/* Return the 8-bit data from the receiver */
return UART5_D;
}
void uart_putchar (char ch)
{
(void) UART5_S1;
/* Send the character */
UART5_D = (char)ch;
while ((UART5_S1 & 0x40)==0)
{asm(nop);}
(void) UART5_S1;
retardo(10);
}
Solved! Go to Solution.
Hi,
Bellow you can find an example of code that sends and receives using the UART.
/********************************************************************/
/*
* Wait for a character to be received on the specified UART
*
* Parameters:
* channel UART channel to read from
*
* Return Values:
* the received character
*/
char uart_getchar (UART_MemMapPtr channel)
{
/* Wait until character has been received */
while (!(UART_S1_REG(channel) & UART_S1_RDRF_MASK));
/* Return the 8-bit data from the receiver */
return UART_D_REG(channel);
}
/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*
* Parameters:
* channel UART channel to send to
* ch character to send
*/
void uart_putchar (UART_MemMapPtr channel, char ch)
{
/* Wait until space is available in the FIFO */
while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
/* Send the character */
UART_D_REG(channel) = (uint8)ch;
}
Hi,
Bellow you can find an example of code that sends and receives using the UART.
/********************************************************************/
/*
* Wait for a character to be received on the specified UART
*
* Parameters:
* channel UART channel to read from
*
* Return Values:
* the received character
*/
char uart_getchar (UART_MemMapPtr channel)
{
/* Wait until character has been received */
while (!(UART_S1_REG(channel) & UART_S1_RDRF_MASK));
/* Return the 8-bit data from the receiver */
return UART_D_REG(channel);
}
/********************************************************************/
/*
* Wait for space in the UART Tx FIFO and then send a character
*
* Parameters:
* channel UART channel to send to
* ch character to send
*/
void uart_putchar (UART_MemMapPtr channel, char ch)
{
/* Wait until space is available in the FIFO */
while(!(UART_S1_REG(channel) & UART_S1_TDRE_MASK));
/* Send the character */
UART_D_REG(channel) = (uint8)ch;
}
Thanks Alejandro, at the end I figure it out how to do it reading some flags, and it works also perfect, although I think your way is more accurate, here is my code
char uart_getchar (void)
{
(void) UART5_S1;/*dummy read from S1 register*/
/* Wait until character has been received */
while ((UART5_S1 & 0x20)==0)
{asm(nop);}
/* Return the 8-bit data from the receiver */
return UART5_D;
}
/****************************************************/
/* 9600 Baud=> BDR=163 BRF=>1 */
/* 57600 Baud=> BDR=27 BRF=>6 */
/****************************************************/
void uart_putchar (char ch)
{
(void) UART5_S1;
/* Send the character */
UART5_D = (char)ch;
while ((UART5_S1 & 0x40)==0)
{asm(nop);}
(void) UART5_S1;
retardo(150);
}
Moreover, thanks for your answer