PWM调速程序

合集下载
相关主题
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
}
void speed_adjust(unsigned int speed)
{OCR1Al=speed;}// change compare value
//
void main(void)
{wenku.baidu.com
init_devices();
//insert your functional code here...
While(1);
PORTA|=(1<<PA0) ;//set PA0
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
//ICC-AVR application builder
// Target : M16
// Crystal: 8.0000Mhz
#include <iom16v.h>
#include <macros.h>
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x01;//set PA0 as PWM wave output port
}
//note: when using the applocation builder tool , some sentences oftheautomaticly produced//program are not necessary for certain use like PWM wave ,delete them.
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x14; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
//note:most of the time, you need to reset the value of counter as 0.
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
//TIMER1 has overflowed
TCNT1H = 0x00; //reload counter high value
TCNT1L = 0x00; //reload counter low value
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x00;
}
//note: even if you use the second function of PD4,PD5 as PWM wave output ports, you should //also set port’s direction.
void timer1_init(void)
{
TCCR1B = 0x00; //stop
//set initial value for counter
TCNT1H = 0x00; //setup
TCNT1L = 0x00;
//set compare value
OCR1AH = 0x00;
OCR1AL = 0xFF;//
//TIMER1 initialize - prescale:256
// WGM: 5) PWM 8bit fast, TOP=0x00FF
// desired value: 100Hz
// actual value: 122.070Hz (18.1%)
//note:there is no particular requirement for the frequency of PWM wave as long as it is not too //low.
TCCR1A = 0x01;
TCCR1B = 0x0C; //start Timer
}
#pragma interrupt_handler timer1_compa_isr:7
void timer1_compa_isr(void)
{
//compare occured TCNT1=OCR1A
PORTA&=~(1<<PA0); // clear PA0
PWM调速程序
假设在硬件电路已经连接好后,要控制直流电机的转速可以通过在电机驱动电路的使能端输入一PWM波形。改变PWM波的脉宽(占空比)即可改变加在电机两端的有效电压,从而改变电机的转速。注意,此处的PWM波只是相当于电机供电电路开关的作用:高电平对应接通,低电平对应断开。
对于Atmega 16单片机,这里利用T/C1定时器中断来产生PWM波形。在ICC AVR编译环境下,利用tool菜单中的application builder生成一个简单的PWM波程序。这段程序以PA0作为PWM波的输出端口。利用T/C1定时器比较匹配和溢出产生两次中断来改变PA0的输出电平。具体过程为:计数器TCNT1从初始值开始不断计数,当发生比较匹配时,把PA0置为低电平,计数器继续计数,当发生溢出中断时,计数器回到初始设定值,并把PA0置为高电平。从而在PA0端口获得一稳定持续的PWM波形,在主程序中改变比较值,即可改变波形占空比,而频率不变。
相关文档
最新文档