Increase your Embedded skills with our new online classes (from Just Rs750/-) for details click here

For all your embedded quires visit our new forum.embed4u.com

Archive

Posts Tagged ‘12 bit converter’

mcp3202 interface with 8051

September 3rd, 2011 Comments off

mcp3202 interface with 8051:

Description:
In this interface session we are interface mcp3202 adc interface with 8051 microcontroller. Mcp3202 is a 12bit spi protocol adc, is interface with P89v51rd2 microcontroller,this microcontroller have spi protocol so no need to worry about SDI,SDO,CLCK.
Interface mcp3202 to 8051 microcontroller CS is P2.2 and MOSI is connected to P1.5 and MISO is P1.6, SCK is P1.7 refer the P89v51rd2 datasheet and MCP3202 datasheet.In this session our kit LCD P0 is for Data and P3.6 is R/s Register Select and P3.7 is for E/N Enable pin.You have interface these adc is very easy just copy we code and compile using keil or Ride compiler without no errors. U have to only P89v51rd2 compiler because the spi protocol library function is there in the header file.

c code:

#include "p89v51rd2.h"
#include "stdio.h"
#include "string.h"
#define LCD_clear() LCD_command(0x01)	/* Clear display LCD */
#define LCD_origin() LCD_command(0x02)	/* Set to origin LCD */
#define LCD_row1() LCD_command(0x80)	/* Begin at Line 1 */
#define LCD_row2() LCD_command(0xC0)
sbit rs = P3^6;
sbit en = P3^7;
sbit cs   = P2^2;

void delay ();
void display(int name1);
void spi ();
//****************************************************************************//
void lcd_en ()
{
//		rw=0;
en  = 1; en  = 1; en  = 1;
en  = 0; en  = 0;
}
void LCD_delay(unsigned char ms)
{
unsigned int n;
unsigned int i;
for (n=0; n 	{
for (i=0; i<1535; i++); /* For 1 ms */
}

}
void LCD_command(unsigned char command)
{
rs=0; rs = 0;
P0		= command;
lcd_en ();
LCD_delay(1);

}

void LCD_init()
{
LCD_command(0x38);
LCD_command(0x06);
LCD_command(0x0c);
LCD_command(0x01);
}

void LCD_putc(unsigned char ascii)
{
rs=1;
P0		= ascii;
lcd_en ();
LCD_delay(2);
}

void LCD_puts(unsigned char *lcd_string)
{
while (*lcd_string)
{
LCD_putc(*lcd_string++);
}
}

void display(int name1)   //LCD display routine for digits display
{

rs = 1;
P0 = name1;
lcd_en();
LCD_delay(10);
}
void delay1()
{
int i =20000;
while(i--);
}

//**************************************************************************//
void spi ()
{
SPCTL = 0X5C;
}
void main (void)
{
char c,i;
int e=0;
LCD_init();
spi ();
cs = 0;
while(1)
{
SPDAT = 0XC0;
while (SPCFG != 0X80);
SPCFG = 0;
c = SPDAT;

SPDAT = 0XFF;
while(SPCFG != 0X80);
SPCFG = 0;
i  = SPDAT;
cs = 1;
cs = 1;
cs = 0;

e = c;
e = e<<8;
e = e | i;
e = e<<1;
e = e & 0x0fff;
LCD_puts("MCP3202 ADC");//display in LCD
LCD_command(0xc0);
delay1();
{
display(((e%10000)/1000)+48);   // ,,
display(((e%1000)/100)+48);    // ,,
display(((e%100)/10)+48);     // ,,
display((e%10)+48);          // ,,
}
delay1();
LCD_delay(500);
LCD_command(0xc5);
LCD_command(0x01);

printf("%d\n",e);
}

Circuit Diagram: