pwm2.c

来自「AVR的PWM波—AVR单片机,AVR,PWM.」· C语言 代码 · 共 82 行

C
82
字号
//ICC-AVR application builder : 2008-01-04 20:30:57
// Target : M48
// Crystal: 8.0000Mhz

#include <iom48v.h>
#include <macros.h>

void port_init(void)
{
 PORTB = 0x00;
 DDRB  = 0x06;   //OC1A OC1B 
 PORTC = 0x00;
 DDRC  = 0x00;
 PORTD = 0xFF;
 DDRD  = 0xFF;   //00110000

}

//TC1初始化
void timer1_init(void)
 {
 TCCR1A = 0xA3;   //两路PWM,匹配清零
 TCCR1B = 0x09;    //快速PWM模式,10位,预分频1
 }

//call this routine to initialize all peripherals
void init_devices(void)
{
 port_init();   //端口初始化
 timer1_init();   //TC1初始化

 MCUCR = 0x00;
 //GICR  = 0x00;
}


//延时函数:入口time 需延时的MS数
void delay_ms(unsigned int time)
 {
 unsigned char c;
 for(;time;time--)
  {
  for(c=220;c;c--)
   {
   ;
   }
  }
 }
 
 
 //主函数
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);
  }
 }


 
 




⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?