\section{aversive\_\-10-03-12/modules/hardware/pwm\_\-ng/pwm\_\-ng.h File Reference}
\label{pwm__ng_8h}\index{aversive\_\-10-03-12/modules/hardware/pwm\_\-ng/pwm\_\-ng.h@{aversive\_\-10-03-12/modules/hardware/pwm\_\-ng/pwm\_\-ng.h}}
{\tt \#include $<$aversive.h$>$}\par
{\tt \#include $<$aversive/timers.h$>$}\par
{\tt \#include $<$aversive/parts.h$>$}\par
\subsection*{Data Structures}
\begin{CompactItemize}
\item 
struct {\bf pwm\_\-ng}
\end{CompactItemize}
\subsection*{Defines}
\begin{CompactItemize}
\item 
\#define {\bf PWM\_\-NG\_\-MODE\_\-NORMAL}~0x00
\item 
\#define {\bf PWM\_\-NG\_\-MODE\_\-REVERSE}~0x01
\item 
\#define {\bf PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED}~0x02
\item 
\#define {\bf PWM\_\-NG\_\-MODE\_\-SIGNED}~0x04
\item 
\#define {\bf PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN}~0x08
\item 
\#define {\bf PWM\_\-NG\_\-TIMER\_\-0\_\-MODE}
\item 
\#define {\bf PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT}(n, t\_\-mode, prsc)
\item 
\#define {\bf PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B}(n, t\_\-mode, prsc)
\item 
\#define {\bf PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT}(n, t\_\-mode, prsc)
\item 
\#define {\bf PWM\_\-NG\_\-INIT8}(pwm, n, pwm\_\-nbits, pwm\_\-mode, sign\_\-port, sign\_\-bit)
\item 
\#define {\bf PWM\_\-NG\_\-INIT8\_\-B}(pwm, n, m, pwm\_\-nbits, pwm\_\-mode, sign\_\-port, sign\_\-bit)
\item 
\#define {\bf PWM\_\-NG\_\-INIT16}(pwm, n, m, pwm\_\-nbits, pwm\_\-mode, sign\_\-port, sign\_\-bit)
\end{CompactItemize}
\subsection*{Functions}
\begin{CompactItemize}
\item 
void {\bf pwm\_\-ng\_\-init} (struct {\bf pwm\_\-ng} $\ast$pwm, uint8\_\-t timer\_\-nbits, uint8\_\-t pwm\_\-nbits, uint8\_\-t pwm\_\-mode, volatile void $\ast$ocrn, uint8\_\-t com0, volatile uint8\_\-t $\ast$tccrn, volatile uint8\_\-t $\ast$pwm\_\-port, uint8\_\-t pwm\_\-bit, volatile uint8\_\-t $\ast$sign\_\-port, uint8\_\-t sign\_\-bit)
\item 
void {\bf pwm\_\-ng\_\-set} (void $\ast$pwm, int32\_\-t value)
\end{CompactItemize}


\subsection{Define Documentation}
\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-INIT16@{PWM\_\-NG\_\-INIT16}}
\index{PWM\_\-NG\_\-INIT16@{PWM\_\-NG\_\-INIT16}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-INIT16]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-INIT16(pwm, \/  n, \/  m, \/  pwm\_\-nbits, \/  pwm\_\-mode, \/  sign\_\-port, \/  sign\_\-bit)}\label{pwm__ng_8h_baed42251af85bffdfce84db38033dc3}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                                                            \
                pwm_ng_init(pwm, 16, pwm_nbits, pwm_mode, &OCR##n##m,   \
                            COM##n##m##0, &TCCR##n##A,                  \
                            &OC##n##m##_PORT, OC##n##m##_BIT,           \
                            sign_port, sign_bit);                       \
        } while(0)
\end{verbatim}
\end{Code}


Definition at line 160 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-INIT8@{PWM\_\-NG\_\-INIT8}}
\index{PWM\_\-NG\_\-INIT8@{PWM\_\-NG\_\-INIT8}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-INIT8]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-INIT8(pwm, \/  n, \/  pwm\_\-nbits, \/  pwm\_\-mode, \/  sign\_\-port, \/  sign\_\-bit)}\label{pwm__ng_8h_4a6dbe41f581170242e319fcc43159af}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                                                            \
                pwm_ng_init(pwm, 8, pwm_nbits, pwm_mode, &OCR##n,       \
                            COM##n##0, &TCCR##n, &OC##n##_PORT,         \
                            OC##n##_BIT, sign_port, sign_bit);          \
        } while(0)
