Other Parts Discussed in Thread: ADS8684, ADS7825, ADS8684A
Hello,
I am interfacing ADS 7825 with P89V51RD2 in serial mode. And I want to see the converted analog value on 16*2 LCD. For i have written a program but i am unable to get the proper output? I have attached the code.
Please help me out.
#include <stdlib.h>
#include <p89v51rd2.h>
#include <stdio.h> // sprint function requires this stdio.h lib
#include <intrins.h> // to use nop instruction in c
#include <math.h>
#include <string.h>
//#include <lcd.h>
#define databus P3
sfr16 RCAP2= 0xCA; // timer 2 address defined
sbit an0=P0^6;
sbit an1=P0^7;
//sbit ext_int= P1^0;
sbit busy=P1^1;
sbit readconvt=P1^2;
sbit sclk=P1^7;
sbit ss = P1^4;
sbit miso = P1^6;
sbit parser =P3^0;
//lcd bits--------
sbit rs = P0^1;
sbit rw = P0^2;
sbit en = P0^0;
//------------------------------------------
char AsciiData1[12];
void lcd_init();
void lcd_cmdwrite(unsigned int dat);
void lcd_datawrite(unsigned char dat);
void lcd_delay(int x);
void lcd_displaystring(char *string_ptr);
//-------------------------------------------
void lcd_delay(int x)
{
int i,j;
for(i=0;i<x;i++)
for(j=0;j<1275;j++);
}
void lcd_cmdwrite(unsigned int dat)
{
databus=dat;
rs = 0;
rw = 0;
en = 0;
en = 1;
lcd_delay(1);
en = 0;
lcd_delay(1);
}
void lcd_datawrite(unsigned char dat)
{
databus=dat;
rs = 1;
rw = 0;
en = 0;
en = 1;
lcd_delay(1);
en = 0;
lcd_delay(1);
}
void lcd_init()
{
P2=0x00;
lcd_cmdwrite(0x30);
lcd_cmdwrite(0x30);
lcd_cmdwrite(0x30);
lcd_cmdwrite(0x0E);
lcd_cmdwrite(0x01);
lcd_cmdwrite(0x06); //CLEAR DISPLAY
lcd_cmdwrite(0x80); //ENTRY MODE
}
//char lcd_displaystring(unsigned char *string_ptr)
//{
// while (*string_ptr)
// lcd_datawrite(*string_ptr++);
//}
//char display(const char *a) //string display
//{
// while(*a!='\0')
// lcd_datawrite(*a++);
//}
void Serial_Config(unsigned int baud_rate)
{
RCAP2 = -(11059200/32/baud_rate);
SCON = 0x50; //mode 1 receive enabled
RCLK = 1; //Timer2 in baud rate
TCLK = 1; //Disable Tx clock , Enable only when required
}
void SerialOut1(char AsciiData1)
{ TR2=1;
SBUF = AsciiData1;
while(!TI);
TI=0;
TR2=0;
}
void dq1(char *AsciiData1, float dev)
{
sprintf( AsciiData1, "%4.8f",dev); // string printf
}
//--------------------------------------------
void spi_config()
{
SPCTL=0x53; //SPI enable bit=1; MSTR bit=1 (to configure it as master) and frequency for SPI comm. = (F)osc/8
//SPCFG=0X00;
//SPDAT=0X00; //initialize the SPI data register to 0X00
}
void send_value_flt(float vl)
{
unsigned int j,tmp;
unsigned char buff[5]={0,0,0,0,0},k=0;
float tmp_vl=0;
j=(int)vl; //vl=11.256 tmp=11
k=0;
while(j!=0)
{
buff[k++]=(j%10);
j/=10;
}
//lcd_datawrite( (buff[3]+'0') );
// lcd_datawrite( (buff[2]+'0') );
lcd_datawrite( (buff[1])+0x30 );
lcd_datawrite( (buff[0])+0x30 );
tmp=(int)vl; //11.25 -> 11
tmp_vl=(float)tmp; //11 -> 11.00
vl=(vl-tmp_vl); //11.250-11.000 => .250
j=(int)(vl*1000); // (11.256-11.000) = 250
lcd_datawrite('.');
buff[0]=0;
buff[1]=0;
buff[2]=0;
buff[3]=0;
buff[4]=0;
k=0;
while(j!=0)
{
buff[k++]=(j%10);
j/=10;
}
lcd_datawrite( (buff[2])+0x30 );
lcd_datawrite( (buff[1])+0x30 );
lcd_datawrite( (buff[0])+0x30 );
}
//-----------------------------------
float read_adc()
{
unsigned int i;
unsigned char d1,n;
unsigned int d4;
long float sum1=0;
float averaged_adcout1=0,adcout1=0;
//long float sum1=0;
//float adcout1=0;
// ss=1;
// lcd_delay(1);
// ss=0;
// lcd_delay(1);
readconvt = 1;
for (n=0;n<100;n++)
{
sclk=0;
lcd_delay(1);
//ext_int=1; // make ext/int bar high to enable external data clk
readconvt = 0;
//lcd_delay(1); //falling edge of r/c
//check busy
while (busy!=0);
//imp** make readconvt high here AFTER .04 TO 12uSEC VVIMP
readconvt = 1;
//lcd_delay(1);
while (busy==0);
//readconvt = 1;
//lcd_delay(1);
SPDAT = 0xE7;
for(i=0;i<3;i++);
//lcd_delay(1);
//while(SPCFG!=0x80);
// if SPIF set indicating end of 8 bits of data, make it 0 and transfer data elsewhere. // make chip select go high to stop transmission of further data.
d4=0x0000;
d1=SPDAT;
d4=d1;
d4=d4<<8; // making d1 occupy the MS Byte
d4= d4 & 0xFF00; // making lower byte of d4 zero
SPCFG=0x00;
SPDAT= 0xCA;
//lcd_delay(1);
//while(SPCFG!=0x80);
for(i=0;i<3;i++);
//SPCFG=0x00;
d1=SPDAT;
d4= d4 | d1;
d4=d4<<1; // LEFTshifting d4 to make space for last bit
SPCFG=0x00;
SPDAT= 0xF8;
////lcd_delay(1);
////while(SPCFG!=0x80);
for(i=0;i<2;i++);
d1=SPDAT;
d1=d1 & 0x80; // anding to make 0, all other bits of d1 except the MSB
d1=d1>>7; // making the MSB of d1, its LSB
d1= d1&0x01; // to confirm only the last bit is 1;
d4= d4|d1; // adding the last bit;
SPCFG=0x00;
//if d2 is MSB is 1, then invert all bits and add 1. and the corresponding decimal
// will start with a negative sign.
if ((d4 & 0x8000)==0x8000)
{
d4=~d4;
d4=d4+1;
adcout1=-(d4/32767.0f)*10.0f;
//lcd_datawrite("-");
}
else {
adcout1=(d4/32767.0f)*10.0f;
sum1=sum1+adcout1;
}
//send_value_flt(adcout1);
}
averaged_adcout1=(sum1/100.0f);
return averaged_adcout1;
}
void main()
{
// P0=0XFF;
//P1=0xFF;
//P3=0XFF;
//P2=0XFF;
float A=0.00;
unsigned int j;
//char AsciiData1;
//busy = 1;
//miso = 1;
lcd_init();
spi_config();
Serial_Config(9600); //setup the master configuration
//parser=0;
an0= 0;
an1= 0;
while(1)
{
lcd_cmdwrite(0x02);
A=read_adc();
// send_value_flt(A);
dq1(AsciiData1, A);
//
//SerialOut1(AsciiData1[1]);
//SerialOut1(AsciiData1[2]);
for(j=0;j<9;++j)
{ lcd_datawrite(AsciiData1[j]);
//SerialOut1(AsciiData1[j]);
}
SerialOut1(A);
// lcd_datawrite(AsciiData1[4]);
// lcd_datawrite(AsciiData1[5]);
SerialOut1(0x0a);
SerialOut1(0x0D);
//lcd_delay(1000);
}
while(1);
}
Thanks.
