Hallo,

Ik ben sinds kort begonnen met de pic-cursus van picbasic.nl. Ondertussen heb ik picbasic onder de knie. Ik heb ook zelf al wat gemaakt. Ik heb nu de volledige versie van de Proton pic basic. Nu wil ik een stapje verder en gaan experimenteren met communicatie tussen pics. Ik wil iets maken met een master-pic en meerdere slaves. Het is de bedoeling dat via een datalijn gegevens van master naar slave wordt gestuurd, en ook andersom. Ik heb al van alles gelezen over UART en I2C, maar ik kom er niet uit. Ik ken nog niet al die moeilijke termen. Nu is mijn vraag, bestaat er een makkelijk communicatiemiddel voor pics zodat er simpel commando's kunnen worden verzonden. Bijvoorbeeld send : 23 en dat een andere pic dat opvangt en verwerkt. Ik heb het domotica project van picbasic.nl gezien, maar kom er niet uit. Alvast bedankt!

Groeten,
Bartjan12

I2C is prima geschikt daarvoor. Gewoon een als master instellen en de rest als slave.
Je kunt dan aan data versturen wat je wilt.

Ja, maar ik snap niet hoe je dan de pics aan elkaar koppelt, en de code in picbasic.

Koppelen is gewoon de SDA en SCL lijnen van alle deelnemers aan elkaar knopen. (met een pull-up van 4k7 aan beiden.)
Picbasic gebruik ik helaas niet...

Op 15 februari 2011 21:44:05 schreef Bart-Jan12:
Ja, maar ik snap niet hoe je dan de pics aan elkaar koppelt, en de code in picbasic.

De code staat in de manual. Niet letterlijk maar wel hoe je I2C inleest en uitstuurt.

Als TS nog maar weinig weet van dit soort zaken lijkt mij de (E)U(S)ART een betere oplossing. Die heeft tevens als voordeel dat heen-en-weer sturen minder complex is dan met I2C.


Pic 1                  Pic2
    TX  -------------  RX
    RX  -------------  TX
    Gnd -------------  Gnd

Kijk nu in je PicBasic documentatie naar de functie's HSERIN en HSEROUT. In de datasheet van je PIC's zoek je de pinnen TX en RX, Gnd = Vss.
Als je PIC's hebt die geen hardware UART hebben moet je de functies Serin en Serout bekijken.

Voor HSerin en HSerout kijk je vervolgens naar de declares die je nodig hebt:
HSERIAL_BAUD
HSERIAL_RCSTA
HSERIAL_TXSTA
HSERIAL_Clear
Beide Pic´s moeten, uiteraard, dezelfde waarde bij HSERIAL_BAUD hebben.
Als je geen hardware UART hebt staan de opties in de functie Serin-Serout. Ook hier natuurlijk dezelfde waarden voor de baudrate gebruiken.

Communiceren doe je nu als volgt, er van uit gaand dat ze om de beurt sturen en luisteren.
Pic1 zendt naar Pic2
Pic1 HSerout .........
Intussen wacht Pic2 braaf
Pic2 HSerin .....

I2C is gemaakt voor master <--> multiple slaves (zoals TS wil), gaat bij usart wat lastiger...

Sorry, niet goed genoeg gelezen...

Is overigens ook prima te realiseren met UART door in de te zenden data een identificatie van de PIC waarvoor de boodschap is bedoeld op te nemen. Ik ben momenteel bezig met ongeveer zo'n oplossing waarbij ik via 433 MHz zenders/ontvangers werk.
Maar idd is I2C mede daar voor ontworpen.

[Bericht gewijzigd door hadv op (18%)]

Heeft iemand misschien een schema? Ik gebruik als pic de PIC16F628A.

Die pic's ondersteunen geen I2C. Dus dat gaat helaas niet door. Of je moet kijken voor een andere pic.

Volgens mij ondersteunt picbasic ook softwarematige I2C, dus dat hoeft geen belemmering te zijn.
Het 'schema' is al eerder gegeven door Arco, namelijk hier

