电机专题

直流电机:

其实直流电机也就是小的时候玩的四驱车的马达,但是在单片机驱动是,需要家驱动电路,我至今还没驱动起直流电机,就是因为目前不知道怎么来连接驱动电路;

控制直流电机的转速的是用PWM波形来控制,就是控制高电平持续时间在一个周期中所占的比例,这个有个专用的名词叫占空比来着

用单片机得到PWM波形有三种方式

方式1:利用软件延时,当高电平延时时间到时的时候,对I/O口电平取反,然后咋延时,当低电平延时时间到后,在对I/O口进行取反,如此循环就得到PWM波形

方式2:利用的是定时器延时,控制方法同上

方式3:利用单片机自带的PWM控制器

示例代码:得到PWM波形

//利用软件延时:并且用键盘扫描得到不同的PWM 
 
#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
sbit PWN = P1^1;
sbit key1 = P3^1;
sbit key2 = P3^2;
uchar num;
uint yanshi;
uchar code table[]={0xc0,0xf9,0xa4,0xb0,
                    0x99,0x92,0x82,0xf8,
                    0x80,0x90,0x88,0x83,
                    0xc6,0xa1,0x86,0x8e};

void delayms(uint z)
{
    uint x,y;
    for(x = z;x>0;x--)
        for(y=110;y>0;y--);
}



void main()
{
    while(1)
    {
    P3 = 0xff;
    if(key1 ==0)
    {
        delayms(2);
        if(key1 ==0)
        {
            while(!key1);
            num++;
            if(num ==7)
            {
                num =0;
            }
            yanshi = 200*num;
        
        }
    }
        switch(num)
            {
                case 1:
                {
                    PWN = ~PWN;
                    P0 = table[num];
                    delayms(yanshi);
                }
                break;
                case 2:
                {
                    PWN = ~PWN;
                    P0 = table[num];
                    delayms(yanshi);
                }
                break;
                case 3:
                {
                    PWN = ~PWN;
                    P0 = table[num];
                    delayms(yanshi);
                }
                break;
                case 4:
                {
                    PWN = ~PWN;
                    P0 = table[num];
                    delayms(yanshi);
                }
                break;
                case 5:
                {
                    PWN = ~PWN;
                    P0 = table[num];
                    delayms(yanshi);
                }
                break;
                case 6:
                {
                    P0 = table[num];
                    PWN = 1;
                }
                break;
            }

    }

}
View Code

 示例代码2:

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char 
#define TIME 600

sbit led1 =  P1^1;

void delay(uint xx)
{
    while(xx--);
}

void main()
{
    uint x;
    while(1)
    {
        for(x = 0;x<TIME;x++)
        {
            led1 = 0;
            delay(TIME-x);
            led1 = 1;
            delay(x);
        }
        delay(65535);
        for(x=TIME;x>0;x--)
        {
            led1 = 0;
            delay(TIME-x);
            led1 = 1;
            delay(x);
        }
    }    
}
View Code

 

 

步进电机:

步进电机是将电脉冲信号转变为角位移和线位移的开环控制元件,在非超载的情况下,电机的转速,停止的位置,只取决于脉冲信号的频率数,和脉冲数,而不受负载变化的影响

基本的步进电机的驱动程序:

#include<reg52.h>
#define uint unsigned int
main()
{
    uint i,j;
    while(1)
    {
    P1 = 0x01;//0000 0001
                  for(i = 0;i<800;i++);
    P1 = 0x02;//0000 0010
    for(i = 0;i<800;i++);
    P1 = 0x04;//0000 0100
    for(i = 0;i<800;i++);
    P1 = 0x08;//0000 1000
    for(i = 0;i<800;i++);
     }
}           
View Code

综合程序;用按键实现步进电机的启动,停止,增速,减速,并在数码管上显示出来:

#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char

sbit dula = P2^7;
sbit wela = P2^6;
sbit key1 = P3^3;//这里是解决我的单片机上按键坏掉的问题
sbit key2 = P2^4;
sbit key3 = P2^5;
sbit key4 = P2^1;
uchar aa,flag,speed,flag1,i;//flag 是用来标记启动和停止的标志位 
                            //flag1 是用来标记正转还是反转的标志位 
