ATMega8 PWM 功能(T/C 1)测试程序
2017-12-18 来源:eefocus
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define FREQ 4
//延时
void DelayMs(uint ms)
{
uint i;
for(i=0;i
}
int main (void)
{
uchar direction=1;
uchar PWM=0;
// 8位相位修正PWM模式
// OC1A 向上计数过程中比较匹配时清零
// 向下计数过程中比较匹配时置位
// 时钟源:4MHz/64 PWM频率:4MHz/(64*256)=122Hz
TCCR1A=_BV(COM1A1)/*|_BV(COM1B1)*/|_BV(WGM10);
TCCR1B=_BV(CS11)|_BV(CS10);
TCNT1=0;
DDRB|=_BV(PB1)/*|_BV(PB2)*/;//OC1A pwm输出引脚允许
//循环改变PWM输出脉宽,使接在OC1A引脚上的发光管亮度发生变化
while(1)
{
if(direction)
{
if(++pwm==254)
direction=0;
}
else
{
if(--pwm==0)
direction=1;
}
OCR1A=pwm;
//OCR1B=pwm;
DelayMs(10);
}
return 0;
}