打印
[Atmel]

用AtmelStudio6.2跑mega16例程(7):控制步进电机

[复制链接]
937|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
本程序是利用两个外部中断来控制步进电机的运行和停止。

以下是proteus的仿真结果


以下是编译通过的结果:


以下是程序:
/*
* GccApplication12.c
*
* Created: 2014-9-3 22:02:36
*  Author: Administrator
*/


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

#define uchar unsigned char
#define uint unsigned int
uchar a;
uchar tab[]={0x01,0x02,0x04,0x08};        //1相励磁,正转
//interrupt[EXT_INT0] void int0_isr(void)         //INT0加速控制
ISR(INT0_vect)
{
        a=1;
}
//interrupt[EXT_INT1] void int1_isr(void)         //INT1减速控制
ISR(INT1_vect)
{
        a=0;
}
void delay(uint k)
{
        long i;
        for(i=0;i<k;i++);
}
int main(void)
{
        uchar i;
        DDRC=0xFF;                                  //端口初始化
        PORTC=0xFF;
        DDRD=0xF3;
        PORTD=0xFF;
        MCUCR=0x0A;                                 //定义INT0和INT1为下降沿时产生中断
        GICR=0xC0;                                 //允许INT0和INT1产生中断
        //#asm("sei")                                //开启总中断
        sei();
        while(1)
        {
                if(a==1)
                {
                        for(i=0;i<4;i++)
                        {
                                PORTC=tab[i];
                                delay(10000);
                        }
                }
                else if(a==0)
                {
                        PORTC=0x00;
                }
        }
}



相关帖子

发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:http://shop34182318.taobao.com/ http://shop562064536.taobao.com

2398

主题

6949

帖子

66

粉丝