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)
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)