En anders de datasheet downloaden, dat scheelt bij het zoeken van de pinnetjes. Voor seriële communicatie geldt: zender TX pin aan ontvanger RX pin en andersom.

Ok. Wat voor pic zou ik dan moeten nemen? Kan ik deze dan ook gewoon met de wisp648 programmeren?

In het domotica-project van picbasic.nl wordt wel gewoon een PIC16F628A gebruikt. Er staat dat er een fire-and-forget protocol is gebruikt. Dat is in mijn geval denk ik genoeg. Zou ik dit kunnen gebruiken?

Bartjan12

Ik denk dat I²C iets te hoog gegrepen is. Het is inderdaad heel makkelijk om de master "BUSIN" en "I2CIN" commando's te laten doen, maar deze commando's zijn onbruikbaar in de slaves. De commando's zijn namelijk alleen voor masters ontworpen, en genereren dus de klokpuls.

Ik ben zelf al een tijdje bezig om mijn kamer een beetje te automatiseren (lichten automatisch, luxaflex), en hierbij heb ik dezelfde verbinding aangelegd als Frits. Hetzelfde schema ook:
http://www.picbasic.nl/images/schema_communicatie2.gif
De voordelen hiervan zijn dat je maar 1 signaaldraad hebt liggen, en dat elke PIC een master kan zijn. Maar in jouw geval heb je maar één master, dus je zou ook ervoor kunnen kiezen om de Tx van de master aan alle Rx-en van de slaves te verbinden. De Rx van de master gaat aan alle Tx-en van de slaves. Het nadeel hiervan wel is is wanneer je data collision krijgt, je de IO's kan mollen... Maar bij een goed protocol (wanneer je dus niet meerdere slaves tegelijkertijd aanspreekt) kan dit natuurlijk nooit gebeuren.

Ok. Ik denk dat ik mijn situatie niet helemaal goed heb uitgelegd. Bij mij is het de bedoeling dat de master naar slaves stuurt, maar ook de slaves naar de master. Ik denk dat ik maar eens een beetje ga experimenteren met de voorbeelden van picbasic.nl. Bedankt voor alle informatie!

Bartjan12

Je bedoelt dus niet master/slave maar een multi-master systeem. (slaves geven alleen antwoord aan de master, en versturen niets uit eigen beweging)

Dus peer-to-peer. Dan is gewone seriele communicatie dus prima geschikt.
Heb je het protocol al op conceptueel nivo ontworpen.
Daarmee bedoel ik:
- wat voor soort informatie/commando wordt er verstuurd van 'master' naar 'slave'
- hoe ziet de reactie van de 'slave' er uit per ontvangen bericht, wat sturen ze terug
- kunnen 'slaves' ook onderling communiceren
- kunnen 'slaves' het initiatief nemen en iets naar de 'master' sturen

Ok. Ik ga even opzoek hoe ik een seriele communicatie kan opzetten. Allemaal bedankt voor jullie medewerking!

Bartjan12

Wat is er mis met UART ?

[edit] Oeps dat is geen bus.

[Bericht gewijzigd door Shiptronic op (41%)]

Ik wil denk ik maar beginnen om communicatie tussen 2 pics te leren. Dus als ik seriele communicatie opzet, gaat Tx aan Rx en Rx aan Tx. Welke commando's heb ik dan nodig om te communiceren (en eventueel andere instellingen)? Alvast bedankt!

Bartjan12

Lees de cursus
Hserout en Hserin
Kijk naar de declares die hierbij horen!

[Bericht gewijzigd door hadv op (71%)]

ehh... Welke cursus?
Laat maar!

[Bericht gewijzigd door Bart-Jan12 op (32%)]

Dus als ik bijvoorbeeld de volgende code gebruik:

Zender


IF S1 AAN THEN
Hserout = 26

Ontvanger


IF Hserin = 26 THEN
ON LED1

Zou dat moeten werken?

Lees lees lees .....

