Home     Projects     Micro     Tools     Delphi     Developer     Site map
HOME  
 

MCS-51 | PIC


Connecting AT89S52 with 12 bit ADC 2 channel

This is example how to interface MCS51 family with MCP3202 which is a 2 channel ADC that
is the ADC device from Microchip.

MCP3202

MCP3202 is a succesive approximation 12 bit ADC with on-board sample and hold circuitry.The MCP3202 is programmable to provice a single pseudo differential input pair or dual single-end inputs like ADC0832 from National semiconductor.Communication with this device is done using a simple serial interface compatible with the SPI protocal.



MCP3202 Timing for MSB first format





MCP3202 Timing for LSB first format



From the figure above to read data from ADC. First, we must send 4 bit command while 3 bit for ADC0832 then read 12 bit data from ADC.MSB first for both command and data. The extra bit is MSBF bit use to enable the LSB first format for result of ADC.If the MSBF bit is high, then the data come from device in MSF first format.If the MSBF is low,then the data from device will output the converted word LSB first after the word has been transmited in the MSB first format. See the two above figure.

Also,you have 2 choice to use MCP3202.One for single pseudo differential input pair and one for dual single-end inputs.

Configurations bit for MCP3202




Interfacing with MCS-51

In this example I use KEIL C51 software for programming the AT89S52 to interfae with ADC0832.The result of reading ADC will send to serial port and display with Hyper terminal program on the Windows or other serial port monitor software.



MCP3202 use a simple serial I/O interface compatible with SPI protocol.We can coding the routine for read the data from MCP3202 as follwing:

Read ADC function dual single-end inputs MSB first Mode (KEIL C51 V7.5)
sbit ADC_CS 	=  P2^0;
sbit ADC_CLK 	=  P2^1;
sbit ADC_DO 	=  P2^2;
sbit ADC_DI 	=  P2^3;

//---------------------------------------
// read analog from ADC
// Single end MSB first mode(2 channel)
//---------------------------------------
char ReadADC(unsigned char channel)
{
   unsigned char i,k;
   unsigned int AdcResult;  // 12 bit

   ADC_CS=0;  	// Active chip select
   k++;			// Delay about 1 uS
   ADC_CLK=0; 	// make clock low first   
   k++;k++;	
   channel = channel? 0xF0 : 0xE0;
   k++;k++;  		// delay about 2 uS
   //--- write command 4 bit ----------
   for(i=0; i< 4;i++) {
      ADC_DI = (channel & 0x80) != 0;
      channel<<=1;
      ADC_CLK=1;
      k++;k++;		// delay about 2 uS
      ADC_CLK=0;
   }

   k++;k++;  		// delay about 2 uS
   ADC_CLK=1;
   k++;k++;  		// delay about 2 uS
   ADC_CLK=0;
   k++;k++;  		// delay about 2 uS 

   //--- read ADC result 12 bit --------
   AdcResult=0;
   for(i=0;i<12;i++) {      
      ADC_CLK=1;
      k++;k++;  		// delay about 2 uS 
      AdcResult<<=1;
      AdcResult=AdcResult | (ADC_DO & 0x01);	        
      ADC_CLK=0;
      k++;k++;  		// delay about 2 uS
   }
   ADC_CS=1;
   return(AdcResult);	
}

The result of this example show as the following figure



Download KEIL C51 example
   

Copyright(c) 2005-2010 sixca.com, All rights reserved.
Best view @ 800X600, IE 6.0 up    Terms of Use  Privacy Policy