Hayee
Member level 4
Hi Guys
I am using two PIC18f46K22 microcontrollers with MPLAB XC8 IDE.
I write a code in which Master sends 1 byte and in return I am receiving 1 byte and display it on LCD. It is working fine. I am receiving from slave letter "H" and displaying it on LCD as the following image shows.
Now I want to receive two bytes from Slave by sending only 1 byte from Master. How can I do that.
I tried it but not successful. even i tried to receive the 2nd byte only but now able to do so.
Can you figure me out where I am making a mistake or how to do it?
I am unable to upload the code files so I am copy pasting here
Master.c
Master.h
Slave.c
Slave.h
I am using two PIC18f46K22 microcontrollers with MPLAB XC8 IDE.
I write a code in which Master sends 1 byte and in return I am receiving 1 byte and display it on LCD. It is working fine. I am receiving from slave letter "H" and displaying it on LCD as the following image shows.
Now I want to receive two bytes from Slave by sending only 1 byte from Master. How can I do that.
I tried it but not successful. even i tried to receive the 2nd byte only but now able to do so.
Can you figure me out where I am making a mistake or how to do it?
I am unable to upload the code files so I am copy pasting here
Master.c
Code:
#include <xc.h>
#include <string.h>
#include "SPI_Host_V2.h"
void OSCILLATOR_Init(){
/*========= 20MHz External Crystal Oscillator Configuration =========*/
OSCCON = 0x38;
OSCCON2 = 0x00;
OSCTUNE = 0x00;
}
/* Settings PIN's Direction for SPI-1 */
void PORT_Init(){
TRISC = 0b00010000; /* SDI as Input, SDO SCK SS1 as Output*/
ANSELC = 0x00; /* PORT C all Pins configured as Digital Pins*/
TRISD = 0b10000000; /* PORT D all Pins configured as Input Pins*/
ANSELD = 0x00; /* PORT D all Pins configured as Digital Pins */
TRISB = 0b00000000; /* PORT B all Pins configured as Output Pins*/
ANSELB = 0x00; /* PORT B all Pins configured as Digital Pins*/
}
// 'putch' function is required for the 'printf' function to work
// here it is used to print a character on the LCD using the function 'LCD_PutC'
void putch(char c)
{
LCD_PutC(c);
}
/* Set the desired setting for SPI-1*/
void SPI1_Init(uint8_t registerValue){
SSP1CON1 = registerValue;
}
/* Routine to send data */
void SPI1_SendData (uint8_t data){
SSP1BUF = data; // Sends the value of data
while(!PIR1bits.SSP1IF); // wait for the transmit/receive to be complete.
PIR1bits.SSP1IF = 0; // reset the SSP1IF register.
}
void Slave_HighLow(uint8_t state){
if(state == LOW) LATCbits.LATC2 = 0;
else LATCbits.LATC2 = 1;
}
void main(void) {
char RcvByte[2];
OSCILLATOR_Init();
PORT_Init();
SPI1_Init(WCOL_DIS|SSP1OV_DIS|SSP1EN_EN|CKP_LOW|SPI_HOST_FOSC_64);
LCD(&PORTD, 0,1,2,3,4,5); // set the connection between the MCU and the LCD
LCD_Begin(16,2); // initialize the LCD module with 16 rows and 2 columns (1602 LCD)
while(1)
{
Slave_HighLow(LOW);
SPI1_SendData(0);
while(!SSP1STATbits.BF);
SSP1STATbits.BF = 0;
RcvByte[0] = SSP1BUF;
// SPI1_SendData(1);
// while(!SSP1STATbits.BF);
// SSP1STATbits.BF = 0;
// RcvByte[1] = SSP1BUF;
Slave_HighLow(HIGH);
__delay_ms(1);
LCD_Goto(1,1);
printf("Hello");
LCD_Goto(1,2);
printf("%s", RcvByte);
}
return;
}
Master.h
Code:
#include <stdio.h>
#include "LCD.h"
#define LOW 0
#define HIGH 1
#define SLAVE_1 1
#define WCOL_EN (1 << 7)
#define WCOL_DIS (0 << 7)
#define SSP1OV_EN (1 << 6)
#define SSP1OV_DIS (0 << 6)
#define SSP1EN_EN (1 << 5)
#define SSP1EN_DIS (0 << 5)
#define CKP_HIGH (1 << 4)
#define CKP_LOW (0 << 4)
#define SPI_HOST_FOSC_64 2
#define SPI_CLIENT_SS_EN 4
#define ENABLE 1
#define DISABLE 0
uint32_t _XTAL_FREQ = 20000000;
Slave.c
Code:
#include "SPI_Slave1_V2.h"
void OSCILLATOR_Init(){
/*========= 20MHz External Crystal Oscillator Configuration =========*/
OSCCON = 0x38;
OSCCON2 = 0x00;
OSCTUNE = 0x00;
}
/* Set the desired setting for SPI-1*/
void SPI1_Init(uint8_t registerValue){
SSP1CON1 = registerValue;
}
/* Function for Enable or Disable the SPI Interrupt*/
void SPI1_INTERRUPT(uint8_t EnorDi){
if(EnorDi) PIE1bits.SSP1IE = 1;
else PIE1bits.SSP1IE = 0;
}
/* Function for Enable or Disable the Global Interrupt*/
void GLOBAL_INTERRUPT(uint8_t EnorDi){
if(EnorDi) INTCONbits.GIE = 1;
else INTCONbits.GIE = 0;
}
/* Function for Enable or Disable the Peripheral Interrupt*/
void PERIPHERAL_INTERRUPT(uint8_t EnorDi){
if(EnorDi) INTCONbits.PEIE = 1;
else INTCONbits.PEIE = 0;
}
/* Settings PIN's Direction for SPI-1 */
void PORT_Init(){
TRISC = 0b00011000; /* SDI SCK as Input, SDO as Output */
ANSELC = 0x00; /* All Pins Configured as Digital */
ANSELA = 0x00; /* SS Pin Configured as Digital*/
TRISD = 0b11111111; /* PORTD all pins configured as Output Pins*/
ANSELD = 0x00; /* PORTD all pins configured as Digital Pins*/
}
/* Routine to send data */
void SPI1_SendData (char data){
SSP1BUF = data; // Sends the value of data
while(!PIR1bits.SSP1IF); // wait for the transmit/receive to be complete.
PIR1bits.SSP1IF = 0; // reset the SSP1IF register.
}
/*SPI Interrupt Routine.*/
void __interrupt() SPI_ISR(){
if(PIR1bits.SSP1IF) // If SPI Interrupt Receive
{
if(SSP1STATbits.BF){
receivedByte = SSP1BUF; // store buffer value in a variable
if(receivedByte == 0) SPI1_SendData(message[0]); //Send Array position 0 value
if(receivedByte == 1) SPI1_SendData(message[1]); //Send Array position 1 value
}
PIR1bits.SSP1IF = 0; // Clear the Interrupt Flag.
}
}
void main(void) {
OSCILLATOR_Init();
PORT_Init();
SPI1_Init(WCOL_DIS|SSP1OV_DIS|SSP1EN_EN|CKP_LOW|SPI_CLIENT_SS_EN);
SPI1_INTERRUPT(ENABLE);
GLOBAL_INTERRUPT(ENABLE);
PERIPHERAL_INTERRUPT(ENABLE);
while(1){
}
return;
}
Slave.h
Code:
#include <xc.h>
#include <stdio.h>
#include <string.h>
#define _XTAL_FREQ 20000000
#define WCOL_EN (1 << 7)
#define WCOL_DIS (0 << 7)
#define SSP1OV_EN (1 << 6)
#define SSP1OV_DIS (0 << 6)
#define SSP1EN_EN (1 << 5)
#define SSP1EN_DIS (0 << 5)
#define CKP_HIGH (1 << 4)
#define CKP_LOW (0 << 4)
#define SPI_HOST_FOSC_64 2
#define SPI_CLIENT_SS_EN 4
#define ENABLE 1
#define DISABLE 0
uint8_t receivedByte;
char message[2] = "HI";
Last edited: