PICcoder.co.uk Forum  


::post new topic::
SPI interface with ADS1211 and MCP23s17
Date: 2008/10/17 12:26 By: Darren Status: Visitor  
 
HIHI,

I'm currently working on a project which involves SPI interface with the MCP23s17 port expander and the ADS1211 4 channel adc. I have a problem on interfacing them.

Regarding MCP1211:

With this IC, when I send data, all fine. Data is written on the output pins. But when I try to read data (from GPIOA/GPIO, a totaly different value from that of the input is read. The simple code I'm using is shown below:

[#include <p18F8527.h>
#include <spi.h>
#include <IO_cfg.h>
#include <delays.h>

void SPIout(unsigned char reg, unsigned char data);
unsigned char SPIin(unsigned char reg);

unsigned int smpl = 0x00;
unsigned int smpl2 = 0x00;

void main( void )
{
InitALLPORTS(); // initialize ports IOs

SPIout(0x0A, 0xA8) // registors in seperate banks, addrs ptr
// does not increment, addrs pins enabled

SPIout(0x00, 0xFF); // GPIOA set to all inputs
SPIout(0x01, 0x00); // GPIOA reflects same logic as input
SPIout(0x06, 0xFF); // GPA latches all set to high

SPIout(0x10, 0xFF); // GPIOB set to all inputs
SPIout(0x11, 0x00); // GPIOB reflects same logic as input
SPIout(0x16, 0xFF); // GPB latches all set to high

smpl = SPIin(0x00); // read data at GPIOA
smpl2 = SPIin(0x10); // read data at GPIOB
}

void SPIout(unsigned char reg, unsigned char data)
{
OpenSPI1(SPI_FOSC_64, MODE_00, SMPMID); // initialise SPI, mode00

port_exp2_CS = 0; // enable MCP23s17
Delay10TCYx(100); // delay for settling time
SSP1BUF = 0x42; // load write instruction
// (MCP23s17 addrs = 001);
while(!SSP1STATbits.BF); // send write instruction
SSP1BUF = reg; // load registor addres
while(!SSP1STATbits.BF); // send registor addres
SSP1BUF = data; // load data value
while(!SSP1STATbits.BF); // send data value
Delay10TCYx(100); // delay for settling time
port_exp2_CS = 1; // disable MCP23s17
CloseSPI1(); // Close SPI

}

unsigned char SPIin(unsigned char reg)
{

OpenSPI1(SPI_FOSC_64, MODE_00, SMPMID); // initialise SPI, mode00
port_exp2_CS = 0; // enable MCP23s17
Delay10TCYx(100); // delay for settling time
SSP1BUF = 0x43; // load read instruction
// (MCP23s17 addrs = 001);
while(!SSP1STATbits.BF); // send read instruction
SSP1BUF = reg; // load registor addres
while(!SSP1STATbits.BF); // send registor addres
SSP1BUF = 0x00; // clear SSP1BUF to recieve data
while(!SSP1STATbits.BF); // wait for data to be recieved
Delay10TCYx(100); // delay for settling time
port_exp2_CS = 1; // disable MCP23s17
CloseSPI1(); // clode spi
return SSP1BUF; // return SSPBUF value
}
][size=1][/size]


Regarding ADS1211:

I'm having difficulty in recieving any data. Sometimes I recieve and sometimes not. Also sime times I recieve wrong data. The code I'm using is shown below:


[/#include <p18F8527.h>
#include <spi.h>
#include <IO_cfg.h>
#include <delays.h>

void SPIout(unsigned char reg, unsigned char data);
unsigned char SPIin(unsigned char reg);

unsigned int smpl = 0x00;
unsigned int smpl2 = 0x00;

void main( void )
{
InitALLPORTS(); // initialize ports IOs
Delay10KTCYx(500);
SPIout(0x0A, 0xA8) // registors in seperate banks, addrs ptr
// does not increment, addrs pins enabled

SPIout(0x04, 0x52); // CMD byte 3 - BIAS off, Reference on,
// 2's complements data, unipolar,MSB
// and MSb first, SDOUT as output

SPIout(0x05, 0x01); // CMD byte2- Normal mode, gain = 1,chnl= 2
SPIout(0x06, 0x27); // CMD byte 1 - Turbo mode rate = 1
SPIout(0x07, 0xD0); // CMD byte 0 - decimation = 2000;

smpl = SPIin(0x00); // read byte 2 of data output registor
}

void SPIout(unsigned char reg, unsigned char data)
{
Delay100TCYx(100);
OpenSPI1(SPI_FOSC_64, MODE_01, SMPMID); // initialise SPI, mode 01
port_exp2_CS = 0; // enable ADS1211
Delay100TCYx(100); // dlay for settling time

SSP1BUF = reg & 0x0F; // load instruction, write at given addrs
while(!SSP1STATbits.BF); // send instruction
Delay100TCYx(100); // delay for settling time

SSP1BUF = data; // load value to e written
while(!SSP1STATbits.BF); // send value
Delay100TCYx(100); // delay for settling time

port_exp2_CS = 1; // disable ADS1211
CloseSPI1(); // close spi
}

unsigned char SPIin(unsigned char reg)
{
Delay10TCYx(100);
OpenSPI1(SPI_FOSC_64, MODE_01, SMPMID);// initialise SPI, mode01
port_exp2_CS = 0; // enable ADS1211
Delay10TCYx(100); // dlay for settling time
SSP1BUF = reg | 0x80; // load instruction, read at given addrs
while(!SSP1STATbits.BF); // send instruction
Delay10TCYx(100); // delay for settling time
SSP1BUF = 0x00; // clear SSP1BUF to recieve data
while(!SSP1STATbits.BF); // wait for data to be recieved
port_exp2_CS = 1; // disbale ads1211
CloseSPI1(); // close spi
return SSP1BUF; // return SSPBUF value
}
]

I've tried to vary the time delays or to write the data in a different way but for nothing. Any one can help please?

Note: I'm using PIC18F8527 with 10Mhz clock.
reply | quote


      Topics Author Date
    thread link
SPI interface with ADS1211 and MCP23s17
Darren 2008/10/17 12:26
    thread link
thread linkthread link Re:SPI interface with ADS1211 and MCP23s17
Charell 2010/04/25 04:50
::post new topic::