之前弄了一块PIC的开发板PICDEM_MCLV_CD_V3.10
采用了dsPIC30F2010 烧写附带的开环程序运行正常,
程序中定义了俩按键
#define S2 !PORTCbits.RC14
#define S3 !PORTCbits.RC13
我想实现以下如下功能:
按下S2,顺时针转,松开后停
按下S3,逆时针转,松开后停
现在按下去转动正常,松开后,感觉霍尔停到一个位置,驱动没有关闭,还在一直输出对应霍尔位置的换相信号
初次使用PIC家的产品,还望各位前辈多多指教
CN中断部分和main循环如下:
void __attribute__((__interrupt__,no_auto_psv)) _CNInterrupt (void)
{
IFS0bits.CNIF = 0; // clear flag
HallValue = PORTB & 0x0038; // mask RB3,4 & 5
HallValue = HallValue >> 3; // shift right 3 times
//OVDCON = StateLoTableClk[HallValue];
if(S2)
OVDCON = StateLoTableClk[HallValue]; // Load the overide control register
if(S3)
OVDCON = StateLoTableAntiClk[HallValue];
if (HallValue == INDEX) // has the same position been sensed?
if (polecount++ == POLEPAIRS) //has one mech rev elasped?
{ // yes then read timer3
timer3value = TMR3;
TMR3 = 0;
timer3avg = ((timer3avg + timer3value) >> 1);
polecount = 1;
lapcounter++;
}
if(lapcounter>=50) FLTACON=0X0007;//start FLTA
}
int main(void)
{
LATE = 0x0000;
TRISE = 0xFFC0; // PWMs are outputs
CNEN1 = 0x00E0; // CN5,6 and 7 enabled
CNPU1 = 0x00E0; // enable internal pullups
IFS0bits.CNIF = 0; // clear CNIF
IEC0bits.CNIE = 1; // enable CN interrupt
InitMCPWM();
InitADC10();
InitUART();
InitTMR3();
timer3avg = 0;
while(1)
{ /*
while (!S2); // wait for start key hit
while (S2) lapcounter=0; // wait till key is released
*/
//DelayNmSec(10);
// read hall position sensors on PORTB
HallValue = PORTB & 0x0038; // mask RB3,4 & 5
HallValue = HallValue >> 3; // shift right to get value 1, 2 ... 6
if(!S2||!S3) //没按下时候关闭
{
PWMCON1 = 0x0700; // disable PWMs
OVDCON = 0x0000; // allow control using OVD
}
if(S2) //按下S2,输出顺时针
{
OVDCON = StateLoTableClk[HallValue]; // Load the overide control register
PWMCON1 = 0x0777; // enable PWM outputs
}
if(S3) //按下S3输出逆时针
{
OVDCON = StateLoTableAntiClk[HallValue];
PWMCON1 = 0x0777; // enable PWM outputs
}
Flags.RunMotor = 1; // set flag
T3CONbits.TON = 1; // start tmr3
polecount = 1;
U1STAbits.UTXEN = 1; // start transmission
U1TXREG = CR;
U1TXREG = LF;
//DelayNmSec(100);
/*
while (Flags.RunMotor) // while motor is running
{
if (S2) // if S2 is pressed
{
PWMCON1 = 0x0700; // disable PWM outputs
OVDCON = 0x0000; // overide PWM low.
U1STA = 0x0000; // clear transmit
Flags.RunMotor = 0; // reset run flag
while (S2) ; // wait for key release
//DelayNmSec(10);
}
if (Flags.SndSpeed)
SendSpeed();
}
*/
} // end of while (1)
}
|