eriksl wrote:If you're using Arduino, that's a whole other story. I am talking about the SDK, from C.
Yeh OK, I only use Arduino IDE and C++ . have not looked at SDK as such and won't unless I have to. Too much clutter in the head but I was trying to understand your post overall ,Thanks.
PWN just makes a square wave with a particular duty cycle. It is usually a hack to avoid having to use a DAC. This is especially helpful with the ESP because you don't have a ton of pins to interface with a DAC (which pretty much need the number of pins as bits of resolution... or you use a shift register to write to the DAC pins from just three pins.). The DAC could be a real DAC or an r2r ladder DAC made up of resistors.
You can write your own software PWM if you need to (interrupt timer to switch pin on an off at particular intervals). Arduino's PWM, i.e. analogWrite(), seems to work in my limited testing. It wanted 10bit values 0-1023.
pwm.h
Go to the documentation of this file.
00001 /*
00002 ChibiOS/RT - Copyright (C) 2006,2007,2008,2009,2010,
00003 2011,2012,2013 Giovanni Di Sirio.
00004
00005 This file is part of ChibiOS/RT.
00006
00007 ChibiOS/RT is free software; you can redistribute it and/or modify
00008 it under the terms of the GNU General Public License as published by
00009 the Free Software Foundation; either version 3 of the License, or
00010 (at your option) any later version.
00011
00012 ChibiOS/RT is distributed in the hope that it will be useful,
00013 but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
00015 GNU General Public License for more details.
00016
00017 You should have received a copy of the GNU General Public License
00018 along with this program. If not, see <http://www.gnu.org/licenses/>.
00019
00020 ---
00021
00022 A special exception to the GPL can be applied should you wish to distribute
00023 a combined work that includes ChibiOS/RT, without being obliged to provide
00024 the source code for any proprietary components. See the file exception.txt
00025 for full details of how and when the exception can be applied.
00026 */
00027
00028 /**
00029 * @file pwm.h
00030 * @brief PWM Driver macros and structures.
00031 *
00032 * @addtogroup PWM
00033 * @{
00034 */
00035
00036 #ifndef _PWM_H_
00037 #define _PWM_H_
00038
00039 #if HAL_USE_PWM || defined(__DOXYGEN__)
00040
00041 /*===========================================================================*/
00042 /* Driver constants. */
00043 /*===========================================================================*/
00044
00045 /**
00046 * @name PWM output mode macros
00047 * @{
00048 */
00049 /**
00050 * @brief Standard output modes mask.
00051 */
00052 #define PWM_OUTPUT_MASK 0x0F
00053
00054 /**
00055 * @brief Output not driven, callback only.
00056 */
00057 #define PWM_OUTPUT_DISABLED 0x00
00058
00059 /**
00060 * @brief Positive PWM logic, active is logic level one.
00061 */
00062 #define PWM_OUTPUT_ACTIVE_HIGH 0x01
00063
00064 /**
00065 * @brief Inverse PWM logic, active is logic level zero.
00066 */
00067 #define PWM_OUTPUT_ACTIVE_LOW 0x02
00068 /** @} */
00069
00070 /*===========================================================================*/
00071 /* Driver pre-compile time settings. */
00072 /*===========================================================================*/
00073
00074 /*===========================================================================*/
00075 /* Derived constants and error checks. */
00076 /*===========================================================================*/
00077
00078 /*===========================================================================*/
00079 /* Driver data structures and types. */
00080 /*===========================================================================*/
00081
00082 /**
00083 * @brief Driver state machine possible states.
00084 */
00085 typedef enum {
00086 PWM_UNINIT = 0, /**< Not initialized. */
00087 PWM_STOP = 1, /**< Stopped. */
00088 PWM_READY = 2, /**< Ready. */
00089 } pwmstate_t;
00090
00091 /**
00092 * @brief Type of a structure representing a PWM driver.
00093 */
00094 typedef struct PWMDriver PWMDriver;
00095
00096 /**
00097 * @brief PWM notification callback type.
00098 *
00099 * @param[in] pwmp pointer to a @p PWMDriver object
00100 */
00101 typedef void (*pwmcallback_t)(PWMDriver *pwmp);
00102
00103 #include "pwm_lld.h"
00104
00105 /*===========================================================================*/
00106 /* Driver macros. */
00107 /*===========================================================================*/
00108
00109 /**
00110 * @name PWM duty cycle conversion
00111 * @{
00112 */
00113 /**
00114 * @brief Converts from fraction to pulse width.
00115 * @note Be careful with rounding errors, this is integer math not magic.
00116 * You can specify tenths of thousandth but make sure you have the
00117 * proper hardware resolution by carefully choosing the clock source
00118 * and prescaler settings, see @p PWM_COMPUTE_PSC.
00119 *
00120 * @param[in] pwmp pointer to a @p PWMDriver object
00121 * @param[in] denominator denominator of the fraction
00122 * @param[in] numerator numerator of the fraction
00123 * @return The pulse width to be passed to @p pwmEnableChannel().
00124 *
00125 * @api
00126 */
00127 #define PWM_FRACTION_TO_WIDTH(pwmp, denominator, numerator) \
00128 ((pwmcnt_t)((((pwmcnt_t)(pwmp)->period) * \
00129 (pwmcnt_t)(numerator)) / (pwmcnt_t)(denominator)))
00130
00131 /**
00132 * @brief Converts from degrees to pulse width.
00133 * @note Be careful with rounding errors, this is integer math not magic.
00134 * You can specify hundredths of degrees but make sure you have the
00135 * proper hardware resolution by carefully choosing the clock source
00136 * and prescaler settings, see @p PWM_COMPUTE_PSC.
00137 *
00138 * @param[in] pwmp pointer to a @p PWMDriver object
00139 * @param[in] degrees degrees as an integer between 0 and 36000
00140 * @return The pulse width to be passed to @p pwmEnableChannel().
00141 *
00142 * @api
00143 */
00144 #define PWM_DEGREES_TO_WIDTH(pwmp, degrees) \
00145 PWM_FRACTION_TO_WIDTH(pwmp, 36000, degrees)
00146
00147 /**
00148 * @brief Converts from percentage to pulse width.
00149 * @note Be careful with rounding errors, this is integer math not magic.
00150 * You can specify tenths of thousandth but make sure you have the
00151 * proper hardware resolution by carefully choosing the clock source
00152 * and prescaler settings, see @p PWM_COMPUTE_PSC.
00153 *
00154 * @param[in] pwmp pointer to a @p PWMDriver object
00155 * @param[in] percentage percentage as an integer between 0 and 10000
00156 * @return The pulse width to be passed to @p pwmEnableChannel().
00157 *
00158 * @api
00159 */
00160 #define PWM_PERCENTAGE_TO_WIDTH(pwmp, percentage) \
00161 PWM_FRACTION_TO_WIDTH(pwmp, 10000, percentage)
00162 /** @} */
00163
00164 /**
00165 * @name Macro Functions
00166 * @{
00167 */
00168 /**
00169 * @brief Changes the period the PWM peripheral.
00170 * @details This function changes the period of a PWM unit that has already
00171 * been activated using @p pwmStart().
00172 * @pre The PWM unit must have been activated using @p pwmStart().
00173 * @post The PWM unit period is changed to the new value.
00174 * @note If a period is specified that is shorter than the pulse width
00175 * programmed in one of the channels then the behavior is not
00176 * guaranteed.
00177 *
00178 * @param[in] pwmp pointer to a @p PWMDriver object
00179 * @param[in] value new cycle time in ticks
00180 *
00181 * @iclass
00182 */
00183 #define pwmChangePeriodI(pwmp, value) { \
00184 (pwmp)->period = (value); \
00185 pwm_lld_change_period(pwmp, value); \
00186 }
00187
00188 /**
00189 * @brief Enables a PWM channel.
00190 * @pre The PWM unit must have been activated using @p pwmStart().
00191 * @post The channel is active using the specified configuration.
00192 * @note Depending on the hardware implementation this function has
00193 * effect starting on the next cycle (recommended implementation)
00194 * or immediately (fallback implementation).
00195 *
00196 * @param[in] pwmp pointer to a @p PWMDriver object
00197 * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1)
00198 * @param[in] width PWM pulse width as clock pulses number
00199 *
00200 * @iclass
00201 */
00202 #define pwmEnableChannelI(pwmp, channel, width) \
00203 pwm_lld_enable_channel(pwmp, channel, width)
00204
00205 /**
00206 * @brief Disables a PWM channel.
00207 * @pre The PWM unit must have been activated using @p pwmStart().
00208 * @post The channel is disabled and its output line returned to the
00209 * idle state.
00210 * @note Depending on the hardware implementation this function has
00211 * effect starting on the next cycle (recommended implementation)
00212 * or immediately (fallback implementation).
00213 *
00214 * @param[in] pwmp pointer to a @p PWMDriver object
00215 * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1)
00216 *
00217 * @iclass
00218 */
00219 #define pwmDisableChannelI(pwmp, channel) \
00220 pwm_lld_disable_channel(pwmp, channel)
00221
00222 /**
00223 * @brief Returns a PWM channel status.
00224 * @pre The PWM unit must have been activated using @p pwmStart().
00225 *
00226 * @param[in] pwmp pointer to a @p PWMDriver object
00227 * @param[in] channel PWM channel identifier (0...PWM_CHANNELS-1)
00228 *
00229 * @iclass
00230 */
00231 #define pwmIsChannelEnabledI(pwmp, channel) \
00232 pwm_lld_is_channel_enabled(pwmp, channel)
00233 /** @} */
00234
00235 /*===========================================================================*/
00236 /* External declarations. */
00237 /*===========================================================================*/
00238
00239 #ifdef __cplusplus
00240 extern "C" {
00241 #endif
00242 void pwmInit(void);
00243 void pwmObjectInit(PWMDriver *pwmp);
00244 void pwmStart(PWMDriver *pwmp, const PWMConfig *config);
00245 void pwmStop(PWMDriver *pwmp);
00246 void pwmChangePeriod(PWMDriver *pwmp, pwmcnt_t period);
00247 void pwmEnableChannel(PWMDriver *pwmp,
00248 pwmchannel_t channel,
00249 pwmcnt_t width);
00250 void pwmDisableChannel(PWMDriver *pwmp, pwmchannel_t channel);
00251 #ifdef __cplusplus
00252 }
00253 #endif
00254
00255 #endif /* HAL_USE_PWM */
00256
00257 #endif /* _PWM_H_ */
00258
00259 /** @} */
pwm.h
Get ChibiOS/RT embedded RTOS at SourceForge.net. Fast, secure and Free Open Source software downloads