设计要点详解:
1.ADC2采样完成(EOC)中断
<pre>/*
* ADC2 End Of Conversion interrupt handler
* Positions are filtered by a 7th-order moving average filter.
*/
@far @interrupt void ADC2_IRQHandler(void)
{
static u8 xFilterPos, yFilterPos;
static s16 xFilterBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
static s16 yFilterBuf[8] = {0, 0, 0, 0, 0, 0, 0, 0};
s16 adcValue, acc = 0;
u8 i;
adcValue = ADC2_GetConversionValue();
if (currentChannel == ADC2_CHANNEL_6)
{
xFilterBuf[xFilterPos & 7] = adcValue;
xFilterPos++;
for (i = 0; i < 8; i++)
{
acc += xFilterBuf;
}
xPos = acc >> 3;
currentChannel = ADC2_CHANNEL_7;
}
else
{
yFilterBuf[yFilterPos & 7] = adcValue;
yFilterPos++;
for (i = 0; i < 8; i++)
{
acc += yFilterBuf;
}
yPos = acc >> 3;
currentChannel = ADC2_CHANNEL_6;
}
// Switch the channel and start new conversion.
ADC2_ClearFlag();
ADC2_ConversionConfig(ADC2_CONVERSIONMODE_SINGLE, currentChannel, ADC2_ALIGN_RIGHT);
ADC2_StartConversion();
}</pre>
// Set the direction and OC polarity based on the PID output (takes effect on next PWM cycle)
if (xPWM >= 0)
{
direction &= (u8) ~X_DIRECTION_FLAG;
TIM1_OC1PolarityConfig(TIM1_OCPOLARITY_LOW);
}
else
{
direction |= X_DIRECTION_FLAG;
xPWM = -xPWM;
TIM1_OC1PolarityConfig(TIM1_OCPOLARITY_HIGH);
}
if (yPWM >= 0)
{
direction &= (u8) ~Y_DIRECTION_FLAG;
TIM1_OC2PolarityConfig(TIM1_OCPOLARITY_LOW);
}
else
{
direction |= Y_DIRECTION_FLAG;
yPWM = -yPWM;
TIM1_OC2PolarityConfig(TIM1_OCPOLARITY_HIGH);
}
// Update the PWM (takes effect on next PWM cycle)
TIM1_SetCompare1(xPWM);
TIM1_SetCompare2(yPWM);