This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

ADC help

Can someone tell me why this code doesnt do its job? I am new to uVision. The code should turn on the LED diodes as you turn the potentiometer. For example, if its on position 0V the first LED should be lit, and if its on position 3.3V the last one. Tnx in advance

#include <stdio.h>
#include <stdlib.h>
#include <LPC214x.h>

void delay(int i){
unsigned int j;
for(j=0; j < i; j++);
}

int main (void) {
unsigned int rez;
IODIR1 = 0x00FF0000; //diode postavljene kao output
AD0CR = 0x00200402; //ADC1: 10-bit AIN1 @ 3MHz

while(1){ AD0CR |= 0x01000000; // start AD konverzije

while ((AD0DR1 & 0x80000000) == 0); // cekanje da se konverzija zavrsi

AD0CR &= ~0x01000000; //stop AD konverzije rez = AD0DR1 & 0x0000FFC0; if(rez < 128) IOSET1 = 1 << 16; else if(rez > 128 && rez < 256) IOSET1 = 1 << 17; else if(rez > 256 && rez < 256+128) IOSET1 = 1 << 18; else if(rez > 256+128 && rez < 512) IOSET1 = 1 << 18; else if(rez > 512 && rez < 512+128) IOSET1 = 1 << 19; else if(rez > 512+128 && rez < 768) IOSET1 = 1 << 20; else if(rez > 768 && rez < 768+128) IOSET1 = 1 << 21; else if(rez > 896 && rez < 1024) IOSET1 = 1 << 22; else IOSET1 = 1 << 23;

delay(100000);
}

}

Parents
  • Well, I am not very good at debuging (but I did my best). I put a breakpoint before the if cases, and when I look at the values of rez and val it sais they are 0x00000000 both. This makes sense, because when I run the program only the first LED is on (which is from case rez < 128).
    Another interesting fact is that the AD0DR1 register is also 0x00000000 (which gives the value to rez and val). Why is it so, I wouldnt know... :/

    Are my conversion steps right?
    1. I start the conversion
    2. Wait for it to finish
    3. Stop it

    And then I just read the results... It should work... :/

    Any ideas?

Reply
  • Well, I am not very good at debuging (but I did my best). I put a breakpoint before the if cases, and when I look at the values of rez and val it sais they are 0x00000000 both. This makes sense, because when I run the program only the first LED is on (which is from case rez < 128).
    Another interesting fact is that the AD0DR1 register is also 0x00000000 (which gives the value to rez and val). Why is it so, I wouldnt know... :/

    Are my conversion steps right?
    1. I start the conversion
    2. Wait for it to finish
    3. Stop it

    And then I just read the results... It should work... :/

    Any ideas?

Children