# PWM.c File Reference

## Detailed Description

Functions for use of PWM.

Contains functions for initializing and controlling PWM output.

Application note:
  AVR458: Charging Li-Ion Batteries with BC100
  AVR463: Charging NiMH Batteries with BC100

Documentation
  For comprehensive code documentation, supported compilers, compiler settings and supported devices see readme.html

Author:
  Atmel Corporation: http://www.atmel.com
  Support email: avr@atmel.com

$Name$

Revision
  2299

$RCSfile$

URL
  http://revisor.norway.atmel.com/AppsAVR8/avr458_Charging_Li-Ion_Batteries_with_BC100/trunk/code/IAR/PWM.c

Date
  2007-08-23 12:55:51 +0200 (to, 23 aug 2007)

Definition in file PWM.c.

`#include <ioavr.h>`
`#include "enums.h"`
`#include "main.h"`
`#include "PWM.h"`
`#include "time.h"`

Include dependency graph for PWM.c:

Go to the source code of this file.

## Functions
unsigned char | PWM_DecrementDutyCycle (void)
| Decrements the PWM duty cycle, if not already at zero.
unsigned char | PWM_IncrementDutyCycle (void)
| Increments the PWM duty cycle, if not already at max.
void | PWM_Start (void)
| Initializes and starts PWM output.
void | PWM_Stop (void)
| Stops PWM output.

## Function Documentation

unsigned char PWM_DecrementDutyCycle | ( | void |  | ) |

Decrements the PWM duty cycle, if not already at zero.

Return values:

  | TRUE | Success, duty cycle could be decremented.
  | FALSE | Failure, duty cycle already at zero.

Definition at line 142 of file PWM.c.

References FALSE, and TRUE.

Referenced by ConstantCurrent(), ConstantVoltage(), and MaxVoltageAndCurrent().

    00142                                           {
    00143
    00144         if (OCR1B > 0)  {
    00145                 OCR1B -= 1;
    00146                 return(TRUE);
    00147         } else {
    00148                 return(FALSE);
    00149         }
    00150 }

unsigned char PWM_IncrementDutyCycle | ( | void |  | ) |

Increments the PWM duty cycle, if not already at max.

Return values:

  | TRUE | Success, duty cycle could be incremented.
  | FALSE | Failure, duty cycle already at maximum.

Definition at line 126 of file PWM.c.

References FALSE, PWM_MAX, and TRUE.

Referenced by ConstantCurrent(), ConstantVoltage(), JumperCheck(), and MaxVoltageAndCurrent().

    00126                                           {
    00127
    00128         if (OCR1B < PWM_MAX) {
    00129                 OCR1B += 1;
    00130                 return(TRUE);
    00131         } else {
    00132                 return(FALSE);
    00133         }
    00134 }

void PWM_Start | ( | void |  | ) |

Initializes and starts PWM output.

Initializes timer1 for use as a PWM with a clock rate of 64 MHz.
Its comparator is connected to PB3 and will output high until timer1 reaches the value of OCR1B. It is then dropped to 0.
The comparator outputs high again when the counter overflows, which will happen at a rate of 250 kHz.

Definition at line 66 of file PWM.c.

References PWM_OFFSET, Time_Left(), Time_Set(), and TIMER_GEN.

Referenced by Charge(), and JumperCheck().

    00067 {
    00068         // Clear OC1B on compare match, enable PWM on comparator OCR1B.
    00069         TCCR1A = (1<<COM1B1)|(0<<COM1B0)|(1<<PWM1B);
    00070
    00071         // Non-inverted PWM, T/C stopped.
    00072         TCCR1B = 0;
    00073
    00074         // Copy shadow bits, disconnect OC1D.
    00075         TCCR1C = (TCCR1A & 0xF0);
    00076
    00077         // No fault protection, use phase & frequency correct PWM.
    00078         TCCR1D = (0<<WGM11)|(1<WGM10);
    00079
    00080         // Does not matter -- PWM6 mode not used.
    00081         TCCR1E = 0;
    00082
    00083         // Does not matter -- OC1A is disabled.
    00084         OCR1A = 0;
    00085
    00086         // Set reset compare level. (Offset is used, or JumperCheck() will fail.)
    00087         OCR1B = PWM_OFFSET;
    00088
    00089         // TOP value for PWM, f(PWM) = 64MHz / 255 = 251kHz.
    00090         OCR1C = 0xFF;
    00091
    00092         // Does not matter -- OC1D is disabled.
    00093         OCR1D = 0;
    00094
    00095         // No dead time values.
    00096         DT1 = 0;
    00097
    00098         // Set PWM port pin to output.
    00099         DDRB |= (1<<PB3);
    00100
    00101         // Enable PLL, use full speed mode.
    00102         PLLCSR = (0<<LSM) | (1<<PLLE);
    00103
    00104         // Use general timer and wait 1 ms for PLL lock to settle.
    00105         Time_Set(TIMER_GEN,0,0,1);
    00106         do{
    00107         }while(Time_Left(TIMER_GEN));
    00108
    00109         // Now wait for PLL to lock.
    00110         do{
    00111         }while((PLLCSR & (1<<PLOCK)) == 0);
    00112
    00113         // Use PLL as clock source.
    00114         PLLCSR |= (1<<PCKE);
    00115
    00116         // CLK PCK = 64MHz / 1 = 64MHz.
    00117         TCCR1B |= (0<<CS13)|(0<<CS12)|(0<<CS11)|(1<<CS10);
    00118 }

Here is the call graph for this function:

void PWM_Stop | ( | void |  | ) |

Stops PWM output.

Definition at line 44 of file PWM.c.

Referenced by Charge(), Error(), HaltNow(), and JumperCheck().

    00045 {
    00046         OCR1B = 0;  // Reset compare level.
    00047         PLLCSR = 0; // Disable PLL, switch to synchronous CLK mode.
    00048         TCCR1A = 0; // Set normal port operation, disable PWM modes.
    00049         TCCR1B = 0; // Stop timer/counter1.
    00050         TCCR1C = 0; // Set normal port operation.
    00051         TCCR1D = 0; // No fault protection, normal waveform.
    00052         OCR1C = 0;  // Reset compare.
    00053         OCR1D = 0;  // Reset compare.
    00054         DT1 = 0;    // No dead time values.
    00055 }