News:

Let's find out together what makes a PIC Tick!

Main Menu

Nesting Procedures

Started by JohnB, Jun 01, 2026, 03:47 PM

Previous topic - Next topic

JohnB

I am trying to develop a frame based protocol to operate between a PIC16F15245 and a PC.
I have implemented buffered serial in and out in the standard way.

A frame consists SOH|LEN|SEQ|CMD|PayLoad|CRC|ETX

I have a Proc Send_Frame(CMD, Seq, Payload, Payload_Len)
I have a Frame_Data array which Send_Frame populates then calls Proc Buf_Put(Value As Byte) for each populated item in the Frame_Array. Proc Buf_Put puts data into the TXBuffer queue which is then emptied by the ISR.

I am getting corruption, generally blocks of 0s being sent occasionally within a message.  So I will see SOH then loads of zeros then the rest of the intended message strung on the end.  I have tried disabling all the other interrupts in the system and removed all calls in my main loop save for a simple heartbeat message which I send every 5 secs. The corruption seems to appear pseudo randomly although rarely less less frequently than 8 messages.

So MainLoop calls Send_Heartbeat, which calls Send_Frame which calls Buf_Put which feeds bytes into the TXBuffer.  While TX Buffer is not empty, the Transmit ISR will empty the TxBuffer.

I have proved the basic operation in a minimised version of the program and it works without corruption but when I transfer that into my main program the corruption appears.  I have been using a logic analyser on the USART output for monitoring.

I have been trying to track this down for a week now without success. I am wondering if I am getting stack issues or memory mapping issues.  I have attached both the main program and minimised program for anyone with the courage to take a look.  BTW, attached is the end product or more accurately the boatyard state of model boat in question. Many more coats of varnish required before I get the required finish but its getting there slowly.

Any suggestion or input would be much appreciated.
 
JohnB

top204

#1
Hello John.

In your Buf_Put procedure.

The code line:

Next_Tail = (TX_Tail + 1) And TX_Buf_Mask

should be

Next_Tail = (TX_Tail + 1) & TX_Buf_Mask

For bit-masking, it is "|", "&" or "^" For Or, And, Xor.

Regards
Les

JohnB

Thanks Les, pointing that out made me realise I echoed that mistake more importantly in the ISR.  After correcting that I am getting clean heartbeats every time.
JohnB