您当前的位置:五五电子网电子知识单片机-工控设备AVR单片机AVR单片机控制交流电机测试程序 正文
AVR单片机控制交流电机测试程序

AVR单片机控制交流电机测试程序

点击数:7988 次   录入时间:03-04 11:42:13   整理:http://www.55dianzi.com   AVR单片机

 MCU:at90s2313
 时钟:4MHz

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

}


11061418075579.rar




本文关键字:单片机  程序  交流电机  AVR单片机单片机-工控设备 - AVR单片机