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

dsPIC30F4012 and SPI

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



Joined: 23 Dec 2010
Posts: 8

View user's profile Send private message

dsPIC30F4012 and SPI
PostPosted: Sat Dec 25, 2010 10:42 am     Reply with quote

Hi CCS programmer,

I have one master using PIC18F4580 and 3 slaves using dsPIC30F4012. below is my code and it is NOT working!!

apparently, if i use PIC18F13K22 as my slaves, it works just fine. some of the forumer said that, it is due to the bugs. my compiler version is 4.114. any hint @ solution?

MASTER:
Code:
#include <18F4580.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=38400, xmit=PIN_C6, rcv=PIN_C7)

int8 instr;

void main() {
   output_bit( PIN_E0, 1);
   output_bit( PIN_E1, 1);
   output_bit( PIN_E2, 1);
   
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H);

   while(true)
   {

////////////////interlock switch///////////////////////
//Simulate swicth pressing
   while (input(PIN_B0));     //while HIGH stay here.
   while (!input(PIN_B0));     //while LOW stay here.
///////////////////////////////////////////////////////


   
/////////////////////////////////////////////////// Slave 1   
      output_bit( PIN_E0, 0);
      spi_write(0x01);                    //send dummy ack to slave
      output_bit( PIN_E0, 1);
     
      delay_us(25);  // Give slave some time to respond (minimum 25us)
     
      while(!spi_data_is_in());
     
      output_bit( PIN_E0, 0);
      instr = spi_read(0);
      output_bit( PIN_E0, 1);
     
      putc(instr);
     
//////////////////////////////////////////// Slave 2
      output_bit( PIN_E1, 0);
      spi_write(0x01);                    //send dummy ack to slave
      output_bit( PIN_E1, 1);
     
      delay_us(25);  // Give slave some time to respond (minimum 25us)
     
      while(!spi_data_is_in());
     
      output_bit( PIN_E1, 0);
      instr = spi_read(0);
      output_bit( PIN_E1, 1);
     
      putc(instr);
     
////////////////////////////////////////////// Slave 3
      output_bit( PIN_E2, 0);
      spi_write(0x01);                    //send dummy ack to slave
      output_bit( PIN_E2, 1);
     
      delay_us(25);  // Give slave some time to respond (minimum 25us)
     
      while(!spi_data_is_in());
     
      output_bit( PIN_E2, 0);
      instr = spi_read(0);
      output_bit( PIN_E2, 1);
     
      putc(instr);
     
//////////////////////////////////////////////// END

   }

}


SLAVE:
Code:
#include <30F4012.h>
#fuses FRC_PLL4, NOWDT, NOPROTECT
#device ADC=8
#use delay(clock=7.37M, internal)
#byte SPIBUF1 = 0x0224                        // Register address for 30F4012

unsigned int8 adc_result, command;

void main(void)
{   
   setup_adc( ADC_CLOCK_INTERNAL );
   setup_adc_ports (sAN0);
   set_adc_channel( 0 );
   delay_us(100);

   setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_XMIT_L_TO_H);

   while(true)
   {
      while(!spi_data_is_in());
   
      command = SPIBUF1;                         //read dummy send by master

      adc_result = Read_ADC();
   
      SPIBUF1 = adc_result;                      //pass adc result to spi buffer   }
   
   }
}
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sat Dec 25, 2010 2:49 pm     Reply with quote

There are no obvious coding errors at first sight. Using spi_data_is_in() on the master side seems useless, but hopefully causes no problems.

I'm using SPI with PIC24, but not in slave mode. Thus I'm not aware of particular pitfalls or bugs. Generally, there's no alternative to debugging the design step-by-step. If you can be sure that the master side is working correctly, check the slave. Will the receive trigger ever be asserted? What's received? Is the slave able to transmit arbitrary constant data (there may be a problem with ADC as well)?

You'll get almost any information of interest with MPLAB and a debugging adapter, in a few cases an oscilloscope would help to verify the actual SPI waveforms.
nia



Joined: 23 Dec 2010
Posts: 8

View user's profile Send private message

PostPosted: Sun Dec 26, 2010 12:02 am     Reply with quote

When I change to PIC18F, it works just fine. With the same code and slight changes on the fuses, its simply not working with dsPIC.

Help needed please.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Sun Dec 26, 2010 2:29 am     Reply with quote

Quote:
With the same code and slight changes on the fuses, its simply not working with dsPIC.

Yes, I understood this from your previous post. But I don't have the dsPIC30 chip and can't check your code. You should try to find out, which step fails.

P.S.: Additionally, the ADC doesn't work correctly with the present zero TAD setting.
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