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

Really basic spi question

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



Joined: 14 Dec 2007
Posts: 257

View user's profile Send private message Send e-mail

Really basic spi question
PostPosted: Fri Dec 14, 2007 6:09 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 6:53 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 14, 2007 7:28 pm     Reply with quote

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

View user's profile Send private message

PostPosted: Fri Dec 14, 2007 8:22 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Fri Dec 14, 2007 8:49 pm     Reply with quote

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








PostPosted: Sat Dec 15, 2007 5:11 am     Reply with quote

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







PostPosted: Sat Dec 15, 2007 7:49 am     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat Dec 15, 2007 8:32 am     Reply with quote

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







PostPosted: Sat Dec 15, 2007 4:28 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Sat Dec 15, 2007 4:46 pm     Reply with quote

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

View user's profile Send private message Send e-mail

PostPosted: Tue Dec 18, 2007 6:45 pm     Reply with quote

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 Very Happy
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