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

Stupid Newb SPI Problem

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



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

Stupid Newb SPI Problem
PostPosted: Fri Oct 02, 2009 1:11 pm     Reply with quote

Hi there,

I've gotten my uart working just fine on my PIC18F2410 on the popular dlp232pc min-dev board. But now, setting up SPI has been nothing short of bewildering. I've looked in the manual and the forum and tried the sample code and never been able to get anything out more than a clock signal.

Here's where I am right now:

Code:

#include <18f2410.h>
//#device adc=10
#include <stdlib.h>
#fuses INTRC,NOWDT,NOPROTECT,CCP2C1
#use delay(clock=4000000)
//#use rs232(baud=9600, bits=8, UART1, SYNC_MASTER)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, errors)
// #use SPI(DO = PIN_C5, DI = PIN_C4, CLK = PIN_C3, SAMPLE_FALL, baud = 9600, BITS = 16, LSB_FIRST, stream = SPI_PORT0 )
// #use SPI(FORCE_HW, BITS=16, stream=SPI_stream)
// #use SPI(DO = PIN_C5, DI = PIN_C4, CLK = PIN_C3, baud = 9600, BITS = 16, LSB_FIRST, SAMPLE_RISE, stream = SPI_PORT0)

#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_oscillator(OSC_4MHZ);

    //setup_adc_ports(NO_ANALOGS);
    //set_tris_a(0x01);
   
    setup_spi(SPI_MASTER | SPI_MODE2 | SPI_CLK_DIV_4);

   while(true)
   {
      //spi_write(0b0001000011110000);
      spi_write(0xFF);
      //output_bit(PIN_C3, 1);
     
      //putc('g');
      //delay_ms(100);
     
      spi_write(0x00);
      //output_bit(PIN_C3, 0);
     
      //putc('y');
      //delay_ms(100);
     
      spi_write(0xFF);
      //output_bit(PIN_C3, 1);
     
      //putc('r');
      //delay_ms(100);
     
      //output_bit(PIN_C3, 0);
      //delay_ms(100);
 

   }



This gives me a 5Hz clk (yes, you read that right 5Hz), and nothing else.... No activity out of SDO, or SS.... Any help would be appreciated. This is on a brand new board that I just pulled out of the packaging.
bside



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 1:17 pm     Reply with quote

wow.... nothing like a typo to ruin your day:

Code:

setup_spi(SPI_MASTER | SPI_MODE_2 | SPI_CLK_DIV_4);


getting something.... more testing, I may be back
bside



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 1:28 pm     Reply with quote

ok, this is good, I'm getting good data out of my clk and data lines, but nothing out of my ~SS. I'm using a National DAC084S085 DAC which requires that the ~SYNC (which I have tied to ~SS) is pulled low at the beginning (when I'm clocking data into the DAC's shift register). Can I enable this in HW?

Also, why can you not touch registers directly with CCS. I.e. why can I not say:

Code:

SSPCON1 = 0b00110000;


Thanks a lot in advance.
bside



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 2:01 pm     Reply with quote

ok.... I see I have to use the #byte directive.... why don't they just include this in the header files for each device?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 2:09 pm     Reply with quote

Quote:
but nothing out of my ~SS

\SS is not an output pin in Master mode. It's not used.
It's only used for an SPI slave.

Quote:
why don't they just include this in the header files for each device?

Read ckielstra's post on how to make a register include file by using
the Chipedit.exe utility.
http://www.ccsinfo.com/forum/viewtopic.php?t=36803
bside



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 3:51 pm     Reply with quote

wow, that chipedit util is pretty nice. thanks a lot PCM!

I get a few errors when I add the .h though:

Code:


--- Info 300 "C:\Program Files\PICC\devices\PIC18F2410_registers.h" Line 150(2,6): More info:   First Declaration of MCU_RD16
*** Error 31 "C:\Program Files\PICC\devices\PIC18F2410_registers.h" Line 273(2,6): Identifier is already used in this scope
*** Error 48 "C:\Program Files\PICC\devices\PIC18F2410_registers.h" Line 273(18,28): Expecting a (
*** Error 43 "C:\Program Files\PICC\devices\PIC18F2410_registers.h" Line 273(20,25): Expecting a declaration
      3 Errors,  0 Warnings.


and when I comment out those lines in the .h, I just turn up more errors...

I went back to setup_spi() or
Code:

   #byte SSPBUF   = Getenv("SFR:SSPBUF")
   #byte SSPSTAT   = Getenv("SFR:SSPSTAT")
   #byte SSPCON1   = Getenv("SFR:SSPCON1")
   #bit  BF      = SSPSTAT.0


for the time being.

When I try to bit bang the SS line to act as my enable, it locks up. Can I not use this line when SPI is enabled? For example if I setup like this:

Code:

   setup_spi(SPI_MASTER | SPI_MODE_2 | SPI_CLK_DIV_4 | SPI_SS_DISABLED);
      output_bit(PIN_A5, 1);
      delay_ms(1);
      output_bit(PIN_A5, 0);
      spi_write(0xFF);


it locks up on output_bit(PIN_A5, 1) with the SS pin stuck high.

Can I not use the SS pin at all if I'm in HW SPI mode (even if I'm in master mode)?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 4:00 pm     Reply with quote

Quote:
setup_spi(SPI_MASTER | SPI_MODE_2 | SPI_CLK_DIV_4 | SPI_SS_DISABLED);

Don't use the SPI_SS_DISABLED constant in a definition for a Master.
It's not involved at all, in a Master. Delete the parameter shown in bold.

Also, post your compiler version. I'll look at the code for your version.
bside



Joined: 01 Oct 2009
Posts: 6

View user's profile Send private message

PostPosted: Fri Oct 02, 2009 4:53 pm     Reply with quote

great, thanks PCM. I have version 4.096 of PCWH.

For some reason it's working now and I did two SPI writes to get the DAC to see 16 bits. Check back in a sec.
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