initial commit
This commit is contained in:
67
PMWFrequency/PWMFrequency.ino
Normal file
67
PMWFrequency/PWMFrequency.ino
Normal file
@@ -0,0 +1,67 @@
|
|||||||
|
/*
|
||||||
|
Fade
|
||||||
|
|
||||||
|
This example shows how to fade an LED on pin 9
|
||||||
|
using the analogWrite() function.
|
||||||
|
|
||||||
|
The analogWrite() function uses PWM, so if
|
||||||
|
you want to change the pin you're using, be
|
||||||
|
sure to use another PWM capable pin. On most
|
||||||
|
Arduino, the PWM pins are identified with
|
||||||
|
a "~" sign, like ~3, ~5, ~6, ~9, ~10 and ~11.
|
||||||
|
|
||||||
|
This example code is in the public domain.
|
||||||
|
*/
|
||||||
|
|
||||||
|
//
|
||||||
|
// =======================================================================================================
|
||||||
|
// INCLUDE LIRBARIES
|
||||||
|
// =======================================================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
#include <PWMFrequency.h> // https://github.com/TheDIYGuy999/PWMFrequency
|
||||||
|
|
||||||
|
//
|
||||||
|
// =======================================================================================================
|
||||||
|
// PIN ASSIGNMENTS & GLOBAL VARIABLES
|
||||||
|
// =======================================================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
int led = 9; // the PWM pin the LED is attached to
|
||||||
|
int brightness = 0; // how bright the LED is
|
||||||
|
int fadeAmount = 5; // how many points to fade the LED by
|
||||||
|
|
||||||
|
///
|
||||||
|
// =======================================================================================================
|
||||||
|
// MAIN ARDUINO SETUP (1x during startup)
|
||||||
|
// =======================================================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
void setup() {
|
||||||
|
// declare pin 9 to be an output:
|
||||||
|
pinMode(led, OUTPUT);
|
||||||
|
|
||||||
|
// PWM frequency: 32 = 984Hz (default), 8 = 3936Hz, 1 = 31488Hz
|
||||||
|
setPWMPrescaler(9, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
//
|
||||||
|
// =======================================================================================================
|
||||||
|
// MAIN LOOP
|
||||||
|
// =======================================================================================================
|
||||||
|
//
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
150
PWMFrequency.cpp
Normal file
150
PWMFrequency.cpp
Normal file
@@ -0,0 +1,150 @@
|
|||||||
|
#include <Arduino.h>
|
||||||
|
#include "PWMFrequency.h"
|
||||||
|
|
||||||
|
|
||||||
|
void setPWMPrescaler(uint8_t pin, uint16_t prescale)
|
||||||
|
{
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divides a given PWM pin frequency by a divisor.
|
||||||
|
*
|
||||||
|
* Arduino Leonardo AtMega 32u4 specific
|
||||||
|
*
|
||||||
|
* Sets the Prescaler (Divisor) for a given PWM pin. The resulting frequency
|
||||||
|
* is equal to the base frequency divided by the given divisor:
|
||||||
|
* - Base frequencies:
|
||||||
|
* o The base frequency for pins 3 and 11 is 64,500 Hz.
|
||||||
|
* o The base frequency for pins 5,6,9,10,13 is 31,250 Hz.
|
||||||
|
* - Divisors:
|
||||||
|
* o The divisors available on pins 3, 5, 9, 10 and 11 are: 1, 8, 64, 256, and 1024.
|
||||||
|
* o The divisors available on pins 6 and 13 are all powers of two between 1 and 16384
|
||||||
|
*
|
||||||
|
* PWM frequencies are tied together in pairs of pins. If one in a
|
||||||
|
* pair is changed, the other is also changed to match:
|
||||||
|
* - Pins 3 and 11 are paired on timer0 8bit (Default prescale=64, Freq=977Hz)
|
||||||
|
* - Pins 9 and 10 are paired on timer1 16bit (Default prescale=64, Freq=490Hz)
|
||||||
|
* - Pins 5 is exclusivly on timer3 16bit (Default prescale=64, Freq=490Hz)
|
||||||
|
* - Pins 6 and 13 are paired on timer4 10bit (default prescale=64, Freq=490Hz)
|
||||||
|
*
|
||||||
|
* Note: Pins 3 and 11 operate on Timer 0 changes this pins will
|
||||||
|
* affect the user of the main time millis() functions
|
||||||
|
*
|
||||||
|
* Thanks to MacTester of the TonyMacX86 forums for his work in defining
|
||||||
|
* the timings and testing this library
|
||||||
|
*/
|
||||||
|
|
||||||
|
//byte mode;
|
||||||
|
#if defined __AVR_ATmega32U4__
|
||||||
|
if(pin==3 || pin==5 || pin==9 || pin==10 || pin==11) {
|
||||||
|
switch(prescale) {
|
||||||
|
case 1: mode = 0b001; break;
|
||||||
|
case 8: mode = 0b010; break;
|
||||||
|
case 64: mode = 0b011; break;
|
||||||
|
case 256: mode = 0b100; break;
|
||||||
|
case 1024: mode = 0b101; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
} else if(pin==6 || pin==13) {
|
||||||
|
switch(prescale) {
|
||||||
|
case 1: mode = 0b0001; break;
|
||||||
|
case 2: mode = 0b0010; break;
|
||||||
|
case 4: mode = 0b0011; break;
|
||||||
|
case 8: mode = 0b0100; break;
|
||||||
|
case 16: mode = 0b0101; break;
|
||||||
|
case 32: mode = 0b0110; break;
|
||||||
|
case 64: mode = 0b0111; break;
|
||||||
|
case 128: mode = 0b1000; break;
|
||||||
|
case 256: mode = 0b1001; break;
|
||||||
|
case 512: mode = 0b1010; break;
|
||||||
|
case 1024: mode = 0b1011; break;
|
||||||
|
case 2048: mode = 0b1100; break;
|
||||||
|
case 4096: mode = 0b1101; break;
|
||||||
|
case 8192: mode = 0b1110; break;
|
||||||
|
case 16384: mode = 0b1111; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pin==3 || pin==11) {
|
||||||
|
TCCR0B = TCCR1B & 0b11111000 | mode;
|
||||||
|
} else if (pin==9 || pin==10) {
|
||||||
|
TCCR1B = TCCR1B & 0b11111000 | mode;
|
||||||
|
} else if (pin==5) {
|
||||||
|
TCCR3B = TCCR3B & 0b11111000 | mode;
|
||||||
|
} else if (pin==6 || pin==13) {
|
||||||
|
TCCR4B = TCCR4B & 0b11110000 | mode;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined (STM32F1xx)
|
||||||
|
//stm pwm config
|
||||||
|
#else
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Divides a given PWM pin frequency by a divisor.
|
||||||
|
*
|
||||||
|
* Sets the Prescaler (Divisor) for a given PWM pin. The resulting frequency
|
||||||
|
* is equal to the base frequency divided by the given divisor:
|
||||||
|
* - Base frequencies:
|
||||||
|
* o The base frequency for pins 5 and 6 is 62500 Hz.
|
||||||
|
* o The base frequency for pins 3, 9, 10, and 11 is 31250 Hz.
|
||||||
|
* - Divisors:
|
||||||
|
* o The divisors available on pins 5, 6, 9, 10 are: 1,8,64,256,1024.
|
||||||
|
* o The divisors available on pins 3, 11 are: 1,8,32,64,128,256,1024.
|
||||||
|
*
|
||||||
|
* PWM frequencies are tied together in pairs of pins. If one in a
|
||||||
|
* pair is changed, the other is also changed to match:
|
||||||
|
* - Pins 5 and 6 are paired on timer0 (Default prescale=64, Freq=977Hz)
|
||||||
|
* - Pins 9 and 10 are paired on timer1 (Default prescale=64, Freq=490Hz)
|
||||||
|
* - Pins 3 and 11 are paired on timer2 (Default prescale=64, Freq=490Hz)
|
||||||
|
*
|
||||||
|
* Note that this function will have side effects on anything else
|
||||||
|
* that uses timers:
|
||||||
|
* - Changes on pins 5, or 6 may cause the delay() and
|
||||||
|
* millis() functions to stop working. Other timing-related
|
||||||
|
* functions may also be affected.
|
||||||
|
* - Changes on pins 9 or 10 will cause the Servo library to function
|
||||||
|
* incorrectly.
|
||||||
|
*
|
||||||
|
* Thanks to macegr of the Arduino forums for his documentation of the
|
||||||
|
* PWM frequency divisors. His post can be viewed at:
|
||||||
|
*
|
||||||
|
* http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
|
||||||
|
*/
|
||||||
|
|
||||||
|
if(pin == 5 || pin == 6 || pin == 9 || pin == 10) {
|
||||||
|
switch(prescale) {
|
||||||
|
case 1: mode = 0b001; break;
|
||||||
|
case 8: mode = 0b010; break;
|
||||||
|
case 64: mode = 0b011; break;
|
||||||
|
case 256: mode = 0b100; break;
|
||||||
|
case 1024: mode = 0b101; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
|
||||||
|
} else if(pin == 3 || pin == 11) {
|
||||||
|
switch(prescale) {
|
||||||
|
case 1: mode = 0b001; break;
|
||||||
|
case 8: mode = 0b010; break;
|
||||||
|
case 32: mode = 0b011; break;
|
||||||
|
case 64: mode = 0b100; break;
|
||||||
|
case 128: mode = 0b101; break;
|
||||||
|
case 256: mode = 0b110; break;
|
||||||
|
case 1024: mode = 0b111; break;
|
||||||
|
default: return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if(pin==5 || pin==6) {
|
||||||
|
TCCR0B = TCCR0B & 0b11111000 | mode;
|
||||||
|
} else if (pin==9 || pin==10) {
|
||||||
|
TCCR1B = TCCR1B & 0b11111000 | mode;
|
||||||
|
} else if (pin==3 || pin==11) {
|
||||||
|
TCCR2B = TCCR2B & 0b11111000 | mode;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#endif
|
||||||
|
}
|
||||||
24
PWMFrequency.h
Normal file
24
PWMFrequency.h
Normal file
@@ -0,0 +1,24 @@
|
|||||||
|
|
||||||
|
//
|
||||||
|
// -----------------------
|
||||||
|
// PWM FREQUENCY PRESCALER
|
||||||
|
// -----------------------
|
||||||
|
// Written by Kiwisincebirth 2014
|
||||||
|
// Revised by MacTester57 (aka TheDIYGuy999) to allow correct PWM frequencies (confirmed with oscilloscope). January 2015
|
||||||
|
// - prescale variable: replaced uint8_t with uint16_t data type (fixes bug, which did not allow frequencies < 492Hz)
|
||||||
|
// - mode variable: replaced hex format with binary to make it more readable, if compared with bit tables in the 32U4 manual
|
||||||
|
// - added comments
|
||||||
|
//
|
||||||
|
|
||||||
|
#ifndef PWMFrequency
|
||||||
|
#define PWMFrequency
|
||||||
|
|
||||||
|
#if ARDUINO >= 100
|
||||||
|
#include <Arduino.h> // Arduino 1.0
|
||||||
|
#else
|
||||||
|
#include <WProgram.h> // Arduino 0022
|
||||||
|
#endif
|
||||||
|
|
||||||
|
void setPWMPrescaler(uint8_t pin, uint16_t prescale);
|
||||||
|
|
||||||
|
#endif
|
||||||
52
README.md
Normal file
52
README.md
Normal file
@@ -0,0 +1,52 @@
|
|||||||
|
PWM Frequency Arduino Library
|
||||||
|
=============================
|
||||||
|
|
||||||
|
Library for Setting the PWM Frequency
|
||||||
|
|
||||||
|
Written by kiwisincebirth 2014
|
||||||
|
|
||||||
|
Please keep in mind that changing the PWM frequency changes the Atmega's timers and disrupts the normal operation of many functions that rely on time (delay(), millis(), Servo library).
|
||||||
|
|
||||||
|
This Library supports both ATMega 328 and 32U4 (Leonardo)
|
||||||
|
|
||||||
|
Example
|
||||||
|
=======
|
||||||
|
|
||||||
|
Here are some usage examples of the function:
|
||||||
|
|
||||||
|
// Set pin 9's PWM frequency to 3906 Hz (31250/8 = 3906)
|
||||||
|
// Note that the base frequency for pins 3, 9, 10, and 11 is 31250 Hz
|
||||||
|
setPwmFrequency(9, 8);
|
||||||
|
|
||||||
|
// Set pin 6's PWM frequency to 62500 Hz (62500/1 = 62500)
|
||||||
|
// Note that the base frequency for pins 5 and 6 is 62500 Hz
|
||||||
|
setPwmFrequency(6, 1);
|
||||||
|
|
||||||
|
// Set pin 10's PWM frequency to 31 Hz (31250/1024 = 31)
|
||||||
|
setPwmFrequency(10, 1024);
|
||||||
|
|
||||||
|
See
|
||||||
|
===
|
||||||
|
|
||||||
|
http://playground.arduino.cc/Code/PwmFrequency
|
||||||
|
|
||||||
|
Thanks to macegr of the Arduino forums for his documentation of the PWM frequency divisors. His post can be viewed at:
|
||||||
|
http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1235060559/0#4
|
||||||
|
|
||||||
|
Thanks to MacTester57 (aka TheDIYGuy999) for providing ATMega 32U4 testing
|
||||||
|
http://www.tonymacx86.com/imac-mods/107859-kiwis-next-project-imac-g5-10.html#post765116
|
||||||
|
|
||||||
|
Version History
|
||||||
|
===============
|
||||||
|
|
||||||
|
2.0 2014-07-30 Added Support for ATMega 32U4 Leonardo timers
|
||||||
|
|
||||||
|
Because it looks like, that this repo (https://github.com/kiwisincebirth/Arduino/tree/master/libraries/PWMFrequency) is not maintained anymore, I've cloned it.
|
||||||
|
|
||||||
|
The following changes were done by me, TheDIYGuy999:
|
||||||
|
|
||||||
|
New in V 2.1:
|
||||||
|
- Syntax coloration enhanced
|
||||||
|
- folder structure changed, so you can download and install it as usual
|
||||||
|
|
||||||
|
|
||||||
25
keywords.txt
Normal file
25
keywords.txt
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
#######################################
|
||||||
|
# Syntax Coloring Map LowPower
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Datatypes (KEYWORD1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
PWMFrequency KEYWORD1
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Methods and Functions (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
setPWMPrescaler KEYWORD2
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Instances (KEYWORD2)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
# Constants (LITERAL1)
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
|
||||||
Reference in New Issue
Block a user