SparkFun Forums 

Where electronics enthusiasts find answers.

Your source for all things Atmel.
By josheeg
#58759
here is the Init. of my 24 bit analog to digital converter please check it out and review the pc eeg project could use YOUR help.


#include "ads1256.h"

//pin defines
//**************************** data lines
#define NEG_CHIP_SELECT 10 //Chip Select active low on pin 10
#define NEG_RESET 11 //RESET PIN active low on pin 11

//EEPROM Example
#define DATAOUT 14//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define SLAVESELECT 10//ss //this is redundant I am merging tutorials and need to decide on one...
//************************ end data lines definition

//*****************************
// global variables

//less efficent gain list but more straight forward
char g_Channel_0=(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM);
char g_Channel_1=(ADS1256_MUXP_AIN1 | ADS1256_MUXN_AINCOM);
char g_Channel_2=(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AINCOM);
char g_Channel_3=(ADS1256_MUXP_AIN3 | ADS1256_MUXN_AINCOM);
char g_Channel_4=(ADS1256_MUXP_AIN4 | ADS1256_MUXN_AINCOM);
char g_Channel_5=(ADS1256_MUXP_AIN5 | ADS1256_MUXN_AINCOM);
char g_Channel_6=(ADS1256_MUXP_AIN6 | ADS1256_MUXN_AINCOM);
char g_Channel_7=(ADS1256_MUXP_AIN7 | ADS1256_MUXN_AINCOM);

char g_Gain=ADS1256_GAIN_1;//gain setting

//global vareables from example*************************************************
unsigned char Regs[11];// holds a copy of the ADS1256 registers
unsigned char Buffer[70];// scratch buffer
int g_RateCode = ADS1256_RATE_30000; // holds the sample rate code for the ADS1256
int g_nSamples = 100;// holds the number of samples to acquire
int g_nChannels = 1;// holds the number of channels in the list

byte clr;//used to clear registers to bit trash bin.

//************************ end of example vareables

//*************************setup for spi to ad
void spi_to_AD_setup(){
SPCR=0;//clear control register
clr=SPCR; //coppy spi control register to clr to print out or clear it similar to clearing a status register
SPSR=0;
clr=SPSR;
//SPE=1 serial port enabled
//MSTR=1 master is AVR
//CPOL=0
//CPHA=0 Sample (Rising) Setup (Falling)
//SPI2X=1, SPR1=0 ,SPR0=1 FOSC/8 //this might have to be slower 7.68mhz/4 =1.92mhz
//spi2x is a little confusing it is different weather in master or slave mode? the MCU is master.
//SPSR uses SPI2X

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0);
SPSR= (1<<SPI2X);

//wait for things to settle
delay(10);
} //end of initilise spi

//*************************** spi sens from master to ad converter transfer function
//simple spi transfer function char in char out
//spdr = byte data register
char spi_transfer(volatile char data)
{
SPDR = data; // Start the transmission
while (!(SPSR & (1<<SPIF))) // Wait the end of the transmission
{
};
return SPDR; // return the received byte
}

//******************************** end spi transfer function

//************************** initilise Analog to digital converter
void ADS1256Init(void){
ads1256Reset();
//set /Chip Select high
digitalWrite(NEG_CHIP_SELECT,HIGH); //disable device
}
//************************** end of init analog to digital converter


void ads1256Reset(void){
digitalWrite(NEG_RESET,HIGH); //disable device
digitalWrite(NEG_RESET,LOW); //enable device

}

void setup() {

// initialize the communication port
Serial.begin(9600);

//set pins to nessisary data direction
pinMode(NEG_CHIP_SELECT,OUTPUT);//10 //Chip Select active low on pin 10
pinMode(NEG_RESET,OUTPUT); //RESET PIN active low on pin 11

pinMode(DATAOUT,OUTPUT);//14 MOSI
pinMode(DATAIN,INPUT);//DATAIN 12//MISO
pinMode(SPICLOCK,OUTPUT);// 13 sck
pinMode(SLAVESELECT,OUTPUT);//10//ss //this is redundant I am merging tutorials and need to decide on one...



// prints state with ending line break
Serial.println("ADS1256 Init soon");

// wait for the long string to be sent
delay(1000); //1 sec delay(1);=delay 1 ms


// initialize Hardware for ADS1256
ADS1256Init();


// prints state with ending line break
Serial.println("ADS1256 Init..ed");

// wait for the long string to be sent
delay(1000); //1 sec delay(1);=delay 1 ms

}

void loop() {
// prints state with ending line break
Serial.println("ADS1256 infinite loop wait 1 sec");

// wait for the long string to be sent
delay(1000); //1 sec delay(1);=delay 1 ms

// prints state with ending line break
Serial.println("waited 1 sec");


// wait for the long string to be sent
delay(100); //100 msec delay(1);=delay 1 ms

}

By josheeg
#58760
the header files..


#ifndef __ADS1256_H
#define __ADS1256_H

// define the port pins used for the ADS1256
#define N_DRDY 0x04
#define N_DRDY_IN P2IN
#define N_DRDY_DIR P2DIR

