UART send and receive help

Status
Not open for further replies.

john blue

Member
My UART keeps sending data out after a character is received. May I know how to do so that, when i send once, it will receive once without ussing interrupt if possible?
 
Here you go:

#include <p18f4550.h>
#include <usart.h>
#include <delays.h>
#include <uart.h>
#include <stdio.h>



void main()
{
char data;
char mybuff [] = "Hello";
char mybuff1 [] = "Hell of UART";
int flag=0,count=0;
TRISBbits.TRISB0=1;
TRISBbits.TRISB2=0;
TRISAbits.TRISA4=0;
ADCON1 = 0b00001110;

OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX& USART_BRGH_LOW, 255);


while(1)
{

if (flag){
putsUSART( mybuff );
flag=0;
}

if(DataRdyUSART()) // This means 'loop here until we get a new character in the receive buffer'
{
data = getcUSART();
if (data=='A'){
putsUSART( mybuff );
}
#if 0
else if ( data == 'b'){
putsUSART( mybuff1);
}
else
putcUSART(data);
#endif
}
else {

if (data=='A'){
if (flag == 1){
putsUSART( mybuff );
flag=0;
}
}
}

}
CloseUSART();
}

I need it to repeat the displayed message according to the number that the user key in in the hyperterminal . Thanks
 
if(DataRdyUSART()) // This means 'loop here until we get a new character in the receive buffer'

No it doesn't. The program won't wait here until data is ready. It data isn't ready, it'll jump to this code:

Code:
else {

if (data=='A'){

You need to pay much more attention to your brackets and program branches! If you want a loop that will wait for an event, the way to do that is like this:

Code:
...

if(!(DataRdyUSART())){}' // This means 'loop here until we get a new character in the receive buffer'

else ......

Or even better:

Code:
while(!(DataRdyUSART())){}' // This means 'loop here until we get a new character in the receive buffer'
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…