uchar code table[]={0x01,0x02,0x04,0x08};
uchar code table1[]={0x3f,0x06,0x5b,0x4f,
                    0x66,0x6d,0x7d,0x07,
                    0x7f,0x6f,0x77,0x7c,
                    0x39,0x5e,0x79,0x71};
void init()
{
    TMOD = 0x01;
    TH0 = (65536-2400)/256;
    TL0 = (65536-2400)%256;
    EA = 1;
    ET0 = 1;


}

void delay(uint z)
{
    uint x ,y;
    for(x = z;x>0 ;x--)
        for(y = 110 ;y>0;y--);
}

void display1()//on显示函数
{

    wela = 1;
    P0 = 0xf7;
    wela = 0;
    P0 = 0;

    dula = 1;
    P0 = 0x3f;
    dula = 0;
    delay(5);
    P0 = 0xff;


    wela = 1;
    P0 = 0xef;
    wela =0;
    P0 = 0;

    dula = 1;
    P0 = 0x54;
    dula = 0;
    delay(5);
}

void display2()//off显示函数
{

    wela = 1;
    P0 = 0xf7;
    wela = 0;
    P0 = 0;

    dula = 1;
    P0 = 0x3f;
    dula = 0;
    delay(5);
    P0 = 0xff;


    wela = 1;
    P0 = 0xef;
    wela =0;
    P0 = 0;

    dula = 1;
    P0 = 0x71;
    dula = 0;
    delay(5);

    
    wela = 1;
    P0 = 0xdf;
    wela = 0;
    P0 = 0;

    dula = 1;
    P0 = 0x71;
    dula = 0;
    delay(5);
    P0 = 0xff;
}


void display()//显示函数
{
    uchar gw,sw;
    gw = speed%10;
    sw = speed/10;
    wela = 1;
    P0 = 0xfe;
    wela = 0;
    P0 = 0;

    dula = 1;
    P0 = table1[sw];
    dula = 0;
    delay(5);
    P0 = 0xff;


    wela = 1;
    P0 = 0xfd;
    wela =0;
    P0 = 0;

    dula = 1;
    P0 = table1[gw];
    dula = 0;
    delay(5);
}

void key_scan()//键盘扫描函数
{
    P3 = 0xff;

    if(key2==0)
    {
        delay(5);
        if(key2==0)
        {
            while(!key2)
            {
                display2();
            }
            flag = 0;
        }
    }

    if(key1==0)
    {
        delay(5);
        if(key1 ==0)
            {
                while(!key1)
                {
                display1();
                display();
                }
        
            flag = 1;//步进电机启动 
            }

    }


    if(flag==1)
    {
        if(key4 == 0)
        {
            delay(5);
            if(key4==0)
            {
                while(!key4)
                {
                    display();
                    display1();
                    
                }
                speed --;
                flag1 = 1;
                if(speed ==-1)
                    speed = 20;
            }
        }
        if(key3==0)
        {
            delay(5);
            if(key3==0)
            {
                while(!key3)
                {
                    display();
                    display1();
                }
                speed ++;
                flag1 = 0;
                if(speed==21)
                {
                    speed = 1;
                }
            }
        }

    }

}

main()
{

    init();
    while(1)
    {
        key_scan();
        if(flag == 1)
        {
            TR0 =  1;
            display();
            display1();
        }
        if(flag == 0)
        {
            TR0 = 0;
            display2();
                     
        }
    }
}

void time0() interrupt 1
{
    TH0 = (65536-2400)/256;
    TL0 = (65536-2400)%256;
    aa ++;
    if(aa ==speed)
    {
        aa = 0;
        if(flag1 == 1)//正转的标志 
        {
            i++;
            if(i ==4)
                i = 0;
            P1 = table[i];
        }
        if(flag1 == 0)//反转的标志 
        {
            i --;
            if(i == -1)
                 i = 3;
            P1 = table[i];
        }
    }
}
View Code

版权所有,转载请注明链接地址:http://www.cnblogs.com/fengdashen/p/3349932.html

 

posted @ 2013-10-03 21:45  跨七海的风  阅读(369)  评论(0编辑  收藏  举报