muratoa
Joined: 14 Sep 2009 Posts: 1
|
SPI Read/Write, mode change |
Posted: Mon Sep 14, 2009 1:02 pm |
|
|
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! |
|