圣龙扬特-AVR电子
标题:
ICCAVR的6路PWM输出实验
[打印本页]
作者:
铜河
时间:
2013-2-28 20:05
标题:
ICCAVR的6路PWM输出实验
//ICC-AVR application builder : 2008-7-12 20:25:58
// Target : M16
// Crystal: 7.3728Mhz
#include <iom16v.h>
#include <macros.h>
unsigned char m; //定义全局变量
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0xFF;
}
//TIMER1 initialize - prescale:64
// WGM: 0) Normal, TOP=0xFFFF
// desired value: 2Hz
// actual value: 2.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00;
TCNT1H = 0x1F;
TCNT1L = 0x01;
TCCR1A = 0x00;
TCCR1B = 0x03;
}
#pragma interrupt_handler timer1_compa_isr:7
void timer1_compa_isr(void)
{
PORTB = 0x00; //清端口
}
#pragma interrupt_handler timer1_ovf_isr:9
void timer1_ovf_isr(void)
{
TCNT1H = 0x1F;
TCNT1L = 0x01;
m++;
if(m>=6)m=0;
PORTB |= 1<<m; //各端口置1
}
//call this routine to initialize all peripherals
void init_devices(void)
{
CLI();
port_init();
timer1_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x14; //允许溢出中断和匹配中断
SEI();
}
void main(void)
{
init_devices();
OCR1A = 20000; //比较匹配值,此值决定脉冲宽度
while(1);
}
欢迎光临 圣龙扬特-AVR电子 (http://avr.cnta.net/)
Powered by Discuz! X2.5