Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F452 SPI Slave mode

Status
Not open for further replies.

adwnis123

Full Member level 4
Joined
Jun 19, 2014
Messages
214
Helped
0
Reputation
0
Reaction score
1
Trophy points
18
Activity points
1,737
Hello, I have a PIC18F452 that is receiving data through SPI.The PIC is connected to an LCD16x2 where I want to display data. The data I send is "Hello, world!", however it does not show it correctly, it shows irrelevant characters! Can you help me ? Here is my code:
Code:
// LCD module connections
sbit LCD_RS at RB7_bit;
sbit LCD_EN at RB6_bit;
sbit LCD_D4 at RB5_bit;
sbit LCD_D5 at RB4_bit;
sbit LCD_D6 at RB3_bit;
sbit LCD_D7 at RB2_bit;
sbit LCD_RS_Direction at TRISB7_bit;
sbit LCD_EN_Direction at TRISB6_bit;
sbit LCD_D4_Direction at TRISB5_bit;
sbit LCD_D5_Direction at TRISB4_bit;
sbit LCD_D6_Direction at TRISB3_bit;
sbit LCD_D7_Direction at TRISB2_bit;
// End LCD module connections

void main() 
{
     char cmd,dummy;
     Lcd_Init(); // Initialize LCD
     Lcd_Cmd(_LCD_CLEAR); // Clear display
     Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off

    
     TRISA.F5 = 1;  // SS (INPUT)
     //PORTA.F5 = 0;

     TRISC.F3 = 1;   // SCLK (INPUT)
     //PORTC.F3 = 0;

     TRISC.F4 = 1;  // DATA IN (INPUT)
     //PORTC.F4 = 0;

     TRISC.F5 = 0;  // DATA OUT (OUTPUT)
     //PORTC.F5 = 0;
/*
bit 7 SMP: Sample bit
SPI Master mode:
1 = Input data sampled at end of data output time
0 = Input data sampled at middle of data output time
SPI Slave mode:
SMP must be cleared when SPI is used in Slave mode
*/
     SSPSTAT.SMP=0;
/*
bit 6 CKE: SPI Clock Edge Select
When CKP = 0:
1 = Data transmitted on rising edge of SCK
0 = Data transmitted on falling edge of SCK
When CKP = 1:
1 = Data transmitted on falling edge of SCK
0 = Data transmitted on rising edge of SCK
*/
     SSPSTAT.CKE=0;
/*
bit 7 WCOL: Write Collision Detect bit (Transmit mode only)
1 = The SSPBUF register is written while it is still transmitting the previous word
(must be cleared in software)
0 = No collision
*/
     SSPCON1.WCOL=0;    //bit 7
/*
bit 6 SSPOV: Receive Overflow Indicator bit
SPI Slave mode:
1 = A new byte is received while the SSPBUF register is still holding the previous data. 
In case
of overflow, the data in SSPSR is lost. Overflow can only occur in Slave mode.The user
must read the SSPBUF, even if only transmitting data, to avoid setting overflow
(must be cleared in software).
0 = No overflow
Note: In Master mode, the overflow bit is not set since each new reception (and
transmission) is initiated by writing to the SSPBUF register.
*/
//     SSPCON1.SSPOV=0;   //bit 6
/*
bit 5 SSPEN: Synchronous Serial Port Enable bit
1 = Enables serial port and configures SCK, SDO, SDI, and SS as serial port pins
0 = Disables serial port and configures these pins as I/O port pins
Note: When enabled, these pins must be properly configured as input or output.
*/
     SSPCON1.SSPEN=1;   //bit 5
/*
bit 4 CKP: Clock Polarity Select bit
1 = IDLE state for clock is a high level
0 = IDLE state for clock is a low level
*/
     SSPCON1.CKP=1;
/*
bit 3-0 SSPM3:SSPM0: Synchronous Serial Port Mode Select bits
0101 = SPI Slave mode, clock = SCK pin, SS pin control disabled, SS can be used as I/O pin
0100 = SPI Slave mode, clock = SCK pin, SS pin control enabled
0011 = SPI Master mode, clock = TMR2 output/2
0010 = SPI Master mode, clock = FOSC/64
0001 = SPI Master mode, clock = FOSC/16
0000 = SPI Master mode, clock = FOSC/4
*/
     SSPCON1.SSPM3=0;   //bit 3
     SSPCON1.SSPM2=1;   //bit 2
     SSPCON1.SSPM1=0;   //bit 1
     SSPCON1.SSPM0=0;   //bit 0

//     SPI1_Init_Advanced(_SPI_SLAVE_SS_ENABLE,_SPI_DATA_SAMPLE_MIDDLE,
//                        _SPI_CLK_IDLE_HIGH,_SPI_HIGH_2_LOW);

     Delay_ms(200);
     while(1)
     {
         while(SSPSTAT.BF==1)
         {
             cmd=SPI1_Read(SSPBUF);
             Lcd_Out(1,1,cmd);
             Delay_ms(1000);
             Lcd_Cmd(_LCD_CLEAR); // Clear display
         }
         SSPCON1.SSPOV=0;

     }
}
 

Are they on the same SPI port?

To talk to the LCD you need to be SPI Master. You may need to reconfigure your SPI port as a Master then back to slave after a read. However this may cause problems if the other SPI Master tries to write while you are in Master mode. My advice would be to use a separate SPI port for the LCD.
 

Hello, my aim is to correctly send data from the Master microcontroller to my PIC18F452(Slave), and I just use the LCD to see if that happens. I do not want to send primarily data to the LCD.But the problem is that the characters are irrelevant.

Does SPI needs to have common VCC,GND to both microcontrollers like I2C?

Do you see anything wrong to my code?

Does the cpu clock frequency between Master (4Mhz) and Slave(8Mhz) causes me the problems?
 

You need common ground for just about every type of communication. The reason is that you need a common reference, so the LCD can determine weather or not the clock and data lines are high or low. Without this reference the lines are floating from the LCD's perspective. So it is just guessing, which can lead to strange behavior. More than likely this is your problem.

The CPU frequency should not matter. The clock speed for SPI is determined by the Master. A faster device can always read a slower device, but not write to it. So you need to make sure that the master is not talking too fast. The max clock speed is determined by the slave. Assuming they are both using the same PLL setting (both on or both off), the master can talk as fast as it can since the slave can talk twice as fast. So this is not likely an issue for you.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top