Hi.. I am using PIC18F4620 working with TCP/IP stack 4.18.
Below is the code which I hope is able to read the ADC value at frequency of 8000Hz.
The function VoIP_Main will be called from within the Main function's while loop and it will monitor boolReadADC value. boolReadADC is a flag to indicate that TIMER2 interrupt has overflow which is every 125us and if overflow occur, it will call the ReadADC function.
I have a counter cnt8000 to count how many times the interrupt occurs and it will count up to 7999 counts in 1 second and then it will reset to 0.
That is the background information of the code below.
Now, the problem is this. Instead of the ReadADC function being called everytime the interrupt occur (ie, one time when cnt8000 = 1, another time when cnt8000 = 2 etc), the first time the ReadADC function is called is when cnt8000 = 208 and then next time the function is called again is when cnt8000 = 708.. This clearly is not sampling the ADC at 8000Hz.
I couldn't figure out what went wrong.. Please advice.. thanks..
Below is the code which I hope is able to read the ADC value at frequency of 8000Hz.
The function VoIP_Main will be called from within the Main function's while loop and it will monitor boolReadADC value. boolReadADC is a flag to indicate that TIMER2 interrupt has overflow which is every 125us and if overflow occur, it will call the ReadADC function.
I have a counter cnt8000 to count how many times the interrupt occurs and it will count up to 7999 counts in 1 second and then it will reset to 0.
That is the background information of the code below.
Now, the problem is this. Instead of the ReadADC function being called everytime the interrupt occur (ie, one time when cnt8000 = 1, another time when cnt8000 = 2 etc), the first time the ReadADC function is called is when cnt8000 = 208 and then next time the function is called again is when cnt8000 = 708.. This clearly is not sampling the ADC at 8000Hz.
I couldn't figure out what went wrong.. Please advice.. thanks..
Code:
// ISR VECTOR
#if defined(__18CXX)
#if defined(HI_TECH_C)
void interrupt low_priority LowISR(void)
#else
#pragma interruptlow LowISR
void LowISR(void)
#endif
{
TickUpdate();
Timer2Interrupt(); // For DoIP purposes
}
/////////////////////////////////////
// ISR FUNCTION
/////////////////////////////////////
void Timer2Interrupt()
{
if (PIR1bits.TMR2IF == 1)
{
PIR1bits.TMR2IF=0;
boolReadADC = 1;
if (cnt8000 < 8000)
cnt8000++;
else
cnt8000 = 0;
}
}
void VoIP_Main() // is inside the Main funtion's while loop
{
if (boolReadADC==1)
{
boolReadADC = 0;
ReadADC();
}
}
void ReadADC()
{
// Code to be added later.
}