The Godfather talking
This is god damn my place! Capisci?
Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
April 19, 2024, 10:35:57 10:35


Login with username, password and session length


Pages: [1] 2  All
Print
Author Topic: MikroC Multiple PICs communcation protocol  (Read 17171 times)
0 Members and 1 Guest are viewing this topic.
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« on: September 16, 2013, 01:44:06 01:44 »

Hi all,
i am trying to implement a sensor system with MikroC.
My system based on a 4 port sensor hub and multiple sensors.

A sensor sends something like this. I decided that 1 is temperature sensor so the output is : #1|23.456#1|23.456#...blabla... like this continuously. As you can guess sensor based on a DS18B20 and PIC12F683 combination.

Hub collects all 4 port's data and adds port number at the begining of the sensor datas like this : #1|1|23.256#2|0|0#3|0|0#4|0|0#1|1|23.256...blabla.. again it goes on continuously. In this example 2nd,3th and 4th ports are empty. Hub is based on a PIC18F2550 for also communicate with pc by USB, you know...

The problem is which protocol that i can use?

Before you say "Use Serial or bla bla" let me point you on some issues.

if i use serial communication it waits for the response so if no sonsor is on the hub port what it does? and i need to collect all 4 port's data in 1 second so hub can poll to computer each second for realtime measurement. And it is hard to change software UART routines to work with multiple tx rx pins...

I tried to implement a one-wire slave for the PIC12F683 but no luck. I tried to write a 0xFF to slave's GPIO.B1. Slave waits for 0xFF and if it gets 0xFF immediately it waits for 150us and sends a 0x1F to GPIO.B1 to response to master and starts to wait 0xFF again. But it didn't work Sad

I have 3 wires from slave sensors to master sensor hub. But my plug supports 4 wires so if you give and advice and if the advice needs 4 wires i can modify my sensors too, no problem Smiley But i prefer 3 wires if it is possible.

Thank you so much,
sebepsiz
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #1 on: September 16, 2013, 02:36:55 02:36 »

Why do you want to do it this way, I mean use a PIC for each sensor (here we got 4 PICs), then a 5th PIC has to collect data?

How do you poll the four sensors, you can't just assume that a sensor sends sth and the hub should receive it, suppose that all sensors sent data at the same time, how will your hub handle it? Polling means that the hub will ask each sensor to send its reading when its turn comes. When you say that each sensor sends data, then we need some way to tell each sensor when to send its data, can you guarantee that your sensors will never send data at the same time, the issue here is not to use serial or not.. So, the hub knocks the sensor's door, and the sensor answers, as simple as this. 3 wires could mean SPI protocol for example, think also about RS-485 and how it works, still there are many other options that are not on my mind at this moment.

Explain so people understand how your brain ticks. What is the distance between each sensor and your hub? Isn't this an important aspect of your design and should be given when asking such a question?




« Last Edit: September 16, 2013, 02:43:43 02:43 by metal » Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #2 on: September 16, 2013, 04:58:38 04:58 »

This should be easy and you are probably just not using conventional methods for implementing this system.

This is also hard to answer until you have answered the questions that metal asked.

BUT, I2C would be one easy way to do this, single bus, addressable for each sensor, scaleable.

UART with 2 or 4 wire RS485 is another great way to do it.  Only one set of RX/TX pins used on hub, addressable etc.

Again, what you are trying to do get's done all the time so you just need to read up on conventional methods.  Single bus, addressable sensors, short timeout if a sensor doesn't exist, etc.  If you don't want the sensors to have an address then you can do some tricks with response time randomization, and response time slots.  Then, without having an address you can write some routines to mitigate collisions and have each sensor pick time windows to respond until you don't have collisions anymore.  Fixed addressable is much easier.
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #3 on: September 16, 2013, 11:36:32 11:36 »

thank you all for your interest guys Smiley

