Can anyone confirm this RS485 Master Slave works please

An area to discuss 8-bit PIC specific problems and examples

Moderator: Benj

Post Reply
crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

I have played around with the Master Slave RS485 example files provide by Matrix (Steve) some years ago. It was done in an earlier version of Flowcode and I have basically just imported it into Flowcode 7 and made a couple of changes. Code is attached. Has anyone got a couple of old 16F877A chips on Eblock boards that can confirm the ADC value transmitted back to the Master from the Slave is actually showing on the Master LCD? It should show on the LCD as
N:1 $(RxVal data)#

I would like to get some basic feedback on a real hardware test before I try and adapt the code further if at all possible.
The UART1 hardware ports of the two PICs are connected together using two RS485 serial transceiver chips at each end of the bus. The ADC input on the slave node is AN1. The alphanumeric LCD is connected to PortB on the Master. The DE /RE control lines to the transceiver chips are tied together and connected to PORTC.5 on the PICs. The PICs use crystals 19.660800MHz connected to osc1 osc2. The baud rate is 9600, 9 bit data flow control off. The serial data is connected to the PICs internal hardware UART1 ports.
Any standard RS485 transceiver chip configured for half duplex mode should work which is why I haven't stipulated a specific part.
Attachments
SlaveNode1_v7.fcfx
(19.55 KiB) Downloaded 248 times
MasterNode_v7.fcfx
(19.72 KiB) Downloaded 249 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

Hi,
There are a few possible issues.
In the Master you have a 3mS delay after sending before you disable transmission, in the Slave you send a reply after a 2mS delay, that could be too quick (the character will take approx 1mS to send at 9600).
Perhaps first try a more simple program sending and receiving (and printing) a character.
Is the hardware OK, such as do you have the RS485 bias and termination resistors?

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

Hello LeighM

Sorry for the delay in getting back. Thanks for catching the 2mS 3mS difference in the code. I have made 3mS the default throughout but sadly it made no difference.
No I'm not using hardware but doing a quick evaluation in Proteus sim just to see if it runs in simulation ..... but it doesn't. I have waded through a boatload of issues, nearly all to do with Proteus and I'm close to tossing Proteus and just purchasing the chips and a breadboard. One of biggest issues that wasted nearly two days is that I discovered the Proteus MAX487 transceiver model is junk. Gives out totally illegal voltages into the UART ports of the PICs. I managed to come up with an elegant workaround to make the model legal using nor gates and schottky diodes. That model now works well and if anyone wants the workaround schematic let me know and I'll post it. However, did that fix the problem. Not a chance!
I debugged the c code in a coff file running inside Proteus and set break points and it looks like the Flowcode 7 is going round in a loop in the UART initialisation inside the slave. Basically the data IS getting from the Master through the RS485 into the Slave now but the Slave UART isn't getting initialised. WHY?
I'm betting its one of two things
a) there is a a bug in Flowcode's PIC_CAL_UART.C (not likely because I think this would have surfaced with other users BUT my system is transmitting 9 bits serial instead of the common or garden 8 bits so I'm really not sure re. this C file being bug free.
b) The more likely culprit is that Proteus sim model just can't handle the 9 bits initialisation. I really don't know but I guess I'll just get the chips and try the hardware.

The only reason I posted about the Flowcode is that it was written by a Matrix M staff member some years back and I was thinking that surely someone on the forum must have experimented with by now and got an opinion on it?

Anyway, the bit of looping code is attached if anyone can see where there may be an issue into why the UART is not initialising?

Finally I have one important question re. Flowcode UART data flow. Microchip have highlighted the importance of setting ERROR detection.
"You will not receive any more characters until you clear the error.
The PIC datasheet tells you how to clear the error. Usually you must read RCREG twice, and then clear the CREN bit, then set the CREN bit."

My question is does the Flowcode UART macro c code behind the scenes automatically handle the error detection and clear error routine? I would have thought so but just want to check. Does anyone know?
Attachments
looping code.txt
(1.29 KiB) Downloaded 229 times

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

I'm close to tossing Proteus and just purchasing the chips and a breadboard
Probably a good idea (although you might want to use something more recent than 16F877A)
there is a a bug in Flowcode's PIC_CAL_UART.C
No
the bit of looping code is attached
No loops in that code, and known to work well
My question is does the Flowcode UART macro c code behind the scenes automatically handle the error detection and clear error routine?
Yes

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

Thanks for the confirmation on the error handling. However I'm puzzled why my debugger was clearly showing the program was stuck in that section of code. Whenever I paused the program it was always stuck at line 0163 (355). If I stepped forward through the next 40 lines of code the program always jumped back to line 0163. I assumed it was a loop. I cannot think why the program never advances beyond line 018A other than the UART is failing to initialise. Is that section of code handled only once during initialisation or is it accessed every time there is incoming data into the RX port? I really have no idea what that code is doing as I don't have sufficient c language skills to read it

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

I presume these are code line numbers in Proteus?
What does it display as the code for those lines?
Are you sure that is not your "while 1" loop? (That calls ReceiveChar)
Yes the initialisation is done only once, or at least that is the case in the Slave code you posted.

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

