SPI COMUNICATION

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

SPI COMUNICATION

Post by ALAN_26 »

Hi Steve & Ian

First of all I wish to congratulate for the great work you have done on V3.

I need some help on V2.2

I have been trying this last week, to communicate between two PIC16f72 using the serial peripheral interface (SPI) but without any success.

For now what I want to do is just send an SPI char from a 16f72 (Master), and receive it on the other 16f72 (slave) and than display the value received on an LCD display. I have connected, Master SDI to slave SDO, Master SDO to slave SDI, master SCK to slave SCK.
I also connected slave select (SS) to GND of the 16f72 slave. I also tried with two pic16f877A @ 4Mhz and 20Mhz with NO success.


It is possible that I am doing something wrong in the flowchart, I tried to look in the tutorials but I did not find nothing.

Will it be possible to develop a new tutorial on this component ?

Thanks for your help.

Regards

Alan

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hello Alan,

Thnaks for your kind words about v3 - it's been a long time coming, but we're happy with the results.

I think the SPI component only works as a master, so it will probably not work as planned.

However, you may be able to "fudge" it. The SPI_Init macro essentially sets the mode of the SPI comms. For your "slave" chip, you could replace the "init" maco with your own C code icon that sets the SPI up to be a slave. The PICmicro datasheet should help with this.

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hay Steve

Thanks a lot for your promt reply

My problem is that I do not know C language that why Ichoose flowcode :)

Can you please guide me to do this task.

Thanks once agian

Regards

Alan

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:

Post by Benj »

Hello Alan

Im quite new to Matrix Multimedia but I think i can point you in the right direction with the SPI. If you create a new file in flowcode and then open up a SPI module and click compile to assembler you will get a .c file with the same name as your flowcode file. If you then open the .c file using notepad or a similar text editor (context is free and highlights c code) you will be able to see the SPI functions and it should make the job of tailoring them to your needs much easier.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

To add to Ben's reply - it is probably just the "init" function you need to replicate within your C code icon. The init function currently contains code similar to the following:

Code: Select all

	//GetSPI_InitCode

	//SPI COMPONENT DEFINES
	#define SPI_DAC_EN 7
	#define SPI_FRM_EN 6

	#define SPI_SSPM 0
	#define SPI_CKP 0
	#define SPI_CKE 64
	#define SPI_SMP 0

	#define FRAM_WREN  6
	#define FRAM_WRDI  4
	#define FRAM_RDSR  5
	#define FRAM_WRSR  1
	#define FRAM_READ  3
	#define FRAM_WRITE 2

	#ifndef MX_SPI
	    asm error "Chip does not have SPI capability"
	#endif

	//initialise spi
	#ifdef MX_SPI_B
	    set_bit(trisb,MX_SPI_SDI);
	    clear_bit(trisb,MX_SPI_SDO);
	    clear_bit(trisb,MX_SPI_SCK);
	    clear_bit(trisb,SPI_DAC_EN);
	    clear_bit(trisb,SPI_FRM_EN);

	    //disable SPI devices
	    set_bit(portb, SPI_DAC_EN);
	    set_bit(portb, SPI_FRM_EN);
	#endif

	#ifdef MX_SPI_C
	    set_bit(trisc,MX_SPI_SDI);
	    clear_bit(trisc,MX_SPI_SDO);
	    clear_bit(trisc,MX_SPI_SCK);
	    clear_bit(trisc,SPI_DAC_EN);
	    clear_bit(trisc,SPI_FRM_EN);

	    //disable SPI devices
	    set_bit(portc, SPI_DAC_EN);
	    set_bit(portc, SPI_FRM_EN);
	#endif

	sspstat = SPI_CKE | SPI_SMP;
	sspcon = SPI_SSPM | SPI_CKP;
	set_bit(sspcon, SSPEN);
The last 3 lines are the important ones. SMP needs to be set to zero and SSPM needs to be 0x04 (slave mode).

To make things easy, I would suggest you call the normal SPI-Init function and follow it immediately with the following code:

Code: Select all

clear_bit(sspstat, SMP);
sspcon = (sspcon & 0xF0) | 0x04;
I hope this works...

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hi Steve

I tried doing the task you suggested ,call the normal SPI-Init function and follow it with the c code you provided .

