문제

I have two different devices that I want to connect with Arduino. An Ethernet shield and a shaft encoder. The first has SPI mode 0 and the second SPI mode 2. They conflict. Is there a solution for this problem?

I use different chip select pins and the two devices work perfectly alone.

What can I do to resolve this? Is there any tutorial that I miss?

도움이 되었습니까?

해결책

You can share the SPI as you like, between the two chip selects, each with its own mode. I do this. Simply write a help function to be used in place of the digitalwrite for each chip select that specifies the complete configuration of the SPI.

void cs1_low() {
    SPI.setDataMode(SPI_MODE0);
    SPI.setClockDivider(spiRate);
    digitalWrite(MP3_XCS, LOW);
}

void cs2_low() {
    SPI.setDataMode(SPI_MODE1);
    SPI.setClockDivider(spiRate/2)
    digitalWrite(MP3_XCS, LOW);
}

...

cs1_low();
foo = SPI.transfer(bar);
cs1_high(); //Deselect Control

...blah blah...

cs2_low();
foo = SPI.transfer(bar);
cs2_high(); //Deselect Control

In some cases, it you may need to account for settling. I use this between SD cards and other chips that are not the same.

It is best practice to not assume the SPI is in the mode you want, as different library or interrupt may have changed it. I have seen plenty of times where the mode is the same, but the speed between the SD card and others are different. Leading to "well it started, but I get junk later".

다른 팁

You must reconfigure the SPI module before initiating a new communication with any of the two devices. In case you have two SPI ports available, you could also use a different port for every device and configuring each one accordingly.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top