spi interfacing between two microcontroller in mikroC

3.6k views Asked by At

I'm doing spi interfacing between two pic18F452 microcontrollers in mikroC but my program doesn't give any output. I send a data from Master and slave have to display it on LCD.

Here is my code for both master and slave:

MASTER CODE:

    unsigned char key='a';

    void main(void)
    {

    TRISC.F2=0; //output for Slave select

    TRISC.F3=0; //SCK output

    TRISC.F4=1; //SDI input

    TRISC.F5=0; //SDO output

    Spi_Init_Advanced(Master_OSC_div64, Data_SAMPLE_MIDDLE, CLK_Idle_HIGH, HIGH_2_LOW);

    SSPCON1.SSPEN=1; //Synchronus serial port enable

    SSPSTAT.SMP=0;  //sampled at middle of data output time


    while(1)

   {

   PORTC.F2=0;

   SSPBUF=key;

   while(!SSPSTAT.bf);

   spi_write(key);

   }

   }

///////end//////

SLAVE CODE:

   #define LCD PORTB

   unsigned char key;

   void main(void)

   {

   TRISB=0; //LCD output

   TRISC.F4=1; //SDI input

   TRISC.F3=1; //SCK input from master

   TRISC.F5=0; //SDO output

   TRISA.F5=1; //Slave select input from master 

   SSPSTAT.SMP=0; //input data sampled at middle

   SSPSTAT.CKE=0; // transition from idle to active

   SSPCON1.=0x22;

   Spi_Init_Advanced(SLAVE_SS_ENABLE, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);

   Lcd_init(&PORTB);

   Lcd_cmd(Lcd_clear);

   Lcd_cmd(Lcd_cursor_off);

   while(1)
   {
   if(SSPSTAT.BF)
         {
            key=spi_read(SSPBUF);

            }

   }
   }

////////end///////

1

There are 1 answers

0
Rakesh Raveendran On

i am not sure about this part

Lcd_init(&PORTB);

Lcd_cmd(Lcd_clear);

Lcd_cmd(Lcd_cursor_off);

try declaring your lcd port like this . i use port d

 //lcd
sbit LCD_RS at RD0_bit;
sbit LCD_RW at RD1_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_RW_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;

and for initing and clearing

LCD_Init(); // Initialize LCD
DELAY_MS(100);
Lcd_Cmd(_LCD_CLEAR);                      // Clear display

that delay part might comes handy some times.