C arduino上的PWM衰减LED
我试图在Arduino上用PWM闪烁led,我不知道出了什么问题。但我的LED灯没有褪色。怎么了?我想我的寄存器设置不好,但我不确定。Led连接在arduino引脚11上。多谢各位C arduino上的PWM衰减LED,c,arduino,pwm,C,Arduino,Pwm,我试图在Arduino上用PWM闪烁led,我不知道出了什么问题。但我的LED灯没有褪色。怎么了?我想我的寄存器设置不好,但我不确定。Led连接在arduino引脚11上。多谢各位 #include <avr/io.h> #include <util/delay.h> const int delay=1000; void initialize_PWM() { TCCR0A|=(1<<WGM00)|(1<<WGM01)|(1<<
#include <avr/io.h>
#include <util/delay.h>
const int delay=1000;
void initialize_PWM()
{
TCCR0A|=(1<<WGM00)|(1<<WGM01)|(1<<COM0A1);
TCCR0B=1;
DDRB|=(1<<PB3);
}
void set_pwm(uint8_t data)
{
OCR0A=data;
}
int main (void)
{
initialize_PWM();
uint8_t brightness=200;
while(1)
{
for(brightness=0;brightness<255;brightness++)
{
set_pwm(brightness);
_delay_ms(1);
}
for(brightness=255;brightness>0;brightness--)
{
set_pwm(brightness);
_delay_ms(1);
}
}
return 0;
}
#包括
#包括
常数int延迟=1000;
无效初始化\u PWM()
{
TCCR0A |=(1您看过“淡入”示例程序了吗
/*
Fade
This example shows how to fade an LED on pin 9
using the analogWrite() function.
This example code is in the public domain.
*/
int led = 9; // the pin that the LED is attached to
int brightness = 0; // how bright the LED is
int fadeAmount = 5; // how many points to fade the LED by
// the setup routine runs once when you press reset:
void setup() {
// declare pin 9 to be an output:
pinMode(led, OUTPUT);
}
// the loop routine runs over and over again forever:
void loop() {
// set the brightness of pin 9:
analogWrite(led, brightness);
// change the brightness for next time through the loop:
brightness = brightness + fadeAmount;
// reverse the direction of the fading at the ends of the fade:
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
// wait for 30 milliseconds to see the dimming effect
delay(30);
}
请参见您的代码似乎正确,但您使用的是定时器0,它可以在Arduino UNO的引脚5和6上生成pwm,如数据表所示。因此,您应该使用
DDRD |=(1)你在使用什么arduino?为什么不直接使用arduino sdk和API(即analogWrite)?你这个变态!你为什么使用纯AVRC?我想你应该使用arduino方法来代替AVRC。我使用的是arduino Uno,当我使用纯AVRC时它会更漂亮。