|
发表于 2006-3-13 16:21:36
|
显示全部楼层
回12楼
可以的
最简单的就是将LED直接接在PWM信号上
下面是我刚刚做的一个
你做的时候你可以让频率自动改变的
//ICC-AVR application builder : 2006-3-7 13:20:09
// Target : M16
// Crystal: 8.0000Mhz
/*PD5做16位PWM相位修正输出 PAO按一次频率向上加10000 加至最大0xffff
PA0按一次频率向下减10000 减至最下0*/
#include <iom16v.h>
#include <macros.h>
void delay_us(unsigned int time) //微秒级延时程序
{
do
{
time--;
//WDR();
}
while (time>1);
}
void delay_ms(unsigned int time) //毫秒级延时程序
{
while(time!=0)
{
delay_us(1000);
time--;
}
//WDR();
}
void port_init(void)
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0xff;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
MCUCR = 0x00;
GICR = 0x00;
TIMSK = 0x00; //timer interrupt sources
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
void pwmout (signed long date)
{
CLI();
TCCR1A=0Xa3;
TCCR1B=0X02;
OCR1A=date;
//delay_ms(10);
SEI();
}
void main (void)
{
signed long sentdate;
char flag, oldp, newp;
oldp=newp=0x01;
flag=1;
sentdate=0;
init_devices();
//delay_ms(10);
do
{
oldp=PINA;
if (!(oldp & newp))
//if (PINA&0x01==0)
delay_ms(20);
if (!(oldp & newp))
{
if (flag)
{
sentdate=sentdate+10000;
if (sentdate>=0xffff)
{
sentdate=0xffff;
pwmout(sentdate);
flag=0;
}
else
{
pwmout(sentdate);
}
}
else
{
sentdate=sentdate-10000;
if (sentdate<=0)
{
sentdate=0;
pwmout(sentdate);
flag=1;
}
else
{
pwmout(sentdate);
}
}
}
}
while(1);
} |
|