×
单片机 > 单片机程序设计 > 详情

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

发布时间:2020-05-28 发布时间:
|

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

}



关键字:AVR  单片机控制  交流电机 

『本文转载自网络,版权归原作者所有,如有侵权请联系删除』

热门文章 更多
51单片机CO2检测显示程序解析