Als je de help van "Proton Picbasic had bekeken" had je een hele duidelijke uitleg gezien, namelijk:


Syntax

HSEROUT [Item { , Item... }]

Overview

Transmit one or more Items from the hardware serial port on devices that support asynchronous serial communications in hardware.

HSerOut2 has exactly the same syntax and operation as HSerOut but uses the second USART available on some PICmicro devices.

Operators
Item - a constant, variable, expression, string list, or inline command.
There are no operators as such, instead there are modifiers. For example, if an at sign'@' precedes an Item, the ASCII representation for each digit is transmitted.

The modifiers are listed below: -
Modifier
AT ypos,xpos
CLS
BIN{1..32}
DEC{1..10}
HEX{1..8}
SBIN{1..32}
SDEC{1..10}
SHEX{1..8}
IBIN{1..32}
IDEC{1..10}
IHEX{1..8}
ISBIN{1..32}
ISDEC{1..10}
ISHEX{1..8}
REP c\n
STR array\n
CSTR cdata
Operation
Position the cursor on a serial LCD
Clear a serial LCD (also creates a 30ms delay)
Send binary digits
Send decimal digits
Send hexadecimal digits
Send signed binary digits
Send signed decimal digits
Send signed hexadecimal digits
Send binary digits with a preceding '%' identifier
Send decimal digits with a preceding '#' identifier
Send hexadecimal digits with a preceding '$' identifier
Send signed binary digits with a preceding '%' identifier
Send signed decimal digits with a preceding '#' identifier
Send signed hexadecimal digits with a preceding '$' identifier
Send character c repeated n times
Send all or part of an array
Send string data defined in a CData statement

The numbers after the BIN, DEC, and HEX modifiers are optional. If they are omitted, then the default is all the digits that make up the value will be displayed. If a floating point variable is to be displayed, then the digits after the DEC modifier determine how many remainder digits are send.i.e. numbers after the decimal point.

DIM FLT AS FLOAT
FLT = 3.145
HSEROUT [DEC2 FLT] ' Send 2 values after the decimal point

The above program will send 3.14

If the digit after the DEC modifier is omitted, then 3 values will be displayed after the decimal point.

DIM FLT AS FLOAT
FLT = 3.1456
HSEROUT [DEC FLT] ' Send 3 values after the decimal point

The above program will send 3.145

There is no need to use the SDEC modifier for signed floating point values, as the compiler's DEC modifier will automatically display a minus result: -

DIM FLT AS FLOAT
FLT = -3.1456
HSEROUT [DEC FLT] ' Send 3 values after the decimal point

The above program will send -3.145

HEX or BIN modifiers cannot be used with floating point values or variables.

The Xpos and Ypos values in the AT modifier both start at 1. For example, to place the text "HELLO WORLD" on line 1, position 1, the code would be: -

HSEROUT [AT 1 , 1 , "HELLO WORLD"]

Example 1

DIM VAR1 AS BYTE
DIM WRD AS WORD
DIM DWD AS DWORD

