⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.c

📁 《AVR单片机GCC程序设计》
💻 C
字号:
/*
	交流电机测试程序 
	main.c
	MCU:at90s2313
	时钟:4MHz
	
	芯艺 2004-09-25
*/

#include <avr/io.h>
#include <avr/delay.h>

#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<m_bCounter;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<ms;k++)
		_delay_loop_2(1000);
}

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

}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -