View previous topic :: View next topic |
Author |
Message |
brutus
Joined: 14 Apr 2004 Posts: 12
|
PIC18F2431 and SPI |
Posted: Wed Feb 08, 2012 8:57 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Wed Feb 08, 2012 1:48 pm |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Feb 08, 2012 3:03 pm |
|
|
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
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. |
|
![](templates/subSilver/images/spacer.gif) |
brutus
Joined: 14 Apr 2004 Posts: 12
|
|
Posted: Thu Feb 09, 2012 6:18 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
temtronic
Joined: 01 Jul 2010 Posts: 9170 Location: Greensville,Ontario
|
|
Posted: Thu Feb 09, 2012 6:26 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
brutus
Joined: 14 Apr 2004 Posts: 12
|
|
Posted: Thu Feb 09, 2012 7:27 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Feb 09, 2012 8:35 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
brutus
Joined: 14 Apr 2004 Posts: 12
|
|
Posted: Thu Feb 09, 2012 9:59 am |
|
|
Hi,
my compiler is 4.110.
Just to check, could you post the code you've used for the SPI?
Thanks,
Claudio |
|
![](templates/subSilver/images/spacer.gif) |
Ttelmah
Joined: 11 Mar 2010 Posts: 19359
|
|
Posted: Thu Feb 09, 2012 11:01 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Feb 09, 2012 12:52 pm |
|
|
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:
|
|
![](templates/subSilver/images/spacer.gif) |
brutus
Joined: 14 Apr 2004 Posts: 12
|
|
Posted: Sat Feb 11, 2012 9:58 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
|