Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

SPI communication between MCP3911 and PIC18F2580

Status
Not open for further replies.

nacedo

New Member
I'm trying to communicate the PIC18F2580 to MCP3911, but i can´t make the interface SPI work
 

Attachments

  • Dibujo.JPG
    Dibujo.JPG
    43.6 KB · Views: 496
Maybe you haven't got a reply because you haven't given any information on what you are currently trying.

If you want code for SPI there is plenty around on the net, if you want your code fixed you would have to post it.
 
Well, this is the source code that i use for the customization of the SPI port, but i maked a few tests with a osciloscope and i had comfirmed that the port work; but the all pads from mcp3911 doesn´t show any signal. i'm using mplab 8 and Hitech picc-18

this is the code:

C:
spi.h

/***************************************
cabecera del protocolo de comunicación 
SPI

****************************************/

//Modos de Trabajo 
//sync_mode
#define   SPI_FOSC_4    0b00000000              // SPI Master mode, clock = Fosc/4
#define   SPI_FOSC_16   0b00000001              // SPI Master mode, clock = Fosc/16
#define   SPI_FOSC_64   0b00000010              // SPI Master mode, clock = Fosc/64
#define   SPI_FOSC_TMR2 0b00000011              // SPI Master mode, clock = TMR2 output/2
#define   SLV_SSON      0b00000100              // SPI Slave mode, /SS pin control enabled
#define   SLV_SSOFF     0b00000101              // SPI Slave mode, /SS pin control disabled

//Modos de Trabajo del Bus
//bus_mode
#define   MODE_00       0b00000000              // Setting for SPI bus Mode 0,0
//CKE           0x40                   // SSPSTAT register 
//CKP           0x00                   // SSPCON1 register 

#define   MODE_01       0b00000001              // Setting for SPI bus Mode 0,1
//CKE           0x00                   // SSPSTAT register 
//CKP           0x00                   // SSPCON1 register

#define   MODE_10       0b00000010              // Setting for SPI bus Mode 1,0
//CKE           0x40                   // SSPSTAT register
//CKP           0x10                   // SSPCON1 register

#define   MODE_11       0b00000011              // Setting for SPI bus Mode 1,1
//CKE           0x00                   // SSPSTAT register
//CKP           0x10                   // SSPCON1 register

//Modos de Trabajo Fase
//smp_phase
#define   SMPEND        0b10000000           // Input data sample at end of data out             
#define   SMPMID        0b00000000           // Input data sample at middle of data out


/* SSPCON1 REGISTER */
#define   SSPENB        0b00100000           // Enable serial port and configures SCK, SDO, SDI



unsigned char DataRdySPI(void);

//Operacion del Puerto
void CloseSPI(void);
void OpenSPI(unsigned char sync_mode,unsigned char bus_mode,unsigned char smp_phase);

//Funciones Lectura
unsigned char ReadSPI(void);
unsigned char getcSPI(void);
void getsSPI(unsigned char *rdptr, unsigned char length);

//Funciones Escritura
unsigned char WriteSPI(unsigned char data_out);
unsigned char putcSPI(unsigned char cx);
void pudsSPI(unsigned char *wrptr);

C:
spi.c
#include	<pic18.h>
#include 	<sys.h>
#include 	<ctype.h>
#include 	<stdlib.h>	
#include	"spi.h"
#include	"config.h"


unsigned char DataRdySPI(void)
{
  if (BF)
    return ( +1 );                // data in SSP1BUF register
  else
    return ( 0 );                 // no data in SSP1BUF register
}

//Funciones Operacion
void CloseSPI(void)
{
	SSPCON1 &= 0xDF;
}

void OpenSPI(unsigned char sync_mode,unsigned char bus_mode,unsigned char smp_phase)
{
  SSPSTAT &= 0x3F;                // power on state 
  SSPCON1 = 0x00;                 // power on state
  SSPCON1 |= sync_mode;           // select serial mode 
  SSPSTAT |= smp_phase;           // select data input sample phase

	TRIS_SDI=1;
	TRIS_SDO=0;	
	TRIS_SCK=0;	
	TRIS_SS=0;	
	TRIS_DATAR=1;	
	TRIS_RST=0;

  switch( bus_mode )
  {
    case 0:                       // SPI bus mode 0,0
      CKE = 1;        // data transmitted on falling edge
      break;    
    case 2:                       // SPI bus mode 1,0
      CKE = 1;        // data transmitted on rising edge
      CKP = 1;        // clock idle state high
      break;
    case 3:                       // SPI bus mode 1,1
      CKP = 1;        // clock idle state high
      break;
    default:                      // default SPI bus mode 0,1
      break;
  }

	SSPCON1 |= SSPENB;              // enable synchronous serial port 
}


