| View previous topic :: View next topic | 
	
	
		| Author | Message | 
	
		| globalsys 
 
 
 Joined: 17 Apr 2007
 Posts: 22
 Location: france
 
 
			    
 
 | 
			
				| shifted bit for spi_read |  
				|  Posted: Fri Jul 06, 2007 3:45 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 4:16 am |   |  
				| 
 |  
				| What is your configuration for setup_spi() on both the master and slave? What is your compiler version?
 |  | 
	
		|  | 
	
		| Ttelmah Guest
 
 
 
 
 
 
 
			
			
			
			
			
			
			
			
			
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 4:21 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 4:33 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 4:40 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 4:42 am |   |  
				| 
 |  
				| Thanks you too, ckielstra |  | 
	
		|  | 
	
		| PCM programmer 
 
 
 Joined: 06 Sep 2003
 Posts: 21708
 
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Fri Jul 06, 2007 11:27 am |   |  
				| 
 |  
				| 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
 
 
			    
 
 | 
			
				|  |  
				|  Posted: Mon Jul 09, 2007 7:54 am |   |  
				| 
 |  
				| 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
 |  | 
	
		|  | 
	
		|  |