Maker Pro
Maker Pro

Learning SPI: Arduino UNO as slave, PIC16F73 as master

Caboose

Aug 1, 2014
2
Joined
Aug 1, 2014
Messages
2
Hi! This is my first try on SPI communication. As title says, I have a PIC connected to my UNO. The idea is to send numbers from the PIC to the UNO via SPI and from there on to my PC via Serial.print . The PIC is powered from the UNO.

I can't get my codes to work however and have no idea what is wrong.

Code on PIC (master):
unsigned char i = 0;

void init(void)
{
ADCON0 = 0;
ADCON1 = 0b0000111;
TRISC.B3 = 0; // SCK
TRISA.B5 = 0; // _SS
TRISC.B5 = 0; // SDO
PORTA.B5 = 1;
PORTC.B3 = 0;
SPI1_Init();
}

void main() {
init();
while(1)
{
for(i=48; i<58; i++)
{
PORTA.B5 = 0;
Delay_ms(100);
SPI1_Write(i);
PORTA.B5 = 1;
Delay_ms(100);
}
}
}


Code on UNO (slave):
#include <SPI.h>

int data = 0;

void setup() {
Serial.begin(9600);
pinMode (SS, INPUT);
pinMode (SCK, INPUT);
pinMode (MOSI, INPUT);
pinMode (MISO, OUTPUT);
digitalWrite(SS, HIGH);
digitalWrite(SCK, LOW);
digitalWrite(MOSI, LOW);
digitalWrite(MISO, LOW);
SPCR &= ~(1<<MSTR);
SPCR = (1<<SPE);

}

void loop() {
Serial.print("SS is HIGH \n");
if (digitalRead(SS) == LOW)
{
Serial.print("SS is LOW \n");
data = SPI.transfer(0x00);
Serial.print(data);
delay(100);
}
delay(10);
}

The problem so far is that UNO's SS pin doesn't ever go LOW and even the string "SS is LOW \n" isn't printed on the monitor although the PIC (master) should constantly pull the SS low with the line PORTA.B5 = 0; .

connections: PIC <-> UNO
Vdd <-> 5v
Vss <-> GND
SS <-> pin 10 (SS)
SDO <-> pin 11 (MOSI)
SDI <-> pin 12 (MISO)
SCK <-> pin 13 (SCK)
 

shumifan50

Jan 16, 2014
579
Joined
Jan 16, 2014
Messages
579
Why do you want to go via the UNO, why not directly from the PIC to the PC?

Remember that each additional device is several additional points of failure.
 

Caboose

Aug 1, 2014
2
Joined
Aug 1, 2014
Messages
2
I'm trying to learn how to implement SPI. Hopefully I can get it to work and use a similar code in the future, when I am trying out something more complicated and need to use SPI.
I'm sorry to have posted such a big block of code to the forum. Didn't even find out how to format it in a good way.
 

BobH

Sep 10, 2014
1
Joined
Sep 10, 2014
Messages
1
Caboose, did you have any luck in getting this figured out? I'm at the same stage as you in learning the implementation of SPI and I'm trying to get a Uno (slave) and pic 16F877A (master) to communicate. Thanks!
 
Top