2014年3月6日木曜日

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

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

AD 8ch module has MCP3208.
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 read12bit8chAD(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 read12bit8chAD(int spichan,char ch){
 
 int value = 0;
 unsigned char spiBuff[3];
 
 if(ch < 8){
  spiBuff[0] = 0x18 + ch;
  spiBuff[0] = (spiBuff[0] << 2);
  spiBuff[1] = 0;
  spiBuff[2] = 0;
   
 }else{
  return -1;
 }
 
 wiringPiSPIDataRW(spichan,spiBuff,3);
 value = spiBuff[1] << 8;
 value = spiBuff[2] + value;
 value = value >> 4;
 
 return value; 
}

0 件のコメント:

コメントを投稿