📄 pwm8h.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : PWM8h.C
** Project : che_free
** Processor : MC9S12DG128BCPV
** Beantype : PWM
** Version : Bean 02.112, Driver 01.09, CPU db: 2.87.280
** Compiler : Metrowerks HC12 C Compiler
** Date/Time : 2008-3-16, 上午 09:57
** Abstract :
** This bean implements a pulse-width modulation generator
** that generates signal with variable duty and fixed cycle.
** Settings :
** Used output pin :
** ----------------------------------------------------
** Number (on package) | Name
** ----------------------------------------------------
** 1 | PP3_SS1_PWM3_KWP3
** ----------------------------------------------------
**
** Timer name : PWM3 [8-bit]
** Counter : PWMCNT3 [175]
** Mode register : PWMCTL [165]
** Run register : PWME [160]
** Prescaler : PWMPRCLK [163]
** Compare 1 register : PWMPER3 [183]
** Compare 2 register : PWMDTY3 [191]
** Flip-flop 1 register : PWMPOL [161]
**
** User handling procedure : not specified
**
** Output pin
**
** Port name : P
** Bit number (in port) : 3
** Bit mask of the port : 8
** Port data register : PTP [600]
** Port control register : DDRP [602]
**
** Runtime setting period : none
** Runtime setting ratio : calculated
** Initialization:
** Aligned : Left
** Output level : low
** Timer : Enabled
** Event : Enabled
** High speed mode
** Prescaler : divide-by-1
** Clock : 5000 Hz
** Initial value of period pulse width (ratio 0.8%)
** Xtal ticks : 800000 6400
** microseconds : 50000 400
** milliseconds : 50 0
** seconds (real) : 0.0500000 0.0004000
**
** Contents :
** SetRatio16 - byte PWM8h_SetRatio16(word Ratio);
** SetDutyUS - byte PWM8h_SetDutyUS(word Time);
** SetDutyMS - byte PWM8h_SetDutyMS(word Time);
**
** (c) Copyright UNIS, spol. s r.o. 1997-2005
** UNIS, spol. s r.o.
** Jundrovska 33
** 624 00 Brno
** Czech Republic
** http : www.processorexpert.com
** mail : info@processorexpert.com
** ###################################################################*/
/* MODULE PWM8h. */
#include "PWM8h.h"
#pragma DATA_SEG PWM8h_DATA
#pragma CODE_SEG PWM8h_CODE
static word RatioStore; /* Ratio of L-level to H-level */
/*
** ===================================================================
** Method : SetRatio (bean PWM)
**
** Description :
** The method reconfigures the compare and modulo registers of
** the peripheral(s) when the speed mode changes. The method is
** called automatically as a part of the bean
** SetHigh/SetLow/SetSlow methods.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void SetRatio(void)
{
PWMDTY3 = (byte)(((250 * (dword)RatioStore) + 32768) >> 16); /* Calculate new value according to the given ratio */
}
/*
** ===================================================================
** Method : PWM8h_SetRatio16 (bean PWM)
**
** Description :
** This method sets a new duty-cycle ratio.
** Parameters :
** NAME - DESCRIPTION
** Ratio - Ratio is expressed as an 16-bit unsigned integer
** number. 0 - 65535 value is proportional
** to ratio 0 - 100%
** Note: Calculated duty depends on the timer possibilities
** and on the selected period.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ===================================================================
*/
byte PWM8h_SetRatio16(word Ratio)
{
RatioStore = Ratio; /* Store new value of the ratio */
SetRatio(); /* Calculate and set up new appropriate values of the duty and period registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM8h_SetDutyUS (bean PWM)
**
** Description :
** This method sets the new duty value of the output signal. The
** duty is expressed in microseconds as a 16-bit unsigned integer
** number.
** Parameters :
** NAME - DESCRIPTION
** Time - Duty to set [in microseconds]
** (0 to 50000 us in high speed mode)
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_MATH - Overflow during evaluation
** ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte PWM8h_SetDutyUS(word Time)
{
dlong rtval; /* Result of two 32-bit numbers multiplication */
if (Time >= 50000) { /* Is the given value out of range? */
return ERR_RANGE; /* If yes then error */
}
PE_Timer_LngMul((dword)Time,(dword)21990233,&rtval); /* Multiply given value and high speed CPU mode coefficient */
if (PE_Timer_LngHi3(rtval[0],rtval[1],&RatioStore)) { /* Is the result greater or equal than 65536 ? */
RatioStore = 65535; /* If yes then use maximal possible value */
}
SetRatio(); /* Calculate and set up new appropriate values of the duty and period registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM8h_SetDutyMS (bean PWM)
**
** Description :
** This method sets the new duty value of the output signal. The
** duty is expressed in milliseconds as a 16-bit unsigned integer
** number.
** Parameters :
** NAME - DESCRIPTION
** Time - Duty to set [in milliseconds]
** (0 to 50 ms in high speed mode)
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ERR_MATH - Overflow during evaluation
** ERR_RANGE - Parameter out of range
** ===================================================================
*/
byte PWM8h_SetDutyMS(word Time)
{
dlong rtval; /* Result of two 32-bit numbers multiplication */
if (Time >= 50) { /* Is the given value out of range? */
return ERR_RANGE; /* If yes then error */
}
PE_Timer_LngMul((dword)Time,(dword)85899346,&rtval); /* Multiply given value and high speed CPU mode coefficient */
if (PE_Timer_LngHi2(rtval[0],rtval[1],&RatioStore)) { /* Is the result greater or equal than 65536 ? */
RatioStore = 65535; /* If yes then use maximal possible value */
}
SetRatio(); /* Calculate and set up new appropriate values of the duty and period registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM8h_Init (bean PWM)
**
** Description :
** Initializes the associated peripheral(s) and the bean's
** internal variables. The method is called automatically as a
** part of the application initialization code.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void PWM8h_Init(void)
{
PWMCNT3 = 0;
RatioStore = 524; /* Store initial value of the ratio */
PWMDTY3 = 2; /* Store initial value to the duty-compare register */
PWMPER3 = 250; /* and to the period register */
PWMCLK_PCLK3 = 1; /* Select clock source */
PWME_PWME3 = 1; /* Run counter */
}
/* END PWM8h. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.96 [03.76]
** for the Freescale HCS12 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -