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

PIC18F2431 and SPI

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



Joined: 14 Apr 2004
Posts: 12

View user's profile Send private message

PIC18F2431 and SPI
PostPosted: Wed Feb 08, 2012 8:57 am     Reply with quote

Hi all,
my SPI stuff on PIC18F2431 is a real headache!
I need send some from the master via SPI and receive them with a slave (same micro).

I've tried so many times to initialize the master, but nothing is working! I can't see any signal on any pin ( SCK - MOSI), only the CS pin works.

Here is my code: (very simple)

Code:

#include <18f2431.h>
#fuses HS,NOWDT,NOBROWNOUT,NOPUT,NOPROTECT
#use delay(clock=10M)
//#use delay(clock=40M, oscillator=10M)// PLL
#use fast_io(B)
#use fast_io(A)
#use fast_io(C)

void main()
{
set_tris_A(0b11111001);
set_tris_B(0xC0); //11000000
set_tris_C(0b00010000);//SPI master

setup_spi(spi_master | spi_l_to_h | spi_clk_div_64 );
While(1)
    {
      delay_ms(1000);
      output_low(PIN_C6);
      delay_ms(1000);
      spi_write(90);
      output_high(PIN_C6); 
   }
}


I've also tried to change 2 micro but...nothing changes.

Any suggestion? Without this step I can't go on with the slave part....

Thanks,
Claudio
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 1:48 pm     Reply with quote

I suggest looking in the examples folder and open up a few of the examples that have SPI code inside them to see how CCS handles it.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Feb 08, 2012 3:03 pm     Reply with quote

This example uses software SPI for the master and
hardware SPI for the slave (all in the same PIC):
http://www.ccsinfo.com/forum/viewtopic.php?t=26888

Here is an example of hardware SPI for master and slave (using 2 PICs
on separate boards):
http://www.ccsinfo.com/forum/viewtopic.php?t=39145

If you need more help, use the forum's search page to search for
Quote:
SPI slave

Set it to "Search for All Terms".
Read all the threads that look good, and you will probably find the answer
to any SPI slave question that you have.
brutus



Joined: 14 Apr 2004
Posts: 12

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 6:18 am     Reply with quote

Thanks both for the suggestions;
the fact is not "how" to use SPI, I've read a lot of documentation ( from microchip and CCS ) and I'm quite sure that the code is working...also because it's so simple and i'ts been used form another guy

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

The problem is: could be the micro? Do I need a particular configuration?

I really don't know...something from the pins should go out!

C
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 6:26 am     Reply with quote

hmmm...

I don't have that PIC / compiler so....

You could cut code for a simple LED blinking program and see if the PIC is alive and well,toggling all pins.esp. the SPI ones.

You might try removing the set_tris() and fast_IO functions...

Also check to see what other onboard peripherals might be enabled in the default configuration.

All of these tests would narrow down the problem.
brutus



Joined: 14 Apr 2004
Posts: 12

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 7:27 am     Reply with quote

Hi temtronic,
I've just done a lot of tests; the led is blinking, other functions seem ok, and I've tested many different configurations...

I'm sure that the problem is somewhere else, but in this moment I can't find it.

Thanks again for the suggestions..
Claudio
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 8:35 am     Reply with quote

Question - what compiler version?.
Have used the SPI on the 2431, without problems. Yours, may be a 'version specific' problem with the compiler.

Best Wishes
brutus



Joined: 14 Apr 2004
Posts: 12

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 9:59 am     Reply with quote

Hi,
my compiler is 4.110.

Just to check, could you post the code you've used for the SPI?

Thanks,
Claudio
Ttelmah



Joined: 11 Mar 2010
Posts: 19496

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 11:01 am     Reply with quote

Unfortunately, the SPI is fairly 'scattered' through a largish project, so not easy to pick out. 'm actually running as switchable master/slave. Things different that I notice:
1) Running much faster. clock/4, not 64.
2) I try to avoid using fast_io mode on pins for the hardware peripherals. Makes future portability much easier to other chips. So using standard_io mode on the SPI pins.
3) Have a bodge in place, where I switch to slave operation, for the erratum regarding this - you have to verify the incoming clock line is being held to the IDLE state for the mode you select, before switching to slave mode. Obviously should not apply to a master only device.

Using SPI Mode2.

However none of these would seem to affect your behaviour, so the next thing is to compiler your code with 4.110, and look at the assembly to see if it does correctly configure everything - it looks as if it should.....

Best Wishes
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Feb 09, 2012 12:52 pm     Reply with quote

Quote:
I can't see any signal on any pin ( SCK - MOSI), only the CS pin works.

1. Tell us the pins that you're looking at. Is it pin C3 or C5, or some other pin ?

2. Tell us how you are looking at the signal on that pin. Is it with an
oscilloscope, logic analyzer, or is this a Proteus project ?

3. Your program is very poor for looking at signals with an oscilloscope.
You have a huge 2 second delay and then a very short burst of pulses,
repeatedly. You probably won't see it on the average scope. You won't
be able to trigger on it. It would work much better if you changed both
delays to 100 usec:
Code:

delay_us(100);
brutus



Joined: 14 Apr 2004
Posts: 12

View user's profile Send private message

PostPosted: Sat Feb 11, 2012 9:58 am     Reply with quote

Hi,
1) according to the PIC18F2431 DS, the hw pins for SPI are

RC4 - SDI
RC5 - SCK
RC6 - SS
RC7 - SDO

So I expect to see some signals over C5 and C7 in MASTER configuration. Is it correct?

2) I've a last generation digital oscilloscope, and I'm quite sure to trigger the signal.

3) I'll try with a short delay.

Thanks,
C
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