CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

SPI Problem

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
pcm_shahid



Joined: 11 Oct 2018
Posts: 2

View user's profile Send private message

SPI Problem
PostPosted: Tue Feb 05, 2019 3:38 pm     Reply with quote

I send data to slave and slave is received perfect, but when slave is send
back same data master is not received data. Please help seniors.

Master Code................
Code:

#include <16F887.h>
#device ADC=10
#use delay(internal=4000000)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES WRT_1000                 //Program Memory Write Protected from 0 to 0x0FFF


 //#include <STRING.H>
// #include <STDLIB.H>


#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7,parity=N,bits=8 )
//#use rs232(baud=9600, xmit=PIN_a7, rcv=PIN_a6,parity=N,bits=8 )

#use     fast_io(a)
#use     fast_io(b)
#use     fast_io(c)
#use     fast_io(d)
#use     fast_io(e)

#byte    porta=0x05
#byte    portb=0x06
#byte    portc=0x07
#byte    portd=0x08

                               
#define LCD_RS_PIN      PIN_d0
#define LCD_RW_PIN      PIN_d1 
#define LCD_ENABLE_PIN  PIN_d2

//#define use_portd_lcd TRUE
#include <lcd.c>


#define cs pin_c2
#define up pin_A1
#define dn pin_A2

//#define SS              PIN_B3

#use spi(FORCE_SW, BITS=8, stream=SPI_STREAM)
#use spi(master,ENABLE=pin_c2,spi1,mode=0,bits=8,stream=SPI_Max31856)

#define spi_xfer_31856(x)   spi_xfer(SPI_Max31856, x,8)
 int8 address,data;
 int8 value_up=0x04,value_dn=0x09;
//===============================
void write_spi(int8 address)
{
 
  // address=spi_xfer_31856(address);
  // data=spi_xfer_31856(data);
  data=spi_xfer_31856(address);
  //spi_xfer_31856(data);
   delay_ms(2);
   printf("\faddress=%X   data=%X\r\n",address ,data );
}
//=============================
//==============================
void main()
{
   set_tris_a(0xff);  output_a(0x00);
   set_tris_b(0x02);  output_b(0x00);
   set_tris_c(0x10);  output_c(0x00);
   set_tris_d(0x00);  output_d(0x00);
   set_tris_e(0xff);  output_e(0x00);

 
   spi_xfer_31856(0x00);
 
   lcd_init();   
  printf(lcd_putc,"\fSPI TEST MASTER2 ");
 
   //enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
 
while (TRUE)

      {
       
       
         if(!input(up)){write_spi(0x01);}
         if(!input(dn)){write_spi(0x02);}
      }
}


SLAVE Code.................
Code:

#include <16F887.h>
#device ADC=10
#use delay(internal=4000000)

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES PROTECT                  //Code protected from reads
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV21                   //Brownout reset at 2.1V
#FUSES WRT_1000                 //Program Memory Write Protected from 0 to 0x0FFF

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
//#use rs232(baud=9600, xmit=PIN_c6, rcv=PIN_c7,parity=N,bits=8 )
//#use rs232(baud=9600, xmit=PIN_a7, rcv=PIN_a6,parity=N,bits=8 )

#use     fast_io(a)
#use     fast_io(b)
#use     fast_io(c)
#use     fast_io(d)
#use     fast_io(e)

#byte    porta=0x05
#byte    portb=0x06
#byte    portc=0x07
#byte    portd=0x08
                               
#define LCD_RS_PIN      PIN_B0
#define LCD_RW_PIN      PIN_B1 
#define LCD_ENABLE_PIN  PIN_B2

#define use_portb_lcd TRUE
#include <lcd.c>

#define cs pin_c2

#use spi(FORCE_SW, BITS=8, stream=SPI_STREAM)
#use spi(slave, spi1,mode=0,bits=8,stream=SPI_1)

#define spi_xfer_31856(x)   spi_xfer(SPI_1, x,8)

//===============================
byte read_spi(int8 data)
{
   int8 data_in;

   data_in=spi_xfer_31856(data_in);

   //if( data_in==0x09)spi_xfer_31856( 0x55);
   //if( data_in==0x04)spi_xfer_31856( 0x46);
   //spi_xfer_31856( data_in);
  /*
   SPI_XFER_9356(0x18);
   SPI_XFER_9356(address);
   data=SPI_XFER_9356(0);
   */
 

   return(data_in);
}


//==============================
void main()
{
   set_tris_a(0x7f);  output_a(0x00);
   set_tris_b(0x02);  output_b(0x00);
   set_tris_c(0x1c);  output_c(0x00);
   set_tris_d(0x00);  output_d(0x00);
   set_tris_e(0xff);  output_e(0x00);

       
   lcd_init();   
   printf(lcd_putc,"\fSPI TEST SLAVE2 ");
   int8 address,data;
   while (TRUE)
     {
      if(!input(cs)){
        address = read_spi(spi_data_is_in());
        data    = read_spi(spi_data_is_in());
       if(address==0x01 ){spi_xfer_31856( 0x04);output_high(pin_d0);output_low(pin_d1);}
       if(address==0x02 ){spi_xfer_31856( 0x09);output_high(pin_d1);output_low(pin_d0);}
       
       lcd_gotoxy(1,2);
       printf(lcd_putc,"adres=%X data=%x ",address,data);
      data=0;
     }
     
    }
}
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Feb 05, 2019 4:24 pm     Reply with quote

Simplify your code to make it easier to read.

Get rid of anything which is not used, remove all the commented out lines.
Provide comments on what is supposed to happen.
Don't make us work it out, we've all got better things to do.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Feb 06, 2019 12:35 am     Reply with quote

These lines:

address = read_spi(spi_data_is_in());

are complete nonsense.

You are trying to clock 'back' to the master, the TRUE/FALSE value
returned by the test 'spi_data_is_in'. Why?.

Then you read a value from the SPI, when you send an address.
You have to understand that SPI always responds one byte 'behind'.

THe sequence would need to be to send the adddress, then pause
(to allow time for the slave to load the reply), then clock out a 'dummy'
byte which returns the reply from the slave. The slave can only
respond.

So:

Master sends byte
Slave receives this and loads reply
Master sends another byte and received the byte from the slave.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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