Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Homepage
The code:-
/ ECG ADS1291 Test program. ADS1291 is a single channel ECG chip with a 24 bit Sigma-Delta ADC
- /
- include "mbed.h"
- include <string.h>
- include <stdio.h>
- define PIN_MOSI PTA16
- define PIN_MISO PTA17
- define PIN_SCLK PTD1 #define PIN_TSC_INTR PTC9
Serial pc(USBTX,USBRX); InterruptIn DRDY_BAR1(PTC8); DigitalIn DRDY_BAR(PTC8); InterruptIn DRDY_BAR(PTD5); DigitalOut CHIPSEL_BAR(PTD0); DigitalOut ADS_START(PTC16); DigitalOut RESET_BAR(PTC17); DigitalOut togg(PTD0);
int freq = 10000; int bits = 8 ; int mode = 1 ; int loop = 1000000 ; unsigned char chk2= 0; unsigned int value = 0; float value = 0; unsigned int value1 = 0; unsigned int value2 = 0; unsigned int value3 = 0; unsigned int value4 = 0; unsigned int value5 = 0; unsigned int value6 = 0; unsigned int data1 = 0; unsigned int count = 0; unsigned int concatenate_value = 0; int concatenate_value = 0; uint32_t adc_buff[2500]; float adc_buff[2500]; SPI commands int CMD_WAKEUP = 0x02; int CMD_STANDBY = 0x04; int CMD_RESET = 0x06; int CMD_START = 0x08; int CMD_STOP = 0x0A; int CMD_OFFSET_CAL = 0x1A; int CMD_RDATAC = 0x10; int CMD_SDATAC = 0x11; int CMD_RDATA = 0x12; Note the following commands are 3 bits and have the following structure CMD_REGr_rrrr; where r_rrrr is the register address given below int CMD_WREG = 0x02; int CMD_RREG = 0x01;
SPI registers addresses, 5 bits width int REG_ID = 0x00; int REG_CONFIG1 = 0x01; int REG_CONFIG2 = 0x02; int REG_LOFF = 0x03; int REG_CH1SET = 0x04; int REG_CH2SET = 0x05; int REG_RLD_SENS = 0x06; int REG_LOFF_SENS = 0x07; int REG_LOFF_STAT = 0x08; int REG_MISC1 = 0x09; int REG_MISC2 = 0x0A; int REG_GIPO = 0x0B;
SPI mySpi(PIN_MOSI, PIN_MISO, PIN_SCLK) ; /* SPI is the spi function written in mbed and mySpi is the instance name*/
typedef void (*func_ptr)(void) ; /*creates typedef to function pointer, does not take any arguement and returns void*/
void doHelp(void) ; void doStatus(void) ; void doFreq(void) ; void doMode(void) ; void doBit(void) ; void doWrite(void) ; void doWrite2(void) ; void doRead(void) ; void doLoop(void) ; void setup(void); void testsetup(void); void ecgsetup(void); void regWrite(int,int); void cmdWrite(int); void regRead(int); void lpf_coef(void); float lpf( float coeff[5], float); void drdy_int(void);
typedef struct _cmd_func { char *name ; func_ptr func ; } cmd_func_type ;
cmd_func_type cmd_list[] = { /*"cmd_func_type cmd_list[]" is same as "struct cmd_list[]"*/ {"help", doHelp}, {"status", doStatus}, {"freq", doFreq}, {"mode", doMode}, {"bit", doBit}, {"write", doWrite}, {"read", doRead}, {"loop", doLoop}, { 0, 0 } } ;
func_ptr getFunc(char *cmd) /*here "func_ptr is same as void*/ { int i = 0 ; while(cmd_list[i].name != 0) { if (strcmp(cmd, cmd_list[i].name) == 0) { return(cmd_list[i].func) ; ; } i++ ; } return(0) ; }
void doHello() { printf("=== spi test program ===\n\r") ; printf("please set your terminal program\n\r") ; printf("local echo on\n\r") ; printf("\n\r") ; }
int main() {
count = 0; unsigned char chk = 1; pc.baud(115200); mySpi.frequency(freq) ; mySpi.format(bits, mode) ; DRDY_BAR1.fall(&doWrite2);
DRDY_BAR1.disable_irq(); DRDY_BAR1.mode(PullUp);
Initialize ADS1291 setup() ;
Put device back in RDATAC mode cmdWrite(CMD_RDATAC) ; printf("The device is in RDATAC mode\n"); Activate conversion, At this point DRDY should toggle ADS_START = 1; int i; printf( "ADC noise data is is= \n"); for(i=0;i<100;i++) { while(DRDY_BAR); doWrite() ; printf( "ADC status is= %X %X %X\n", value1,value2,value3); concatenate_value = ((value4<< 16)|(value5 <<8) |(value6)); printf( "%X\n", concatenate_value); }
testsetup(); wait(0.2); cmdWrite(CMD_RDATAC) ; printf("The device is in RDATAC mode\n"); printf("Test data is\n"); ADS_START = 1; while(1) int j; for(j=0;j<1000;j++) { while(DRDY_BAR); doWrite() ; concatenate_value = ((value4<< 16)|(value5 <<8) |(value6)); value = concatenate_value * 0.000000144243249; printf("%f\n",value); }
ecgsetup(); printf("\n Reading"); DRDY_BAR1.enable_irq(); cmdWrite(CMD_RDATAC) ; printf("The device is in RDATAC mode\n"); ADS_START = 1; chk = 1;
/*while(1) { while(DRDY_BAR); doWrite() ; concatenate_value = ((value4<< 16)|(value5 <<8) |(value6)); value = concatenate_value * 0.0000001442432403564; printf( "%X\n", concatenate_value); printf("%f\n", value); wait(0.5); }*/
DRDY_BAR1.mode(PullUp);
count = 0; chk2=0; while(chk == 1) { if(chk2 == 1) { DRDY_BAR1.disable_irq(); printf("\n Reading and displaying buffer"); for(i=0;i<= 2500;i++) { printf("%f\n",(float)(adc_buff[i]*0.0000008654594421384)); printf("%d\n",adc_buff[i]); } printf("Reading Over"); chk=0; chk2 = 0; } else { count = count; togg = !togg; }
} }
for initial configurations void setup() { initially make all inputs low until power is up and stabilized CHIPSEL_BAR = 0; ADS_START = 0; RESET_BAR = 0; wait for oscillator to wake up wait(1);
CHIPSEL_BAR = 1; RESET_BAR = 1; wait for tpor time wait for power on reset wait(1); RESET_BAR = 0; send a reset pulse and wait for t_reset amount of time wait(1); RESET_BAR = 1;release the reset Wait for 18 tclks = 36 us wait_us(36);
Device wakes up in RDATAC mode so send SDATAC command to write to registers cmdWrite(CMD_SDATAC) ; printf("Device is in SDATAC mode\n");
regRead(REG_ID); printf("DEVICE ID register read from ADS is= 0x%X\n",data1); wait(1);
Since we are using internal 2.42V reference and enable clock on the CLK PIN Write 0xA8 to CONFIG2 register regWrite(REG_CONFIG2,0xA8);
Set continuous sampling mode, 500 SPS regWrite(REG_CONFIG1,0x02);
PGA Gain = 6, inputs shorted for noise measurements regWrite(REG_CH1SET, 0x01);
read data from CONFIG2 register regRead(REG_CONFIG2); printf("REG_CONFIG2 register read from ADS for initial setup is= 0x%X\n",data1);
read data from CONFIG1 register regRead(REG_CONFIG1); printf("REG_CONFIG1 register read from ADS for initial setup is= 0x%X\n",data1);
read data from CONFIG1 register regRead(REG_CH1SET); printf("REG_CH1SET register read from ADS for initial setup is= 0x%X\n",data1); }
void testsetup() { Send SDATAC command to write to registers to set test signals cmdWrite(CMD_SDATAC) ; printf("Device is in SDATAC mode\n");
Since we are using internal 2.42V reference and enable clock on the CLK PIN Write 0xA3 to CONFIG2 register to set test signal and its freq = 1Hz regWrite(REG_CONFIG2,0xA3);
PGA Gain = 6, test signal selected regWrite(REG_CH1SET, 0x05);
read data from CONFIG2 register regRead(REG_CONFIG2); printf("REG_CONFIG2 register read from ADS for test signal setup is= 0x%X\n",data1);
read data from CONFIG1 register regRead(REG_CH1SET); printf("REG_CH1SET register read from ADS for test signal setup is= 0x%X\n",data1); }
void ecgsetup() { cmdWrite(CMD_SDATAC) ; Set to SDATAC mode to set CH1SET register
regWrite(REG_CONFIG1,0x02); regWrite(REG_CONFIG2, 0xE0);put INT_TEST and TEST_FREQ bits to default mode(set those bits to '0') regWrite(REG_LOFF, 0xF0); regWrite(REG_CH1SET, 0x00); Set to read normal electrode input regWrite(REG_RLD_SENS, 0x23);Set RLD_SENS regWrite(REG_LOFF_SENS, 0x03);Set LOFF_SENS regWrite(REG_MISC1, 0x02);Set RESP1 regWrite(REG_MISC2, 0x03);Set RESP2 printf("connect ECG leads\n"); wait(0.5); regRead(REG_LOFF_STAT);Read LOFF_STAT register printf("REG_LOFF_STAT register read from ADS for ecg setup is= 0x%X\n",data1); }
void doHelp(void) { printf("=== spi test ===\n\r") ; printf("commands available\n\r") ; printf("help\n\r") ; printf("status\n\r") ; printf("freq freq_in_hz\n\r") ; printf("mode (0 | 1 | 2 | 3)\n\r") ; printf("bit (4 - 16)\n\r") ; printf("write value\n\r") ; printf("read\n\r") ; printf("loop number (set repeat number for read/write)\n\r") ; }
void doStatus(void) { printf("=== Status Report ===\n\r") ; printf("bits: %d\n\r", bits) ; printf("mode: %d\n\r", mode) ; printf("freq: %d Hz\n\r", freq) ; printf("loop: %d\n\r", loop) ; }
void doFreq(void) { int freq = 0 ; scanf("%d", &freq) ; printf("setting frequency to %d\n\r", freq) ; mySpi.frequency(freq) ; }
void doMode(void) { scanf("%d", &mode) ; printf("setting format(%d, %d)\n\r",bits, mode) ; mySpi.format(bits, mode) ; }
void doBit(void) { scanf("%d", &bits) ; printf("setting format(%d, %d)\n\r",bits, mode) ; mySpi.format(bits, mode) ; }
void doWrite(void) {
DRDY_BAR1.disable_irq();
CHIPSEL_BAR = 0; int freq1 = 50000; mySpi.frequency(freq1) ; value1 = mySpi.write(0x00); value2 = mySpi.write(0x00); value3 = mySpi.write(0x00); value4 = mySpi.write(0x00); value5 = mySpi.write(0x00); value6 = mySpi.write(0x00);
CHIPSEL_BAR = 0; DRDY_BAR1.enable_irq(); } void doWrite2(void) {
DRDY_BAR1.disable_irq(); DRDY_BAR1.disable_irq(); uint32_t adc_val; CHIPSEL_BAR = 0; int freq1 = 50000; mySpi.frequency(freq1) ; value1 = mySpi.write(0x00); value2 = mySpi.write(0x00); value3 = mySpi.write(0x00); value4 = mySpi.write(0x00); value5 = mySpi.write(0x00); value6 = mySpi.write(0x00); if(count<=2499) { adc_buff[count] = ((value4<< 16)|(value5 <<8) |(value6)); count = count + 1; } else { count = 0; chk2 = 1; } CHIPSEL_BAR = 0; DRDY_BAR1.enable_irq();
} void doRead(void) { int dummy = 0 ;
while(!DRDY_BAR) ADS_START = 0;
value = mySpi.write(dummy) ;
}
void doLoop(void) { scanf("%d", &loop) ; printf("repeat number has been set to %d\n\r", loop) ; }
void cmdWrite(int data) { CHIPSEL_BAR = 0; mySpi.write(data) ; wait_ms(1); CHIPSEL_BAR = 1; }
void regWrite(int address, int data) { int data_to_send = CMD_WREG << 5; data_to_send = data_to_send | address; CHIPSEL_BAR = 0; mySpi.write(data_to_send); wait_ms(1); mySpi.write(0x00) ; wait_ms(1); mySpi.write(data) ; wait_ms(1); CHIPSEL_BAR = 1; }
void regRead(int address) { int data_to_receive = CMD_RREG << 5; data_to_receive = data_to_receive | address; CHIPSEL_BAR = 0; mySpi.write(data_to_receive); wait_ms(1); mySpi.write(0x00) ; wait_ms(1); data1 = mySpi.write(0x00) ;
printf("DEVICE ID register read from ADS is= 0x%X\n",data1);
wait_ms(1); CHIPSEL_BAR = 1; }