I can’t get it to work . I am using Proteus ISIS 6 professional, to simulate the task .I also tried it on a breadboard with real components .It seems that the process hangs as it arrives to the get SPI char .When I simulate the receiver with flowcode on the lcd I get a value of 255 which is OK ( ASCII 255 = noting received ) , when I tried the real thing I did not receive any data or values (blank lcd ) . the lcd is working ok i print a test at the start of the program .

Any help will be greatly appreciated


Thanks in advance

Regards

Alan .

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi alan,

I have no experience with using SPI in slave mode, so hopefully others may be able to offer help on this. Also, it could be a hardware problem rather that a software one, which makes it doubly tricky to solve.

One quick thing to try is to turn the SPI unit off before your C code using this:

Code: Select all

clear_bit(sspcon, SSPEN);
and then turn it on again after your code using this:

Code: Select all

set_bit(sspcon, SSPEN);
Apart from this suggestion, I'm not sure how to help further.

User avatar
goldwingers
Posts: 118
Joined: Wed Sep 06, 2006 1:22 pm
Location: London
Been thanked: 1 time
Contact:

Hmmm

Post by goldwingers »

Well i cant write asm, or C, if V3 is still not available cant we have a macro or function that can be inserted into a flow chart for both the serial TX AND RX , and Ir modules, seems a bit half done and left to me.......bit like buying a bike with no seat.

Come on MM YOUR PRODUCTS ARE GREAT BUT were not all whiz kids out here

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

goldwingers:

I feel your last comment is a bit unfair, at least in the context of this thread.

The SPI component was only ever designed to work in a "master" mode, and this is the first ever request we have had to allow it to work in "slave" mode. I have tried my best to offer a workaround to this.

If I get some free time at home, I will try to look into this problem further by mocking up some hardware and writing some programs - but this goes beyond my technical support remit and is something I cannot do within work time.

User avatar
goldwingers
Posts: 118
Joined: Wed Sep 06, 2006 1:22 pm
Location: London
Been thanked: 1 time
Contact:

Sorry Steve

Post by goldwingers »

Sorry Steve,
Now that i have read it again, it does sound a bit Offish,Once again Sorry, However, I have been browsing through this forum and really do notice that its very ASM orientated, and not really "Flowcode" if you get my drift,

Some of us Oldies dont get all this ASM and C lingo nor do we have the time to learn it, hence purchasing Flowcode...I have just upgraded from Ver 1, yes, Ver 1 and eagerly await my V3 copy. I think its great I can mess around with my bits and always look forward to new bits to add.

So I shall say sorry once more and good work.

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hi Steve

Thank a lot for your help I appreciated a lot your concern about this issue !

I think This SPI task is getting a bit complicated and is causing you trouble , I don’t want that your free time at home be spend trying to find solution for my problem, so maybe its better leave the SPI component as Master only for now.
According to the pdf Flowcode V3 will support I2c (Inter-Integrated Circuit ) will it be possible to use this bus to communicate between two microcontrollers that dose not support RS232 interface like 16f72 ?

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:

Post by Benj »

Alan

Communication between microcontrollers via I2C is definatly possible and hopefully will be part of an upgrade for flowcode 3. However at the moment it is possible to communicate between two pics using one digital i/o pin and a bit bashing method. This is made simpler if the two microcontrollers are operating at the same crystal frequency and a start and stop bit protocal is used.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi goldwingers,

Your apology is graciously accepted, but I can see where you're coming from.

Flowcode is a tool that is primarily used by novice programmers with little or no experience of C or assembly - both as a teaching tool for learning C and as a tool for creating programs in its own right.

But at the other end of the scale, more experienced users use it as a "RAD" tool (Rapid Application Development) to get projects up to speed quickly, or as a proof-of-concept pre-design tool.

Apart from simplicity of use and short learning curve, one of Flowcode's biggest strengths is the ability to extend its functionality by embedding your own C and assembly code into it.

And because of this, you will often find tips and workarounds that involve adding C code to Flowcode to make it do something it doesn't do at the moment. These can often be added as-is, but sometimes they need to be tinkered with to get them to work.

I hope that explains a bit of where we're coming from too. And I'm sure you'll love v3 of Flowcode - it's such a big improvement over v2 (and indeed v1!)

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Hi Alan,

We're still working on the I2C component and we've not totally decided its capabilities - hopefully it will allow both Master and Slave modes of operation, but at the moment I do not want to commit...

A better approach would be to use the UART (i.e. the RS232 component within Flowcode) if it's available - this will allow PIC-to-PIC comms as it is. Obviously this is not possible for you as you intend to use the 16f72.

