Jump to content


Photo

Spi Interface & Boostc++


1 reply to this topic

#1 mityeltu

mityeltu

    Regular

  • EstablishedMember
  • Pip
  • 79 posts
  • Gender:Male
  • Location:Tennessee
  • Interests:Communications

Posted 16 April 2010 - 05:40 PM

I have been looking into using an spi interface to a number of different modules as well as master/slave arrangements for my pics. I use sourceboost for most of my pic programming, but have used mikrobasic also in the past.

Is there not a set of SPI commands included with the sourceboost compilers? I would have imagined that this would be something the authors would want to include as it is a ncie easy way to communicate with a large number of peripheral devices.

on a different note, although maybe the same one, what is this one-wire communications set I see? Can anyone explain this?

Thanks.

#2 TimC

TimC

    Regular

  • EstablishedMember
  • Pip
  • 26 posts
  • Gender:Male
  • Location:Boston MA

Posted 17 April 2010 - 08:02 PM

The SPI interface can be handled using software or hardware and by interrupts as well. Mikrobasic as I recall does not have all the options one might need. Since I am not sure what you want, here is some software bit bang driven SPI code that will work in the most situations. Also see the bottom for a link to some One Wire code.

Also look for Lieven Hollevoet's code at http://boostc.lika.be


Regards
Tim


FIRST THE H FILE

#ifndef _SOFTSPI_H_
#define _SOFTSPI_H_

#include <system.h>

#define INPUT   1
#define OUTPUT   0
#define SET	  1
#define CLEAR   0
#define DISABLE   0
#define ENABLE   1
#define HIGH   1
#define LOW	  0



// clock
#define CLK_TRIS TRISC	  // trisa, trisb or trisc
#define CLK_PORT PORTC
#define CLK_PIN 3
// chip select
#define CS_TRIS TRISC
#define CS_PORT PORTC
#define CS_PIN 0
// chip select polarity &#40;Comment out the wrong one&#41;
//#define CS_POSITIVE
#define CS_NEGATIVE

// data i
#define DATAI_TRIS TRISC
#define DATAI_PORT PORTC
#define DATAI_PIN 4
// data o
#define DATAO_TRIS TRISC
#define DATAO_PORT PORTC
#define DATAO_PIN 5




//------------- Configure pins above this line -----------
///////////////////////////////////////////////////////////
// Don&#39;t change below this line

// The TRIS register bits
volatile bit spi_clk_dir @ CLK_TRIS . CLK_PIN;
//volatile bit spi_data_dir  @ DATA_TRIS . DATA_PIN;
volatile bit spi_cs_dir  @ CS_TRIS . CS_PIN;

volatile bit spi_datai_dir  @ DATAI_TRIS . DATAI_PIN;
volatile bit spi_datao_dir  @ DATAO_TRIS . DATAO_PIN;


//  another example of defining a pin
//volatile bit pinB1@0x6.1; //declare bit variable mapped to pin 1, port B

// The chip select of the device connected to the port
volatile bit spi_cs	@ CS_PORT . CS_PIN;
#ifdef CS_NEGATIVE
#define spi_enable   spi_cs = 0&#59; delay_us&#40;5&#41;;
#define spi_disable   spi_cs = 1&#59; delay_us&#40;5&#41;;
#else
#define spi_enable   spi_cs = 1&#59; delay_us&#40;5&#41;;
#define spi_disable   spi_cs = 0&#59; delay_us&#40;5&#41;;
#endif

volatile bit spi_clock	@ CLK_PORT . CLK_PIN;
#define clock_high	  spi_clock = 1&#59; delay_us&#40;5&#41;;
#define clock_low	  spi_clock = 0&#59; delay_us&#40;5&#41;;
#define clock_high_low   spi_clock = 1&#59; delay_us&#40;5&#41;; spi_clock = 0&#59; delay_us&#40;5&#41;;

