Microcontroller › 8051 › Query about MCP3201 and 8051 communication – program is given
- This topic has 4 replies, 3 voices, and was last updated 11 years, 4 months ago by Sanket.
-
AuthorPosts
-
April 20, 2013 at 5:37 am #4808SanketParticipant
Dear sir/madam
In my following program I established complete SPI communication between 8051 & MCP3201, using SPI mode 00 (clock idles low) principle. Program works very well, but it has some problem. MCP gives counts ranging from 0000 to 4096 for 0% to 50% of POT position. For 50% to 100% POT position MCP again repeats counts ranging from 0000 to 4096. But i want count from 0000 to 4096 for the POT position from 0% to 100%. So what is the remedy to do that?
If anybody knows then please reply.
I have used keil v4 compiler and proteus 7.6 simulator.
/********PROGRAM*********/
#include <reg51.h>
#include <stdio.h>
#include <string.h>sbit cs = P1^2;
sbit datao = P3^1;
sbit clko = P1^0;#define lcdport P0 // LCD databus connected to PORT0
sbit rs = P2^0; // Register select pin connected to P0.0
sbit rw = P2^1; // Read Write pin connected to P0.1
sbit en = P2^2; // Enable pin connected to P0.2void lcd_ini();
void lcdcmd (unsigned char);
void lcddata (unsigned char);
unsigned int x, y, i = 0;
unsigned char temp_reg = 0;union
{
char databyte[2]; // declare temp array for adc data
unsigned int result; // declare integer for adc result
} adc;//***********ADC routine*********/
void Read_Adc( void )
{
cs = 0;
for(y=0;y<8;y++)
{
clko = 1;
clko = 0;
temp_reg <<= 1;
temp_reg |= datao & 0x01;
}adc.databyte[0] = temp_reg;
temp_reg = 0;
for(x=0;x<8;x++)
{
clko = 1;
clko = 0;
temp_reg <<= 1;
temp_reg |= datao & 0x01;
}
cs = 1;
adc.databyte[1] = temp_reg;
temp_reg = 0;
adc.result >>= 1; // adjust composite integer for 12 valid bits
adc.result &= 0x0FFF; // mask out upper nibble of integer
}//***************delay routine*****************//
void __delay_us(unsigned int us_count)
{
while(us_count!=0)
{
us_count–;
}
}void __delay_ms(unsigned int ms_count)
{
while(ms_count!=0)
{
__delay_us(112); //delay_us is called to generate 1ms delay
ms_count–;
}
}/******************************
MAIN CODE BEGINS HERE
******************************/
void main(void)
{
unsigned int convert_value = 0;
lcd_ini();while(1)
{
Read_Adc(); // initiate MCP3201 conversion and read result
convert_value=adc.result;
lcdcmd(0x88);
lcddata(((convert_value%10000)/1000)+48); __delay_ms(10);
lcddata(((convert_value%1000)/100)+48); __delay_ms(10);
lcddata(((convert_value%100)/10)+48); __delay_ms(10);
lcddata((convert_value%10)+48); __delay_ms(10);
}
}void lcd_ini()
{
lcdcmd(0x38); __delay_ms(10); // Configure the LCD in 8-bit mode, 2 line and 5×7 font
lcdcmd(0x0C); __delay_ms(10); // Display On and Cursor Off
lcdcmd(0x01); __delay_ms(10); // Clear display screen
lcdcmd(0x06); __delay_ms(10); // Increment cursor
lcdcmd(0x80); __delay_ms(10); // Set cursor position to 1st line, 1st column
}void lcdcmd (unsigned char cmdout)
{
lcdport=cmdout; //Send command to lcdport=PORTB
rs=0;
rw=0;
en=1;
__delay_ms(10);
en=0;
}void lcddata (unsigned char dataout)
{
lcdport=dataout; //Send data to lcdport=PORTB
rs=1;
rw=0;
en=1;
__delay_ms(10);
en=0;
}July 22, 2013 at 4:48 am #10174shaileshParticipantafter many research in your program find out error……
remove rotation of bit in stored result at 15…..or make simple routine like
//Routine for spi data read from mcp3201.
unsigned int Get_ADC_Sample(void)
{
unsigned int TempCount;
unsigned int Temp_Data=0;
SPI_CS_PIN=1;
for(TempCount=0;TempCount<16;TempCount++) //counter for 16 databits
{
SPI_CLK_PIN=0;
SPI_CS_PIN=0;
SPI_CLK_PIN=1;
if(TempCount!=15)
Temp_Data=Temp_Data<<1;
if(SPI_DATA_PIN==1)
{Temp_Data = Temp_Data | 0x01;}
}
SPI_CS_PIN=1; //keep high when not doing the conversion.
Temp_Data &=0x0fff; //mask upper 4 bits.
return Temp_Data;
}
July 22, 2013 at 5:05 am #10175AJISH ALFREDParticipantHi Sanket,
Why don’y you try it on real hardware?
August 1, 2013 at 4:57 pm #10256SanketParticipantThank you shailesh,
i must say you have taken very great efforts to my program.
it helps me a lot in solving my query……
after understanding your logic, i found the problem with my logic..
thanks again….
August 1, 2013 at 5:01 pm #10257SanketParticipantHello ajish,
As per your opinion with thistopic, i will definately try this code with real hardware as early as possible.
I’ll get back to you with my result soon.
thanks
-
AuthorPosts
- You must be logged in to reply to this topic.