Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

UART buffer keeps the last character?

Status
Not open for further replies.

Vandal S

Member level 3
Joined
May 2, 2009
Messages
54
Helped
3
Reputation
6
Reaction score
1
Trophy points
1,288
Activity points
1,749
rcif buffer

I have a PIC 16f877 and i want to interface it with the pc. I am sending characters through UART and depending on those characters, it activates some pins. But how do I know when characters aren't transmitted no more? At the present, I made it with a delay, and it runs continuously for 400ms, but after that, it becomes 0 for a short perioad and then 5V again.
How do I eliminate the 0 Volts period? if i try with a while ( while (uart_recive='w') {uart_read, do stuff} ) it will always stay in 5V until another key is pressed, as if the buffer remains with the last character, doen't refresh.
If I haven't been very clear, please say so.
 

uart1_read array

The 'USART Receive Interrupt Flag bit' (RCIF) will be set when there is a character in the Receive FIFO to be read. This is bit 5 of the PIR register. This bit is cleared by the hardware when you read the character from the FIFO.

What you should do in your code is test this bit, and if it is set read the character from the receive FIFO. If is is Not set there is no character to read. You don't need to use delays.
 

    Vandal S

    Points: 2
    Helpful Answer Positive Rating
uart buffer

once again, GSM Man thank you for the precious information. it seems that it's exactly what i'm looking for.
the problem, of course, is implementing :cry: hopefuly, this would be the last thing i ask, as the project is in a few days and this is the last problem (I hope)
this is the code i'm using (a big part of it)

Code:
char i, error, byte_read, com_byte; 
int t;             // Auxiliary variables 
void main(){ 
... bits initialisation 
    error = Soft_UART_Init(&PORTC, 7, 6,1200,0); 

  while(1)                                
  {    PORTD=0b10000000;   // if i don't do this, other problem occurs 
       byte_read = Soft_UART_Read(&error);   // Read byte, then test error flag 
       switch(byte_read){ 
             case 'W':{ 
                        for(t=0;t<=400;t++) 
                             {  delay_ms(1); 
            PORTD=0b00000001; 
                             }   Soft_UART_Write('W'); 
                         break; 
                      } 
        case 'S' ...




void interrupt () {
if (PIR1.RCIF) {
rxchar = Usart_Read(); //
} // end if (PIR1.RCIF)
} // end interrupt

and in main
INTCON.GIE = 1;
INTCON.PEIE = 1;
PIE1.RCIE = 1; //enable interrupt.


I read about the RCIF. The buffer won't get full (i think) and maybe the pin will remain in 5V as long as a character will be received (no short times when it's in 0 every 400ms).
But the problem is interfacing it with the code above. Maybe another function void interupts should be created, some other registers should be set active and if(RCIF==1) should be used in the main function


But I can't make it work. Even patching code is difficult for a beginner
 

how to clear uart buffer

i reached to this code, but it doens't work; if i press w a single time, it remains in that state, doesn't reset to 0x00 if it doens't get a character through UART

char byte_read, i = 0, flag = 0; // Variable for storing the data from UART and array counter
unsigned short cnt = 0;


Code:
char byte_read, i = 0, flag = 0; 
unsigned short cnt = 0;

#define Direction PORTD 
#define Forward 0b00000001 
#define Backward 0b00000010 
#define Stop 0b00000000 

void interrupt () { 
  if (PIR1.TMR1IF) { 
      cnt++ ; 
     PIR1.TMR1IF = 0; 
      } 
  if (PIR1.RCIF) {          
    byte_read = UART1_Read();  
      flag = 1; 
      cnt = 0; 
    } 
  } 
  
  
void main () { 
unsigned short j; 

 TRISD = 0;  

 INTCON.GIE = 1; 
 INTCON.PEIE = 1; 
 T1CON = 1; 
 PIE1.RCIE = 1;
 PIE1.TMR1IE = 1; 
 UART1_Init(1200); 
 Direction = Stop; 
  
 while(1) {   

  if (flag ==1) { 
     switch(byte_read){ 
             	  case 'W':{ 
                        Direction=Forward; 
                         break; 
                      } 
      		  case 'S':{ 
                        Direction=Back; 
                         break; 
                      } 
         
       byte_read = 0; 
	flag = 0;

		 } 

  } 
  
  if (cnt == 152) {         
      Direction = Stop; 
      cnt = 0;       

    } 

  
 } 
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top