MCU:at90s2313
时钟:4MHz
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define SET_RED_LED PORTD|=_BV(5) //PD5接红色发光管
#define CLR_RED_LED PORTD&=~_BV(5)
#define SET_GRN_LED PORTD|=_BV(4) //PD4接绿色发光管
#define CLR_GRN_LED PORTD&=~_BV(4)
class CControl
{
publIC:
CControl();
public:
uchar m_bCounter;
void DelayMs(uint ms);
void RunMotor(uchar direction);
};
CControl::CControl()
{
m_bCounter=0;
}
void CControl::RunMotor(uchar direction)
{
if(direction==1)
{
SET_GRN_LED;
CLR_RED_LED;
}
else if(direction==2)
{
CLR_GRN_LED;
SET_RED_LED;
}
else
{
CLR_GRN_LED;
CLR_RED_LED;
}
for(uchar i=0;i
while((PINB&_BV(0))==1);
while((PINB&_BV(0))==0);
if(direction==1)
{
PORTB|=_BV(PB3);
DelayMs(2);
PORTB&=~_BV(PB3);
}
else if(direction==2)
{
PORTB|=_BV(PB2);
DelayMs(2);
PORTB&=~_BV(PB2);
}
else
PORTB=0;
}
}
void CControl::DelayMs(uint ms)
{
uint k=0;
for(k=0;k
}
CControl g_oMotorCtl;
int main(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);
}
}
上一篇:AVR 模拟比较器使用范例
下一篇:AVR单片机万年历程序
推荐阅读最新更新时间:2024-03-16 15:49