swapan
Full Member level 4
- Joined
- Feb 20, 2009
- Messages
- 204
- Helped
- 27
- Reputation
- 54
- Reaction score
- 24
- Trophy points
- 1,298
- Location
- Kolkata
- Activity points
- 2,839
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
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);
}