|
PWM控制直流电机,可通过示波器观察,key2键怎么按 总不能改变占空比,请大家帮忙解决!
我是用M16的AVR,用相频可调模式,(模式8),通过OC1A输出PWM,
程序如下:
#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
#define Key1 (PIND&0x04) //前进
#define Key2 (PIND&0x08) //后退
uint sun, n;
void delay_ms(uint k)
{
uint i,j;
for(i=0;i<k;i++)
{
for(j=0;j<1140;j++)
;
}
}
scan_key() //功能选择键。
{
while(Key1==0)
{delay_ms(5);
while(Key1==0)
{n=1;
while(Key1==0);
}
}
while(Key2==0)
{delay_ms(5);
while(Key2==0)
{n=2;
while(Key2==0);
}
}
while(Key5==0)
{delay_ms(5);
while(Key5==0)
{n=3;
while(Key5==0);
}
}
return n;
}
void port_init(void)
{
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x30;
DDRD = 0x30;
}
void for_ward(void) //前进
{ OCR1A=0x005f;
}
void speed_add(uint sun ) //加速
{
sun=sun+100;
OCR1A=sun;
}
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup
TCNT1L = 0x06;
ICR1 = 0x01F4;
TCCR1A = 0xA0;
TCCR1B = 0x12; //start Timer
}
void init_devices(void)
{
CLI(); //disable all interrupts
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x80; //timer interrupt sources
SEI(); //re-enable interrupts
}
void main(void)
{
init_devices();
while(1)
{
scan_key();
switch(n)
{case 1:for_ward();
break;
case 2:speed_add(100);
break;
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|