//Funciones Lectura
unsigned char ReadSPI(void)
{
  unsigned char TempVar;
  TempVar = SSPBUF;        // Clear BF
  SSPIF = 0;              // Clear interrupt flag
  SSPBUF = 0x00;           // initiate bus cycle
  //while ( !BF );          // wait until cycle complete
  while(!SSPIF)continue;          // wait until cycle complete
  return SSPBUF;       // return with byte read 
}

unsigned char getcSPI(void)
{
	SSPBUF 	= 0; 			// trigger 8 SCK pulses to shift in data
	while(!BF)continue;            // wait until the whole byte has been shifted in
	return SSPBUF;			// return with byte read
}


void getsSPI(unsigned char *rdptr, unsigned char length)
{
  while ( length )                // stay in loop until length = 0
  {
    *rdptr++=getcSPI();        // read a single byte
    length--;                     // reduce string length count by 1
  }
}

//Funciones Escritura
unsigned char WriteSPI(unsigned char data_out)
{
  unsigned char TempVar;  
  TempVar = SSPBUF;           // Clears BF
  SSPIF = 0;         // Clear interrupt flag
  WCOL = 0;			//Clear any previous write collision
  SSPBUF = data_out;           // write byte to SSPBUF register
  if ( SSPCON1 & 0x80 )        // test if write collision occurred
	return ( -1 );              // if WCOL bit is set return negative #
	else
   	//while( !BF );  // wait until bus cycle complete 
    while( !SSPIF )continue;  // wait until bus cycle complete  
	return ( 0 );                // if WCOL bit is not set return non-negative#
}

unsigned char putcSPI(unsigned char cx)
{

	if ( SSPCON1 & 0x80 )   	// test if write collision occurred
   	{
	return (-1);		// if WCOL bit is set return negative #
  	}
	else
	{
	SSPBUF	= cx;
   	while( !BF )continue; // wait until bus cycle complete 
	}
	return ( 0 ); 
}

void pudsSPI(unsigned char *wrptr)
{
  while ( *wrptr )                // test for string null character
  {
     SSPBUF = *wrptr++;           // initiate SPI bus cycle 
	while( !BF );    // wait until 'BF' bit is set
  }
}
this is the section of the main code i'm using for the firmware
C:
// Archivos Header
#include	<pic18.h>
#include 	<sys.h>
#include 	<ctype.h>
#include 	<stdlib.h>
#include	<math.h>
#include	"spi.h"

static bit bMCP3911_DATAR	@((unsigned)&PORTB*8+0);
static bit bMCP3911_SCK		@((unsigned)&PORTC*8+3);
static bit bMCP3911_SDI		@((unsigned)&PORTC*8+4);
static bit bMCP3911_SDO	@((unsigned)&PORTC*8+5);
static bit bMCP3911_SS		@((unsigned)&PORTC*8+6);
static bit bMCP3911_RST		@((unsigned)&PORTC*8+7);

__CONFIG(1, HS & IESODIS & FCMDIS);
__CONFIG(2, PWRTDIS & WDTPS4K & WDTDIS & BOREN);
__CONFIG(4, XINSTDIS & STVREN & LVPDIS);

	//SPI
	bMCP3911_RST=0;
	DelayMs(20);
	bMCP3911_RST=1;
	bMCP3911_SS=1;
	SSPIE=0;
	OpenSPI(SPI_FOSC_16, MODE_00, SMPMID);

       				//Primero
				di();
				bMCP3911_SS=0;
		
				ResultadoSPI=putcSPI(0b00011010);
	
				ResultadoSPI=putcSPI(0b11000100);

				bMCP3911_SS=1;
				ei();
				
				//Segundo
				di();

				ResultadoSPI=putcSPI(0b00011010);
				
				ResultadoSPI=putcSPI(0b00000100);
				
				bMCP3911_SS=1;
				ei();

				//Tercero
				di();			
				bMCP3911_SS=0;
			
				ResultadoSPI=putcSPI(0b00011011);
			
				ResultadoSPI=getcSPI();
	
				bMCP3911_SS=1;
				ei();
 
Last edited by a moderator:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top