- //ICC-AVR application builder : 2013/5/16 23:37:13
- // Target : M16
- // Crystal: 16.000Mhz
- #include
- #include
- void port_init(void)
- {
- PORTA = 0xFF;
- DDRA = 0xFF;
- PORTB = 0xFF;
- DDRB = 0xFF;
- PORTC = 0xFF; //m103 output only
- DDRC = 0xFF;
- PORTD = 0xFF;
- DDRD = 0xFF;
- }
- //TIMER0 initialize - prescale:256
- // WGM: PWM Phase correct
- // desired value: 10mSec
- // actual value: 8.160mSec (18.4%)
- void timer0_init(void)
- {
- TCCR0 = 0x00; //stop
- TCNT0 = 0x01; //set count
- OCR0 = 0xFF; //set compare
- TCCR0 = 0x64; //start timer
- }
- #pragma interrupt_handler timer0_ovf_isr:iv_TIM0_OVF
- void timer0_ovf_isr(void)
- {
-
- //PORTD = 0x00;//TCNT0 = 0x01; //reload counter value
- }
- #pragma interrupt_handler timer0_comp_isr:iv_TIM0_COMP
- void timer0_comp_isr(void)
- {
- //TCNT0 = 0x01;
- //delay_ms(15);
- if(PORTD == 0xFE)
- PORTD = 0xFF;
- else
- PORTD =0xFE;
- //PORTD += 0x02;//compare occured TCNT0=OCR0
- }
- //call this routine to initialize all peripherals
- void init_devices(void)
- {
- //stop errant interrupts until set up
- CLI(); //disable all interrupts
- port_init();
- timer0_init();
- MCUCR = 0x00;
- GICR = 0x00;
- TIMSK = 0x02; //timer interrupt sources
- SEI(); //re-enable interrupts
- //all peripherals are now initialized
- }
- void delay_ms( unsigned int ms ) // ms 延时
- {
- unsigned int j;
- for( ; ms > 0; ms-- )
- {
- for( j = 1600; j > 0; j-- )
- {
- NOP();
- NOP();
- }
- }
- }
- void main(void)
- {
- init_devices();
- PORTD = 0xFE;
- PORTB = 0xE3;
- OCR0 = 0xC8;
- //insert your functional code here...
- }
m16的每个I/O口都为LED灯,低电平亮,高电平灭......
主要为了实现各个I/O口的占空比控制......
搞了好久都不行,求大神帮忙了......先说声谢谢了........