First hub always works with 4 sensors not more. (because box design of device supports 4 socket holes Smiley

Second yes i am polling. I am sending a magic byte to all sensors step by step. first i send to first sensor. It must answer with the format that i said before like #1|23.65... and then second... third... forth... and at the end hub concatenates all data including port numbers and sends to pc...

Third, only one-wire protocol allows me to change pin numbers in mikroc if i will use a build-in library. Others dont allow to change pins if i use internal hardware. Software versions of the communication controllers like Software_UART or Software_SPI initializes only at first then uses the same pins to communicate so they are useless for me i think...

Forth distance is not so far maximum 1.5 meters.

I dont want to connect all sensors in one communication bus. I am not experienced on SPI or I2C but they are working with Peripheral's address right? So my sensors are not fixed to system. Also sometimes i can connect 4 temperature sensors sometimes 1 temp 1 distance 1 light sensors.
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #4 on: September 16, 2013, 05:36:16 17:36 »

Well good luck.  It sounds like you are making it harder than it needs to be.

SPI is not appropriate really as it requires a CS line and isn't software addressable really (though you could use it for this if you wanted)

I2C is designed as a data bus that is addressable

UART can be done either way, using a tristatable driver like an RS485 transceiver makes it usable for a software addressable system.

I don't really understand why you are opposed to a bus with addressable components.  I should work fine regardless of how many sensors you have connected.

So... You've already decided how you want to do this in spite of suggestions, which is fine, it's your project.  What exactly do you want help with?  You asked about protocols and shot the suggestions down.
« Last Edit: September 16, 2013, 07:56:49 19:56 by Gallymimu » Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #5 on: September 16, 2013, 06:26:36 18:26 »

1-wire can support 1.5m I think for multiple parasitic sensors, and without any problems, I am wondering why he wanted to kill more four PICs. Anyway, there is sth that you are missing about how to use RS-232 over RS-485:

When using RS-485, you need an interfacing IC.  To achieve full-duplex communication, you need 4 wires, in your case, you only need 2-wires, I will explain it later. Look at page 2 of "sn75176a.pdf" Unlike RS-232, one pin is Tx and the other is Rx, RS-485 uses differential signaling and needs two wires for signaling, the direction is defined by DE and /RE, usually these two pins are connected together.

When using RS-485, all you need is two wires for the whole system.. The hub and the four sensors are connected to the same exact two wires. The hub is the master, while the other four sensors are the slaves. For SN75176, connect DE and RE together where each PIC needs one SN75176, when the hub sends the packet (DE/RE=1), all sensors (DE/RE=0) will hear it, assume that sensor 1 is the required sensor to be read. After sending that packet by the hub, hub has to change its (DE/RE=1) to (DE/RE=0) waiting for sensor 1 to send its reading, hence sensor 1 should have (DE/RE=0) to (DE/RE=1) to be able to send data, once hub receives data within a given timeout duration, it toggles (DE/RE=0) to (DE/RE=1) and sensor 1 should toggle its (DE/RE=1) to (DE/RE=0), the same is repeated for other sensors.

I am trying to explain how signaling works. You can use any protocol behind RS-485, hence you can use the same 2-pins for RS-232 protocol on each PIC; the hub only needs one RS-232, not four x 2-pin Wink

Do you understand?
« Last Edit: September 16, 2013, 06:30:05 18:30 by metal » Logged
max
Senior Member
****
Offline Offline

Posts: 314

Thank You
-Given: 1598
-Receive: 52


« Reply #6 on: September 16, 2013, 09:28:19 21:28 »

see the following link for the 2-wire (only), serial network

http://www.romanblack.com/blacknet/blacknet.htm
Logged

Fate arrived and made the conscious unconscious It silenced the activity of life.
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #7 on: September 17, 2013, 10:18:24 10:18 »

look at PCA9600, P82B715 and other ICs which work as I2C bus buffers/extenders. This can solve your problem as well.
Logged
zab
Active Member
***
Offline Offline

Posts: 137

Thank You
-Given: 25
-Receive: 58


« Reply #8 on: September 17, 2013, 10:31:15 10:31 »

ome years back I had a project of such multi micro-controller communication having more than 40 ic linked togather by rs485 network spread over large area.The master and slave way was adopted to avoid data loss. the distance can increased by reducing baud rate. Remember one thing more in hilly area where the clouds are near the ground lightning may damage rs485 ics so it need additional protection
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #9 on: September 17, 2013, 11:40:22 11:40 »

Your opinions are important for me, for sure Smiley
I didn't decide to use any protocol but only i tried to explain handicaps that i saw before Smiley

I am not experienced on SPI but if i use it, how can hub see the type of the sensors? Because type and number of sensors is not fixed. Yes we have 4 ports but i can use 4 temp sensors on 4 ports or 2 temp sensors and 2 distance sensors for example. How can i know which type of devices are connected on one single spi bus? Because they are not fixed number and fixed port based...

Also thanks i will check for two-wire too.

And i dont want to use any extra components because only PIC12F683 fits into the sensor plug perfectly. And also it doesn't need any external components becauseof i am using internal OSC... So rs485 is not suitable for me...

Can you make a fiction on SPI with my scenario? I explained whole system very well i think...

Also is there a way to implement a custom simple protocol that doesn't waits for response from slave and works with timing? Is there anyone that experienced this type of communication? Because i can implement something on MikroC for this too.
« Last Edit: September 17, 2013, 11:44:56 11:44 by sebepsiz » Logged
Catcatcat
Senior Member
****
Offline Offline

Posts: 417

Thank You
-Given: 275
-Receive: 1533



WWW
« Reply #10 on: September 17, 2013, 12:11:16 12:11 »

If it is a DS18B20, control the availability and number of sensors is easily realizable. If interested poprobuyde my library http://catcatcat.d-lan.dp.ua/en/skachat/biblioteki/biblioteka-dlya-rabotyi-s-datchikami-temperaturyi-ds18b20/  in it on one bus and 16 Those switches control
Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #11 on: September 17, 2013, 12:18:22 12:18 »

the problem is in your design to be honest, once you use the word "fit" then it explains your problem very well because such decisions should be made before building the whole system, you might be able to use a smaller PIC, and spread more ICs on both sides of the PCB, also use SMD if you have a space problem. If you did not take into considerations such issues, then your design should seek a way not to add any additional components and then you are stuck.

SPI, you need a "chip select" line for each sensor...  If you really have space problems, then go for 1-wire and seek how to use it on long distances, no needfor the other four PICs..

In what design phase you project is at the moment? I have not seen any documents, nor codes, not even schematics..
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #12 on: September 17, 2013, 10:12:48 22:12 »

Here is my completed PIC18F2550 based sensor hub. I drilled all holes with myself on a prototyping chic plastic case.




Here is my sensor based on PIC12F683 and isolated DS18B20 from DealExtreme. If i can complete the sensor issues i will use macaron isolator to cover the wires.





Metal : I think these photos are enough to figure out the phase ha? Smiley Thanks for all these suggestions.

I need an example to use SPI with my current scenario.

After all these stuff, i think i will use one-wire but i need to implement a slave one-wire library. MikroC only contains for master mode. Existing one-wire library communicates with DS18B20 very well for direct connection but as you know i have extra pics (PIC12F683) between hub and sensors for unique sensor data format. For example if i use DS18B20 it uses one-wire, if i use Ultrasonic Distance Transducer it uses SPI...

Can you help about one-wire slave usage? Or is there anyone that experienced this?
« Last Edit: September 17, 2013, 10:22:58 22:22 by sebepsiz » Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #13 on: September 17, 2013, 10:21:48 22:21 »

You can increase the PCB size to do the required magic.
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #14 on: September 17, 2013, 10:26:37 22:26 »

For which protocol?
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #15 on: September 18, 2013, 04:41:31 04:41 »

I suppose you could make SPI work, you'll need 3 wires minimum for each sensor (SDI, SCK, and CS) data in from sensor, clock, and chip select.

you mentioned wanting to know how to detect what type of sensor is connected?  well you should have a command response type query that interrogates your sensor and returns the type... OR if using one way communication like with 3 wire SPI you'd probably need every packet to also include a byte to indicate sensor type.

in SPI the chip select is used to indicate which sensor you are talking to, and the SDI and SCK are connected in parallel to all devices.  Only the ones with the chip select active (low) will respond so you won't have collisions.  If you put a pullup or pulldown on the SDI line (can't remember which is appropriate for SPI) then you know a sensor is not present if you get all 0xFF or all 0x00 for your data.  No need to have timeouts or anything.  The only thing that might be tricky is making sure the slave device is always ready and fast enough to push data on the SPI bus, if it's in the middle of sampling the ADC while you are trying to collect data over SPI you might have an issue.  This can of course be handled but you have to think about it when designing the software and interrupt routines.