//volatile bit spi_data	@ DATA_PORT . DATA_PIN;
//#define data_high	  spi_data = 1&#59; delay_us&#40;5&#41;;
//#define data_low	  spi_data = 0&#59; delay_us&#40;5&#41;;

volatile bit spi_datai	@ DATAI_PORT . DATAI_PIN;
//#define datai_high	  spi_datai = 1&#59; delay_us&#40;5&#41;;
//#define datai_low	  spi_datai = 0&#59; delay_us&#40;5&#41;;

volatile bit spi_datao	@ DATAO_PORT . DATAO_PIN;
//#define datao_high	  spi_datao = 1&#59; delay_us&#40;5&#41;;
//#define datao_low	  spi_datao = 0&#59; delay_us&#40;5&#41;;


void spi_init&#40;&#41;;
void spi_write_lsbfirst&#40;unsigned char cmd&#41;;
unsigned char spi_read_lsbpre&#40;&#41;;
unsigned char spi_xfer_lsb&#40;unsigned char cmd&#41;;
unsigned char  spi_xfer_lsb&#40;unsigned char  cmd, unsigned char data1, unsigned char data2&#41;;

void spi_write_lsb&#40;unsigned char data&#41;;
void spi_write_lsb&#40;unsigned char cmd,unsigned char data&#41;;
void spi_write_lsb&#40;unsigned char cmd, unsigned char data, unsigned char data2&#41;;

void spi_write_msbfirst&#40;unsigned char cmd&#41;;
unsigned char spi_read_msbpre&#40;&#41;;
unsigned char spi_xfer_msb&#40;unsigned char cmd&#41;;
unsigned char  spi_xfer_msb&#40;unsigned char  cmd, unsigned char data1, unsigned char data2&#41;;
// these are the ones we mainly call 
void spi_write_msb&#40;unsigned char data&#41;;
void spi_write_msb&#40;unsigned char cmd,unsigned char data&#41;;
void spi_write_msb&#40;unsigned char cmd, unsigned char data, unsigned char data2&#41;;
void spi_write_msb&#40;unsigned char cmd, unsigned char data, unsigned char data2, unsigned char data3&#41;;

#endif   //_SPI_H_


NOW THE C FILE

// TIMS SPI FACTORY THIS WORKS SO WE ARE HAPPY

#include <system.h>
#include &#34;softspi.h&#34;
//#include &#34;timtest.h&#34;

