Serial

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Serial

Post by amishra123 »

Hey everyone, I am quite new to this and require your help!
I am using Flowcode 4 and 2 PIC 18F4520

What I have to do is this. There are 8 switches on Port B (one each byte)on a PIC(1) and I have to somehow send the signals of when they are pressed via serial to another PIC(2) which has 8 LED's on the PORT C. So when switch 1 is pressed on PIC 1, LED 1 come on on PIC 2.

I have absolutely no idea how to go about this and require your help!
I have a feeling I might need to use variables somehow maybe (Variable1=port B), I literally have no idea as I am am a complete beginner!

I would really appreciate any help you could give me and look forward to your replies!

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Serial

Post by Spanish_dude »

The UART pins of the 18F4520 are RC6(TX) and RC7(RX) so you won't be able to drive 2 LEDs on those I/O, as they are used for the UART.

From your description you have a Master µc (PIC1) and a Slave µc (PIC2).

What you'll need to do:
  • Master:
    • Read PORTB
    • Send PORTB byte to Slave µc
  • Slave:
    • Read data from UART
    • Write data to PORTX (PORTC has the UART pins, you could use PORTB instead)
It's a bit late to do a Flowcode program right now but if you have a bit of knowledge of programming you should be able to start with your program.
If you don't then I (or someone else as I'm not available all the time) can help you make this program.

PS: May I ask you why you're in such a hurry to make this program ? ^^

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi thank you! I figured I would have to do something like that! The problem is I don't know how to do it!! I have very basic pic knowledge (Used logicator a while ago , which is only vaguely similar!)

So if you could tell me how to do each of those steps!!

Thank you again I really do appreciate your effort!
THanks Amishra

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi!! Could someone please help!! I really need this ASAP!! :?

If someone can make a program for me I am willing to donate up to £10! :mrgreen:

Thanks you very much!!!
Amishra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello Amishra,

This should do the job to get you up and running.
Attachments
serial example.fcf
(9.5 KiB) Downloaded 427 times

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi, Its says unexpected file format!!

Thanks Amishra

Spanish_dude
Posts: 594
Joined: Thu Sep 17, 2009 7:52 am
Location: Belgium
Has thanked: 63 times
Been thanked: 102 times

Re: Serial

Post by Spanish_dude »

Have you upgraded your Flowcode v4 to the latest revision ?

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi! Thanks yeah i have updated and can open it now! It doesn't really do what I needed!

What I need is when a switch is pressed on the master pic, it is sent by serial to the slave pic and the corresponding LED comes on. I don't really know how to go about this and could really do with your help!!!

Thanks AMishra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello,
What I need is when a switch is pressed on the master pic, it is sent by serial to the slave pic and the corresponding LED comes on. I don't really know how to go about this and could really do with your help!!!
That is exactly what my example file is doing. The file will work for both the master and slave programs.

Eg when the input on portB changes a byte is sent out containing the switch values. When an incoming byte is received it is passed directly through to the LEDs.

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

In your file, when I press play all of the LED's come on?

Also as i press the different switches switches 0-4 give the same serial out "**". :?

Is there any way I can send a serial in to test it?

Thanks Amishra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello,

The simulation only works with no data = 255 which is why the LEDs are always seemingly on. Hardware should work correctly as I have changed the invalid incoming message to 512 instead of 255.

To test you could maybe use the vNet functionality or simply click the + symbol on the rs232 component during simulation to queue up bytes.

http://www.youtube.com/watch?v=_vzwgYKdBBI

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi, Thank you for that! In the end I realised it was as simple as input to variable then send! And receive on the other side!!

Now I have to step things up with using analogue inputs!!!
I started of as not knowing anything but fiddling around with the one you made I am beginning to understand a little!

So now I need to input 8 analogue inputs send them by Serial then on the other pic send them out by SPI to an 8 channel DAC to give out the analogue at the other side. The serial will eventually be sent wirelessly using Mewi.

I am guessing I read each analogue switch in using a macro to a string variable. But how would i get each of the 8 variables to as a charecter in the string??!

Sorry I hope I have made it clear!
Thanks Amishra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello Amishra,

Take for example you have a string variable called data.

You would do something like this.

component macro: sample = adc0_Read_byte
calculation: data[0] = sample

component macro: sample = adc0_Read_byte
calculation: data[1] = sample

etc..

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Thanks

Do you mean say for two sample it would have to be like this:

component macro: sample0 = adc0_Read_byte
calculation: data[0] = sample0

component macro: sample1 = adc1_Read_byte
calculation: data[1] = sample1
etc

?
Thanks Amishra

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Great thanks for that! How would I decode it on the other side?