\end{verbatim}
\end{Code}


Definition at line 146 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-INIT8\_\-B@{PWM\_\-NG\_\-INIT8\_\-B}}
\index{PWM\_\-NG\_\-INIT8\_\-B@{PWM\_\-NG\_\-INIT8\_\-B}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-INIT8\_\-B]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-INIT8\_\-B(pwm, \/  n, \/  m, \/  pwm\_\-nbits, \/  pwm\_\-mode, \/  sign\_\-port, \/  sign\_\-bit)}\label{pwm__ng_8h_a6642271b8f5d20cc7fb74012aa2e203}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                                                            \
                pwm_ng_init(pwm, 8, pwm_nbits, pwm_mode, &OCR##n##m,    \
                            COM##n##m##0, &TCCR##n##A, &OC##n##m##_PORT, \
                            OC##n##m##_BIT, sign_port, sign_bit);       \
        } while(0)
\end{verbatim}
\end{Code}


Definition at line 153 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-MODE\_\-NORMAL@{PWM\_\-NG\_\-MODE\_\-NORMAL}}
\index{PWM\_\-NG\_\-MODE\_\-NORMAL@{PWM\_\-NG\_\-MODE\_\-NORMAL}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-MODE\_\-NORMAL]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-MODE\_\-NORMAL~0x00}\label{pwm__ng_8h_19c0c15535e1cf4ebd54da13588b9c50}




Definition at line 41 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-MODE\_\-REVERSE@{PWM\_\-NG\_\-MODE\_\-REVERSE}}
\index{PWM\_\-NG\_\-MODE\_\-REVERSE@{PWM\_\-NG\_\-MODE\_\-REVERSE}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-MODE\_\-REVERSE]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-MODE\_\-REVERSE~0x01}\label{pwm__ng_8h_6184e1ff1cc8ff791245f5cb10821f48}




Definition at line 42 of file pwm\_\-ng.h.

Referenced by pwm\_\-ng\_\-init().\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED@{PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED}}
\index{PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED@{PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED~0x02}\label{pwm__ng_8h_dfaf7b4818b19c28ce6cfa5913d39ac2}




Definition at line 43 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-MODE\_\-SIGNED@{PWM\_\-NG\_\-MODE\_\-SIGNED}}
\index{PWM\_\-NG\_\-MODE\_\-SIGNED@{PWM\_\-NG\_\-MODE\_\-SIGNED}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-MODE\_\-SIGNED]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-MODE\_\-SIGNED~0x04}\label{pwm__ng_8h_74dac6b9b2c40d87bdabc78704ab73d7}




Definition at line 44 of file pwm\_\-ng.h.

Referenced by pwm\_\-ng\_\-init(), and pwm\_\-ng\_\-set().\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN@{PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN}}
\index{PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN@{PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-MODE\_\-SPECIAL\_\-SIGN~0x08}\label{pwm__ng_8h_7157e76c5a26960f18631a1606a9937d}




Definition at line 45 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-TIMER\_\-0\_\-MODE@{PWM\_\-NG\_\-TIMER\_\-0\_\-MODE}}
\index{PWM\_\-NG\_\-TIMER\_\-0\_\-MODE@{PWM\_\-NG\_\-TIMER\_\-0\_\-MODE}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-TIMER\_\-0\_\-MODE]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-TIMER\_\-0\_\-MODE}\label{pwm__ng_8h_b3b003c06c76cacea10d80be7f49935f}




