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/PROTEUS Problem

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



Joined: 12 Jan 2015
Posts: 5

View user's profile Send private message

SPI/PROTEUS Problem
PostPosted: Mon Jan 12, 2015 2:36 pm     Reply with quote

Hello

I need your help. I have a problem with SPI communication. I would like to make a hardware SPI communication between two PIC18F27J53.

Could you please tell me where is the problem?

Thank you

MASTER:
Code:

#include <18F27J53.h>
//#fuses INTRC_PLL_IO, PLL2, PLLEN, NOCPUDIV, NOWDT

#use delay(internal=48MHZ)
 
#use spi(FORCE_SW ,  stream=PORT ,DI=PIN_B5, DO=PIN_C7, CLK=PIN_B4, BITS=8,MASTER,SPI1, MSB_FIRST)

void main(){
     
SETUP_ADC(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
setup_spi(spi_master | spi_l_to_h |SPI_XMIT_L_TO_H   |SPI_SAMPLE_AT_END);
     
OUTPUT_LOW(PIN_A5);   
int dato;

while(1){
     
   //  spi_write(0x04);
   //  delay_ms(1000);
           
   // dato=spi_read(0x00);
   dato=SPI_XFER(PORT, 0x00);
     
   delay_ms(1000);
  }

}

SLAVE:
Code:

#include <18F27J53.h>
//#fuses INTRC_PLL_IO, PLL2, PLLEN, NOCPUDIV, NOWDT

#use delay(internal=48MHZ)
 #use spi(FORCE_HW , stream=PORT ,DI=PIN_B5, DO=PIN_C7, CLK=PIN_B4,BITS=8,SLAVE,SPI1, MSB_FIRST)

void main(){
       
SETUP_ADC(ADC_OFF);
setup_adc_ports(NO_ANALOGS);
setup_spi(spi_slave | spi_l_to_h |SPI_XMIT_L_TO_H   |SPI_SAMPLE_AT_END);

while (1){ 
   
// spi_write(0x0E);
SPI_XFER(PORT, 0x0F);
delay_ms(1000);
// SPI_XFER(PORT, 0x05);
   
  }

}
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Mon Jan 12, 2015 2:48 pm     Reply with quote

As a rule we don't do Proteus here. Proteus is not a good peripheral emulator.
You might find someone willing to help but we generally don't do Proteus.
_________________
Google and Forum Search are some of your best tools!!!!
Ttelmah



Joined: 11 Mar 2010
Posts: 19520

View user's profile Send private message

PostPosted: Mon Jan 12, 2015 3:02 pm     Reply with quote

As comments though, setup_spi, and #use spi, are _two _mutually exclusive_ ways of configuring the SPI. The 'old' way, was 'setup_spi', and spi_read/spi_write (which can only use the hardware). The 'new' way is #use spi, and spi_xfer. You _must not_ mix these. If you look at the manual, the #use spi entry _only_ refers you to spi_xfer, and setup_spi refers you to spi_read/spi_write.

Then learn to use the code buttons...
+++++++++++++
Fixed.
- Forum Moderator
+++++++++++++

Then realise that since the slave will wait for the transfer, you should always have delays in the slave loop, faster than those in the master.

Then since you have got rid of the NOWDT fuses, the watchdog will be enabled (it defaults to on).

Then you are telling the setup in your master to both use software, and use the hardware SPI1.....
billy6



Joined: 12 Jan 2015
Posts: 5

View user's profile Send private message

PostPosted: Mon Jan 12, 2015 3:57 pm     Reply with quote

Thank you very much Ttelmah

I have made all the changes but it doesn´t work yet...
I show you the updated codes:


MASTER
Code:

#include <18F27J53.h>
 #fuses NOWDT 

#use delay(internal=48MHZ)
 
#use spi(FORCE_HW ,  stream=PORT ,DI=PIN_B5, DO=PIN_C7, CLK=PIN_B4, BITS=8,MASTER,SPI1, MSB_FIRST)
void main(){
     
      SETUP_ADC(ADC_OFF);
      setup_adc_ports(NO_ANALOGS);
     //  setup_spi(spi_master | spi_l_to_h |SPI_XMIT_L_TO_H   |SPI_SAMPLE_AT_END);//| spi_clk_div_16
     
      OUTPUT_LOW(PIN_A5);   
     int dato;
     

    while(1){
 
       dato=SPI_XFER(PORT, 0x00);
     
      delay_ms(2000);
       
     }
}





SLAVE

Code:
#include <18F27J53.h>

 #fuses NOWDT 

#use delay(internal=48MHZ)
 #use spi(FORCE_HW , stream=PORT ,DI=PIN_B5, DO=PIN_C7, CLK=PIN_B4,BITS=8,SLAVE,SPI1, MSB_FIRST)//,MODE=3

void main(){
       
       SETUP_ADC(ADC_OFF);
       setup_adc_ports(NO_ANALOGS);
   

   while (1){ 
   
 
   SPI_XFER(PORT, 0x0F);
   delay_ms(500);
 
   
   }

}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Wed Jan 14, 2015 7:17 am     Reply with quote

One of the reasons you get little response is because you are using Proteus. Proteus is known to have many problems and we don't want to spent time on helping you to work around Proteus.

I don't think I can help you solve your problem, but here are some suggestions:
- Add more #fuses settings. Now the oscillator fuses are missing and I'm sure real hardware won't be running at 48MHz then. Most likely Proteus will work, but that's exactly one of the reasons why we don't like Proteus.
- Why using the PIC18F27J53? It is a very powerful chip, but that makes it also more complex. For example, in this chip you have to configure which devices are mapped to what I/O-pin. This configuration is missing in your program. Since you are not using real hardware I suggest you choose a more basic chip like the PIC18F458 to get things started and then later on you can upgrade if you want to.
- Add a mode to the #spi setup. Most likely both programs will default to the same mode setting, but why take the risk? Especially because you have configured many other options that are of less importance.
- You don't have a slave select line. Without this line it is very difficult to synchronize the two SPI devices.
- For debugging purposes, add one or more LED outputs that will blink at certain program stages so you can see the program is actually running. You wouldn't be the first to debug a program on a processor that isn't running at all.

Final hint: search this forum. SPI communication has been discussed here many times before. The search function of this forum is weak, but use Google with the right keywords and you'll find lots of hits.
Code:
On Google use:
     site:ccsinfo.com/forum SPI two PIC
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