MCP3208 is a 12-bit 8-channel SAR ADC. MCP3208 is interfaced to Arduino Uno to read ADC input. SD interface of Arduino is used to connect to MCP3208 ADC. Connectivity is shown below:
Below is the code for the above interfacing:
#define DATAOUT 11//MOSI
#define DATAIN 12//MISO
#define SPICLOCK 13//sck
#define CHIPSELECT 10//ss
byte ADC_output_data;
byte ADC_input_data=0;
byte clr;
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
}
void setup()
{
Serial.begin(9600);
pinMode(DATAOUT, OUTPUT);
pinMode(DATAIN, INPUT);
pinMode(SPICLOCK,OUTPUT);
pinMode(CHIPSELECT,OUTPUT);
digitalWrite(CHIPSELECT,HIGH); //disable device
spi_init();
delay(10);
digitalWrite(CHIPSELECT,LOW);
spi_transfer(0x06);
delay(10);
spi_transfer(0x00);
delay(10);
}
void loop()
{
ADC_output_data = read_ADC();
Serial.print(ADC_output_data,DEC);
Serial.print('\n');
delay(500);
digitalWrite(CHIPSELECT,HIGH);
delay(500);
digitalWrite(CHIPSELECT,LOW);
delay(10);
spi_transfer(0x06);
delay(10);
spi_transfer(0x00);
delay(10);
}
void spi_init()
{
// SPCR = 01010000
//interrupt disabled,spi enabled,msb 1st,controller,clk low when idle,
//sample on leading edge of clk,system clock/4 rate (fastest)
SPCR = (1<<SPE)|(1<<MSTR);
clr=SPSR;
clr=SPDR;
}
byte read_ADC()
{
//READ ADC
int data;
data = spi_transfer(0xFF); //get data byte
return data;
}
0 Comments