ШИМ гаснет светодиод на ардуино
Я пытаюсь моргнуть светодиодом с ШИМ на Arduino, и я не знаю, что не так. Но мой светодиод не гаснет. Что случилось? Я думаю, что у меня плохие настройки регистров, но я не уверен. Led подключен на Arduino контакт 11. Спасибо.
#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;
}
2 ответа
Ваш код кажется правильным, но вы используете timer0, который может генерировать pwm на выводах Arduino UNO 5 и 6, как показано в таблице данных. Таким образом, вы должны установить бит 6 ddrd с DDRD |= (1 << PD6), то есть выводом 6 на Arduino, а не выводом 11. Если вы хотите использовать pwm на выводе 11, вы должны вместо этого использовать timer2.
Вы смотрели пример программы Fade?
/*
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);
}
Смотрите http://arduino.cc/en/Tutorial/Fade