Moving SPI to alternate port

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
User avatar
petesmart
Valued Contributor
Valued Contributor
Posts: 395
Joined: Thu May 06, 2010 11:42 am
Location: Sydney, Australia
Has thanked: 187 times
Been thanked: 140 times

Moving SPI to alternate port

Post by petesmart »

Hi Ben,
to make it easier thought I would start up a specific topic around the question that I posted (and you kindly responded to) last night. Quote below:

Re: SPI Master
by Benj » Tue Mar 01, 2011 11:25 am

Hello Pete,

The Flowcode I2C component allows you to use it in hardware or software mode. The software mode does not require a MSSP peripheral so you can basically move the component anywhere you like on the chip.

SPI can also be bit banged quite easily but the SPI legacy component does not currently support this mode of operation.Regards
Ben Rowland
Matrix Multimedia
Benj
Matrix Staff

Posts: 3923
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix Multimedia Ltd
Has thanked: 14 times
Have thanks: 38 times
Website
I think the best approach would be to move the CAN module from Port C to an alternate port. Is this simply a matter of of editing the can module in the custom code facility and exchanging Port C for example to Port B, or is there a quick way to config.

As I have not had any experience with bit banging...can you point me to info pls...

thanks for your quick response.

All the best

Pete
sorry about that Chief!

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: Moving SPI to alternate port

Post by Benj »

Hello Pete,

Ok to modify the CAN component should be fairly straightforward. There are two sections of code you will need to modify. The same should go for just about any SPI component so this technique could be used for other components too.

The first function we are going to edit is:
SPI_Init

All we need to do here is to initialise the port direction registers and the port registers, all other code in this function can be removed as we are no longer using hardware SPI.

Code: Select all

	//initialise spi
	set_bit(trisb, 0);   //SDI pin
	clear_bit(trisb,1);  //SDO Pin
	clear_bit(trisb,2);   //SCK Pin
	clear_bit(trisb,3);  //CS Pin
	set_bit(trisb,4);  //INT pin

	//disable the CAN chip select line
	set_bit(portb, 3);
The other function you will need to edit is the SPI_SendByte function. Here is a quick example of code for a bit banged SPI transaction.

Code: Select all

		char idx;
		for (idx = 0; idx < 8; idx++)
		{
			if (test_bit(byte, 7))
				set_bit(portb, 1);		// Set SDO bit
			else
				clear_bit(portb, 1);	// Clear SDO bit

			byte = byte << 1;

			set_bit(portb, 2);			// SCK Output 1

			delay_us(1);	//Added BR 06/10

			if(test_bit(portb, 0))
				byte = byte | 0x01;
			else
				byte = byte & 0xFE;

			clear_bit(portb, 2);		// SCK Output 0
		}
		return byte;

User avatar
petesmart
Valued Contributor
Valued Contributor
Posts: 395
Joined: Thu May 06, 2010 11:42 am
Location: Sydney, Australia
Has thanked: 187 times
Been thanked: 140 times

Re: Moving SPI to alternate port

Post by petesmart »

Thanks Ben much appreciated... will give it a go...

do i have to do something similar with SDI?

best

Pete
sorry about that Chief!

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: Moving SPI to alternate port

Post by Benj »

Hi Pete,

SDI is covered with this section of code. Sorry missed out a comment.

Code: Select all

         if(test_bit(portb, 0))   //Test SDI Input
            byte = byte | 0x01;
         else
            byte = byte & 0xFE;

User avatar
petesmart
Valued Contributor
Valued Contributor
Posts: 395
Joined: Thu May 06, 2010 11:42 am
Location: Sydney, Australia
Has thanked: 187 times
Been thanked: 140 times

Re: Moving SPI to alternate port

Post by petesmart »

no problem... many thanks for the quick response

I should have read it more carefully for shouting for help... sorry to waste your time
sorry about that Chief!

Post Reply