Files
Cnomicon/src/libPIC/pwm.c
2021-01-22 10:16:20 -05:00

88 lines
2.8 KiB
C

/*****************************************************************************
* Copyright (C) 2008
* ProbeStar Telematics, LLC
* All Rights Reserved. Proprietary and Confidential.
*============================================================================
* PWM Output
*----------------------------------------------------------------------------
* Output PWM (8 bit resolution for now)
*****************************************************************************/
/*****************************************************************************
* includes
*****************************************************************************/
#include "system.h"
#include "delay.h"
#include "pwm.h"
/*****************************************************************************
* defines
*****************************************************************************/
/*****************************************************************************
* macros
*****************************************************************************/
/*****************************************************************************
* structs & typedefs
*****************************************************************************/
/*****************************************************************************
* global constants
*****************************************************************************/
/*****************************************************************************
* global variables
*****************************************************************************/
/*****************************************************************************
* static constants
*****************************************************************************/
/*****************************************************************************
* static variables
*****************************************************************************/
/*****************************************************************************
* static prototypes
*****************************************************************************/
/*****************************************************************************
* C functions
*****************************************************************************/
void PwmInit( void )
{
RC1 = RC2 = 0;
PR2 = 255; // max period
T2CON = 0b00000111; // 16:1 prescale
// PWM1
TRISC1 = 0;
CCPR1L = 0;
CCP1CON = 0b00001100;
// PMW2
TRISC2 = 0;
CCPR2L = 0;
CCP2CON = 0b00001100;
}
// duty cycle 0-PWM_DUTY_MAX (use macro)
void PwmOut( BYTE byPin, WORD wDuty )
{
// clip
if ( wDuty > PWM_DUTY_MAX )
wDuty = PWM_DUTY_MAX;
// allows custom rate for timer 2
if ( PR2 != 255 )
wDuty = wDuty * PR2 / PWM_DUTY_CYCLE;
// scale duty cycle
if ( byPin == 1 )
CCPR1L = wDuty;
if ( byPin == 2 )
CCPR2L = wDuty;
}