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

SPI Read/Write, mode change

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



Joined: 14 Sep 2009
Posts: 1

View user's profile Send private message

SPI Read/Write, mode change
PostPosted: Mon Sep 14, 2009 1:02 pm     Reply with quote

Hi all,

I am trying to write to and read from registers on a HMC 700 PLL module using SPI on a PIC18F26J50. My program writes to the registers fine, but I am having problems reading. I believe it has something to do with the setup_spi command, although I am very new to this process and am not quite sure. Below is some code I’ve often seen in reference to SPI on this forum.
Code:

// SPI Mode | MOTOROLA | MICROCHIP | CCS
//----------------------------------------------------------------
//          | CPOL CPHA|  CKP CKE  |
//    0     |  0    0  |   0   1   | SPI_L_TO_H | SPI_XMIT_L_TO_H
//    1     |  0    1  |   0   0   | SPI_L_TO_H
//    2     |  1    0  |   1   1   | SPI_H_TO_L
//    3     |  1    1  |   1   0   | SPI_H_TO_L | SPI_XMIT_L_TO_H
//
#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)


As seen on the datasheet for the Hittite 700 PLL module, http://www.hittite.com/products/view.html/view/HMC700LP4 (pg. 16-17), registers are written to with SPI_MODE_0. Read cycles, however, are read on falling edges (SPI_MODE_3 I believe). In order to read the registers from the PLL, I must (1) write the address that I want to read from the module, and (2) change the SPI mode to correctly read the data. I have tried changing the “setup_spi2” command to MODE_3 for one read cycle only, but no data is read. I am unsure if the spi_mode is changing, or if this is even the correct approach. I have attached some relevant samples from my code to better illustrate the problem and my current approach.

Compiler version: 4.097

Code:
#device icd=true
#fuses HSPLL,NOWDT,NOPROTECT,DEBUG,PLLDIV3,NOCPUDIV
#use delay(clock=48000000)
#use rs232(UART1, baud=9600, errors)
void main(void)
{
setup_spi2(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);
while(TRUE)
{
   switch (z)
   {
         case 100:                 //Write to registers
         OUTPUT_HIGH(PIN_A1);
         spi_write2(0x02);        //Register 01h
         spi_write2(0x00);
         spi_write2(0x01);
         spi_write2(0x0C);
         OUTPUT_LOW(PIN_A1);
         
         delay_us(10);
         
         OUTPUT_HIGH(PIN_A1);
         spi_write2(0x04);             //Register 02h
         spi_write2(0x00);
         spi_write2(0x00);
         spi_write2(0x02);
         OUTPUT_LOW(PIN_A1);
   
         break;

         case 88:                   //Read from registers
         
         OUTPUT_HIGH(PIN_A1);   //Initialize write/read
         spi_write2(0b10001010);
         
         setup_spi2(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);         
         delay_us(10);
         
         locked2=spi_read2(0);   //Returns 0
         locked3=spi_read2(0);   //Returns 0
         locked4=spi_read2(0);   //Returns 0
         OUTPUT_LOW(PIN_A1);

    setup_spi2(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);

         break;
   }
}
}


Hopefully my explanation is sufficient, but please let me know if you need any more information. Any insight on the topic would be greatly appreciated. Thanks in advance!
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Sep 14, 2009 1:16 pm     Reply with quote

It uses SPI mode 0 only.
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