Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MicroC help need - Result is not defined in function

Status
Not open for further replies.

swapan

Full Member level 4
Joined
Feb 20, 2009
Messages
199
Helped
27
Reputation
54
Reaction score
24
Trophy points
1,298
Location
Kolkata
Activity points
2,806
Friends,

I am absolutely new in C. After many studies of C codes available in this forum and a book on C language, I took a chance to develop a simple code. This code is intended for comparing AC Mains voltage and turning a relay ON or OFF depending on mains voltage. The AC Mains supply, after stepping down and rectifying to pulsating DC (without smoothing by Capacitor), is fed to ADC module of MCU. For this reason I have taken multiple samples (20 samples) at an interval of 300 uS. During this time at least one peak position value will be obtained. This value will be compared with a reference.

When the code built on MikroC, a message is returned "Result is not defined in function : 'sample'. However, the code has been built successfully.

Please see my novice code and see its viabilit. I hope esteemed comment/guidance from yours.

regards,

swapan

Code:
int sample(int volt)  {
unsigned int cnt, adc_value;
for (cnt=0; cnt<20; cnt++){
    adc_value = ADC_Read(0);
    if  (adc_value > volt);
    volt = adc_value;
    Delay_us (300);
    }				// Take 20 samples of ADC at an interval of 
//300 uS and choose only the highest one. 
}



void main() {
    int volt;
     TRISB    =   0x00;
     TRISA    =   0xFF;
     ADCON1   =   0x00;
     do {
     sample(volt);			 

     if  (volt > 128);		// Compare the chosen value with a reference.
     (PORTB = 0b00000001);	// If greater than reference, do this.

     (PORTB = 0b00000000);	// Otherwise do this.
     }  while (1);
}
 

Re: Please help this newbie

Code:
if  (adc_value > volt);
In this line, adc_value is compared to what value?

HTML:
int sample(int volt)  {
unsigned int cnt, adc_value;
for (cnt=0; cnt<20; cnt++){
    adc_value = ADC_Read(0);
    if  (adc_value > 0)
    volt = adc_value;
    return volt;
    Delay_us (300);
    }				// Take 20 samples of ADC at an interval of 
//300 uS and choose only the highest one. 
}



void main() {
   
     TRISB    =   0x00;
     TRISA    =   0xFF;
     ADCON1   =   0x00;
     do {
     sample();			 

     if  (volt > 128);		// Compare the chosen value with a reference.
     PORTB = 0b00000001;	// If greater than reference, do this.
else
     PORTB = 0b00000000;	// Otherwise do this.
     }  while (1);
}
 

Re: Please help this newbie

Thanks hemnath for your observation and guidance. As per your suggestion, if the ADC value is greater than 0, it will be stored in 'volt'. But I like to store the highest value only. Hence adc_value is compared to value already stored in 'volt'. For the first time adc-value is compared to 0. Hence if the code is written as below!

Code:
int sample(int volt)  {
unsigned int cnt, adc_value;
for (cnt=0; cnt<20; cnt++){
    adc_value = ADC_Read(0);
    if  (adc_value > volt);
    volt = adc_value;
    Delay_us (300);
    }				// Take 20 samples of ADC at an interval of 
//300 uS and choose only the highest one. 
}



void main() {
    int volt=0;
     TRISB    =   0x00;
     TRISA    =   0xFF;
     ADCON1   =   0x00;
     do {
     sample(volt);			 

     if  (volt > 128);		// Compare the chosen value with a reference.
     (PORTB = 0b00000001);	// If greater than reference, do this.

     (PORTB = 0b00000000);	// Otherwise do this.
     }  while (1);
}


Please comment.

- - - Updated - - -

The rectified code suggested by hemnath has been built. But returns error message as 'not enough parameters' under function 'sample ();'.
 

Re: Please help this newbie

Try this code


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
int sample();
 
int sample()  {
unsigned int cnt, adc_value, lvolt;
for (cnt=0; cnt<20; cnt++){
    adc_value = ADC_Read(0);
    if  (adc_value > lvolt);
    lvolt = adc_value;
    Delay_us (300);
    }               // Take 20 samples of ADC at an interval of 
//300 uS and choose only the highest one.
    return lvolt; 
}
 
int gvolt=0;
 
void main() {
    
     TRISB    =   0x00;
     TRISA    =   0xFF;
     ADCON1   =   0x00;
     do {
     gvolt = sample();           
 
     if  (gvolt > 128);     // Compare the chosen value with a reference.
     (PORTB = 0b00000001);  // If greater than reference, do this.
 
     (PORTB = 0b00000000);  // Otherwise do this.
     }  while (1);
}



The problem in you code is you are using volt as local variable for main() and then using another variable volt for called function sample() but volt is not available in sample() because you have not declared volt inside sample(). If you fix those things then you still have to return volt in sample() and assign it to volt in the main() function i.e., volt = sample(). You have to do that because when sample() returns, volt in the function sample() will be lost as volt is not a global variable.
 
Last edited:

hello,

your function
Code:
int sample(intvolt)
must return an int value !
you didn't affect back result inside your function.


else use void sample(int volt)
and affect the result in a global variable.
to use it after in your main.
 

So many thanks jayanth.devarayanadurga for your nice clarification. I shall be much benefited by such guidance from hon'ble members like you.


regards,

swapan
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top