and thanks for sharing pics of your project.
« Last Edit: September 18, 2013, 05:35:25 05:35 by Gallymimu » Logged
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #16 on: September 18, 2013, 08:43:15 08:43 »

For which protocol?

RS485.



For SPI, you can share SDI and SDO as well as SCK pins on PIC18F2550 for all sensors except SC pin, each sensor needs its own SC pin on PIC18F2550. Let's hope that you did it that way Cheesy
« Last Edit: September 18, 2013, 08:46:43 08:46 by metal » Logged
pickit2
Moderator
Hero Member
*****
Offline Offline

Posts: 4647

Thank You
-Given: 826
-Receive: 4207


There is no evidence that I muted SoNsIvRi


« Reply #17 on: September 18, 2013, 08:55:50 08:55 »

Metal he don't want to show you his...
A few things I don't understand
why build the enclosure before project is finished?
why have the 12f683 outside the enclosure?
why did you not use search?

you then may have seen this

http://www.sonsivri.to/forum/index.php?topic=52139.msg150175#msg150175
Logged

Note: I stoped Muteing bad members OK I now put thier account in sleep mode
metal
Global Moderator
Hero Member
*****
Offline Offline

Posts: 2420

Thank You
-Given: 862
-Receive: 678


Top Topic Starter


« Reply #18 on: September 18, 2013, 09:08:23 09:08 »

This member has made excellent work (for PIC) and DS18B20.
I looked at his library yesterday, he deserves thumbs up indeed.

