• AVR Freaks

Hot!PIC16F877a controlled I/O LED by serial port Send command

Author
sageliu
New Member
  • Total Posts : 1
  • Reward points : 0
  • Joined: 2016/12/04 06:15:38
  • Location: 0
  • Status: offline
2019/09/11 01:20:48 (permalink)
0

PIC16F877a controlled I/O LED by serial port Send command

Hi all 
   I have a project need to send command control LED On/OFF by serial Port,actually send frist command was right and second command sent was stuck,what problem with the code,please help handle it,thanks in advance!!
 
 

Attached Image(s)

#1

1 Reply Related Threads

    pcbbc
    Super Member
    • Total Posts : 1703
    • Reward points : 0
    • Joined: 2014/03/27 07:04:41
    • Location: 0
    • Status: offline
    Re: PIC16F877a controlled I/O LED by serial port Send command 2019/09/11 09:15:02 (permalink)
    +3 (3)
    This line is pointless/nonsense...
    RCIF=0;

    You cannot clear the RCIF like that, it is readonly.  The only way to clear a RCIF is read RCREG (like you do in the very next line).  If the byte you read is the last one in the FIFO receive buffer then the flag will be cleared automatically.
     
    Where are you resetting rxcount to zero after the first command has been received?
    Eventually you will have a buffer overrun, because buf is only defined as 4 bytes.
    char buf[]={0x00,0x00,0x00,0x00};

     
    Please explain your serial protocol.
    It looks like you are expecting a 4 byte packet starting 0xEA and ending 0x55?
    Somewhat strange choices, but okay, let's roll with it.
     
    Things you need to think about....
    1. What happens to bytes before you receive an 0xEA header byte?  Currently they are added to the buffer until it overflows.  Probably not wise.  It would be better to ignore them.
    2. What happens to bytes after you receive 0xEA?  Specifically what happens if you receive more than the 2 payload bytes the buffer can hold? Currently they are also added to the buffer until it overflows.  Again you'd do well to ignore them and probably reject the command as invalid (as it doesn't fit the protocol).
    3. What happens after you receive the 0x55 trailer?  Currently sflag gets set to 1, but then there's nothing to to reset rxcount or rxheader for the next packet.  That's probably the reason your second command packet gets ignored.
    post edited by pcbbc - 2019/09/11 09:16:23
    #2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5