Hi guys,
I was experimenting with my QG8 micro the other day and was using the ADC. So below is a piece of code i used to make a VCO(voltage controlled oscillator) which increases the frequency of blinking of the LED. I got the result I wanted for low frequencies but at higher frequencies the pattern is not right. So I wanted to know what sort of effect an infinite loop in an interrupt has on the MCU?
Any advice will be much appreciated. Thanks guys
__interrupt void isrVadc_complete(void)
{
unsigned int set_time;
unsigned int ADC_val;
unsigned int count = 0;
for( ;; )
{
ADC_val = ADCRL;
set_time = ADC_val * 256;
if(count == set_time)
{PTAD_PTAD0 = ~PTAD_PTAD0; }
else if( count > set_time)
{count=0; }
count++;
}
}
Hello,
If you are timing the blink of a LED, where is your timing reference? At the moment, any timing will be associated with execution cycles of the code.
I suspect that you should be utilising say the TPM overflow interrupt, rather than the ADC interrupt. The count variable would then count each TPM overflow, and then test whether a threshold value had been reached. This would give a more consistent timing, not dependent on execution cycles.
The approach might be to commence the first single ADC conversion during initialisation. Then, when each TPM overfow occurs, to read the previous conversion result, and to start a new single conversion, to be read during the next overflow. You might even delay the use of the previous conversion until the threshold count is reached, and you commence a new LED flash period.
Note that where tha ADC value directly determines the count value, the LED flash period (and not the frequency) will be directly proportional to the control voltage. If the frequency is required to be proportional to the control voltage, the ADC value will require further mathematical processing to determine the current threshold value.
Note that any local variables within an ISR (such as count and set_time) will need to be static or global type. Other variables used be the ISR that require access by other functions within the main loop will need to be volatile global type.
Regards,
Mac
Hello karthick
You have your toggle condition as equals and you are comparing to basically a live ADC result.
Because of this it is very possible you can miss the compare due to the ADC result jittering around due to noise etc.
Also, locking yourself in interrupt here is pretty pointless. It means the only thing this processor can do is this code.
The reason you use an interrupt is to get very fast response to an event, here you don't get that.