10 years, 1 month ago.

finite loop that is only looping once instead of 500 times

HI guys, I'm working on the LPC1768. trying to make a loop iterates 500 times reading the 5 ADC channels and spitting out the results in serial. My obvious choice was a for loop, but it is not working as intended, it's only reading all 5 channels and then it stops, short changing me with 499 solid loops. Could any of you lovely people please throw a kind suggesting my way as to how best I could tackle this. Thanks a lot.

include the mbed library with this snippet


#include "mbed.h"


/*
adc input using control register
*/

Serial pc(USBTX, USBRX);

// variable declarations
char channel;           // ADC channel 1
int ADCdata;                    // this will hold result of the conversion
int DigOutData = 0;             // a buffer for the output display pattern
int ADCout[4];
int count = 0;
// function prototype
void delay(void);

// define addresses of control registers, as pointers to volatile data
//(i.e the memory contents)




#define PINSEL3     (*(volatile unsigned long *)(0x4002C00C))
#define PINSEL1     (*(volatile unsigned long *)(0x4002C004))
#define PCONP       (*(volatile unsigned long *)(0x400FC0C4))
#define ADOCR       (*(volatile unsigned long *)(0x40034000))
#define ADOGDR      (*(volatile unsigned long *)(0x40034004))
#define FIO2DIR0    (*(volatile unsigned long *)(0x2009C040))
#define FIO2PIN0    (*(volatile unsigned long *)(0x2009C054))

int main()
{
    
    // intilialize the ADC
    PINSEL1 = 0x150000;        // set bits 17-16, 19-18, 20-21 to 01 to enable AD0.1, AD0.2, AD0.3 (mbed pin 16,17,18)
    PINSEL3 = 0xF0000000;       // sets bits 29-28, 31-30 to 11 to enable AD0.4, AD0.5 (mbed pin 19, 20)
    PCONP   |=  (1 << 12);       // enable ADC clock 

    while(1)
    {
        for(count = 0; count <= 500; count ++)
        {
            for(channel =1; channel <=5; channel += 1)
            { 
            
            
                ADOCR   = ( 1 << channel)|( 1 << 8)|(0 << 16)|(1<<21)|(1<<24);      // enable ADC clock, select channel, divide incoming by (1+1), giving 4.8MHz, BURST = 0, conversation under software control, PDN =1, enables power, START = 1 start A/D conversion now
                ADOCR = ADOCR | 0x01000000; // start conversion by setting bit 24 to 1 by ORing
                                    // wait for the conversion to finish by polling the ADC DONE bit

                while((ADOGDR & 0x80000000) == 0)
                    {
                        // test DONE bit, wait till it's 1
                    }
                ADCdata = ADOGDR;           // get data from ADOGDR
                ADOCR &= 0xF8FFFFFF;        // stop ADC by setting START bits to zero
                                            // shift data 4 bits to right justify, and 2 more to give 10-bit ADC
                                            // value given a between 0 & 1024 = 2^8
                ADCdata=(ADCdata>>6)&0x03FF;    // and mask
                DigOutData = 0x00;              // clear the output buffer

                //displaying the data
                pc.printf("ADC reading in channel %d is %d  \n", channel, ADCdata);
                ADCout[channel-1] = ADCdata;        // write read data to array for output
            }   
        
            channel = 1;
        }
    }    
}

Does it work without your custom ADC stuff (or just with AnalogIn)? Your loop looks correct, so I wonder if it for example hangs on the while part.

posted by Erik - 23 Feb 2014

1 Answer

10 years, 1 month ago.

You have 5 channels but ADCout is only a 4 element array. Writing to ADCout[4] will overwrite the next variable which happens to be count.

Accepted Answer

heh you are right, that is a 'funny' way to break your code :P.

posted by Erik - 23 Feb 2014

Thank you very much guys. Commented out the last line and presto it works, thanks a lot. Learned something new.

posted by Donald K 24 Feb 2014