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

SPI on 18lf46j11

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



Joined: 12 Oct 2010
Posts: 1

View user's profile Send private message

SPI on 18lf46j11
PostPosted: Tue Oct 12, 2010 11:27 am     Reply with quote

Help me please, I have some big trouble using the SPI...

I have a system composed by 1 Master and 2 Slave (all 18lf46j11), a transmission protocol composed of 2 byte (address + value) and separate SlaveSelect.

There is 2 problems:
1) every 2 transmission of the master the slave will freeze until the next transmission (ex: M_tx_indirizzo, M_tx_dato , Slave_works, M_tx_indirizzo, M_tx_dato , Slave_FREEZE, M_tx_indirizzo, M_tx_dato , Slave_works, ecc...)Confused

2) I can't read the data from SLAVE1 Sad

MASTER:
Code:
#use delay(clock=8000000)
#define SPI_CLK   PIN_C3
#define SPI_DI   PIN_C4
#define SPI_DO   PIN_C5
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B6,bits=8)
...
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
...
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
...

  output_high(PIN_B0);// SS for slave0
  output_high(PIN_B1);// SS for slave1
   
LOOP:
   printf("START (Y,N)? ");
   do {
      answer=getch();
      }
      while (answer!='Y'&&answer!='N');
   if (risposta=='Y'){
            printf("\r\nVado.... \r\n");
            //
            output_low(PIN_B0);
            delay_ms(10);
            spi_write(0);
            spi_write(vel);
            output_high(PIN_B0);
            printf("SPEED=%u\r\n",vel);
            vel=vel+20;

            printf("READ");
            output_low(PIN_B1);
            delay_ms(10);
            spi_write(4);
            temp = spi_read(0);
               
            output_high(PIN_B1);           
            printf("= %u \r\n",temp);
           
            }
      else{
            printf("\r\nSTOP.... \r\n");
            output_low(PIN_B0);
            spi_write(0x00);
            spi_write(100);
            output_high(PIN_B0);
            vel=100;
      };

goto LOOP;


SLAVE0: (write only)
Code:
#use delay(clock=8000000)
#define SPI_SS   PIN_A5
#define SPI_CLK   PIN_C3
#define SPI_DI   PIN_C4
#define SPI_DO   PIN_C5
...
#int_SSP
void  SSP_isr(void)
{
   disable_interrupts(INT_TIMER0);

   //receive address
      while(!spi_data_is_in());
      indirizzo = spi_read();
   //write data in position
      while(!spi_data_is_in());
      dato = spi_read(0);
      if(indirizzo >= 0 && indirizzo <= 7)
      {
      array_valori[indirizzo] = dato;
      }

   //comandi da eseguire una volta ricevuto il dato

   //riabiltare Interrupt
   enable_interrupts(INT_TIMER0);

}
...
setup_spi(SPI_SLAVE|SPI_L_TO_H);
....
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
...

   LOOP:
   goto LOOP;


SLAVE1: (Read only)
Code:
#use delay(clock=8000000)
#define SPI_CLK   PIN_C3
#define SPI_DI   PIN_C4
#define SPI_DO   PIN_C5
#define SPI_SS   PIN_A5
...
#int_SSP
void  SSP_isr(void)
{

   //disabiltare Interrupt
   disable_interrupts(INT_TIMER0);
   
   //receive address
         while(!spi_data_is_in());
         indirizzo = spi_read();
   //isend data on SPI: array_valori[indirizzo]
         if(indirizzo >= 0 && indirizzo <= 7)
            {
               while(!spi_data_is_in());
               temp = spi_read(array_valori[indirizzo]);
            }
            else
            {
               while(!spi_data_is_in());
               temp = spi_read(0);
            }//se viene passato un indirizzo errato restituisce 0
           
   //comandi da eseguire a fine trasmissione
   enable_interrupts(INT_TIMER0);

   
   if(flag2==1){output_high(PIN_B6);}else{output_low(PIN_B6);};//test
   flag2 = !flag2;
}
...
setup_spi(SPI_SLAVE|SPI_L_TO_H);
...
setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_31250|OSC_PLL_OFF);
...   
   //test
   array_valori[0] = 10;
   array_valori[1] = 20;
   array_valori[2] = 30;
   array_valori[3] = 40;
     
 LOOP:
 goto LOOP;
Sad Sad
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 12, 2010 1:59 pm     Reply with quote

Here are two threads with example code for an SPI slave using #int_ssp
interrupts.
http://www.ccsinfo.com/forum/viewtopic.php?t=39145
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
Ttelmah



Joined: 11 Mar 2010
Posts: 19383

View user's profile Send private message

PostPosted: Tue Oct 12, 2010 2:19 pm     Reply with quote

Also, some comments.
You can only have _one_ oscillator selection in the setup line. Having both 31KHz, and 8MHz, is not legitimate....
Don't bother to disable interrupts in the interrupt handler. Unless you are using high priority interrupts, all other interrupts are automatically disabled by the hardware.

Best Wishes
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