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] Can't sent data from slave to master

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



Joined: 19 Mar 2016
Posts: 2

View user's profile Send private message

[SPI] Can't sent data from slave to master
PostPosted: Sat Mar 19, 2016 6:31 am     Reply with quote

Quote:
I want to test SPI communication using 2 pics 16f887, I want to send a byte to SLAVE, then send back a byte from slave to master.
- The data from master is successfully generated out (I'm not sure the slave received successfully or not)
- But the data from slave I tried to send back is not generated.
=> I attached the image of protus and the waveform on oscillator for your convenience of checking

Please help me by give me your idea and correct my program.
Thank you all in advance!

[img] http://postimg.org/image/ygeugn4vd/ [/img]
Quote:
MASTER code

Code:

#include <16f887.h>
#include <stdio.h> 
#fuses INTRC_IO, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=TERMINAL)


#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_0_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_1_0  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3

//#define CS       PIN_C2
//#define DI       PIN_C4
//#define DOUT   PIN_C5
//#define CLK    PIN_C3


void main ()
{
int8 data;
setup_spi(SPI_MASTER | SPI_MODE_0_0 | SPI_CLK_DIV_64);
set_tris_c(0b00010000);
output_bit(pin_c2,1); //pin c2 is the master pin connect to slave SS
data = 0xaa;
while (1){
        output_bit(pin_c2,0);
      //   delay_us(10);
        spi_write(data);
     //   delay_us(80);       
         output_bit(pin_c2,1);
         delay_us(80); //give time for slave interrupt, usually >60
         while(!spi_data_is_in());
         output_bit(pin_c2,0);
         spi_read(0);
      output_bit(pin_c2,1);
      delay_ms(100);
}
}
   

Quote:
Slave code

Code:

#include <16f887.h>
#include <stdio.h> 
#fuses INTRC_IO, NOWDT, PUT ,BROWNOUT, NOLVP
#use delay(clock=8000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7,stream=TERMINAL)

#define SPI_MODE_0_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)    //SPI Mode 0
#define SPI_MODE_0_1  (SPI_L_TO_H)                      //SPI Mode 1
#define SPI_MODE_1_0  (SPI_H_TO_L)                      //SPI Mode 2
#define SPI_MODE_1_1  (SPI_H_TO_L | SPI_XMIT_L_TO_H)    //SPI Mode 3

int data;
#int_ssp
void spi(void)
{
   disable_interrupts(INT_SSP);
   data = spi_read(0);//Vua nhan vua truyen.
    //  while(!spi_data_is_in())
   spi_write(data);
   enable_interrupts(INT_SSP);
}

void main ()
{
//SETUP_ADC_PORTS(NO_ANALOGS);
setup_spi(SPI_SLAVE | SPI_MODE_0_0);
clear_interrupt(INT_SSP);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
output_high(pin_a5); //pin a5 is slave SS
while (1){

}
}
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Mar 19, 2016 7:01 am     Reply with quote

when you have tried this in HARDWARE - why not come again?

the proteus isis "schematic" is woefully INCOMPLETE and would not work if it WAS hardware. the code you posted is irrelevant ( and it is flawed anyway)
but you ought to read this first:

http://www.ccsinfo.com/forum/viewtopic.php?t=47549

for proteus - try another forum where people are more apt to help with homework.
chienthanbka



Joined: 19 Mar 2016
Posts: 2

View user's profile Send private message

PostPosted: Sat Mar 19, 2016 7:31 am     Reply with quote

Hi Asmboy,

First, thank you very much for your time.
Quote:

when you have tried this in HARDWARE - why not come again?

I did not process on HARDWARE yet, just a simulation on PROTUS, did you see any problem in my code ? if not, then I will start checking protus issue or apply on hardware.
Quote:

the proteus isis "schematic" is woefully INCOMPLETE and would not work if it WAS hardware

Would you please point out my woeful points ? sorry, i'm just a beginner, it will be very help for me later times.
Quote:

the code you posted is irrelevant ( and it is flawed anyway)

I'm not so clear your idea, what is the irrelevance? what is your mean with "flawed anyway" ?
Thanks
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Mar 19, 2016 8:23 am     Reply with quote

1- your schematic has no power connections -woefully incomplete - no decoupling caps
2-proteus simulation is WORTHLESS and no serious programmer uses it for anything.
3-#use rs232 is missing ERRORS

4-set_tris is only for use with #use fastio

5- slave code has useless enable/disable commands -

6- SPI setup looks defective for both
7- the "oscilloscope" picture isn't worth the pixels it takes to show it.

you can't tell what is wrong when using proteus - that is the big problem... over and out
Mike Walne



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

View user's profile Send private message

PostPosted: Sat Mar 19, 2016 9:13 am     Reply with quote

What you're being told is, if you ever get you're code to work in Proteus/ISIS, it's highly unlikely to work on real hardware.
There will be loads of faults (both hardware and software) and you'll have to start debugging from scratch all over again.
You're therefore better off starting with real hardware from the outset.

None of the regulars here will bother wasting their time on Proteus/ISIS.
They are usually more than willing to help you from the beginning.
We were all novices once and appreciated guidance from mentors.

Mike
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Sat Mar 19, 2016 10:27 am     Reply with quote

what Mike is saying is this:
Take the trouble to "Build IT! And we will come "
Very Happy Very Happy Very Happy
Ttelmah



Joined: 11 Mar 2010
Posts: 19553

View user's profile Send private message

PostPosted: Sat Mar 19, 2016 3:01 pm     Reply with quote

As comments though, SS, is an input on the slave. It is defined in the hardware. You should not be outputting on it in the slave. The master _software_ pin, needs to connect to the slave _hardware_ pin. Then there needs to be time between sending a byte, and trying to read back. Understand INT_SSP occurs when the byte _finishes_ receipt. The master can't clock it back until after the slave has loaded it, so there has to be perhaps 20uSec delay after sending before trying to receive.

The point about Proteus, is you can get the whole project 'working' on this, and then when you switch to hardware find it doesn't work. Conversely you can have 'real' hardware projects that refuse to work in Proteus. So time trying to develop code in Proteus is basically wasted.

We see so many people not realising this, that the first line on seeing the word 'Proteus', is 'go away'.....
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