But don't give up on the SPI - I've asked Ben to have a look into it and see if he can solve it.

One thing I will add with all of this is that I wouldn't recommend connecting microcontroller pins directly - always connect them via a smallish resistor (100 - 220 ohms) as this will eliminate the possibility of accidentally damaging the i/o lines.

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:

Post by Benj »

Ok guys

I have designed two working programs for flowcode to solve this problem.

The first sends out ASCII characters over the SPI bus in Master mode. The program is pretty self explanitory

The second uses the SPI in slave mode and passes the ASCII to the LCD.

Here's the code to initialise the SPI into slave mode:

//SPI Init Slave Mode
set_bit(trisc,MX_SPI_SDI); //Serial Data In
clear_bit(trisc,MX_SPI_SDO); //Serial Data Out
set_bit(trisc,MX_SPI_SCK); //Serial Clock In
#define SPI_CKE 64 //Default SPI constants
#define SPI_SMP 0
#define SPI_SSPM 5
#define SPI_CKP 0
sspstat = SPI_CKE | SPI_SMP; //Configure SPI
sspcon = SPI_SSPM | SPI_CKP;
set_bit(sspcon, SSPEN); //Enable SPI

Here's to code to check whether or not an SPI byte has been received:

//If Data Received
if (test_bit(sspstat,BF)) FCV_BF = 1; //Recieved byte
else FCV_BF = 0; //Nothing Received

now you can have a flowcode decision depending on variable bf

Here's the code to read the received byte into a Flowcode variable:

//Read SPI Buffer
FCV_CHAR_IN = sspbuf;


The hardware is connected as follows
Master SDO to Slave SDI
Master SDI to Slave SDO
and both SCK are connected together
Each of the lines has a serial 150R resistor to avoid damaging the PIC devices.

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

At last , Thanks a lot Ben !

Nice work ! I will try this tomorrow .

Once again I would like to tank you and all mm stuff for your 5 Star Deluxe help :wink:

Mark
Posts: 209
Joined: Thu Oct 19, 2006 11:46 am
Location: Bakewell, UK
Has thanked: 20 times
Been thanked: 16 times
Contact:

SPI

Post by Mark »

Benj,

Looks good, any chance of putting this code into the SPI component , such as for activation using a check box in the component? When you have done the 1001 other things on the to do list that is...
Go with the Flow.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

That would make a good addition to the existing SPI component. I'll put it on the list, but it's too late to make the initial release of Flowcode v3.

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hi Benj

Can you please send me both fcf files ( master AND slave ) cause I still can’t get it to work .

If it is possible I will be needing to transmit from the slave side too in other to have a bidirectional bus .

Thanks in advance

Alan .

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

HI EVERY BODY

CHECK THIS OUT THIS IS REALY INTERESTING AND NOT FOR SPI ONLY !!!!

http://www.microchip.com/stellent/idcpl ... e=en020194

Mikat
Posts: 258
Joined: Wed Nov 29, 2006 6:32 pm
Location: Finland
Has thanked: 7 times
Been thanked: 36 times
Contact:

Post by Mikat »

Thanks to your code Ben,it seems working fine,i can transfer byte from master to slave..
Can you help me to transfer multiple bytes,and sent some data back to master?

ALAN_26
Posts: 84
Joined: Sat Apr 29, 2006 3:36 pm
Location: MALTA
Contact:

Post by ALAN_26 »

Hi Steve or Ben

Is it possible to update the SPI component to function as Master OR Slave ?

Maybe in the next update


Thanks

Regards , Alan cilia.

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

it's on the list...

User avatar
FCCHow
Posts: 27
Joined: Fri Jul 28, 2006 10:40 am
Contact:

Post by FCCHow »

Hello. There is one question I would like to know, which is, can the PIC16F628A be used for SPI? I have seen a circuit which uses PIC16F84 for SPI, though.

This is because I am going to try to use it to control the ISD4002 sound chip which uses SPI.

Is there any other tutorials for the Flowcode3? Thanks

User avatar
Steve
Matrix Staff
Posts: 3424
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Post by Steve »

Neither the 628a nor the 84a have inbuild SPI comms. You can write your own bit-bang SPI functions, but this would require a fair bit of effort. It is best to look for a chip with native SPI capability (e.g. 16F88 or 16F877a).

Post Reply