Help me write a C code for A/D sampling

Status
Not open for further replies.

patrick

Junior Member level 1
Joined
Apr 20, 2004
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
161
C Code for A/D Sampling

Hi there,
Can anyone please help me to write a C code to get the ADC to make a sample for a data acquisition system which has a successive approximation ADC.

The following is requirements:

The ADC control port is at address x7851.
ADC status port is at address x7852.
ADC data port is at address x7853.
Start conversion is bit 3 of control port.
End conversion is bit 0 of the status port.
The control port is bidirectional
The control port is preconfigured;
I dont need to change any other control port bits when starting conversion.
The code should some form of bail-out in case the ADC fails to convert
I am aksing in general, not any specific MCU.
Thanks
 

Re: C Code for A/D Sampling

Hi, thanks for your advices. Here is what I tried:
Code:
int * ADControl = 0x7851;
int * ADStatus = 0x7852;
int * ADdata = 0x7853;
int counter = 0;

void main(void)
{
 ADControl.3 = 0;
 ADStatus.0 = 0;

 startconv();
 getresult();
}
// assume the max of conversion is 256, this is to catch arror
void startconv(void)
{
 while(ADStatus.0 ==0 && counter <=256)
 {
  delay100ms();
  counter ++;
 }
 counter = 0;
}

// assume result variable is where conversion is stored
void getresult(void)
{
result = ADdata;
}
Please correct me if I am wrong.
Thanks
Pat
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…