How to Interface SPI ADC with 8051

Tags: Circuit Diagram to Interface SPI ADC with 8051, C Program to interface SPI ADC using 8051,spi adc source code in c,interfacing analog to digital converter with 8051,
Overall rating
Interfacing SPI ADC with 8051
8051 Slicker Board

The 8051 Slicker board is specifically designed to help students to master the required skills in the area of embedded systems. The kit is designed in such way that all the possible features of the microcontroller will be easily used by the students. The kit supports in system programming (ISP) which is done through serial port.


NXP’s 8051 (AT89V51RD2), 8051 Slicker Kit is proposed to smooth the progress of developing and debugging of various designs encompassing of speed 8-bit Microcontrollers.


ADC (Analog to Digital Converter):

The Microchip Technology Inc. MCP3202 is a successive approximation 12-bit Analog-to-Digital (A/D) Converter with on-board sample and hold circuitry. The MCP3202 is programmable to provide a single pseudo-differential input pair or dual single-ended inputs. Differential Nonlinearity (DNL) is specified at ±1 LSB, and Integral Nonlinearity (INL) is offered in ±1 LSB (MCP3202-B) and ±2 LSB (MCP3202-C) versions. Communication with the device is done using a simple serial interface compatible with the SPI protocol. The device is capable of conversion rates of up to 100ksps at 5V and 50ksps at 2.7V.


Pin Assignment with 8051

 

SPI - ADC

8051 Lines

SPI - ADC

MCP 3202

CS

P2.0

8051-adc

CLK

P2.1  

MISO

P2.2

MOSI

P2.3

Make switch SW28 to ADC label marking position.

8051-adc-pin 


 


Circuit Diagram to Interface SPI ADC with 8051

Interfacing SPI ADC with 8051


Source Code

The Interfacing SPI ADC with 8051 program is very simple and straight forward, that uses a delay procedure loop based software delay. In C programs you cannot be sure of delay, because it depends on compiler how it optimizes the loops as soon as you make changes in the options the delay changes.


C Program to SPI ADC using 8051

 

#include<reg52.h>

#include<stdio.h>

 

sbit ADC_CS             = P2^4;

sbit ADC_CLK           = P2^5;

sbit ADC_DO           = P2^6;

sbit ADC_DI             = P2^7;

 

 

void InitSerial(void);

void write_adc_byte(char data_byte);

unsigned int ReadADC(unsigned char channel);

void DelayMs(unsigned int count);

 

//---------------------------------------

// Initialize serial port

//---------------------------------------

void InitSerial(void)

{

          SCON = 0x52;   // setup serial port control

          TMOD = 0x20;   // hardware (9600 BAUD @11.05592MHZ)

          TH1 = 0xFD;   // TH1

            TR1     = 1;               // Timer 1 on

}

 

//---------------------------------------

// read analog from ADC

// Single end mode(2 channel)

//---------------------------------------

unsigned int 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? 0xA0 : 0xD0;       //Refer Datasheet of MCP3202 - in Single Mode for Independent Channel Processing

               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);         

}

 

//---------------------------------------

// Delay mS function

//---------------------------------------

void DelayMs(unsigned int count)

{                                                                                                                      // mSec Delay 11.0592 Mhz

   unsigned int i;                                         

   while(count) {

       i = 115;

                        while(i>0) i--;

       count--;

   }

}

 

//---------------------------------------

// Main program

//---------------------------------------

void main(void)

{

            unsigned int CH0, CH1;

            InitSerial();              // Initialize serial port

            putchar(0x0C); // clear Hyper terminal

            while(1)

            {                                                                                                                          

 

                        CH0    =          ReadADC (0);

                        DelayMs (200);

                        printf("Ch 0 : %4d %cF     ", CH0/2, 176);

                        DelayMs (200);

                        CH1    =          ReadADC (1);

                        DelayMs (200);

                        printf("Ch 1 : %4d \r", CH1);

                        DelayMs(100);       

                        DelayMs(500);       

            }

}

 

Join the World's Largest Technical Community

we respect your privacy.