|
|
View previous topic :: View next topic |
Author |
Message |
luqman82303
Joined: 17 Feb 2020 Posts: 7
|
spi communication between arduino and pic18f452 |
Posted: Tue Feb 25, 2020 5:54 am |
|
|
I have an issue in my code that it couldn't read the value from the slave (pic18f4520).
The master sends data is received on slave-like LEDs blink.
Please help me out how the data is sent from the PIC (slave0) to Arduino (master).
master:
Code: |
//SPI MASTER (ARDUINO)
//SPI COMMUNICATION BETWEEN TWO ARDUINO
//CIRCUIT DIGEST
#include<SPI.h> //Library for SPI
#define LED 7
#define ipbutton 2
int buttonvalue;
int x;
int val;
void setup (void)
{
Serial.begin(115200); //Starts Serial Communication at Baud Rate 115200
pinMode(ipbutton,INPUT); //Sets pin 2 as input
pinMode(LED,OUTPUT); //Sets pin 7 as Output
SPI.begin(); //Begins the SPI commnuication
SPI.setClockDivider(SPI_CLOCK_DIV2); //Sets clock for SPI communication at 8 (16/8=2Mhz)
digitalWrite(SS,HIGH); // Setting SlaveSelect as HIGH (So master doesnt connnect with slave)
}
void loop(void)
{
byte Mastersend,Mastereceive;
//buttonvalue = digitalRead(ipbutton); //Reads the status of the pin 2
//if(buttonvalue == HIGH) //Logic for Setting x value (To be sent to slave) depending upon input from pin 2
digitalWrite(SS, LOW);
//Starts communication with Slave connected to master
x=5;
Mastersend = x;
SPI.transfer(0);
digitalWrite(SS,HIGH);
delay(100);
digitalWrite(SS,LOW);
delay(100);
SPI.transfer(0);
digitalWrite(SS,HIGH);
delay(100);
SPI.transfer(1);
digitalWrite(SS,LOW);
delay(100);
SPI.transfer(1);
digitalWrite(SS,HIGH);
delay(100);
digitalWrite(SS,LOW);
delay(100);
//Mastereceive=SPI.transfer(x); //Send the mastersend value to slave also receives value from slave
Mastereceive=SPI.transfer(1); //Send the mastersend value to slave also receives value from slave
Mastereceive=SPI.transfer(0); //Send the mastersend value to slave also receives value from slave
digitalWrite(SS,HIGH);
// digitalWrite(SS,HIGH);
//delay(100);
Serial.println(Mastereceive);
//delay(100);
if(Mastereceive == 1) //Logic for setting the LED output depending upon value received from slave
{
digitalWrite(LED,HIGH); //Sets pin 7 HIGH
Serial.println("Master LED ON");
}
else
{
digitalWrite(LED,LOW);
Serial.println("Master LED OFF");
}
delay(1000);
}
|
In Arduino, I read that the same function is to transmit and receive data.
slave:
Code: |
//LCD Module Connections
#define LCD_RS_PIN PIN_D1
#define LCD_RW_PIN PIN_D2
#define LCD_ENABLE_PIN PIN_D3
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
// Software SPI pins used by Master.
#define MASTER_SS PIN_A5 // Connect to Slave \SS (Pin A5)
#define MASTER_SCK PIN_C3 // Connect to Slave SCK (Pin C3)
#define MASTER_SDI PIN_C5 // Connect to Slave SDO (Pin C5)
#define MASTER_SDO PIN_C4 // Connect to Slave SDI (Pin C4)
//End LCD module connections
// Use Charlie U's SPI mode definitions.
#define SPI_MODE_0_0 0x4000
#define SPI_MODE_0_1 0x0000
#define SPI_MODE_1_0 0x0010
#define SPI_MODE_1_1 0x4010
#include <18f452.h>
#use delay(clock=16000000)
#FUSES HS
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <lcd.c>
#byte SSPBUF = 0x00 // Register address for 16F877
//!#define BUFFER_SIZE 80
//!int8 buffer[BUFFER_SIZE];
//!int8 next_in = 0;
//!int8 next_out = 0;
int8 value;
#int_SSP
void SSP_isr()
{
value=spi_read();
if(value == 0)
{
output_high(PIN_B0);
}
if(value == 1)
{
output_high(PIN_A0);
}
delay_ms(100);
spi_write(1);
spi_write(2);
}
void main()
{
// Initialize the software Master SPI pins.
setup_spi(SPI_SLAVE | SPI_MODE_0_0 );
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
while(TRUE)
{
}
} |
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Tue Feb 25, 2020 6:59 am |
|
|
An SPI interrupt, means one byte has been received. Just one. No 'triple'
transfers in the interrupt.
When this byte is received, the hardware clocks back to the master the
byte that is in the SSPBUF register. So to give a reply, you need to
preload this register with the value that is to be received by the master
on it's 'next' transaction. Always needs to be loaded 'in advance' of
the transaction.
What you need to do is 'prewrite' the byte to the SPI buffer. This then
sits there and is what the master will receive when it sends it's first byte.
Look at 'spi_prewrite' in the CCS manual.
Then in the interrupt, receive one byte and prewrite the next reply byte.
Next interrupt receive one byte and prewrites the next reply etc. etc.
So you only do one read, and one prewrite in each interrupt call. |
|
|
luqman82303
Joined: 17 Feb 2020 Posts: 7
|
|
Posted: Tue Feb 25, 2020 9:33 am |
|
|
I am sorry !!
I am new on pic controller also on ccs compiler.
I try to grab your point and even check spi_prewrite(), but failed.
Can you please give me an example code lines to implement that thing
also, I change remove send extra byte.
Thanks |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19544
|
|
Posted: Tue Feb 25, 2020 1:29 pm |
|
|
Use the CCS manual. F1 when in the IDE. |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9244 Location: Greensville,Ontario
|
|
Posted: Tue Feb 25, 2020 1:33 pm |
|
|
and 'F11' if using MPLAB.... |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|