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 support@ccsinfo.com

shifted bit for spi_read

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



Joined: 17 Apr 2007
Posts: 22
Location: france

View user's profile Send private message

shifted bit for spi_read
PostPosted: Fri Jul 06, 2007 3:45 am     Reply with quote

Hello.

I've got a problem with the spi_read function.
My master is a pic 18lf4520.
My slave is an integrated circuit which actes as a noise canceller filter.
The communication between this two circuit is good.
I managed to write on the slave'registers, and the slave register answeres correctly.
When I use my scope, I see that all my signals are rigth.
But when I do a printf() of the value returned by the slave, the displayed value is wrong:

1.) On the register there is 0x2e (=0010 1110)
2.) On the scope screen there is 0010 1110
3.) The displayed value is 0x97 (=10010111) => left shifting

my code:
while(1)
{
output_low(DEN_BARRE);
delay_us(1);
spi_write(0x80); //----------------command:read register 0
tmp=spi_read(0);//---------------command:read spi port out
printf("\n\r TMP : %x",tmp);
output_high(COM_VERT);
output_high(DEN_BARRE);
delay_ms(2500);
//tmp=mcu_data_read(0x00);
if(tmp==0x2e)
{
output_low(COM_VERT);
}
}

I would like to know if somebody can advise me. Thanks

Best regards
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 4:16 am     Reply with quote

What is your configuration for setup_spi() on both the master and slave?
What is your compiler version?
Ttelmah
Guest







PostPosted: Fri Jul 06, 2007 4:21 am     Reply with quote

The 'odds' are that you are reading the data on the wrong edge of the SPI clock, and hence the shift.

Best Wishes
globalsys



Joined: 17 Apr 2007
Posts: 22
Location: france

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 4:33 am     Reply with quote

hello ckielstra.

here comes the description of the spi set up for the pic:

// Hardware SPI :
// - Master mode
// - Data valid when clock goes High to Low
// - SPI clock divide by 64 (115.2 kHz)
setup_spi(SPI_SS_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);

my compiler version:

IDE 3.249
PCB 3.249
PDCM 3.249
PCH 3.249
globalsys



Joined: 17 Apr 2007
Posts: 22
Location: france

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 4:40 am     Reply with quote

hello Ttelmah

I will try to change the front rising for the read.
I will tell tou soon.

Thanks
globalsys



Joined: 17 Apr 2007
Posts: 22
Location: france

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 4:42 am     Reply with quote

Thanks you too, ckielstra
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Jul 06, 2007 11:27 am     Reply with quote

Determine the SPI mode by reading the data sheet for your chip, and
then by looking at the timing diagrams shown on this website:
http://www.totalphase.com/support/articles/article03/#modes

Then set the SPI mode in the setup_spi() function by using ckielstra's
mode settings. It's much easier to do it that way, then to remember
what the CCS or Microchip bit settings mean. Example:
http://www.ccsinfo.com/forum/viewtopic.php?t=31271


Quote:

setup_spi(SPI_SS_DISABLED);
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_64);

Also, this doesn't work. Only the last call to the setup_spi() function
will take effect. You can't do sequential calls and expect each call
to have a lasting effect. Also, the "SPI_SS_DISABLED" constant
is only used with an SPI slave. You're doing an SPI Master program
so you don't even need it.
globalsys



Joined: 17 Apr 2007
Posts: 22
Location: france

View user's profile Send private message

PostPosted: Mon Jul 09, 2007 7:54 am     Reply with quote

Dear ckielstra, Ttelmah, PCM programmer,

I have foollowed your advises, and I finally managed to get a good answer from my slave.
First I modified the spi setup following the ckielstra method given by PCM programmer, and I modified the edge of the spi clock as Ttelmah said.

Thanks you very much.

You are the best.

Energetic regards
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