Jump to content
mityeltu

Spi Interface & Boostc++

Recommended Posts

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.

Share this post


Link to post
Share on other sites

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 (Comment out the wrong one)
//#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'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; delay_us(5);
#define spi_disable   spi_cs = 1; delay_us(5);
#else
#define spi_enable   spi_cs = 1; delay_us(5);
#define spi_disable   spi_cs = 0; delay_us(5);
#endif

volatile bit spi_clock	@ CLK_PORT . CLK_PIN;
#define clock_high	  spi_clock = 1; delay_us(5);
#define clock_low	  spi_clock = 0; delay_us(5);
#define clock_high_low   spi_clock = 1; delay_us(5); spi_clock = 0; delay_us(5);

//volatile bit spi_data	@ DATA_PORT . DATA_PIN;
//#define data_high	  spi_data = 1; delay_us(5);
//#define data_low	  spi_data = 0; delay_us(5);

volatile bit spi_datai	@ DATAI_PORT . DATAI_PIN;
//#define datai_high	  spi_datai = 1; delay_us(5);
//#define datai_low	  spi_datai = 0; delay_us(5);

volatile bit spi_datao	@ DATAO_PORT . DATAO_PIN;
//#define datao_high	  spi_datao = 1; delay_us(5);
//#define datao_low	  spi_datao = 0; delay_us(5);


void spi_init();
void spi_write_lsbfirst(unsigned char cmd);
unsigned char spi_read_lsbpre();
unsigned char spi_xfer_lsb(unsigned char cmd);
unsigned char  spi_xfer_lsb(unsigned char  cmd, unsigned char data1, unsigned char data2);

void spi_write_lsb(unsigned char data);
void spi_write_lsb(unsigned char cmd,unsigned char data);
void spi_write_lsb(unsigned char cmd, unsigned char data, unsigned char data2);

void spi_write_msbfirst(unsigned char cmd);
unsigned char spi_read_msbpre();
unsigned char spi_xfer_msb(unsigned char cmd);
unsigned char  spi_xfer_msb(unsigned char  cmd, unsigned char data1, unsigned char data2);
// these are the ones we mainly call 
void spi_write_msb(unsigned char data);
void spi_write_msb(unsigned char cmd,unsigned char data);
void spi_write_msb(unsigned char cmd, unsigned char data, unsigned char data2);
void spi_write_msb(unsigned char cmd, unsigned char data, unsigned char data2, unsigned char data3);

#endif   //_SPI_H_

 

 

NOW THE C FILE

 

// TIMS SPI FACTORY THIS WORKS SO WE ARE HAPPY

#include <system.h>
#include "softspi.h"
//#include "timtest.h"

void spi_init() {
  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(unsigned char cmd) {
  unsigned char  i;
  bool z;

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

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

  spi_enable
  spi_write_lsbfirst(data);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 1 byte ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb(unsigned char data) {

  spi_enable
  spi_write_msbfirst(data);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 2 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsb(unsigned char cmd, unsigned char data) {

  spi_enable
  spi_write_lsbfirst(cmd);
  spi_write_lsbfirst(data);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 2 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb(unsigned char cmd, unsigned char data) {

  spi_enable
  spi_write_msbfirst(cmd);
  spi_write_msbfirst(data);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 3 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_lsb(unsigned char cmd, unsigned char data, unsigned char data2) {

  spi_enable
  spi_write_lsbfirst(cmd);
  spi_write_lsbfirst(data);
  spi_write_lsbfirst(data2);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 3 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb(unsigned char cmd, unsigned char data, unsigned char data2) {

  spi_enable
  spi_write_msbfirst(cmd);
  spi_write_msbfirst(data);
  spi_write_msbfirst(data2);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* write 4 bytes ------- */
/* ----------------------------------------------------------------------- */
void spi_write_msb(unsigned char cmd, unsigned char data, unsigned char data2, unsigned char data3) {

  spi_enable
  spi_write_msbfirst(cmd);
  spi_write_msbfirst(data);
  spi_write_msbfirst(data2);
  spi_write_msbfirst(data3);
  spi_disable
}
/* ----------------------------------------------------------------------- */
/* read byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_read_lsbpre() {
  unsigned char  i,x,data=0;

  spi_datai_dir = INPUT;

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

unsigned char  spi_read_msbpre() {
  unsigned char  i,x,data=0;

  spi_datai_dir = INPUT;

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

unsigned char  spi_xfer_lsb(unsigned char  cmd) {
  unsigned char  data=0;

  spi_enable
  spi_write_lsbfirst(cmd);
  data = spi_read_lsbpre();
  spi_disable
  return(data);
}

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

unsigned char  spi_xfer_msb(unsigned char  cmd) {
  unsigned char  data=0;

  spi_enable
  spi_write_msbfirst(cmd);
  data = spi_read_msbpre();
  spi_disable
  return(data);
}

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

unsigned char  spi_xfer_lsb(unsigned char  cmd, unsigned char data1, unsigned char data2) {
  unsigned char  data=0;

  spi_enable
  spi_write_lsbfirst(cmd);
  spi_write_lsbfirst(data1);
  spi_write_lsbfirst(data2);
  data = spi_read_lsbpre();
  spi_disable
  return(data);
}
/* ----------------------------------------------------------------------- */
/* New xfer byte ------- */
/* ----------------------------------------------------------------------- */

unsigned char  spi_xfer_msb(unsigned char  cmd, unsigned char data1, unsigned char data2) {
  unsigned char  data=0;

  spi_enable
  spi_write_msbfirst(cmd);
  spi_write_msbfirst(data1);
  spi_write_msbfirst(data2);
  data = spi_read_msbpre();
  spi_disable
  return(data);
}

 

For your last question here is a one wire sample

 

 

One wire for DS18S20

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×