1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
|
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>
#include <time.h>
#include <wiringPi.h>
#include <wiringPiSPI.h>
#define CS_MCP3208 8
#define SPI_CHANNEL 0
#define SPI_SPEED 100000000
int read_mcp3208_adc(unsigned char adcChannel)
{
unsigned char buff[3];
int adcValue = 0;
buff[0] = 0x06 | ((adcChannel & 0x07) >> 7);
buff[1] = ((adcChannel & 0x07) << 6);
buff[2] = 0x00;
digitalWrite(CS_MCP3208, 0);
wiringPiSPIDataRW(SPI_CHANNEL, buff, 3);
buff[1] = 0x0F & buff[1];
adcValue = ( buff[1] << 8) | buff[2];
digitalWrite(CS_MCP3208, 1);
return adcValue;
}
int main (void)
{
int adc1Channel = 0;
int adc1Value = 0;
int adc2Channel = 1;
int adc2Value = 0;
int adc3Channel = 2;
int adc3Value = 0;
if(wiringPiSetup() == -1)
{
fprintf (stdout, "Unable to start wiringPi: %s\n", strerror(errno));
return 1 ;
}
if(wiringPiSPISetup(SPI_CHANNEL, SPI_SPEED) == -1)
{
fprintf (stdout, "wiringPiSPISetup Failed: %s\n", strerror(errno));
return 1 ;
}
FILE *fp;
fp = fopen("mesureEDP.csv", "a");
if(fp == NULL){
printf("cannot open the file");
return -1;
}
fprintf(fp, "Ecg Resp1 Resp2\n");
pinMode(CS_MCP3208, OUTPUT);
while(1)
{
adc1Value = read_mcp3208_adc(adc1Channel);
adc2Value = read_mcp3208_adc(adc2Channel);
adc3Value = read_mcp3208_adc(adc3Channel);
printf("ECG = %d\t", adc1Value);
printf("Resp1 = %d\t", adc2Value);
printf("Resp2 = %d\n", adc3Value);
fprintf(fp, "%d\t", adc1Value);
fprintf(fp, "%d\t", adc2Value);
fprintf(fp, "%d\n", adc3Value);
system("clear");
usleep(1000);
}
fclose(fp);
return 0;
} |
Partager