View previous topic :: View next topic |
Author |
Message |
mkuang
Joined: 14 Dec 2007 Posts: 257
|
Really basic spi question |
Posted: Fri Dec 14, 2007 6:09 pm |
|
|
Hello all,
I am new to writing C code for the PIC so please bear with me. I am using the PCH compiler (purchased a few weeks ago) to program the PIC18f2525. I am really having trouble getting the SPI to work. Basically I am trying to drive a MAX5522 DAC. The problem I have is that I am NOT getting a SLK output or SDO output. THe SLK appears to be either HIGH or LOW depending on whether I set SPI_L_TO_H or SPI_H_TO_L. My test code looks like this:
#include <18F2525>
#use delay(clock=4000000) //I am using an external 4Mhz oscillator
#fuses NOWDT,XT
#define LED_PLUS PIN_A4
#define LED_MINUS PIN_A5
void main()
{
setup_spi(spi_master | SPI_L_TO_H |spi_clk_div_16 );
while (1)
{
output_high(LED_PLUS);
output_low(LED_MINUS);
spi_write(0b10101010)
delay_ms(1000);
output_low(LED_PLUS);
delay_ms(1000);
}
}
I put in the LED to see whether my code is compiling. Sure enough the LEDs flash every other second but there is NOTHING at all coming out of SCK or SDO when I look at them in the scope. If I use SPI_L_TO_H then the SCK is always zero volts and if I use SPI_H_TO_L the SCK is always at Vcc. What is going on? The LEDs are blinking so some part of my code is working. I am not even getting gibberish out of the SCK or SDO. Could something be wrong with the PIC or is there something obvious I am missing.
Thank you for your help.
MK |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 6:53 pm |
|
|
If you have anything connected to the PIC's SPI pins, then disconnect it.
Try the following test program. Look at the SCLK pin with an oscilloscope.
Code: |
#include <18F2525.h>
#fuses XT,NOWDT,NOPROTECT,BROWNOUT,PUT,NOLVP
#use delay(clock=4000000)
#define SPI_MODE_0_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_0_1 (SPI_L_TO_H)
#define SPI_MODE_1_0 (SPI_H_TO_L)
#define SPI_MODE_1_1 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
//===============================
main(void)
{
setup_spi(SPI_MASTER | SPI_MODE_0_1 | SPI_CLK_DIV_16);
while(1)
{
spi_write(0x55);
delay_us(100);
}
} |
|
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Fri Dec 14, 2007 7:28 pm |
|
|
I only have the DAC connected to SCK and SDO on the PIC. What do you think the problem could be? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri Dec 14, 2007 8:22 pm |
|
|
I made my suggestion. If you don't want to try it, then maybe someone
else can help. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Fri Dec 14, 2007 8:49 pm |
|
|
Hi,
Of course I'll try it. I have to get back to the office to to do that since I am at home right now. Thanks in advance. |
|
|
Guest
|
|
Posted: Sat Dec 15, 2007 5:11 am |
|
|
if u take SPI_CLK_DIV_4 instead of SPI_CLK_DIV_16 the convertion time is faster? because the convertion time = time of the clock for 16 bits + setting time? |
|
|
Ttelmah Guest
|
|
Posted: Sat Dec 15, 2007 7:49 am |
|
|
As a comment on the original code, the odds are, you won't see the clock as shown, with a scope. Remember the SPI, takes 16 cycles of the master clock (as shown - or 4 cycles if DIV_4 is selected), to send a bit, and just 8 repeats of this, to send the byte. As shown, the whole byte takes just:
(8*16)/4000000th second.
Just 32uSec.
Given this is happening just once every two seconds, you will only see it on the scope, if you put the scope into 'manual' trigger, (rather than 'auto', which will keep drawing the trace, when not triggered), an set the timebase up to something like 4uSec/division. You will get just one byte sequence flashing across the screen at 2 second intervals.
Much better to reduce the interval between bytes (PPM_programmer's code has a much 'better' interval), which then gives you a better chance to see the data.
If DIV_4 is selected for the clock, the whole byte will take just 8uSec.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Sat Dec 15, 2007 8:32 am |
|
|
Ttelmah wrote: | As a comment on the original code, the odds are, you won't see the clock as shown, with a scope. Remember the SPI, takes 16 cycles of the master clock (as shown - or 4 cycles if DIV_4 is selected), to send a bit, and just 8 repeats of this, to send the byte. As shown, the whole byte takes just:
(8*16)/4000000th second.
Just 32uSec.
Given this is happening just once every two seconds, you will only see it on the scope, if you put the scope into 'manual' trigger, (rather than 'auto', which will keep drawing the trace, when not triggered), an set the timebase up to something like 4uSec/division. You will get just one byte sequence flashing across the screen at 2 second intervals.
Much better to reduce the interval between bytes (PPM_programmer's code has a much 'better' interval), which then gives you a better chance to see the data.
If DIV_4 is selected for the clock, the whole byte will take just 8uSec.
Best Wishes |
Yeah, my scope probe as on "triggered" and not "auto" which should have captured the clock pulses, and it was at a couple of uSec per division so I think my setup was correct. |
|
|
Ttelmah Guest
|
|
Posted: Sat Dec 15, 2007 4:28 pm |
|
|
Do you know which silicon revision you have for the chip?. You have chosen, one of the chips that is going for a 'record', in terms of number of MSSP errata, and though I can't spot one that would stop the clock being generated, it'd be worth checking the revision involved...
Was the scope actually triggering?. Unless your scope has reasonable persistence, the waveform will be hard to see at the slow repeat rate, but if the scope was triggering on the clock, then it says that something is there.
Best Wishes |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Sat Dec 15, 2007 4:46 pm |
|
|
It's triggered all right. I have another board with the same PIC (but a different circuit) and I see the SCK on that one and SDO also.
I didn't know about the errata...Not sure what revision it is, I got it from Digikey last week. I will check when I get back to work on Monday.
I'll load PCM's code into my board and see what happens. Then I can load it into another board that is working properly to see what happens. |
|
|
mkuang
Joined: 14 Dec 2007 Posts: 257
|
|
Posted: Tue Dec 18, 2007 6:45 pm |
|
|
Looks like after using PCM's code the SPI function works. I can even write the correct values to the DAC.
Thanks for everyone's help |
|
|
|