void spi_init&#40;&#41; {
   unsigned char x;

   spi_cs_dir = OUTPUT;
   spi_disable
	spi_clk_dir = OUTPUT;
   spi_clock = LOW;
   spi_datao_dir = OUTPUT;
   spi_datai_dir = INPUT;
}
/* ----------------------------------------------------------------------- */
/* write byte ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsbfirst&#40;unsigned char cmd&#41; {
   unsigned char  i;
   bool z;

   spi_datao_dir = OUTPUT;
   for&#40;i=0;i<=7;i++&#41; {
	  z = test_bit&#40;cmd,i&#41;;
	  spi_datao = z;
	  clock_high_low
   }
 }
/* ----------------------------------------------------------------------- */
/* write byte ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msbfirst&#40;unsigned char cmd&#41; {
   unsigned char  i;
   bool z;

   spi_datao_dir = OUTPUT;
   for&#40;i=8;i>0;i--&#41; {
	  z = test_bit&#40;cmd,&#40;i-1&#41;&#41;;
	  spi_datao = z;
	  clock_high_low
   }
 }
/* ----------------------------------------------------------------------- */
/* write 1 byte ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsb&#40;unsigned char data&#41; {

   spi_enable
   spi_write_lsbfirst&#40;data&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 1 byte ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb&#40;unsigned char data&#41; {

   spi_enable
   spi_write_msbfirst&#40;data&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 2 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsb&#40;unsigned char cmd, unsigned char data&#41; {

   spi_enable
   spi_write_lsbfirst&#40;cmd&#41;;
   spi_write_lsbfirst&#40;data&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 2 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb&#40;unsigned char cmd, unsigned char data&#41; {

   spi_enable
   spi_write_msbfirst&#40;cmd&#41;;
   spi_write_msbfirst&#40;data&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 3 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsb&#40;unsigned char cmd, unsigned char data, unsigned char data2&#41; {

   spi_enable
   spi_write_lsbfirst&#40;cmd&#41;;
   spi_write_lsbfirst&#40;data&#41;;
   spi_write_lsbfirst&#40;data2&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 3 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb&#40;unsigned char cmd, unsigned char data, unsigned char data2&#41; {

   spi_enable
   spi_write_msbfirst&#40;cmd&#41;;
   spi_write_msbfirst&#40;data&#41;;
   spi_write_msbfirst&#40;data2&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 4 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb&#40;unsigned char cmd, unsigned char data, unsigned char data2, unsigned char data3&#41; {

   spi_enable
   spi_write_msbfirst&#40;cmd&#41;;
   spi_write_msbfirst&#40;data&#41;;
   spi_write_msbfirst&#40;data2&#41;;
   spi_write_msbfirst&#40;data3&#41;;
   spi_disable
}
/* ----------------------------------------------------------------------- */
/* read byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_read_lsbpre&#40;&#41; {
   unsigned char  i,x,data=0;

   spi_datai_dir = INPUT;

   for&#40;i=0;i<=7;++i&#41; {
	  data = data >> 1;
	  x = spi_datai;
	  if &#40;x&#41;
		 data |= 0x80;
	  clock_high_low
   }
   return&#40;data&#41;;
}
/* ----------------------------------------------------------------------- */
/* read byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_read_msbpre&#40;&#41; {
   unsigned char  i,x,data=0;

   spi_datai_dir = INPUT;

   for&#40;i=0;i<=7;++i&#41; {
	  data = data << 1;
	  x = spi_datai;
	  if &#40;x&#41;
		 data |= 0x01;
	  clock_high_low
   }
   return&#40;data&#41;;
}
/* ----------------------------------------------------------------------- */
/* xfer byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_xfer_lsb&#40;unsigned char  cmd&#41; {
   unsigned char  data=0;

   spi_enable
   spi_write_lsbfirst&#40;cmd&#41;;
   data = spi_read_lsbpre&#40;&#41;;
   spi_disable
   return&#40;data&#41;;
}

/* ----------------------------------------------------------------------- */
/* xfer byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_xfer_msb&#40;unsigned char  cmd&#41; {
   unsigned char  data=0;

   spi_enable
   spi_write_msbfirst&#40;cmd&#41;;
   data = spi_read_msbpre&#40;&#41;;
   spi_disable
   return&#40;data&#41;;
}

/* ----------------------------------------------------------------------- */
/* New xfer byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_xfer_lsb&#40;unsigned char  cmd, unsigned char data1, unsigned char data2&#41; {
   unsigned char  data=0;

   spi_enable
   spi_write_lsbfirst&#40;cmd&#41;;
   spi_write_lsbfirst&#40;data1&#41;;
   spi_write_lsbfirst&#40;data2&#41;;
   data = spi_read_lsbpre&#40;&#41;;
   spi_disable
   return&#40;data&#41;;
}
/* ----------------------------------------------------------------------- */
/* New xfer byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_xfer_msb&#40;unsigned char  cmd, unsigned char data1, unsigned char data2&#41; {
   unsigned char  data=0;

   spi_enable
   spi_write_msbfirst&#40;cmd&#41;;
   spi_write_msbfirst&#40;data1&#41;;
   spi_write_msbfirst&#40;data2&#41;;
   data = spi_read_msbpre&#40;&#41;;
   spi_disable
   return&#40;data&#41;;
}

For your last question here is a one wire sample


One wire for DS18S20



Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users