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 Help
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Tue Jul 24, 2012 7:33 am     Reply with quote

HI Ttelmah,

Because my C is not that good, I am finding it difficult to understand.
I fully understand that SPI is a two way information path (controlled by the master) and that the slave has to ensure that the buffer is pre-loaded before the master Tx the information.

For now, all I need to do is send a buffer (4 bytes) from master to slave - and read/print from the slave.

In your slave code, where would I insert the print command?
Ttelmah



Joined: 11 Mar 2010
Posts: 20161

View user's profile Send private message

PostPosted: Tue Jul 24, 2012 7:38 am     Reply with quote

Where I have transferred the sequence into rx_val, I then divide it by pi, and send it back. If you wanted to print, you can print rx_val instead. _But_ data should be read out of the buffer 'straight away', and only 'used' once transferred, since a new byte could (potentially) arrive at any time, and then the buffer would start overwriting.

Best Wishes
carl



Joined: 06 Feb 2008
Posts: 240
Location: Chester

View user's profile Send private message

PostPosted: Thu Jul 26, 2012 2:49 am     Reply with quote

Hi Ttelmah,

Sorry but I just couldn't understand how to fully use you example code. It is beyond my understanding - using unions and pointers. But I will keep trying to learn it because obviously it ensures direct access to the SSPBUF and this is much better than using the inbuilt functions - which you have commented on several times.

Anyway I have got it working by adapting PCM example, but the strangest thing is occuring and I dont know why - can anyone point out to me how this is possibly working??

Below is code that fully works with four bytes declared for upto 32bit read/write - however 24bits are only currently used. notice that the bytes are used for seperate reads and also reconstructed to make the 32bit word.master program
Code:
/////// MASTER PROGRAM ////////

#include <18F4550.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=48000000)


#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)

#include "flex_lcd_16x1.c"
#define SPI_SS   PIN_E2

//============================
void main()
{
output_high(SPI_SS);  // Initial Slave Select to a high level
setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_16);
SETUP_ADC_PORTS(NO_ANALOGS);

while(1)
  {
   output_low(SPI_SS);
   spi_write(1);
   delay_ms(1);
   spi_write(1);
   delay_ms(1);
   spi_write(1);
   delay_ms(1);
   output_high(SPI_SS);
   delay_ms(2000);
  }
}


Slave Program
Code:
/////// SLAVE PROGRAM ////////

#include <18F4550.H>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=48000000)

#define tohex(x) (x & 0xF) < 10 ? x + '0' : x + '7'
#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)

#include "flex_lcd_16x1.c"
int16 val =50;

// Variables used for the SPI slave receive buffer.
#define BUFFER_SIZE  80
int16 buffer[BUFFER_SIZE];
int16 next_in = 0;
int16 next_out = 0;

#define spi_kbhit (next_in != next_out)



//----------------------------
// Create an interrupt-driven SPI receive
// buffer, similar to the way it's done for
// RS-232 in the CCS example file, EX_SISR.C.

#int_ssp
void ssp_isr(void)
{
buffer[next_in] = spi_read();
next_in++;
if(next_in >= BUFFER_SIZE)
   next_in = 0;
}

//----------------------------
// Wait for a character to become available
// in the spi slave's receive buffer.  Then
// get it from the buffer and return it.

int16 spi_bgetc(void)
{
int16 c;
while(!spi_kbhit);
c = buffer[next_out];
next_out++;
if(next_out >= BUFFER_SIZE)
   next_out = 0;     
return(c);
}

void main()
{
   int32 fullbyte_read;                       
   int8 byte1, byte2, byte3, byte4;
   setup_spi(SPI_SLAVE | SPI_MODE_1); //set up SPI hardware as a slave in mode 0
   SETUP_ADC_PORTS(NO_ANALOGS);
   clear_interrupt(INT_SSP);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   while(true) {
 lcd_init();
 if(spi_kbhit)  // Char available from SPI slave ?
     {
      byte1 = spi_bgetc();  // If so, get it
      byte2 = spi_bgetc();
      byte3 = spi_bgetc();
      fullbyte_read = make32(0, byte3, byte2, byte1);
//    return fullbyte_read;
      printf(lcd_putc,"\f Value: %Lu     ", fullbyte_read);
      delay_ms(500);
     }
else
{
printf(lcd_putc,"\f Value: %Lu     ", val);
   }
  }
}


And the code above displays the correct value on the LCD (I have tried different values). For the one above it is 65793 - this equates to the correct master write of '10000000100000001'.

Now this is the starnage part. If I comment out the byte 2 and 3 in the slave, it still works correctly - but how because byte's 2 and 3 are now not used?? here is the amended slave code section, the master code remains unchanged.
Code:
void main()
{
   int32 fullbyte_read;                       
   int8 byte1, byte2, byte3, byte4;
   setup_spi(SPI_SLAVE | SPI_MODE_1); //set up SPI hardware as a slave in mode 0
   SETUP_ADC_PORTS(NO_ANALOGS);
   clear_interrupt(INT_SSP);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   while(true) {
 lcd_init();
 if(spi_kbhit)  // Char available from SPI slave ?
     {
      byte1 = spi_bgetc();  // If so, get it
 //     byte2 = spi_bgetc();
//      byte3 = spi_bgetc();
      fullbyte_read = make32(0, byte3, byte2, byte1);
//    return fullbyte_read;
      printf(lcd_putc,"\f Value: %Lu     ", fullbyte_read);
      delay_ms(500);
     }
else
{
printf(lcd_putc,"\f Value: %Lu     ", val);
   }
  }
}


So what is going on - although I presume it is something I am not understanding correctly about the SPI_Write command.

Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
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