qinzhifeng 发表于 2009-8-12 16:54:54

求助:控制电机转速

//刚学完atmega16,写了个小程序控制电机转速。不知是否正确,有什么缺点错误,多指教
//s1——停止
//s2——低速
//s3——高速
//s4——最大速度
//小弟初出茅庐,望各位仁兄不吝赐教!

//ICC-AVR application builder : 2009-8-4 14:35:24
// Target : M16
// Crystal: 8.0000Mhz

#include <iom16v.h>
#include <macros.h>
#define s1 (PIND&0xfe)
#define s2 (PIND&0xfd)
#define s3 (PIND&0xfb)
#define s4 (PIND&0xf7)

void port_init(void)
{
PORTA = 0x00;
DDRA= 0x00;
PORTB = 0x00;
DDRB= 0x08;                                  //PWM波输出口——PB3
PORTC = 0x00; //m103 output only
DDRC= 0x00;
PORTD = 0xFF;                              
DDRD= 0x00;                                 //按键s1,s2,s3,s4对应PD1~4

}

//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();

MCUCR = 0x00;
GICR= 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}

//
void main(void)
{
init_devices();
//insert your functional code here...
TCCR0=0x61;                      //相位修正模式,无分频,上升匹配时OCR0清零,下降匹配时置位OCR0
while(1)
{
if(s1==0)
   {
    OCR0=0x00;          //S1_STOP
    TCNT0=0x00;
   }
   else if(s2==0)
   {
    OCR0=0x4b;          //S2-LOW SPEED
    TCNT0=0x00;
   }
   else if(s3==0)
   {
    OCR0=0x96;          //S3-HIGH SPEED
    TCNT0=0x00;
   }
   else
   {
    OCR0=0XFF;          //S4-MAX SPEED
    TCNT0=0x00;
   }
}
}

qinzhifeng 发表于 2009-8-12 16:56:55

还有工作于相位修正模式时,当OCR0=0时,pwm输出一直是低吗?
OCR0=0xff时,一直为高电平吗?
页: [1]
查看完整版本: 求助:控制电机转速