当前位置:首页 > 单片机 > 单片机
[导读] MCU:at90s2313时钟:4MHz#include#include#defineucharunsignedchar#defineuintunsignedint#defineSET_RED_LEDPORTD|=_BV(5)//PD5接红色发光管#defineCLR_RED_LEDPORTD&=~_BV(5)#defineSET_GRN_LEDPORT

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_delay_loop_2(1000);
}

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);
}

}



本站声明: 本文章由作者或相关机构授权发布,目的在于传递更多信息,并不代表本站赞同其观点,本站亦不保证或承诺内容真实性等。需要转载请联系该专栏作者,如若文章内容侵犯您的权益,请及时联系本站删除( 邮箱:macysun@21ic.com )。
换一批
延伸阅读
关闭