# [SOLVED]PIC16F877A controlled 74HC595 by serial command

#### sherryliu

##### Member level 1
Hi all

I have a project need controlled 74hc595 by serial port send command such as send"0xEA 0xFC 0x01 55"controlled Q0,and send"0xEA 0xFC 0x02 55"controlled Q1,
but i have a problem when i send first command is right,then send second command or third command MCU was stuck,as snapshot,who can help me fixed it ,thanks in advance!

#### Attachments

• 123.9 KB Views: 8
• 383.8 KB Views: 18

#### betwixt

##### Super Moderator
Staff member
I cannot use Proteus but looking at the code it seems you overflow the receive buffer after a few characters have arrived fro the USART.
You use
Code:
char buf[]={0x00,0x00,0x00,0x00}; ......

buf[rxcount]=RCREG;
rxcount++;
but you do not check that "rxcount" has got bigger than 3. Remember you need a zero at the end of the characters to mark the end of the data so "buf" has to be at least one character bigger than your expected input.
There should be something to reset "rxcount" to zero again (maybe "rxcount = 0; buff[0] = 0;") when you have finished processing it.

Brian.

#### baileychic

I can help you with your project. You really need to use a delimiter at the end of each UART command transmission which controls the output of the 74HC595. A 0x0D delimeter will be fine. If you use delimiters like '\r\ or '\n' that is ENTER key then Proteus Virtual Terminal will not handle it and it will not send those characters to your PIC.

It is better if you can use COMPIM model without MAX232 in Proteus and link Proteus with any Serial Terminal software which can send delimiters like '\r' or '\n' at the end of each 4-byte command that you are using.

I will check your code today but I can't help much with MPLAB Hi-Tech PICC code because it is very old and I don't have it installed.

Tell me which delimiter you can use at the end of your 4-byte commands and I will tell you how to fix the code.

Once the end delimiter is received or if the byte lenght exceeds your 4-byte command then you have to reset the buffer index to 0 and set a flag which will be tested for true in the main loop and HC595 controlled in the main loop and then flag creared so that UART can receive new commands after processing the previous one.

#### sherryliu

##### Member level 1
Hi all

Thank for all comment,I have been fixed it,thanks!!