Hello,
Since last week I needed to use RS485 in one of my projects. I tested the ME rs485 boards with an easypic 6 and 7 and Pic18F4620
I had to use a lower cpu then K series because easypic6 does not suport the K series, and I do not know how to upgrade it.
It was a total failure as you can see in one of my previous posts. http://www.mikroe.com/forum/viewtopic.php?f=97&t=53377
I am not a guru, but I read a lot in the last days and decided that 485, beside some special protocol in formating the data, is in fact just simple rs232 transmission - TX, RX, and Control pin.
O yes, here is the problem many users I found on the web they did not solve it - control pin
As a last resort, thousands of them used additional hardware to solve the problem.
I did not have access to ME source code for 485 library so I decided to make my own.
Using 485, imply that the control line is High when you transmit and Low at receiving
It is not the purpose of this notes to explain how to avoid collision on the bus, and everybody can use what they fit to them or the application
All I want is to explain how simple is to control that line with 5 lines of code
First you prepare your data so you can send it in one shot
The number of bytes used is what you need plus TWO extra bytes as $00 at the end
Your message will be START byte , X number of bytes of data, checksum byte or any method to check integrity of data , END BYTE and then the two zero bytes
On the code , I put all the data together ( I omitted for test the error checking), then I PULL-UP the control line, Then I went to Subroutine when I just have
Code: Select all
sub procedure Transmit
TxRx_pin = 1
delay_ms(2)
for j = 0 to 6
Uart1_Write(TxData[j])
if j = 6 then TxRx_pin = 0 end if
next j
end sub
The problem most users have is to pull down the line at the end of transmission.
Most advanced guru, use interrupts, or a flag from Txsta register :bit 1 aka TRMT = Transmit shift register status bit . It is 1 when the register is empty (data was sent) or zero when is data to be shifted out in register.
I tried and is not reliable for me. During the transmission, I got spikes , line went down for few microseconds, and transmission was garbage for that portion.
As I said I am not a good programmer , but I try to solve it the easy way.
First, as you can see in the picture, I can put a delay - 2 ms in my case after I pull the line up for transmission.
The TRMT, is mentioned in the data sheet that if you use it you must have a dely before pulling the line down, or you risk to lose the last byte.
Here they come the two zero bytes. After the END transmission byte, I send the two zero bytes, then I pull the line down. So in this case, even if I miss the last byte, I do not care because in the receiver side I got my FULL data between START and END
In the picture, you see that I lost the last byte, but I do not care, as it was a spare to discard it anyway.
I will make soon a movie and posted in the you tube so you can see it
Steps:
Pack your data
Code: Select all
dim txdata as byte[7]
TxData[0] = $02
TxData[1] = (data_rec[1]+1)
TxData[2] = (data_rec[2]+1)
TxData[3] = (my_ID)
TxData[4] = ($03)
TxData[5] = $00
TxData[6] = $00
Transmit ()
It is your responsibility to prepare the software and extract the data between Start and End, do error checking and discard unused bytes
No fancy libraries to learn, no number of bytes limitation, no headache controlling interrupts, no speed limit.
Video http://www.youtube.com/watch?v=ZrJKRvyd ... e=youtu.be
Have fun
Ion










