Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronic Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Register Log in

Help needed in using AT89C4051 as an ADC

Status
Not open for further replies.

vinash

Member level 2
Joined
Nov 3, 2005
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,999
Hi,
I am using AT89C4051 to utilize it as an AD Converter for an application as a two digit Voltmeter. I am following the circuit design given in the following address:
h**p://www.atmel.com/dyn/resources/prod_documents/DOC0524.PDF .
I have written a code in C and it does not work. It would be very helpful if someone were to help me if there are any mistakes in my code. My code is:

#include<AT89x051.h>
#define TON 79;
#define TOFF 79;

unsigned int ta,tb;
char a[10]={0x07,0x17,0x27,0x37,0x47,0x57,0x67,0x77,0x87,0x97};
char b[10]={0x0B,0x1B,0x2B,0x3B,0x4B,0x5B,0x6B,0x7B,0x8B,0x9B};
scanled();

void main(void)
{
int td,tc,m;
tc=TON;
td=TOFF

while(1){

P3_7=1;
for(m=0;m<tc;m++){
if(P3^6==0)
ta++; }


P3_7=0;
for(m=0;m<tb;m++){
if(P3^6==1)
tb++;
}

scanled();

}

}

scanled(){
unsigned int i,flag,j;


if(tb==0){

for(i=0;i<2;i++){
flag=1-flag;
if(flag==1){
P1=a[(ta)/10];
for(j=0;j<32000;j++);
P1=0xFF;
}

if(flag==0){
P1=b[(ta)%10];
for(j=0;j<32000;j++);
P1=0xFF;
}


}

}

else if(ta==0){
for(i=0;i<2;i++){
flag=1-flag;
if(flag==1){
P1=a[(tb)/10];
for(j=0;j<32000;j++);
P1=0xFF;
}

if(flag==0){
P1=b[(tb)%10];
for(j=0;j<32000;j++);
P1=0xFF;
}


}

}
}





I really hope someone would be able to assist me as i have been desperately doing this for the past weeks without any yield. Thank you so much
 

Jack// ani

Advanced Member level 3
Joined
Dec 2, 2004
Messages
759
Helped
106
Reputation
220
Reaction score
58
Trophy points
1,308
Activity points
5,018
Which A/D convertor are you using??
 

vinash

Member level 2
Joined
Nov 3, 2005
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,999
Hi,
I am not using an external AD converter.I am trying to utilize the analog comparator in AT89C4051
 

silvio

Advanced Member level 3
Joined
Dec 31, 2001
Messages
716
Helped
105
Reputation
210
Reaction score
16
Trophy points
1,298
Location
Romania
Activity points
11,497
Hi,

You wrote:

P3_7=0;
for(m=0;m<tb;m++){
if(P3^6==1)
tb++;

If it's not a typewriter mistake, the "tb" wrote in red color shouldn't be "td" ?.

Did you read carefully the doc ?

It's clearly stated that before doing any display, the value must be adjusted according to a lookup table.
I don't see this in your code.
It's true that you used two array for either charge or discharge portions of the measurement cycle (ta or tb).
But it's useless as long as the charging and discharging of the capacitor it's not linear.

Are you sure that :

1. when you compile the code, the value of XTAL is 12 Mhz ? Just because all delays are strong related to that assumption.
I mean the value of TON 79 is defined as the minimum number of samples which must be taken to guarantee that the voltage on the capacitor has reached Vcc/2.
More, the maximum conversion time is 7ms (sample interval = 5us) according to the choosen values of resistor = 267 kilohms; capacitor = 2 nanofarads; Vcc = 5.00 Volts.

2. I don't see in your code the additional NOPs mentioned in the original code used to delay the first
sample in the charge and discharge portions of the measurement cycle and as a result an effect on mesurement accuracy (why making worst than it is with this kind of measurement)

3. When you get into scanled() routine, are you sure that the value of "flag" is either 0 or 1 ? What would happen if has a random value when "flag=1-flag;"
 

    vinash

    points: 2
    Helpful Answer Positive Rating

vinash

Member level 2
Joined
Nov 3, 2005
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,999
Hi,
Thanks for pointing out the mistake of tb.

As for the lookup table, i am not using the lookup table. I just want to know the number of counts. From the number of counts, i could refer to the lookup table manually and get the voltage output.

Thanks for your suggestion on the 7ms interval, i forgot to include that.

Thank you for your suggestions....
 

vinash

Member level 2
Joined
Nov 3, 2005
Messages
45
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,999
HI silvio,
I have done the changes as you had stated, but i still am not able to get it working...Pls do help...My corrected code is below for your reference.

#include<AT89x051.h>
#define TON 79;
#define TOFF 79;

unsigned int ta,tb;
void delay(int count);
char a[10]={0x07,0x17,0x27,0x37,0x47,0x57,0x67,0x77,0x87,0x97};
char b[10]={0x0B,0x1B,0x2B,0x3B,0x4B,0x5B,0x6B,0x7B,0x8B,0x9B};
scanled();

void main(void)
{
int td,tc,m;
tc=TON;
td=TOFF

while(1){

P3_7=1;
for(m=0;m<tc;m++){
if(P3&0x40==0)
ta++;
}
delay(3);

P3_7=0;
for(m=0;m<td;m++){
if(P3&0x40==1)
tb++;
}
tb=tb+td;
delay(3);

scanled();

}

}

scanled(){
unsigned int i,flag=0,j;
static unsigned int k,h;
k=ta;
h=tb;

if(tb==0){

for(i=0;i<2;i++){
flag=1-flag;
if(flag==1){
P1=a[(k)/10];
for(j=0;j<32000;j++);
P1=0xFF;
}

if(flag==0){
P1=b[(k)%10];
for(j=0;j<32000;j++);
P1=0xFF;
}


}

}

else if(ta==0){
for(i=0;i<2;i++){
flag=1-flag;
if(flag==1){
P1=a[(h)/10];
for(j=0;j<32000;j++);
P1=0xFF;
}

if(flag==0){
P1=b[(h)%10];
for(j=0;j<32000;j++);
P1=0xFF;
}


}

}
}

void delay(int count)
{
int j,l;
for(j=0;j<= count;j++)
{
for (l=0;l<=120;l++);
}
}

Really do appreciate your help. Thank you.
 

Status
Not open for further replies.
Toggle Sidebar

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top