Skip to content

Instantly share code, notes, and snippets.

@alexishida
Last active June 8, 2020 15:50
Show Gist options
  • Select an option

  • Save alexishida/5e2624a6ecf9e0c632b6 to your computer and use it in GitHub Desktop.

Select an option

Save alexishida/5e2624a6ecf9e0c632b6 to your computer and use it in GitHub Desktop.
Sketch feito para aumentar a frequência dos pinos PWM do Arduino
/*
Sketch feito para aumentar a frequência dos pinos PWM do Arduino Mega 2560 (serve para o Uno só tem que alterar os timers)
Autor: Alex Ishida
E-mail: [email protected]
Twitters: @alexishida
Data: 19/08/2014
Versão: 1.0.0
Documentação utilizada:
http://usethearduino.blogspot.com.br/2008/11/changing-pwm-frequency-on-arduino.html
http://playground.arduino.cc/Code/PwmFrequency
https://www.embarcados.com.br/timers-do-atmega328-no-arduino
http://www.avrbeginners.net/architecture/timers/timers.html
https://mil.ufl.edu/5666/handouts/AVR_PWM_Reg_Info_Summary.pdf
https://arduinoinfo.mywikis.net/wiki/Arduino-PWM-Frequency
*/
int led_status = 13; // Led da placa do arduino, utilizei só para saber se o timer 0 não tinha desregulado.
/* Pino dos canais pwm */
int led1 = 12;
int led2 = 11;
int led3 = 10;
int led4 = 9;
int led5 = 8;
/* Parametros para fazer o fade nos canais pwm */
int brightness = 0;
int fadeAmount = 5;
void setup() {
pinMode(led_status, OUTPUT);
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(led3, OUTPUT);
pinMode(led4, OUTPUT);
pinMode(led5, OUTPUT);
/*
Aqui eu altero a frequência dos timer 1,2,3,4
Se alterar o timer 0 irá influenciar nas funções de controle de tempo do Arduino ex.: delay();
*/
TCCR1B = (TCCR1B & 0xF8) | 0x01; // 31.374 KHz
TCCR2B = (TCCR2B & 0xF8) | 0x01; // 31.374 KHz
TCCR3B = (TCCR3B & 0xF8) | 0x01; // 31.374 KHz
TCCR4B = (TCCR4B & 0xF8) | 0x01; // 31.374 KHz
}
void loop() {
analogWrite(led1, brightness);
analogWrite(led2, brightness);
analogWrite(led3, brightness);
analogWrite(led4, brightness);
analogWrite(led5, brightness);
brightness = brightness + fadeAmount;
if (brightness == 0 || brightness == 255) {
fadeAmount = -fadeAmount ;
}
digitalWrite(led_status, HIGH);
delay(500);
digitalWrite(led_status, LOW);
delay(500);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment