//延时函数:入口time 需延时的MS数
void delay_ms(unsigned int time)
{
unsigned char c;
for(;time;time--)
{
for(c=220;c;c--)
{
;
}
}
}
//端口初始化
void port_init(void)
{
PORTA = 0xFF;
DDRA = 0x00;
PORTB = 0xFF;
DDRB = 0xFF;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x30;
}
//TC1初始化
void timer1_init(void)
{
TCCR1A = 0xA3; //两路PWM,匹配清零
TCCR1B = 0x09; //快速PWM模式,10位,预分频1
}
//器件初始化
void init_devices(void)
{
port_init(); //端口初始化
timer1_init(); //TC1初始化
MCUCR = 0x00;
GICR = 0x00;
}
//主函数
void main(void)
{
unsigned int a=511,b=511;
init_devices(); //器件初始化
OCR1A=a; //匹配初值
OCR1B=b;
while(1)
{
a-=5; //两个值向不同方向变化
b+=5; //PWMA宽度减小,PWMB宽度增加
if(a<=5) //上下限
{
a=1000;
b=5;
}
OCR1A=a;
OCR1B=b;
delay_ms(50);
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』