- Main Page
- Related Pages
- Modules
- Data Structures
- Files

- File List
- Globals

# pwm.c

Go to the documentation of this file.

    00001 /*This file is prepared for Doxygen automatic documentation generation.*/
    00013
    00014 /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
    00015  *
    00016  * Redistribution and use in source and binary forms, with or without
    00017  * modification, are permitted provided that the following conditions are met:
    00018  *
    00019  * 1. Redistributions of source code must retain the above copyright notice,
    00020  * this list of conditions and the following disclaimer.
    00021  *
    00022  * 2. Redistributions in binary form must reproduce the above copyright notice,
    00023  * this list of conditions and the following disclaimer in the documentation
    00024  * and/or other materials provided with the distribution.
    00025  *
    00026  * 3. The name of Atmel may not be used to endorse or promote products derived
    00027  * from this software without specific prior written permission.
    00028  *
    00029  * 4. This software may only be redistributed and used in connection with an Atmel
    00030  * AVR product.
    00031  *
    00032  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
    00033  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
    00034  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE EXPRESSLY AND
    00035  * SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT,
    00036  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
    00037  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
    00038  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
    00039  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
    00040  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
    00041  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
    00042  */
    00043
    00044 #include "config.h"
    00045 #include "enums.h"
    00046
    00047 #ifdef __GNUC__
    00048    #include "lib_mcu/pwm/pwm.h"
    00049    #include "lib_mcu/pll/pll_drv.h"
    00050    #include "lib_mcu/timer/time.h"
    00051 #elif __ICCAVR__
    00052    #include "PWM.h"
    00053    #include "lib_mcu/timer/time.h"
    00054    #include "lib_mcu/pll/pll_drv.h"
    00055 #else
    00056    #error Current COMPILER not supported
    00057 #endif
    00058
    00059
    00060 #define DEBUG
    00061
    00062 //******************************************************************************
    00063 // Functions
    00064 //******************************************************************************
    00068 void PWM_Stop(void)
    00069 {
    00070    OCR4B = 0;  // Reset compare level.
    00071    TCCR4A = 0; // Set normal port operation, disable PWM modes.
    00072    TCCR4B = 0; // Stop timer/counter4.
    00073    TCCR4C = 0; // Set normal port operation.
    00074    TCCR4D = 0; // No fault protection, normal waveform.
    00075    OCR4C = 0;  // Reset compare.
    00076    OCR4D = 0;  // Reset compare.
    00077    DT4 = 0;    // No dead time values.
    00078 }
    00079
    00080
    00089 void PWM_Start(void)
    00090 {
    00091
    00092    // Clear OC4A on compare match, enable PWM on comparator OCR4A.
    00093    TCCR4A = (1<<COM4A1)|(1<<PWM4A);
    00094
    00095    // Non-inverted PWM, T/C stopped.
    00096    TCCR4B = 0;
    00097
    00098    // Copy shadow bits, disconnect OC4D.
    00099    TCCR4C = (TCCR4A & 0xF0);
    00100
    00101    // No fault protection, use phase & frequency correct PWM.
    00102    TCCR4D = (0<<WGM41)|(1<WGM40);
    00103
    00104    // Does not matter -- PWM6 mode not used.
    00105    TCCR4E = 0;
    00106
    00107    // Set reset compare level. (Offset is used, or JumperCheck() will fail.)
    00108    OCR4A = PWM_OFFSET;
    00109
    00110    // Does not matter -- OC4B is disabled.
    00111    OCR4B = 0;
    00112
    00113    // TOP value for PWM, f(PWM) = 64MHz / 255 = 251kHz.
    00114    OCR4C = 0xFF;
    00115
    00116    // Does not matter -- OC4D is disabled.
    00117    OCR4D = 0;
    00118
    00119    // No dead time values.
    00120    DT4 = 0;
    00121
    00122    // Set PWM port pin to output (PC7).
    00123    DDRC |= (1<<PORTC7);
    00124
    00125    // Enable PLL, use full speed mode.
    00126 // PLLCSR = (1<<PLLE);
    00127
    00128    //cgs update
    00129 #ifdef __GNUC__
    00130    Enable_interrupt();
    00131 #else
    00132    __enable_interrupt();
    00133 #endif
    00134
    00135    // Use general timer and wait 1 ms for PLL lock to settle.
    00136    Time_Set(TIMER_GEN,0,0,1);
    00137
    00138    //cgs update
    00139    Time_Start();
    00140
    00141    do{
    00142    }while(Time_Left(TIMER_GEN));
    00143
    00144    // Now wait for PLL to lock.
    00145    do{
    00146    }while((PLLCSR & (1<<PLOCK)) == 0);
    00147
    00148    //SELECT 64MHz for PWM
    00149    Pll_set_hs_tmr_pscal_1dot5();
    00150
    00151    // CLK PCK = 64MHz / 1 = 64MHz.
    00152    TCCR4B |= (0<<CS43)|(0<<CS42)|(0<<CS41)|(1<<CS40);
    00153 }
    00154
    00155
    00161 unsigned char PWM_IncrementDutyCycle(void){
    00162    if (OCR4A < PWM_MAX) {
    00163       OCR4A += 1;
    00164       return(TRUE);
    00165    } else {
    00166       return(FALSE);
    00167    }
    00168 }
    00169
    00170
    00176 unsigned char PWM_DecrementDutyCycle(void){
    00177    if (OCR4A > 0) {
    00178       OCR4A -= 1;
    00179       return(TRUE);
    00180    } else {
    00181       return(FALSE);
    00182    }
    00183 }
    00184
    00185