📄 pwm1.c
字号:
/** ###################################################################
** THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
** Filename : PWM1.C
** Project : RTOSDemo
** Processor : MC9S08GT60CFD
** Beantype : PWM
** Version : Bean 02.098, Driver 01.09, CPU db: 2.87.074
** Compiler : Metrowerks HCS08 C Compiler
** Date/Time : 3/10/2006, 1:44 PM
** 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
** ----------------------------------------------------
** 22 | PTD2_TPM1CH2
** ----------------------------------------------------
**
** Timer name : TPM1 [16-bit]
** Counter : TPM1CNT [0031]
** Mode register : TPM1SC [0030]
** Run register : TPM1SC [0030]
** Prescaler : TPM1SC [0030]
** Compare register : TPM1C2V [003C]
** Flip-flop register : TPM1C2SC [003B]
**
** User handling procedure : not specified
**
** Port name : PTD
** Bit number (in port) : 2
** Bit mask of the port : 0004
** Port data register : PTDD [000C]
** Port control register : PTDDD [000F]
**
** Initialization:
** Output level : low
** Timer : Enabled
** Event : Enabled
** High speed mode
** Prescaler : divide-by-32
** Clock : 624857 Hz
** Initial value of period pulse width
** Xtal ticks : 269745 499
** microseconds : 67436 125
** milliseconds : 67 0
** seconds (real) : 0.0674362 0.0001248
**
** Low speed mode
** Prescaler : divide-by-4
** Clock : 972000 Hz
** Initial value of period pulse width
** Xtal ticks : 269695 498
** microseconds : 67424 124
** milliseconds : 67 0
** seconds (real) : 0.0674239 0.0001245
**
** Contents :
** SetRatio16 - byte PWM1_SetRatio16(word Ratio);
** SetDutyUS - byte PWM1_SetDutyUS(word Time);
** SetDutyMS - byte PWM1_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 PWM1. */
#include "PE_Error.h"
#include "PWM1.h"
#pragma MESSAGE DISABLE C2705 /* WARNING C2705: Possible loss of data */
#pragma MESSAGE DISABLE C5919 /* WARNING C5919: Conversion of floating to unsigned integral */
static word RatioStore; /* Ratio of L-level to H-level */
/*
** ===================================================================
** Method : SetRatio (bean PWM)
**
** Description :
** The method stores duty value to compare register(s) and sets
** necessary bits or (in List mode) call SetReg method for duty
** value storing.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
static void SetRatio(void)
{
if(RatioStore == 0xFFFF) { /* Duty = 100%? */
TPM1C2V = 0xFFFF; /* Store new value to the compare reg. */
} else {
TPM1C2V = (word)((((dword)TPM1MOD + 0x01) * (dword)RatioStore) >> 0x10); /* Calculate new value of compare register according to the given ratio */
}
}
/*
** ===================================================================
** Method : PWM1_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 - 0xFFFF value is proportional
** to ratio 0 - 100%
** Note: Calculated duty depends on the timer possibilities
** and on the selected period.
** Returns :
** --- - Error code
** ===================================================================
*/
byte PWM1_SetRatio16(word Ratio)
{
RatioStore = Ratio; /* Store new value of the ratio */
SetRatio(); /* Calculate and set up new appropriate values of the compare and modulo registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM1_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 65535 us in high speed mode)
** (0 to 65535 us in low 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 PWM1_SetDutyUS(word Time)
{
dlong rtval; /* Result of two 32-bit numbers multiplication */
if (CpuMode == HIGH_SPEED) { /* Is it active high speed CPU mode? */
PE_Timer_LngMul((dword)Time,0xF8C95484,&rtval); /* Multiply given value and High speed CPU mode coefficient */
if (PE_Timer_LngHi4(rtval[0],rtval[1],&RatioStore)) /* Is the result greater or equal than 65536 ? */
RatioStore = 0xFFFF; /* If yes then use maximal possible value */
}
if (CpuMode == LOW_SPEED) { /* Is it active low speed CPU mode? */
PE_Timer_LngMul((dword)Time,0xF8D4FDF4,&rtval); /* Multiply given value and Low speed CPU mode coefficient */
if (PE_Timer_LngHi4(rtval[0],rtval[1],&RatioStore)) /* Is the result greater or equal than 65536 ? */
RatioStore = 0xFFFF; /* If yes then use maximal possible value */
}
SetRatio(); /* Calculate and set up new appropriate values of the compare and modulo registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM1_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 67 ms in high speed mode)
** (0 to 67 ms in low 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 PWM1_SetDutyMS(word Time)
{
dlong rtval; /* Result of two 32-bit numbers multiplication */
if (CpuMode == HIGH_SPEED) { /* Is it active high speed CPU mode? */
if (Time > 0x43) /* Is the given value out of range? */
return ERR_RANGE; /* If yes then error */
PE_Timer_LngMul((dword)Time,0x03CBD272,&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 = 0xFFFF; /* If yes then use maximal possible value */
}
if (CpuMode == LOW_SPEED) { /* Is it active low speed CPU mode? */
if (Time > 0x43) /* Is the given value out of range? */
return ERR_RANGE; /* If yes then error */
PE_Timer_LngMul((dword)Time,0x03CC0000,&rtval); /* Multiply given value and Low speed CPU mode coefficient */
if (PE_Timer_LngHi2(rtval[0],rtval[1],&RatioStore)) /* Is the result greater or equal than 65536 ? */
RatioStore = 0xFFFF; /* If yes then use maximal possible value */
}
SetRatio(); /* Calculate and set up new appropriate values of the compare and modulo registers */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM1_Init (bean PWM)
**
** Description :
** Initializes the associated peripheral(s) and the beans
** 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 PWM1_Init(void)
{
/* TPM1C2SC: CH2F=0,CH2IE=0,MS2B=1,MS2A=1,ELS2B=1,ELS2A=1,??=0,??=0 */
TPM1C2SC = 0x3C; /* Set up PWM mode with output signal level low */
RatioStore = 0x7A; /* Store initial value of the ratio */
PWM1_SetHigh(); /* Initial speed CPU mode is high */
}
/*
** ===================================================================
** Method : PWM1_SetHigh (bean PWM)
**
** Description :
** The method reconfigures the bean and its selected peripheral(s)
** when the CPU is switched to the High speed mode. The method is
** called automatically as s part of the CPU SetHighSpeed method.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void PWM1_SetHigh(void)
{
TPM1MOD = 0xA499;
SetRatio(); /* Calculate and set up new values of the compare according to the selected speed CPU mode */
TPM1SC = 0x0D; /* Run the counter (set CLKSB:CLKSA) */
}
/*
** ===================================================================
** Method : PWM1_SetLow (bean PWM)
**
** Description :
** The method reconfigures the bean and its selected peripheral(s)
** when the CPU is switched to the Low speed mode. The method is
** called automatically as a part of the CPU SetLowSpeed method.
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
void PWM1_SetLow(void)
{
TPM1MOD = 0xFFFF;
SetRatio(); /* Calculate and set up new values of the compare according to the selected speed CPU mode */
TPM1SC = 0x0A; /* Run the counter (set CLKSB:CLKSA) */
}
/* END PWM1. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.97 [03.74]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -