Discussion in "PIC Microcontroller Discussion" started by    nacedo    May 11, 2013.
Sat May 11 2013, 02:20 am
#1
I working with a PIC18F2580, and i'm using the MCP3911 as AFE for analog readings of voltage and electric current, but i'm trying to see the default settings of internal registers but i don't recive any answer of mcp3911. this is the schematic



[ Edited Mon May 13 2013, 09:39 pm ]
Mon May 13 2013, 10:07 am
#2
make sure MCP is not in reset state (reset pin should be high) when reading from it. You can post your code for better guidance.

[Topic moved to PIC Microcontroller Discussion Forum]
Mon May 13 2013, 07:43 pm
#3
Thanks for your reply

I think, the pin reset is "1" logic, but this is the source code to make rewiews, I'm using the Hi tech compiler PICC18

#include        <pic18.h>

#include        <sys.h>

#include        <ctype.h>

#include        <stdlib.h>

#include        <math.h>

#include        "stdio.h"
#include        "string.h"
#include        "delay.h"
#include        "config.h"

__CONFIG(1, HS & IESODIS & FCMDIS);
__CONFIG(2, PWRTDIS & WDTPS4K & WDTDIS & BOREN);
__CONFIG(4, XINSTDIS & STVREN & LVPDIS);
  
 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);
  
 //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

//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

//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

void CloseSPI(void);
void OpenSPI(unsigned char sync_mode,unsigned char bus_mode,unsigned char smp_phase);
unsigned char getcSPI(void);
unsigned char putcSPI(unsigned char cx);
 
  
 
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 
}

unsigned char getcSPI(void)
{      
        unsigned char Temp;
        Temp    = SSPBUF;
        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
}

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 ); 
}


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

di();
DelayMs(20);
bMCP3911_SS=0;
DelayMs(20);
ResultadoSPI=putcSPI(0x1A);
DelayMs(20);
ResultadoSPI=putcSPI(0xC0);
DelayMs(20);
bMCP3911_SS=1;
DelayMs(20);
ei();

di();
DelayMs(20);
bMCP3911_SS=0;
DelayMs(20);
ResultadoSPI=putcSPI(0x0E);
DelayMs(20);
ResultadoSPI=putcSPI(0x00);
DelayMs(20);
ResultadoSPI=putcSPI(0x00);
DelayMs(20);
ResultadoSPI=putcSPI(0x80);
DelayMs(20);
ResultadoSPI=putcSPI(0x1F);
DelayMs(20);
ResultadoSPI=putcSPI(0xF8);
DelayMs(20);
ResultadoSPI=putcSPI(0xFF);
DelayMs(20);
bMCP3911_SS=1;
DelayMs(20);
ei();

di();
DelayMs(20);
bMCP3911_SS=0;
DelayMs(20);
ResultadoSPI=putcSPI(0x1A);
DelayMs(20);
ResultadoSPI=putcSPI(0x04);
DelayMs(20);
bMCP3911_SS=1;
DelayMs(20);
ei();

DelayMs(20);
bMCP3911_SS=0;
DelayMs(20);
ResultadoSPI=putcSPI(0x1B);
DelayMs(20);
ResultadoSPI=getcSPI();
DelayMs(20);
bMCP3911_SS=1;
DelayMs(20);


[ Edited Mon May 13 2013, 09:18 pm ]
Mon May 13 2013, 09:35 pm
#4
ok.. first I do not see your BF being configured anywhere.. is it a software flag? where it gets set or cleared? I do not see any define for it. I always like to write a single function for read/write on SPI bus. It makes things simple and less complicated.

#define SENDSPI(x)	transferSPI(x)
#define RECVSPI()	transferSPI(0xFF)

unsigned char transferSPI(unsigned char dat)
{
	SSPBUF = dat;
	while(!SSPIF);
	dat = SSPBUF;
	SSPIF = 0;
	return dat;
}
Mon May 13 2013, 09:51 pm
#5
Yes the BF flag is defined in the libraries of HITECH compiler therefore you don't see any definition for it besides his cleared automatic (Don't need be cleared by software);-it's indicated by the datasheet on the point 17.3.2 http://ww1.microchip.com/downloads/en/DeviceDoc/39637b.pdf-

Concerning of the read and write for spi communication you are right, but the transmition the pic is ok, i tested with an oscilloscope (although you're right this must be more easy) i'll make more simple in the new code

Do you see any another upgrade?

thank for your reply


[ Edited Mon May 13 2013, 10:07 pm ]
Mon May 13 2013, 10:53 pm
#6
You're not checking "bMCP3911_DATAR" anywhere.. coz thats the pin tells you when to read data. Did you monitor data out pin of MCP3911? is it actually sending anything?
Mon May 13 2013, 11:08 pm
#7
exactly that is my problem therefore i am not checking bMCP3911_DATAR, because i can't read internal bit setting; in overview the IC MCP3911 is not sending anything

thank for your reply


[ Edited Mon May 13 2013, 11:08 pm ]
Tue May 14 2013, 12:00 am
#8
Does the Hi Tech compiler have any built in SPI routines. ?
Tue May 14 2013, 01:38 am
#9
I don't think that exist any libraries for pic18, i know that exist for pic32; but i reviewed the pin of spi interface from pic18F2580 with an oscilloscope and this work correctly

thank for your reply


[ Edited Tue May 14 2013, 01:50 am ]
Tue May 14 2013, 03:09 am
#10


I don't think that exist any libraries for pic18, i know that exist for pic32; but i reviewed the pin of spi interface from pic18F2580 with an oscilloscope and this work correctly

thank for your reply

nacedo


The oscilloscope can show the levels changing, but the timing could be wrong.
Try talking to a simpler device to test your SPI communication.

Get Social

Information

Powered by e107 Forum System

Downloads

Comments

RandyBence
Wed May 15 2024, 02:00 pm
JordanDic
Wed May 15 2024, 01:55 pm
DavidDeelf
Wed May 15 2024, 11:16 am
ytaletjkca
Wed May 15 2024, 09:45 am
MildredWoumb
Wed May 15 2024, 04:07 am
NaKrutkADamb
Wed May 15 2024, 12:03 am
MichaelGot
Tue May 14 2024, 04:08 pm
FrankTrelm
Tue May 14 2024, 10:39 am