Definition at line 50 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT@{PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT}}
\index{PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT@{PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-TIMER\_\-16BITS\_\-INIT(n, \/  t\_\-mode, \/  prsc)}\label{pwm__ng_8h_827600088cac10e83bbfca6d0a743cd9}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                    \
                TCCR##n##A &= ~((1 << WGM##n##0) | (1 << WGM##n##1));   \
                TCCR##n##A |= ((t_mode & 0x01) ? (1 << WGM##n##0):0) |  \
                        ((t_mode & 0x02) ? (1 << WGM##n##1):0);         \
                TCCR##n##B = ((t_mode & 0x04) ? (1 << WGM##n##2):0) |   \
                        ((t_mode & 0x08 ) ? (1 << WGM##n##3):0) |       \
                        (prsc << CS##n##0);                             \
        } while(0)
\end{verbatim}
\end{Code}
Initialize a 16 bits timer to do a PWM. This macro must be called before \doxyref{pwm\_\-ng\_\-init()}{p.}{pwm__ng_8c_ec71511876da9c859dd174f8d01db8cb}. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em n}]is the number of the timer. \item[{\em timer\_\-mode}]can be TIMER\_\-16\_\-MODE\_\-PWM\_\-PC, TIMER\_\-16\_\-MODE\_\-CTC, TIMER\_\-16\_\-MODE\_\-PWM... \item[{\em prsc}]is the value to be loaded in the prescaler register for this timer. For instance, TIMER1\_\-PRESCALER\_\-DIV\_\-256. \end{description}
\end{Desc}


Definition at line 103 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT@{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT}}
\index{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT@{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT(n, \/  t\_\-mode, \/  prsc)}\label{pwm__ng_8h_1e835d3b6217724481ae6ad305cc8af6}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                                                            \
                TCCR##n =  ( ((t_mode & 0x01) ? (1 << WGM##n##0):0)  |  \
                             ((t_mode & 0x02) ? (1 << WGM##n##1):0)  |  \
                             ((prsc << CS##n##0)) );                    \
        } while(0)
\end{verbatim}
\end{Code}
Initialize a 8 bits timer to do a PWM. This macro must be called before \doxyref{pwm\_\-ng\_\-init()}{p.}{pwm__ng_8c_ec71511876da9c859dd174f8d01db8cb}. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em n}]is the number of the timer. \item[{\em t\_\-mode}]can be TIMER\_\-8\_\-MODE\_\-PWM\_\-PC, TIMER\_\-8\_\-MODE\_\-CTC, TIMER\_\-8\_\-MODE\_\-PWM. \item[{\em prsc}]is the value to be loaded in the prescaler register for this timer. For instance, TIMER0\_\-PRESCALER\_\-DIV\_\-256. \end{description}
\end{Desc}


Definition at line 75 of file pwm\_\-ng.h.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B@{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B}}
\index{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B@{PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B]{\setlength{\rightskip}{0pt plus 5cm}\#define PWM\_\-NG\_\-TIMER\_\-8BITS\_\-INIT\_\-B(n, \/  t\_\-mode, \/  prsc)}\label{pwm__ng_8h_a781c7cbc4605c80844209017df60c70}


\textbf{Value:}

\begin{Code}\begin{verbatim}do {                                                            \
                TCCR##n##A =  ( ((t_mode & 0x01) ? (1 << WGM##n##0):0)  | \
                                ((t_mode & 0x02) ? (1 << WGM##n##1):0)  | \
                                ((prsc << CS##n##0)) );                 \
                TCCR##n##B = (prsc << CS##n##0);                        \
        } while(0)
\end{verbatim}
\end{Code}
same, but with timer8 with several OCx (like on atm2560) 

Definition at line 85 of file pwm\_\-ng.h.

\subsection{Function Documentation}
\index{pwm\_\-ng.h@{pwm\_\-ng.h}!pwm\_\-ng\_\-init@{pwm\_\-ng\_\-init}}
\index{pwm\_\-ng\_\-init@{pwm\_\-ng\_\-init}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[pwm\_\-ng\_\-init]{\setlength{\rightskip}{0pt plus 5cm}void pwm\_\-ng\_\-init (struct {\bf pwm\_\-ng} $\ast$ {\em pwm}, \/  uint8\_\-t {\em timer\_\-nbits}, \/  uint8\_\-t {\em pwm\_\-nbits}, \/  uint8\_\-t {\em pwm\_\-mode}, \/  volatile void $\ast$ {\em ocrn}, \/  uint8\_\-t {\em com0}, \/  volatile uint8\_\-t $\ast$ {\em tccrn}, \/  volatile uint8\_\-t $\ast$ {\em pwm\_\-port}, \/  uint8\_\-t {\em pwm\_\-bit}, \/  volatile uint8\_\-t $\ast$ {\em sign\_\-port}, \/  uint8\_\-t {\em sign\_\-bit})}\label{pwm__ng_8h_ec71511876da9c859dd174f8d01db8cb}


Inititialize a PWM: set its mode, output pin DDR, DDR for sign bit if any. Example for 8 bits (for atmega128): pwm\_\-ng\_\-init(\&pwm, 8, 8, PWM\_\-NG\_\-MODE\_\-SIGNED$|$PWM\_\-NG\_\-MODE\_\-SIGN\_\-INVERTED, \&OCR0, COM00, \&TCCR0, \&PORTB, 4, \&PORTE, 3); Example for 16 bits (for atmega32): pwm\_\-ng\_\-init(\&pwm, 16, 9, PWM\_\-NG\_\-MODE\_\-NORMAL, \&OCR1B, COM1B0, \&TCCR1A, \&PORTD, 5, NULL, 0); Note that you can use the helper macros \doxyref{PWM\_\-NG\_\-INIT8()}{p.}{pwm__ng_8h_4a6dbe41f581170242e319fcc43159af} or \doxyref{PWM\_\-NG\_\-INIT16()}{p.}{pwm__ng_8h_baed42251af85bffdfce84db38033dc3} instead. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em pwm}]is the pointer to the pwm structure that will be filled. \item[{\em nbits}]is the number of bits for the timer (8 or 16). \item[{\em pwm\_\-mode}]is the mode of the PWM. See the PWM\_\-NG\_\-MODE\_\-xxx flags above. \item[{\em ocrn}]is a pointer to the OCRn register for this PWM. \item[{\em com0}]is the COMn0 for this PWM. \item[{\em pwm\_\-port}]is the pointer to the PORT of the pwm corresponding to the configured PWM. For instance \&PORTB. This is specified in the datasheets, and depends on the AVR part. \item[{\em pwm\_\-bit}]is the bitnum of the configured pwm output. This is specified in the datasheets, and depends on the AVR part. \item[{\em sign\_\-port}]is a poinrter to the PORT for the sign bit if any, else, it can be set to NULL. \item[{\em sign\_\-bit}]is the bitnum of the configured sign output. \end{description}
\end{Desc}


Definition at line 40 of file pwm\_\-ng.c.

References DDR, pwm\_\-ng::mode, pwm\_\-ng::nbits, pwm\_\-ng::ocr16, pwm\_\-ng::ocr8, PWM\_\-NG\_\-MODE\_\-REVERSE, PWM\_\-NG\_\-MODE\_\-SIGNED, PWM\_\-NG\_\-NBITS\_\-10, PWM\_\-NG\_\-NBITS\_\-8, PWM\_\-NG\_\-NBITS\_\-9, PWM\_\-NG\_\-TYPE\_\-16, PWM\_\-NG\_\-TYPE\_\-8, pwm\_\-ng::sign\_\-bit, pwm\_\-ng::sign\_\-port, pwm\_\-ng::type, and pwm\_\-ng::u.\index{pwm\_\-ng.h@{pwm\_\-ng.h}!pwm\_\-ng\_\-set@{pwm\_\-ng\_\-set}}
\index{pwm\_\-ng\_\-set@{pwm\_\-ng\_\-set}!pwm_ng.h@{pwm\_\-ng.h}}
\subsubsection[pwm\_\-ng\_\-set]{\setlength{\rightskip}{0pt plus 5cm}void pwm\_\-ng\_\-set (void $\ast$ {\em pwm}, \/  int32\_\-t {\em value})}\label{pwm__ng_8h_f4633c26142c4911f0faffd4913ae083}


apply a PWM. \begin{Desc}
\item[Parameters:]
\begin{description}
\item[{\em pwm}]is a pointer to the struct pwm. \item[{\em value}]is the value of the pwm. The value is between 0 and 4095 for a non-signed pwm or -4096 and 4095 for a signed one. \end{description}
\end{Desc}


Definition at line 112 of file pwm\_\-ng.c.

References MAX, MIN, pwm\_\-ng::mode, pwm\_\-ng::nbits, pwm\_\-ng::ocr16, pwm\_\-ng::ocr8, pwm\_\-invert\_\-value, PWM\_\-MAX, PWM\_\-MIN, PWM\_\-NG\_\-MODE\_\-SIGNED, PWM\_\-NG\_\-TYPE\_\-8, PWM\_\-SIGNIFICANT\_\-BITS, pwm\_\-ng::type, and pwm\_\-ng::u.