由定时器产生的PWM 通过PD4,PD5输出
- /*
- * GccApplication6.c
- *
- * Created: 2014-8-28 21:50:18
- * Author: Administrator
- */
- #include <avr/io.h>
- #include <avr/interrupt.h>
- //PWM输出程序
- //在端口PD4,PD5(led5,led6)输出PWM脉冲,可用示波器测量,或者外加一LED灯,可以看到LED由于脉宽的变化引起的灯的亮度不同
- //#include <macros.h>
- void port_init(void)
- {
- PORTA = 0x00;
- DDRA = 0x00;
- PORTB = 0x00;
- DDRB = 0x08;
- 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
- TCNT1L = 0x83;
- OCR1AH = 0x00;
- OCR1AL = 0x65; //占空比
- OCR1BH = 0x00;
- OCR1BL = 0x1f; //占空比
- 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();//CLI(); //disable all interrupts
- port_init();
- timer1_init();
- MCUCR = 0x00;
- GICR = 0x00;
- TIMSK = 0x00; //timer interrupt sources
- sei();//SEI(); //re-enable interrupts
- //all peripherals are now initialized
- }
- int main(void)
- {
- init_devices();
- }
|