Coder Social home page Coder Social logo

matzefriedrich / arduino-pwm-frequency-library Goto Github PK

View Code? Open in Web Editor NEW
5.0 3.0 0.0 19 KB

A fork of the well-known "PWM frequency library" initially created by Sam Knight

License: MIT License

C 40.69% C++ 59.31%
arduino pwm arduino-library atmega328p atmega328 atmega168 atmega48 atmega88 frequency

arduino-pwm-frequency-library's Introduction

Arduino PWM frequency library

This is a fork (backup) of the well-known "PWM frequency library" initially created by Sam Knight; see http://code.google.com/p/arduino-pwm-frequency-library/downloads/list for further information on the original project. As Sam stated in his post, "the internet is full of partial examples and code snippets for changing PWM frequency", which makes it quite hard for beginners to get started with custom PWM settings, and it was the same for me. This library is great for its simplicity and a good starting point for creating prototype software for the Arduino. Since the Google Code Hosting service retired in 2016 I moved the code into this GitHub repository.

Global functions

Method Description
InitTimers() Initializes all timers. Needs to be called before changing the timers frequency or setting the duty on a pin
InitTimersSafe() Same as InitTimers() except timer 0 is not initialized in order to preserve time keeping functions.
pwmWrite(uint8_t pin, uint8_t val) Same as analogWrite(), but it only works with initialized timers. Continue to use analogWrite() on uninitialized timers
SetPinFrequency(int8_t pin, int32_t frequency) Sets the pin's frequency (in Hz) and returns a bool for success
SetPinFrequencySafe(int8_t pin, int32_t frequency) Same as SetPinFrequency except it does not affect timer 0

Timer functions

The library also has five functions for each Timer object. I could not get the code size down to what I felt was reasonable so I ditched C++ classes and did some fancy macro work instead. Each of these functions are technically preprocessor macros with nice self explanatory names that swap out for more cryptic functions inside the library header just before compile time. For timer 1 the functions are:

Method Description
Timer1_GetFrequency() Gets the timer's frequency in Hz.
Timer1_SetFrequency(int frequency) Sets the timer's frequency in Hz.
Timer1_GetPrescaler() Gets the value (not bits) of the prescaler. Don't know what this means? Don't worry about it, just use SetFrequency(int frequency).
Timer1_SetPrescaler(enum value) Sets the prescaler.
Timer1_GetTop() Gets the timer register's maximum value.
Timer1_SetTop(int top) Sets the timer register's maximum value.
Timer1_Initialize() Initializes the timer.

The prescaler is inconsistent among different timers. I figured using enumerators was the best solution because most types of invalid input will be caught at compile time. For a normal timer, use one of these as a parameter: ps_1, ps_8, ps_64, ps_256, ps_1024. If those give a type error, then the timer you are using is one of the inconsistent ones, and you should use psalt_1, psalt_8, psalt_32, psalt_64, psalt_128, psalt_256, or psalt_1024 instead.

arduino-pwm-frequency-library's People

Contributors

matzefriedrich avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

arduino-pwm-frequency-library's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.