Actually that is the line number of the UART c code file. Proteus has the ability using the cof file to display the source code (high level) so any time you pause the simulation you can see what the high level source line is (equivalent to the low level code) that is being executed at the moment the simulation is halted. It does not show output values like a low level code debugger, although you can look at variables and processor status settings in other windows. You can set breakpoints on the c source code however so its fairly easy to watch the program moving through all the c source files that comprise the compiled cof file. You can NOT watch it in real time. You have to pause the simulation and then the source code window shows you whereabouts the program execution has got up to. However, I realise now that I may have screwed up (it was in the early hours of the night) in that I was running (obviously there are 2 PICS master and slave) 2 lots of code and I may have forgotten to clear some old breakpoints on the master side while I was looking at the c source on the slave side. The program execution will halt wherever there is a breakpoint. I haven't yet worked out how to separate the breakpoints between the 2 processors. I really can't afford to spend any more time on Proteus. Its great for simple sims but for something like this when there is too much going on it falls short. Having said that some folks swear by its RS232 terminal emulator which doesn't help me much as I'm not using a terminal but another PIC as the communication device.

Could I simulate this system using EBlocks. I'm correct I would need to use 2 of the main Eblock programmer/mother boards right (one for slave, the other for master), plus a few of the smaller plug in modules such as the LCD and a transducer ADC unit? Just trying to calculate what I would need. I haven't got any Eblock equipment so maybe I should just bite the bullet and go for it. I have Pickit 2/3 modules for programming my chips so they would probably become redundant as the main Eblock board does programming anyway. Sad!

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

Yes, E-blocks will certainly help speed development,
as you can run on real hardware but still debug via the Ghost features, ICD/ICT
e.g. step, set breakpoints and monitor variables etc.

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

Well I think I've nearly cracked it and hopefully with your greater coding experience you might be able to look at my Master unit Flowcode once again and provide the missing link.
Summary: 1. Information from the Master IS arriving at the Slave and being processed.
2. The Slave is sending back the right information to the Master AND
3. The Master's RCREG in the UART is getting the correct data BUT
4. In the Flowcode where I ask for the received data to be put into my RXVal variable is broken. It is NOT getting into the variable. RxVal just stays at 0. Can you please look at the attached image of the vars for both simulated PICs which substantiates the above. Can you also please look at the code (which has been changed a bit) and see why the data isn't going into the var? I'm attaching the jpg image and the flowcode file.
Thanks for all the help so far. I'm hoping I may have a resolution to this after all.
Attachments
MasterNode_v7.fcfx
(19.78 KiB) Downloaded 207 times
Master_Slave_Vars.jpg
Master_Slave_Vars.jpg (646.8 KiB) Viewed 9240 times

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

As a follow up I've just tried changing RxVal to an unsigned integer and then I tried changing to a signed integer. In both cases RxVal now takes the value of 2048 and my LCD displays 204. However, this is still wrong. The decimal value going into the var should be 127.
I'm mystified!

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

Hi,
Yes, some good investigations you have there.
When in 9 bit mode and the Receive code returns a number greater than 511 it indicates an error.
The 2048 indicates an Overrun Error, which is returned when the OERR flag in RCSTA is true.
So maybe Proteus is not supporting the 9 bit receive properly.
I've checked our initialization code and we do set 9 bit mode for both transmit and receive.

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

I suspected that the sim wasn't handling the 9 bit but what totally stumped me is why it handles it in the slave (same chip) when the master signals to the slave? For the slave to transmit anything it first has to receive a 9 bit code (0x181) from the master, It doesn't make sense at all. So I decided to change the slave code to sending a response of 0x181 instead of the ADC value, changed the RxVal to an integer and ran the sim again only to find I was still getting the overflow. So going back to looking at the data coming into the slave from the master a little closer I noticed that it also is going into overflow SOMETIMES BUT it sometimes gets it right allowing the RxVal in the slave to get the right value and trick you into believing its all hunky dory.

So Proteus is definitely not handling the 9 bit code correctly in its simulation of the UART, however why its error is spasmodic instead of constant is a mystery. You'd think it would be either totally wrong or totally correct in whatever its doing unless somehow it was making its decisions on some internal count values which are a changeable feast. The good news is that what I have seen from this investigation is that the data is getting in to and out of the chips and with hardware using a real UART and your affirmation that Flowcode does set up the UARTs correctly for 9 bit frames then odds are on the code works as expected. Not sure yet when i can get to building the hardware but will let you know as and when.

Just out of interested (slightly off topic here) do you know whether I can have 2 different eblock multiprogrammer boards with Ghost data going into a shared USB port on a PC at the same time (each Ghost session going into a different Flowcode instance)? I notice you can get more than one Flowcode on the desktop and simply switch task between them. That's how I've been able to switch quickly between my Master Slave code experiments. So then Im wondering about whether I could have two Ghosts going at the same time from two Eblock systems?

User avatar
LeighM
Matrix Staff
Posts: 2178
Joined: Tue Jan 17, 2012 10:07 am
Has thanked: 481 times
Been thanked: 699 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by LeighM »

Hi,
Yes, you will probably get differing results depending upon the data content.
0x181 will most likely be OK as the last (ninth) bit is 1 so will look like a stop bit in 8 bit mode.

The 2 different eblock multiprogrammer boards with Ghost data going into a shared USB port on a PC won't work.
Flowcode will only recognize one of them. (Both instances would probably try to talk to the same board)
So you would need to debug the Master and Slave separately.
Running two separate PCs/laptops might work.

crispin12
Posts: 51
Joined: Fri Apr 07, 2017 5:51 pm
Has thanked: 6 times
Been thanked: 11 times
Contact:

Re: Can anyone confirm this RS485 Master Slave works please

Post by crispin12 »

Thanks for the heads up on the Ghost question. The suggestion of using 2 PCs is something I had considered because you can pick up a reasonably good second hand middle of the road laptop quite cheaply. The idea of using 2 Ghost sessions, one for transmit, the other for receive to single step debug bus communications would make an attractive development systems with 2 screens as well. 8)

Post Reply