As in how does the syntax work in the component macro box?
It asks for two parameters in the first input box nTimout(byte) and Numbytes(byte) what do they signify?
Also how does the reciever know that the numbers coming in on the serial is a string of 6 for example and not 6 different characters.

Otherwise would I just invert send one as in
Variable 1 = string[1]
Variable 2 = string[2]
etc??
Thank you Amishra

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello Amishra,

The component help file should answer all of your questions.

The string send function takes a string of length defines in the Flowcode variables manager. If you always send a fixed number of bytes then I would recommend changing the array size in the variable manager to match the number you require.

The string receive function needs a timeout between bytes this is simply how long to wait for incoming data. 0 is don't wait, 1 is wait up to 1 time, 254 is wait up to 254 counts and 255 is wait forever. The numbytes is the number of bytes you wish to try and receive before a timeout occurs, this should match the maximum number of bytes you are going to be sending.

You can then strip the readings out of the string variable by again using a calculation icon.

reading = string[0]
etc...

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Thank you for the reply!

Another thing I have noticed and can't figure out why! If you put ADC0 to 0 then it doesnt read and send the rest of them.

Why is that?! I have attached the file so you can see what i mean
Thank you so much Amishra
Attachments
Transmit serial analogue.fcf
(13 KiB) Downloaded 259 times

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello,

Ah yes that is a problem. A value of 0 basically terminates the string and stops the value from being sent.

This version of the component code should allow the 0 to be passed correctly on the hardware. Simply copy it into your Flowcode v4/Components directory before starting Flowcode.
Attachments
PIC_RS232.c
(13.84 KiB) Downloaded 277 times

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi!
Thanks for that but it still stops at 0. I downloaded the file you gave me and copied it over to the components folder overwriting the other file but it doesnt seem to have made a difference. Ideally when all the Analogue inputs are 0 it should send 0,0,0,0,0,0. Then if the last one is on it would send 0,0,0,0,0,134. etc . Because at the moment it means that it won't send the last input unless the 5 before it are on!

The idea is basically that the analogue inputs are being read in one pic. This is then sent wirelessly using Mewi(which is why i am using serial) to another pic which then sends the information to a 8 channel DAC which gives the analogue voltage out again. I am using it basically to make some analogue switches wireless. I now only have 1 and a 1/2 weeks left to do it aswell :/!!

Also I don't understand why it is always giving 255 on the first character on the receiving one(attached)

Ha anyway one step at a time!!
Thanks Amishra
Attachments
Reciever SPI.fcf
(8.5 KiB) Downloaded 254 times

Mathy
Posts: 333
Joined: Mon Oct 05, 2009 2:39 pm
Has thanked: 30 times
Been thanked: 33 times

Re: Serial

Post by Mathy »

Hello,

I think you can passtrough the problem using the sendchar macro.
I'm sure that I already sent 0x00 data over a RS232 sendchar macro with success.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello,

Ok there must be some other reason the send string is not working. I will look into this when I get a min.

Yes the send byte function will pass 0's correctly.

I have modified your Receiver program slightly to allow it to detect when valid data is available and you shouldn't see the 255 in the first byte any more.
Reciever SPI.fcf
(8.5 KiB) Downloaded 238 times
If you need to be able to pass the value 255 then you will need to modify the RS232 component properties in both programs to change the receive type to Int and then use multiple readbyte macros in your receive program instead of one readstring macro. Then use a INT type variable to get the value from the readbyte macro and check if it is less then 256. If it is then you know that you have valid data ranging from 0 to 255. I have done this mod to the program below for you to see, to get it to work you must change the receive type in the component properties of your transmitter program.
Reciever SPI 255 Allowed.fcf
(8.5 KiB) Downloaded 274 times

amishra123
Posts: 14
Joined: Fri Mar 25, 2011 4:16 pm
Has thanked: 2 times

Re: Serial

Post by amishra123 »

Hi!New problem!, basically we are now sending charecters because we couldn't send the string over Mewi, it just wouldn't work!

The program sends 0x0A,0,0,0,0,0,0,0,0 . So a 0x0A then 8 characters the first is the first sensor the second the second sensor and so on!

I can't figure out how to put each of the characters into its own variable.

The referene point would be the 0x0A , so how to get it to see a 0x0A then know the next character is the first analogue signal then the second chracter the second signal?!!

This i think is the last thing then it should all work!!
Thank you so much!

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Serial

Post by Benj »

Hello,

I have created a small example program that checks for the first character of 0x0A. Once this has been received an array of 8 bytes is filled with the 8 sensor readings.
Collect 8 values.fcf
(5.5 KiB) Downloaded 327 times
Hope this helps.

Post Reply