# PWM (Pulse Width Modulation)
[Core]

The PWM subsystem provides control of the 4 PWM outputs on the SAM7X. More...

## Functions
int | Pwm_Set (int index, int duty)
| Set the duty of a PWM device.
int | Pwm_Get (int index)
| Read the current duty of a PWM device.
int | Pwm_SetDividerA (int val)
| Adjust the clock period on Divider A.
int | Pwm_GetDividerA ()
| Read the clock period on Divider A.
int | Pwm_SetDividerB (int val)
| Adjust the clock period on Divider B.
int | Pwm_GetDividerB ()
| Read the clock period on Divider B.
int | Pwm_SetClockSource (int channel, int val)
| Set the clock divider for a particular channel.
int | Pwm_GetClockSource (int channel)
| Read the clock source for a particular channel.
int | Pwm_SetWaveformProperties (int channel, int val)
| Set the wave form properties of the PWM contoller for a given channel.
int | Pwm_GetWaveformProperties (int channel)
| Read the waveform configuration of a specified PWM channel.

## Detailed Description

The PWM subsystem provides control of the 4 PWM outputs on the SAM7X.

The Make Controller has 4 PWM lines. These can each be configured separately and can control up to 2 output lines directly, the 2 lines running either parallel or inverted. For a very simple start, just see Pwm_Set( ) and Pwm_Get( ) as these will start driving your PWMs immediately with very little hassle.

## Period adjustment of the PWM unit

Configuring and setting the clock for the PWM system can be quite a complicated matter. Here are some of the relevant issues.

Each of the 4 PWM channels is fed in a clock, as determined by its clock source:

- Clock source 0-10 represent the Master clock divided by 2 to the power of the Clock Source Value. eg. a clock source value of 5 = a clock rate of MasterClock/(2^5)
- A value of 11 sets the Clock source to be generated by clock Divider A (Default)
- A value of 12 sets the Clock source to be generated by clock Divider B

If either Clock Divider A or Clock Divider B is used, you can adjust their values individually to allow the clock period to precisely match your needs. Each clock divider has two values, a Mux value and a Divider value.

The mux works just like the clock source values, and chooses Master clock divided by 2 to the power of the Clock Source Value, eg. a DividerXMux value of 5 == a clock rate of MasterClock/(2^5). The Divider value sets a linear divider, which is fed the clock value as selected by the Mux, and returns that clock value divided by the divider value. This output value is what is fed out of the divider unit. A output formula:

     output = MCLK / ( (2^DividerMux) * DividerValue )

The PWM subsystem of the Controller Board can be used independently from the PWM Out library, since the PWM Out library relies on the core PWM.

## Function Documentation

int Pwm_Get | ( | int | index | ) |

Read the current duty of a PWM device.

Parameters:

  | index | An integer specifying which PWM device (0-3).

Returns:
  The duty - (0 - 1023).

Definition at line 119 of file pwm.c.

int Pwm_GetClockSource | ( | int | channel | ) |

Read the clock source for a particular channel.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | channel | The PWM channel (0-3) whose channel you'd like to read.

Returns:
  The clock source.

See also:
  Pwm_SetClockSource( )

Definition at line 386 of file pwm.c.

int Pwm_GetDividerA | ( | void |  | ) |

Read the clock period on Divider A.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Returns:
  The clock period on Divider A.

Definition at line 299 of file pwm.c.

int Pwm_GetDividerB | ( | void |  | ) |

Read the clock period on Divider B.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Returns:
  The clock period on Divider B.

Definition at line 338 of file pwm.c.

int Pwm_GetWaveformProperties | ( | int | channel | ) |

Read the waveform configuration of a specified PWM channel.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | channel | The PWM channel (0-3) to read from.

Returns:
  A bitmask describing the waveform configuration.

See also:
  Pwm_SetWaveformProperties( )

Definition at line 437 of file pwm.c.

int Pwm_Set | ( | int | index,
|  | int | duty |
| ) |  |  |

Set the duty of a PWM device.

Parameters:

  | index | An integer specifying which PWM device (0-3).
  | duty | The duty - (0 - 1023).

Returns:
  0 on success.

Definition at line 102 of file pwm.c.

int Pwm_SetClockSource | ( | int | channel,
|  | int | val |
| ) |  |  |

Set the clock divider for a particular channel.

For values 0-10, the Master_Clock is divided by (2^val). For example, for 4 the resulting period will be Master Clock / 16, as 2 ^ 4 == 16.

When val is 11, Clock Divider A is used. When val is 12, Clock Divider B is used. Values other than 0 - 12 are not valid.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | channel | The PWM channel (0-3) you'd like to configure.
  | val | The new clock divider.

Returns:
  0 on success.

Definition at line 356 of file pwm.c.

int Pwm_SetDividerA | ( | int | val | ) |

Adjust the clock period on Divider A.

See AT91SAM7X manual for more information p.421

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | val | An int between 0 and 4096.

Returns:
  0 on success.

Definition at line 276 of file pwm.c.

int Pwm_SetDividerB | ( | int | val | ) |

Adjust the clock period on Divider B.

See AT91SAM7X manual for more information p.421

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | val | An int between 0 and 4096.

Returns:
  0 on success.

Definition at line 312 of file pwm.c.

int Pwm_SetWaveformProperties | ( | int | channel,
|  | int | val |
| ) |  |  |

Set the wave form properties of the PWM contoller for a given channel.

The waveform properties are controlled by bits 0, 1, and 2.

- bit 0 sets whether the PWModule is Left aligned (0) or Center aligned (1)
- bit 1 sets whether the PWMoudle's polarity starts out low (0) or high (1)
- bit 2 is not supported at this time.

Contributed by TheStigg - http://www.makingthings.com/author/thestigg

Parameters:

  | channel | the PWM channel (0-3) that you want to configure
  | val | The mask of values as described above.

Returns:
  0 on success.

Definition at line 406 of file pwm.c.