[Problem] Using array to store read voltage level

Status
Not open for further replies.

VoltVolt

Junior Member level 1
Joined
Sep 15, 2013
Messages
15
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
124
Hi all,
I'm try to use array to store voltage read from ADC module...
But it seems some logical errors...
Desired result: voltage[0] = xxx, voltage[1] = xxx ,voltage[2] = xxx......

Code:
void main() {

    LCDinit();
    init_ADC();
    delay_ms(10); //idle period

    while (1) {

        int adc_eqn;
        float voltage[16];
        char strStore[16];

        for (int channel = 0; channel < 10; channel++) {
            if ((channel == 1) || (channel == 5) || (channel == 9)) {
                //AN0 CHANNEL
                ADCON0 = channel;
                for (int i = 0; i < 3; i++) {
                    //ADC start
                    GO_DONE = 1;

                    //wait for ADC to complete
                    while (GO_DONE) {
                        continue;
                    }

                    //retrieve adc equavalent value
                    adc_eqn = ((ADRESH << 8) | ADRESL);
                    voltage[i] = (float) (adc_eqn * 5.0 / 1023);

                    sprintf(strStore, "ADC %d %f V", i, voltage[i]);
                    LCDWrite(strStore, 10, 0, 0);

                    delay_ms(1000);
                }
            }
        }
    }
}

Thank you...View attachment Test Program.X.rarView attachment Test Program.X.rar
 


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
float voltage[16];
char strStore[16];
int channel = 0;
 
void main(){
 
    LCDinit();
    init_ADC();
    delay_ms(10); //idle period
 
    while (1) {
 
        for (channel = 0; channel < 10; channel++){
            if ((channel == 1) || (channel == 5) || (channel == 9)){
                //AN0 CHANNEL
                ADCON0 = channel;
                for (int i = 0; i < 3; i++) {
                    //ADC start
                    GO_DONE = 1;
 
                    //wait for ADC to complete
                    while (GO_DONE);
 
                    //retrieve adc equavalent value
                    
                    voltage[i] = ((ADRESH << 8) | ADRESL) * 5.0 / 1023.0;
 
                    sprintf(strStore, "ADC %d %f V", i, voltage[i]);
                LCDWrite(strStore, 10, 0, 0);
 
                    delay_ms(1000);
                }
            }
        }
    }
}

 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…