# [PIC]Using Hardware and Software SPIs wtih 18F8722

Status
Not open for further replies.

#### ycnr

##### Newbie level 5
I use 18F8722 in a project. This device has two hardware SPIs and I have run to read two encoders. I need one more SPI channel. May i use Hardware and sofware SPIs together. I tried but i couldn't succeed. What are your opinions and advices? Thanks in advance.

ycnr

#### bigdogguru

I use 18F8722 in a project. This device has two hardware SPIs and I have run to read two encoders. I need one more SPI channel. May i use Hardware and sofware SPIs together. I tried but i couldn't succeed. What are your opinions and advices? Thanks in advance.

Why is there a need to utilize more than one SPI module? A single SPI interface can read/write numerous devices by controlling which particular Slave Select (SS/CS) line is currently active. All you need is an additional Slave Select line for each attached device.

BigDog

#### ycnr

##### Newbie level 5
Hello bigdogguru,

I should read two encoders at same time and they should move to forward or backward synchronously. On the other hand i want to read one more encoder, not same time with others. So i need three separeted spi lines. As i said 18f8722 has only two HW SPIs. I have already designed schematic and pcb. I don't want to start with different MCU. Also if i start with different MUC that has 3 or more SPIs, it means dsPIC. But i didn't study and work dsPICs before. If i can use hardware and software SPI together, it will be more easy for me.

Best regards

#### bigdogguru

I should read two encoders at same time and they should move to forward or backward synchronously.

Can you supply a datasheet for these encoders?

What type of encoders are they?

What is the resolution or granularity of the encoders?

What SPI clock rates do the encoders support? And what is the current SPI clock rate?

What exactly are the encoders measuring?

What is the effective RPM range of rotational/angular movement?

The phrase "at the same time" is relative, in relation to reading a mechanical device, like an encoder, the time interval between reading the value of one encoder followed immediate by reading the value of the other encoder is typically insignificant, in the tens of microseconds range for higher SPI clock rates. Typically, changes of state at the mechanical side occur a considerable slower rates than that of the time interval between sequential reads of two encoders.

BigDog

#### KlausST

##### Super Moderator
Staff member
Hi,

How can two software SPI work at the same time? And maybe a third one..

I know, (especially with the use of assembly), it could be possible, but is it really an improvement over a fast hardware solution?

Klaus

bigdogguru

### bigdogguru

Points: 2

#### bigdogguru

As Klaus suggested, there is inherent latency when utilizing two hardware SPI modules, each module must be sequential triggered to perform the read task, therefore even with the application of two hardware SPI modules, the encoder values are not retrieved simultaneously.

Another option, likely more prudent and practical, is to attach the third encoder to one of the existing hardware SPI modules. First the values of the two primary encoders are retrieve, then the value of the third encoder is retrieved, upon which the cycle begins again. Implementing this method, provides a more deterministic approach, not relying on the processor to implement and perform the SPI communications protocol and thereby freeing it up to execute calculations on the retrieved values, off loading the SPI communications to the hardware SPI modules.

Certainly, implementation of a soft SPI is possible, however in terms practicality, feasibility and functionality in comparison to the above outlined method, doubtful.

BigDog

#### doraemon

##### Super Moderator
Staff member
Hello!

I should read two encoders at same time and they should move to forward or backward synchronously.

How can two software SPI work at the same time? And maybe a third one..

I know, (especially with the use of assembly), it could be possible, but is it
really an improvement over a fast hardware solution?

