AVR单片机控制交流电机测试程序
扫描二维码
随时随地手机看文章
MCU:at90s2313
时钟:4MHz
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#defineSET_RED_LEDPORTD|=_BV(5)//PD5接红色发光管
#defineCLR_RED_LEDPORTD&=~_BV(5)
#defineSET_GRN_LEDPORTD|=_BV(4)//PD4接绿色发光管
#defineCLR_GRN_LEDPORTD&=~_BV(4)
classCControl
{
publIC:
CControl();
public:
ucharm_bCounter;
voidDelayMs(uintms);
voidRunMotor(uchardirection);
};
CControl::CControl()
{
m_bCounter=0;
}
voidCControl::RunMotor(uchardirection)
{
if(direction==1)
{
SET_GRN_LED;
CLR_RED_LED;
}
elseif(direction==2)
{
CLR_GRN_LED;
SET_RED_LED;
}
else
{
CLR_GRN_LED;
CLR_RED_LED;
}
for(uchari=0;i
while((PINB&_BV(0))==1);
while((PINB&_BV(0))==0);
if(direction==1)
{
PORTB|=_BV(PB3);
DelayMs(2);
PORTB&=~_BV(PB3);
}
elseif(direction==2)
{
PORTB|=_BV(PB2);
DelayMs(2);
PORTB&=~_BV(PB2);
}
else
PORTB=0;
}
}
voidCControl::DelayMs(uintms)
{
uintk=0;
for(k=0;k
}
CControlg_oMotorCtl;
intmain(void)
{
DDRD=_BV(4)|_BV(5);//发光管I/O初始化
PORTD=0X00;
PORTB=0;//控制口I/O初始化
DDRB=_BV(PB3)|_BV(PB2);
g_oMotorCtl.m_bCounter=200;
//SET_GRN_LED;
g_oMotorCtl.DelayMs(2000);
while(1)
{
g_oMotorCtl.RunMotor(1);
g_oMotorCtl.RunMotor(0);
g_oMotorCtl.RunMotor(2);
g_oMotorCtl.RunMotor(0);
}
}