#define N_CS 0x20
#define N_CS_OUT P3OUT
#define N_CS_DIR P3DIR

// define the command bytes (see table 24 on page 32 in the PDS
#define CMD_WAKEUP 0x00
#define CMD_RDATA 0x01
#define CMD_RDATAC 0x03
#define CMD_SDATAC 0x0f
#define CMD_RREG 0x10
#define CMD_WREG 0x50
#define CMD_SELFCAL 0xf0
#define CMD_SELFOCAL 0xf1
#define CMD_SELFGCAL 0xf2
#define CMD_SYSOCAL 0xf3
#define CMD_SYSGCAL 0xf4
#define CMD_SYNC 0xfc
#define CMD_STANDBY 0xfd
#define CMD_RESET 0xfe
#define CMD_WAKEUP2 0xff

// define the ADS1256 registers
#define ADS1256_STATUS_REG 0x00
#define ADS1256_MUX_REG 0x01
#define ADS1256_ADCON_REG 0x02
#define ADS1256_DRATE_REG 0x03
#define ADS1256_IO_REG 0x04
#define ADS1256_OFC0_REG 0x05
#define ADS1256_OFC1_REG 0x06
#define ADS1256_OFC2_REG 0x07
#define ADS1256_FSC0_REG 0x08
#define ADS1256_FSC1_REG 0x09
#define ADS1256_FSC2_REG 0x0A

// define multiplexer codes
#define ADS1256_MUXP_AIN0 (0x0 << 4)
#define ADS1256_MUXP_AIN1 (0x1 << 4)
#define ADS1256_MUXP_AIN2 (0x2 << 4)
#define ADS1256_MUXP_AIN3 (0x3 << 4)
#define ADS1256_MUXP_AIN4 (0x4 << 4)
#define ADS1256_MUXP_AIN5 (0x5 << 4)
#define ADS1256_MUXP_AIN6 (0x6 << 4)
#define ADS1256_MUXP_AIN7 (0x7 << 4)
#define ADS1256_MUXP_AINCOM (0x8 << 4)
#define ADS1256_MUXN_AIN0 (0x0)
#define ADS1256_MUXN_AIN1 (0x1)
#define ADS1256_MUXN_AIN2 (0x2)
#define ADS1256_MUXN_AIN3 (0x3)
#define ADS1256_MUXN_AIN4 (0x4)
#define ADS1256_MUXN_AIN5 (0x5)
#define ADS1256_MUXN_AIN6 (0x6)
#define ADS1256_MUXN_AIN7 (0x7)
#define ADS1256_MUXN_AINCOM (0x8)

// define sample rates
#define ADS1256_RATE_30000 0xf0
#define ADS1256_RATE_15000 0xe0
#define ADS1256_RATE_7500 0xd0
#define ADS1256_RATE_3750 0xc0
#define ADS1256_RATE_2000 0xb0
#define ADS1256_RATE_1000 0xa1
#define ADS1256_RATE_500 0x92
#define ADS1256_RATE_100 0x82
#define ADS1256_RATE_60 0x72
#define ADS1256_RATE_50 0x63
#define ADS1256_RATE_30 0x53
#define ADS1256_RATE_25 0x43
#define ADS1256_RATE_15 0x33
#define ADS1256_RATE_10 0x23
#define ADS1256_RATE_5 0x13
#define ADS1256_RATE_2_5 0x03

// define gain codes
#define ADS1256_GAIN_1 0x00
#define ADS1256_GAIN_2 0x01
#define ADS1256_GAIN_4 0x02
#define ADS1256_GAIN_8 0x03
#define ADS1256_GAIN_16 0x04
#define ADS1256_GAIN_32 0x05
#define ADS1256_GAIN_64 0x06

typedef struct tagADS1256RegStruct
{
unsigned char Status;
unsigned char Mux;
unsigned char Adcon;
unsigned char Drate;
unsigned char Io;
unsigned char Ofc0;
unsigned char Ofc1;
unsigned char Ofc2;
unsigned char Fsc0;
unsigned char Fsc1;
unsigned char Fsc2;
} ADS1256_REG_STRUCT;

void __inline__ ADS1256EnableCS(void);
void __inline__ ADS1256DisableCS(void);

void ADS1256Init(void);
void ADS1256WaitForDataReady(void);

void ADS1256Reset(void);
void ADS1256Sync(void);
void ADS1256Wakeup(void);

void ADS1256SetRate(unsigned char Rate);
void ADS1256SetMux(unsigned char MuxCode);
void ADS1256SetGain(unsigned char GainCode);
long ADS1256ReadData(int fWaitFordataReady);
void ADS1256ReadRegister(int StartAddress, int NumRegs, unsigned char * pData);
void ADS1256WriteRegister(int StartAddress, int NumRegs, unsigned char * pData);
void ADS1256Calibrate(unsigned char CalCommand);

#endif

By josheeg
#58961
sorry the reset function should start high go low momentarily then go high again.