I would be curious of an implementation that reads 2 devices at the same time
using assembly. Basically assembly or C does not change anything. There is
absolutely nothing that you can do in assembly and that you can't in C,
except maybe accessing a specific processor register (although there might
be C tricks I don't know).

The only solution to use 2 SPIs at the same time (i.e. clocks and data are
synchronized) would be a hardware implementation. Maybe it would be possible
on some processors with large instructions (I'm thinking about one processor
I used in the past, called "equator" with VLIW, which stands for very large
instruction word). One instruction word was divided in more instructions.
In such an implementation, you could imagine that you have 2 synchronous
move instructions that both set one byte in each SPI engine.

Otherwise, using 2 SPIs at the same time in small processors is simply not
possible, assembly or not.

As BigDog said, it really depends on what you call "read 2 encoders at the same
time". Here is a blind calculation (blind because I don't know what you want to do).

Suppose your SPI can run at 10 MHz clock (which is the case of many encoders I know).
If you read a 40 bit data frame (which is also a common case), then you will need
4 µs. Reading 2 encoders will therefore give you values that are 4 µs apart.
Now let's suppose that you are reading an encoder spinning at 6000 rpm, which
is a very common speed for motors. What would be the angular error?
6000 rpm is 100 turns per second, which is 36000 degrees per second.
Therefore, within 4 µs, your motor moves by 36000 x 4 / 1000000 = 0.144 degree.
This is quite acceptable in many cases.

Dora.

AhmedGalal

### AhmedGalal

Points: 2

#### KlausST

##### Super Moderator
Staff member
Hi,

Code:
I would be curious of an implementation that reads 2 devices at the same time
using assembly. Basically assembly or C does not change anything. There is
absolutely nothing that you can do in assembly and that you can't in C,
except maybe accessing a specific processor register (although there might
be C tricks I don't know).

I thought of two SPI interfaces at the same port. Then instead of a bit manipulation you could use a byte write for two spi_clock generation and two spi_mosi generation at the same time, also you could do the read of two spi_miso at the same time. After that a lot of bit manipulations are needed to bring the read in data in a useful format. This part of code i'd write in assembler.

Although possible, I'd not recommend such programming.

Klaus

#### ycnr

##### Newbie level 5
Can you supply a datasheet for these encoders?

What type of encoders are they?

What is the resolution or granularity of the encoders?

What SPI clock rates do the encoders support? And what is the current SPI clock rate?

What exactly are the encoders measuring?

What is the effective RPM range of rotational/angular movement?

The phrase "at the same time" is relative, in relation to reading a mechanical device, like an encoder, the time interval between reading the value of one encoder followed immediate by reading the value of the other encoder is typically insignificant, in the tens of microseconds range for higher SPI clock rates. Typically, changes of state at the mechanical side occur a considerable slower rates than that of the time interval between sequential reads of two encoders.

BigDog

Hi,

How can two software SPI work at the same time? And maybe a third one..

I know, (especially with the use of assembly), it could be possible, but is it really an improvement over a fast hardware solution?

Klaus

As Klaus suggested, there is inherent latency when utilizing two hardware SPI modules, each module must be sequential triggered to perform the read task, therefore even with the application of two hardware SPI modules, the encoder values are not retrieved simultaneously.

Another option, likely more prudent and practical, is to attach the third encoder to one of the existing hardware SPI modules. First the values of the two primary encoders are retrieve, then the value of the third encoder is retrieved, upon which the cycle begins again. Implementing this method, provides a more deterministic approach, not relying on the processor to implement and perform the SPI communications protocol and thereby freeing it up to execute calculations on the retrieved values, off loading the SPI communications to the hardware SPI modules.

Certainly, implementation of a soft SPI is possible, however in terms practicality, feasibility and functionality in comparison to the above outlined method, doubtful.

BigDog

Hello!

I would be curious of an implementation that reads 2 devices at the same time
using assembly. Basically assembly or C does not change anything. There is
absolutely nothing that you can do in assembly and that you can't in C,
except maybe accessing a specific processor register (although there might
be C tricks I don't know).

The only solution to use 2 SPIs at the same time (i.e. clocks and data are
synchronized) would be a hardware implementation. Maybe it would be possible
on some processors with large instructions (I'm thinking about one processor
I used in the past, called "equator" with VLIW, which stands for very large
instruction word). One instruction word was divided in more instructions.
In such an implementation, you could imagine that you have 2 synchronous
move instructions that both set one byte in each SPI engine.

Otherwise, using 2 SPIs at the same time in small processors is simply not
possible, assembly or not.

As BigDog said, it really depends on what you call "read 2 encoders at the same
time". Here is a blind calculation (blind because I don't know what you want to do).

Suppose your SPI can run at 10 MHz clock (which is the case of many encoders I know).
If you read a 40 bit data frame (which is also a common case), then you will need
4 µs. Reading 2 encoders will therefore give you values that are 4 µs apart.
Now let's suppose that you are reading an encoder spinning at 6000 rpm, which
is a very common speed for motors. What would be the angular error?
6000 rpm is 100 turns per second, which is 36000 degrees per second.
Therefore, within 4 µs, your motor moves by 36000 x 4 / 1000000 = 0.144 degree.
This is quite acceptable in many cases.

Dora.

Hi,

Code:
I would be curious of an implementation that reads 2 devices at the same time
using assembly. Basically assembly or C does not change anything. There is
absolutely nothing that you can do in assembly and that you can't in C,
except maybe accessing a specific processor register (although there might
be C tricks I don't know).

I thought of two SPI interfaces at the same port. Then instead of a bit manipulation you could use a byte write for two spi_clock generation and two spi_mosi generation at the same time, also you could do the read of two spi_miso at the same time. After that a lot of bit manipulations are needed to bring the read in data in a useful format. This part of code i'd write in assembler.

Although possible, I'd not recommend such programming.

Klaus

Firstly thank you so much for your kindly replies.

My two encoders are linear type magnetic and incremental encoders.
Resolution = 5μm
6 outputs but i use A, B channels and Z reference
Traverse speed (max) = 2m/s
Travel Length = 25 cm

Other one is Incremental Rotary encoder.
Maximum rpm = 6.000 [rpm]
Max. output frequency = 200 [kHz]
Housing Diameter = ø58 [mm]
Hollow shaft = ø10 [mm]

Of course i can't read at same time two encoders. First of all i should say about my project. I use to read each encoder as Slave by 18F2431 with QEI module. As i said before I use 18F8722 as Master. They communicate by SPI. I read two of them in a loop sequentially.

However i start to study two linear encoders and i added rotary encoder. Unfourtunately 8722 has two SPIs. Then i search a solution my issue.

I undertand from your what you said that I can connect two linear encoders to one SPI module and read by selecting SS pins. Therefore other SPI can use to read Rotary Encoder.

Have i understood correctly?

Best regards.

#### KlausST

##### Super Moderator
Staff member
Hi,

I undertand from your what you said that I can connect two linear encoders to one SPI module and read by selecting SS pins. Therefore other SPI can use to read Rotary Encoder.

Have i understood correctly?
Yes. This is the way to go.

Maybe a HC138 can help yo to decode the SS signals.

Klaus

#### doraemon

##### Super Moderator
Staff member
Hello!

Resolution = 5μm
6 outputs but i use A, B channels and Z reference
Traverse speed (max) = 2m/s
Travel Length = 25 cm

So you will output AB edges at 400 kHz at max speed, and the encoder is incremental.

Other one is Incremental Rotary encoder.
Maximum rpm = 6.000 [rpm]
Max. output frequency = 200 [kHz]
Housing Diameter = ø58 [mm]
Hollow shaft = ø10 [mm]

So you have a resolution of about 1/2000 turn, which is 0.18 degree resolution.
Is this one also incremental?

Something I don't get: what are you using SPI for? To configure encoders? Usually you don't have
to read an incremental encoder to get the pulses. The pulses are automatically output, so I guess
you only access configuration registers, right?
Or do your encoders have an internal counter that can be read by SPI? This would be pseudo-absolute
encoders.

Dora.

Status
Not open for further replies.