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 support@ccsinfo.com

From master to slave and back

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



Joined: 20 Apr 2005
Posts: 1

View user's profile Send private message

From master to slave and back
PostPosted: Mon Apr 25, 2005 6:18 am     Reply with quote

Hi everybody!I have a board with two 18F4525 and they are connected via 3 wires (Data,Clock and RB0).I want that one work like master and other like slave.After some independent operations I'd like that master became slave and slave master.It's possible?I've tried something like this but it doesn't work:

Code:
#include <18f4525.h>
#device adc=10
#fuses H4,NOLVP,NOWDT,PUT,NOBROWNOUT
#use delay(clock=40000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int get_com(int);
int get_frec(void);
void init_spi_M(void);
void init_spi_S(void);

//MASTER
int get_mpp(void);

void main(void)
{
    int indicator;
    int value;
    int mpp_new,mpp_old;
    int com1,com2,com3,f,D;

    printf("SPI test start(MASTER).\r\n");
    delay_ms(500);
while(TRUE){
    if (PIN_B0){
      init_spi_M();
      mpp_old=get_mpp();
      spi_write(indicator);
      spi_write(value);
      !(PIN_B0);
    }
    else{
      init_spi_S();
      spi_read(com1);
      spi_read(com2);
      spi_read(com3);
      spi_read(f);
      spi_read(D);
//    mpp_new=get_mpp();
      if (mpp_new>=mpp_old)
      PIN_B0;
    }
 }
}




//SLAVE
void main(void)
{
    int indicator;
    int value;
    int mpp_new,mpp_old;
    int fase1,fase2,fase3;
    int com1,com2,com3,f,D;

    printf("SPI test start(SLAVE).\r\n");
    delay_ms(500);
while(TRUE){
    if (!(PIN_B0)){
      init_spi_M();
      com1=get_com(fase1);
      com2=get_com(fase2);
      com3=get_com(fase3);
      f=get_frec();
      spi_write(com1);
      spi_write(com2);
      spi_write(com3);
      spi_write(f);
      (PIN_B0);
    }
    else{
      init_spi_S();
      spi_read(indicator);
      spi_read(value);
      spi_read(com3);
      if (D==1)
      !(PIN_B0);
    }
 }
}


void init_spi_M(void)
{
    setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_16);
    return;
}

void init_spi_S(void)
{
    setup_spi(SPI_SLAVE| SPI_L_TO_H);
    return;
}


Thank you.
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Mon Apr 25, 2005 2:18 pm     Reply with quote

Before I read your code too closely, have you tried using one a a slave and the other as a master and have it work? Then, did you switch and test again? Just to confirm they are both capable of being a master or a slave. Sanity checks always help before getting into the mess.
_________________
-Matt
Ttelmah
Guest







PostPosted: Mon Apr 25, 2005 2:36 pm     Reply with quote

First comment:
The statement:
if (PIN_B0){

Will allways evaluate as 'true'. PIN_B0, is a numeric value, which is used with the commands 'input', and 'output', to talk to the pins. You need to replace this with an if (input(PIN_B0)) statement to actually access the pin. The same is true where you try to access the pin by just putting a line 'PIN_B0'. Here you need 'output_bit(PIN_B0,1)' to set it , or use a '0' to clear it. To invert it, you need 'output_bit(PIN_B0,!input(PIN_B0))', or use the 'toggle' command (depending on the version of your compiler...).

What you describe, won't work. The problem is that the SPI itself uses three pins. there is CLK, and a data_in, and data_out line at each end. When operating to send data as a master, this chip would be using it's 'data_out' pin, while when expected to operate as a slave, it'd need to use it's 'data_in' pin. The same reversal wuld happen at the other end.

Consider instead using the I2C bus instead. The advantage here is that the bus only uses two pins for the transfer (being mono-directional, instead of transferring data simultaneaously in both directions), and these are 'open collector' drives, which removes a potential bus 'clash' if both chips try to be 'masters' at once. Program both chips as 'slaves', and only switch to being 'master' if you want to send something. Add a short delay after seeing the pin change, to verify that the bus is genuinely not in use. I have done this in the past with assembler, and it can be made to work OK, so should be possible.

Best Wishes
DragonPIC



Joined: 11 Nov 2003
Posts: 118

View user's profile Send private message

PostPosted: Wed Apr 27, 2005 8:07 am     Reply with quote

Ttelmah wrote:
What you describe, won't work. The problem is that the SPI itself uses three pins. there is CLK, and a data_in, and data_out line at each end. When operating to send data as a master, this chip would be using it's 'data_out' pin, while when expected to operate as a slave, it'd need to use it's 'data_in' pin. The same reversal wuld happen at the other end.

Best Wishes


This was the reason for my reply. Good programming technique is to start as small as possible first. Once that works, move on to the more complicated stuff. Verify that the bus is working in one configuration first before trying to switch master and slave. This helps you understand the real problem as well as making it easier for people in this forum to help you.
_________________
-Matt
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Wed Apr 27, 2005 10:46 am     Reply with quote

You could use I2C to do it.
PicNewboy



Joined: 20 Apr 2005
Posts: 1

View user's profile Send private message

Sorry everybody!
PostPosted: Thu Apr 28, 2005 2:46 am     Reply with quote

I didn't was clear in my question but I'm using 4 wires (1 clock,2 data and one between RB0 pins of pics).I have time limits so I have think that SPI were the best solution instead of I2C.Sorry again and thank you for the answers.
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