Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

C code for ADC sampling

Status
Not open for further replies.

patricktran

New Member
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. This is not for any specific MCU, but in general.

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

Thanks
 
This should be fairly easy.


Since this sounds like homework I'm not going to give you any code but here's what you need to do:

Set the start bit. Then while the end conversion bit isn't set you just loop. When the loop finishes then the conversion is complete. To catch a converter failure you just put a counter in the loop and generate a failure condition when the counter reaches a value that higher than it should have gotten to with normal converter operation.

To write the memory locations in C the compiler will usualy have a keyword like _at_ that lets you put a variable at a specific location. If you dont have that you can dereference a pointer to get that memory location.

int* Status_Port = 0x7851;
(*StatusPort)= Write_value;

Hope this helps
Brent
 
There is no "general" ADC procedure, each one has specific procedures. Check a spec sheet. The conversion itself always involves setting register bits and reading result regs. Pick a processor and a task and we can give advice.
 
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
 
Your idea is correct but there are a couple weird things about your code.

The ADControl.3 notation only works for bit addressable memory - it's not standard C. The pointer derefrencing trick I talked about earlier doesn't really make sense if you are going to treat the AtoD registers as if they are bit addressable (especially with 16 bit memory locations). Most compilers have special extensions to the C language that let you define this sort of bit addressable memory. Keil uses "sfr" as the declaration. This is one of the reasons Oznog is saying that there really isn't a generic AtoD converter routine.

You don't need the delay100ms() - all it would do is slow down your capture speed. Presumably the A to D is significantly faster than 100 ms. You're better off just making counter larger. This way you test to see if the conversion is done much more often.

Also there really isn't any reason to break the AtoD routine into the startconv() and getresult(). Having one function that returns the value from the converter makes much more sense. If you had an interrupt driven system (or a more complicated polling routine) then splitting the two would make sense but in this case it just complicates things.

Hope this Helps
Brent
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top