2014年3月6日木曜日

Raspberry Pi 12bit 2ch AD Module (Manual Mode with wiringPi)

Raspberry Pi 12bit 2c AD Module (Manual Mode with wiringPi)

AD 2ch module has MCP3202.
You can use it easily, just put these functions on your main.c file or make header file and link these.
void setIOSimpleSPI(MCUGear *mcugear);
int read12bit2chAD(int spichan,char ch);

void setIOSimpleSPI(MCUGear *mcugear){
 //for AD 2ch, AD 8ch and multifunction module
 //1:MISO 13
 //2:MOSI 12
 //3:SCK  14
 //4:CE0  10
    mcugear->setWire(IO_13, IO_REG_IN_DIR, 0);
    mcugear->setWire(IO_12, IO_REG_OUT_DIR, 1);
    mcugear->setWire(IO_14, IO_REG_OUT_DIR, 2);
    mcugear->setWire(IO_10, IO_REG_OUT_DIR, 3);
 
}
int read12bit2chAD(int spichan,char ch){
 
 int value = 0;
 unsigned char spiBuff[3];
 
 if(ch == 0)
  spiBuff[0] = 0x18;
 else if(ch == 1)
  spiBuff[0] = 0x1C;
 else
  return -1;
 
 spiBuff[1] = 0x00;
 spiBuff[2] = 0x00;
 
 wiringPiSPIDataRW(spichan,spiBuff,3);
 //printf("%x %x %x\n",spiBuff[0],spiBuff[1],spiBuff[2]);
 
 value = (spiBuff[1] <<4 data-blogger-escaped-spibuff="">>4);
 //printf("%d\n", value);
 return value;
 
}

0 件のコメント:

コメントを投稿