I've already mentioned that he can abandon using 4 PIC12F683 and use 1-wire protocol to connect PIC18F2550 to the sensors.
Logged
zuisti
Senior Member
****
Offline Offline

Posts: 409

Thank You
-Given: 242
-Receive: 780


« Reply #19 on: September 18, 2013, 09:09:23 09:09 »

i think i will use one-wire but i need to implement a slave one-wire library. MikroC only contains for master mode. Existing one-wire library communicates with DS18B20 very well for direct connection but as you know i have extra pics (PIC12F683) between hub and sensors for unique sensor data format. For example if i use DS18B20 it uses one-wire, if i use Ultrasonic Distance Transducer it uses SPI...

1-wire slave emulator for PIC (verbose description, schematic and C source) ...
http://www.fabiszewski.net/1-wire-slave/

It uses a PIC16F84A (and Hi-Tech C), I2C sensor and a PC as one-wire master but I hope helps you
zuisti
Edit:
other links (googled):
http://www.austech.info/electronics/35439-emulate-ds2401-pic.html
(using avr!):
http://www.mcselec.com/index.php?option=com_content&task=view&id=256&Itemid=57
http://bascom.at.ua/publ/1-1-0-1
« Last Edit: September 18, 2013, 10:30:19 10:30 by zuisti » Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #20 on: September 18, 2013, 04:30:16 16:30 »

Thank you for all you guys but i couldn't explain some issues to you after all my explanations. First form factor of the device can be choosed before device design in some projects. There is not a happy path like you said : "first design the device and then select a closure to finish the device." for all projects.

and i want to say that all of you are valuable people that knows a lot of knowledge about sensors and mcu's but you are lookin into issues as blinder.

User can change the port that the sensor connected. You are talking about simple DS18B20 sensor protocol but this is a very basic process to calculate so it is not a problem for me. My problem is to connect any number of and any type of sensor to any port for any time. You are saying SPI but on SPI you need to know the addresses of the devices on SPI bus. User that uses the sensor hub, can change sensors also can broke the sensors so he/she can buy a new sensor from me so it won't work on SPI, guyssss!

In my opinion, i need a custom communication protocol similir to one-wire. I started to implement something like this. Also still if there is a good suggestion about this subject it is welcome. For example a slave one-wire implementation can be useful or as i said a custom simple communication protocol.
« Last Edit: September 18, 2013, 05:30:40 17:30 by sebepsiz » Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #21 on: September 18, 2013, 10:16:34 22:16 »

SPI does not need/use addressing.  I think we understand your project but I don't think you are understanding the explanations that have been given to you.

If you used SPI for instance the sensor wouldn't care which port it was plugged into and the hub wouldn't care either.  the hub would activate the chip select for each port and then collect data from the port.  All ports would see the same clock signal but only the port, with a device, that saw the proper chip select active, would modulate SDI.
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #22 on: September 18, 2013, 10:48:45 22:48 »

So how can i say to developer that this sensor is connected to port 1? Smiley
There is a desktop application that i coded before and it shows which port has which sensor. So where is the port information?
And it opens a socket to connect from HTML5 applications and also i coded a Flash/Flex and a Javascript API to connect to this mini local sensor server.
So how can developer query that which sensor is on port 1 with my API? I need to have the port data Smiley ?
Logged
Gallymimu
Hero Member
*****
Offline Offline

Posts: 704

Thank You
-Given: 151
-Receive: 214


« Reply #23 on: September 18, 2013, 11:00:26 23:00 »

As I said a few posts ago you know because of the chip select (CS).  You have a chip select line for each port, only the sensor on the port with an active chip select will respond.

So if you have a temp sensor on port 1,2,3 and 4, and I activate CS2, start clocking SCK and I get a response on SDI then I know that I received a message back from port 2 since the devices on port 1,3,4 won't respond because their chip select lines are not active.  If you get a response of all 0xFF or 0x00 then you know there is not a sensor on port 2.

again this scheme requires that SCK and SDI are connected to ALL PORTS, and CS1, CS2, CS3, and CS4 are connected to the CS pin of each port/sensor.

http://www.click-server.com/amicus/Port%20Expansion/Higher%20Res%20Images/Amicus18%20to%20Multiple%20SPI%20slaves.png

Note you would not need to use SDO (data going to the sensor) in the image since one way communication is probably sufficient.
Logged
sebepsiz
Newbie
*
Offline Offline

Posts: 11

Thank You
-Given: 1
-Receive: 0


« Reply #24 on: September 18, 2013, 11:08:00 23:08 »

Now this is more clear for me. Many thanks but there another issue that it will require 6 wires i think? right? 4 for CS and the other SPI stuff and VDD and VSS. My connectors have 4 pins. Sad
Logged
Pages: [1] 2  All
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC