Sometime 10bit ADC is not enough for precious calculation. In that case we have 12bit ADC i.e. MCP3208.


SPI Configuration


Here B11-B0 is the 12bit ADC value. Thus the operating logic consist of MCP3208 consists of following steps-

Let’s use two function one is only write and other is read-write.
- void SPIWrite(uint8_t input) -> Only SPI write
void SPIWrite(uint8_t input)
{
SPDR=input;
while(!(SPSR&(1<<SPIF)))
{
;//wait until transmission complete
}
}
- uint8_t SPIReadWrite(uint8_t input) -> Both SPI write and read function
uint8_t SPIReadWrite(uint8_t input)
{
SPDR=input;
while(!(SPSR&(1<<SPIF)))
{
;//wait until transmission complete
}
return(SPDR);
}
So the full user-defined function for ADC read as follow-
uint16_t READAdc(uint8_t ch)
{
uint8_t dataHIGH,dataLOW,bytestream;
SPI_PORT&=~(1<<SPI_CS); //enable latch
bytestream=0b00000110;
if(ch>3)
{
bytestream=0b00000111;
}
SPIWrite(bytestream);
bytestream=(ch<<6);
dataHIGH=SPIReadWrite(bytestream); //send any bit to receved data
dataHIGH&=0b00001111; //4bit high value
dataLOW=SPIReadWrite(0xff); //send any bit to receved data
SPI_PORT|=(1<<SPI_CS); //disable latch
return((dataHIGH<<8)|(dataLOW));
}
Let’s make a Temperature meter with 12 bit ADC. If we use LM35 then it gives 10millivolts per degree centigrade. Since we use 12bit ADC than the resolution should be-
Resolution=5/212 =1.2207mV
So the Temperature = (12bit ADC return value) x 0.12207
Download the mcp3208.c & mcp3208.h file and the main program as follow-
#include<avr/io.h>
#include<stdio.h>
#include"mcp3208.h"
#include"lcd.h"
int main(void)
{
LCD_INIT();
ADCInitx();
float temp;
uint16_t ADCValue;
char LCD[20];
LCD_write_string(1,1,"ADC result Display");
while(1)
{
ADCValue=READAdc(3);
temp=ADCValue*0.12207;
sprintf(LCD,"Temperature=%.2f",(double)temp);
LCD_write_string(1,2,LCD);
}
return 0;
}








Visit Today : 50
Total Visit : 28465