谁能告诉我用AVR单片机(ATmega16)怎么输出不同的波形,程序怎么写??先谢谢了,急用

2025-03-10 18:49:56
推荐回答(1个)
回答1:

//PWM输出程序
//在端口PD4,PD5(led5,led6)输出PWM脉冲,可用示波器测量,或者外加一LED灯,可以看到LED由于脉宽的变化引起的灯的亮度不同
#include
#include

void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0xff;
DDRD = 0xff;
}

//TIMER1 initialize - prescale:64
// WGM: 10) PWM phz correct, TOP= ICRn
// desired value: 1000Hz
// actual value: 1000.000Hz (0.0%)
void timer1_init(void)
{
TCCR1B = 0x00; //stop
TCNT1H = 0xFF; //setup 255
TCNT1L = 0x83; //131
OCR1AH = 0x00;
OCR1AL = 0x5A; //占空比 100
OCR1BH = 0x00;
OCR1BL = 0x1E; //占空比 10
ICR1H = 0x00;
ICR1L = 0x7d;
TCCR1A = 0xA2;
TCCR1B = 0x13; //start Timer
}

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

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