HSEROUT ["Hello World" ' Transmit the text "Hello World"
HSEROUT ["VAR1= " , DEC VAR1] ' Transmit the decimal value of VAR1
HSEROUT ["VAR1= " , HEX VAR1] ' Transmit the hexadecimal value of VAR1
HSEROUT ["VAR1= " , BIN VAR1] ' Transmit the binary value of VAR1
HSEROUT ["VAR1= " , @VAR1] ' Transmit the decimal value of VAR1
HSEROUT ["DWD= " , HEX6 DWD] ' Transmit 6 hex characters of a DWORD type variable

Example 2

' Display a negative value on a serial LCD.
SYMBOL NEGATIVE = -200
HSEROUT [AT 1 , 1 , SDEC NEGATIVE]

Example 3

' Display a negative value on a serial LCD with a preceding identifier.
HSEROUT [AT 1 , 1 , ISHEX -$1234]

Example 3 will produce the text "$-1234" on the LCD.

Some PICmicros such as the 16F818, 16F87x, and 18FXXX range have the ability to read and write to their own flash memory. And although writing to this memory too many times is unhealthy for the PICmicro, reading this memory is both fast, and harmless. Which offers a unique form of data storage and retrieval, the CData command proves this, as it uses the mechanism of reading and storing in the PICmicro's flash memory. Combining the unique features of the 'self modifying PICmicro's ' with a string format, the compiler is capable of reducing the overhead of printing, or transmitting large amounts of text data. The CSTR modifier may be used in commands that deal with text processing i.e. SerOut, HRSOut and Print etc.

The CSTR modifier is used in conjunction with the CData command. The CData command is used for initially creating the string of characters: -

STRING1: CDATA "HELLO WORLD" , 0

The above line of case will create, in flash memory, the values that make up the ASCII text "HELLO WORLD", at address STRING1. Note the NULL terminator after the ASCII text. NULL terminated means that a zero (NULL) is placed at the end of the string of ASCII characters to signal that the string has finished.

To display, or transmit this string of characters, the following command structure could be used:

HSEROUT [CSTR STRING1]

The label that declared the address where the list of CData values resided, now becomes the string's name. In a large program with lots of text formatting, this type of structure can save quite literally hundreds of bytes of valuable code space. Try both these small programs, and you'll see that using CSTR saves a few bytes of code. First the standard way of displaying text: -

DEVICE 16F877
CLS
HSEROUT ["HELLO WORLD",13]
HSEROUT ["HOW ARE YOU?",13]
HSEROUT ["I AM FINE!",13]
STOP

Now using the CSTR modifier: -

CLS
HSEROUT [CSTR TEXT1]
HSEROUT [CSTR TEXT2]
HSEROUT [CSTR TEXT3]
STOP

TEXT1: CDATA "HELLO WORLD" , 13, 0
TEXT2: CDATA "HOW ARE YOU?" , 13, 0
TEXT3: CDATA "I AM FINE!" , 13, 0

Again, note the NULL terminators after the ASCII text in the CData commands. Without these, the PICmicro will continue to transmit data in an endless loop. The term 'virtual string' relates to the fact that a string formed from the CData command cannot be written too, but only read from.

The STR modifier is used for sending a string of bytes from a byte array variable. A string is a set of bytes sized values that are arranged or accessed in a certain order. The values 1, 2, 3 would be stored in a string with the value 1 first, followed by 2 then followed by the value 3. A byte array is a similar concept to a string; it contains data that is arranged in a certain order. Each of the elements in an array is the same size. The string 1,2,3 would be stored in a byte array containing three bytes (elements). Below is an example that displays four bytes (from a byte array): -

DIM MYARRAY[10] AS BYTE ' Create a 10-byte array.
MYARRAY [0] = "H" ' Load the first 5 bytes of the array
MYARRAY [1] = "E" ' With the data to send
MYARRAY [2] = "L"
MYARRAY [3] = "L"
MYARRAY [4] = "O"
HSEROUT [STR MYARRAY \5] ' Display a 5-byte string.

Note that we use the optional \n argument of STR. If we didn't specify this, the PICmicro would try to keep sending characters until all 10 bytes of the array were transmitted. Since we do not wish all 10 bytes to be transmitted, we chose to tell it explicitly to only send the first 5 bytes. The above example may also be written as: -

DIM MYARRAY [10] AS BYTE ' Create a 10-byte array.
STR MYARRAY = "HELLO" ' Load the first 5 bytes of the array
HSEROUT [STR MYARRAY \5] ' Send 5-byte string.

The above example, has exactly the same function as the previous one. The only difference is that the string is now constructed using STR as a command instead of a modifier.

Declares

There are four Declare directives for use with HSerOut and five for HSerOut2. These are: -

DECLARE HSERIAL_BAUD Constant value
Sets the BAUD rate that will be used to transmit a value serially. The baud rate is calculated using the XTAL frequency declared in the program. The default baud rate if the Declare is not included in the program listing is 2400 baud.

DECLARE HSERIAL_RCSTA Constant value (0 to 255)
HSERIAL_RCSTA, sets the respective PICmicro hardware register RCSTA, to the value in the Declare. See the Microchip data sheet for the device used for more information regarding this register. Refer to the upgrade manual pages for a description of the RCSTA register.

DECLARE HSERIAL_TXSTA Constant value (0 to 255)
HSERIAL_TXSTA, sets the respective PICmicro hardware register, TXSTA, to the value in the Declare. See the Microchip data sheet for the device used for more information regarding this register. The TXSTA register BRGH bit (bit 2) controls the high speed mode for the baud rate generator. Certain baud rates at certain oscillator speeds require this bit to be set to operate properly. To do this, set HSERIAL_TXSTA to a value of $24 instead of the default $20. Refer to the Microchip data sheet for the hardware serial port baud rate tables and additional information. Refer to the upgrade manual pages for a description of the TXSTA register.

DECLARE HSERIAL_PARITY ODD or EVEN
Enables/Disables parity on the serial port. For both HSreOut and HSerIn The default serial data format is 8N1, 8 data bits, no parity bit and 1 stop bit. 7E1 (7 data bits, even parity, 1 stop bit) or 7O1 (7data bits, odd parity, 1 stop bit) may be enabled using the HSERIAL_PARITY declare.

DECLARE HSERIAL_PARITY = EVEN ' Use if even parity desired
DECLARE HSERIAL_PARITY = ODD ' Use if odd parity desired

Second USART Declares

DECLARE HSERIAL2_BAUD Constant value
Sets the BAUD rate that will be used to transmit a value serially. The baud rate is calculated using the XTAL frequency declared in the program. The default baud rate if the Declare is not included in the program listing is 2400 baud.

DECLARE HSERIAL2_RCSTA Constant value (0 to 255)
HSERIAL2_RCSTA, sets the respective PICmicro hardware register RCSTA2, to the value in the Declare. See the Microchip data sheet for the device used for more information regarding this register. Refer to the upgrade manual pages for a description of the RCSTA2 register.

DECLARE HSERIAL2_TXSTA Constant value (0 to 255)
HSERIAL2_TXSTA, sets the respective PICmicro hardware register, TXSTA2, to the value in the Declare. See the Microchip data sheet for the device used for more information regarding this register. The TXSTA register BRGH2 bit (bit 2) controls the high speed mode for the baud rate generator. Certain baud rates at certain oscillator speeds require this bit to be set to operate properly. To do this, set HSERIAL2_TXSTA to a value of $24 instead of the default $20. Refer to the Microchip data sheet for the hardware serial port baud rate tables and additional information. Refer to the upgrade manual pages for a description of the TXSTA2 register.

DECLARE HSERIAL2_PARITY ODD or EVEN
Enables/Disables parity on the serial port. For both HSreOut2 and HSerIn2 The default serial data format is 8N1, 8 data bits, no parity bit and 1 stop bit. 7E1 (7 data bits, even parity, 1 stop bit) or 7O1 (7data bits, odd parity, 1 stop bit) may be enabled using the HSERIAL2_PARITY declare.

DECLARE HSERIAL2_PARITY = EVEN ' Use if even parity desired
DECLARE HSERIAL2_PARITY = ODD ' Use if odd parity desired

Notes

HSerOut and HSerOut2 can only be used with devices that contain a hardware USART. See the specific device's data sheet for further information concerning the serial input pin as well as other relevant parameters. Since the serial transmission is done in hardware, it is not possible to set the levels to an inverted state in order to eliminate an RS232 driver. Therefore a suitable driver should be used with HSerOut . See HSerIn for circuit examples

See Also

DECLARE
RSIN
RSOUT
SERIN
SEROUT
HRSIN

Als je dit niet begrijpt of je wilt niet investeren in jou kennis om verder te kijken, is hetgeen jij wilt waarschijnlijke te moeilijk voor jou.

Dus HSEROUT[26] (in jouw voorbeeld).