|
|
View previous topic :: View next topic |
Author |
Message |
Nora
Joined: 23 Mar 2008 Posts: 50
|
SPI problem (not even clock) with PIC18F452 |
Posted: Sat Feb 19, 2011 1:15 pm |
|
|
Hello-
I am trying to get SPI communications working with a PIC18F452. I do not even get clock. My oscilloscope is able to read 10MHz signal (on slave transceiver) but seems to stop the chip's operation (I have a blinking LED) when I put it to read on the PIC's 20 MHz external oscillator.
Therefore, I used the SPI_CLK_DIV_64, hoping that this means that the SPI clock signal is 20 MHZ / 64 = 312 KHz... so if I am understanding this correctly, I should be able to read the clock signal.
I am reading all over this forum that it is best not to use both setup_SPI and #use spi at the same time. True?
I'm using the pins that the PIC says to use for SPI : C3 = SCK, CC4 = SDI, C5 = SDO
When I uncomment the #use spi, the word "DO" goes navy blue.
???
SPI_Write() can only put out 8 bits? What if I need to send 16 bits all at once? Should I use the SPI_XMIT function? Does it send LSB or MSB first?
I guess the biggest problem is no clock signal. So I can't even get to the rest to troubleshoot. Hoping there is just some simple setting that I got wrong. I did not do anything with the SPI pins (ie: no extra hardware).
Here's the code. All I am trying to do at this point is get a clock output.
Thanks in advance,
Nora
Code: |
#include <18F452.H>
#fuses HS,NOWDT,NOPROTECT,BROWNOUT
#use delay(clock=20000000) // one instruction=0.2us
#include <stdlib.h>
//#include <math.h>
//#use spi(Di=PIN_C4, Do=PIN_C5, CLK=PIN_C3)
//BITS = 16, BAUD = 9600,
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//***********************************************************
void main()
{
//set_tris_c(0x00);
// sets transmitter up as master (master generates clock), low to high clock transmission
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
while(1){
//spi_write(0b1000000010110000); //
//spi_write(0x38);
//delay_ms(100);
output_low(PIN_A5);
delay_ms(1000);
output_high(Pin_A5);
delay_ms(1000);
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat Feb 19, 2011 1:25 pm |
|
|
Run a simple test program. Then look at Pin C3 for the SPI clock.
Example:
Code: |
#include <18F452.h>
#fuses HS, NOWDT, PUT, BROWNOUT, NOLVP
#use delay(clock = 20000000)
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//========================================
void main()
{
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_64);
while(1)
{
spi_write(0x55);
delay_us(100);
}
} |
Don't run it in Debug mode. Don't attempt to step through the code.
Compile and run it in Release mode. You should see the SPI clock. |
|
|
Nora
Joined: 23 Mar 2008 Posts: 50
|
|
Posted: Sat Feb 19, 2011 1:33 pm |
|
|
I DO see the clock.
Thanks! |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19550
|
|
Posted: Sat Feb 19, 2011 4:17 pm |
|
|
And, on sending 16bits, just send two lots of 8bits. The _hardware_ only sends 8bits at a time, and if you load a new byte immediately, the transaction is effectively 'non stop' (which shouldn't matter anyway with SPI - this is why it sends a clock...).
Best Wishes |
|
|
|
|
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
|