⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm6.c

📁 基于MC9S12C64的CAN总线通讯源程序
💻 C
字号:
/** ###################################################################
**     THIS BEAN MODULE IS GENERATED BY THE TOOL. DO NOT MODIFY IT.
**     Filename  : PWM6.C
**     Project   : NODE_A
**     Processor : MC9S12C64CFA16
**     Beantype  : PWM
**     Version   : Bean 02.112, Driver 01.09, CPU db: 2.87.339
**     Compiler  : Metrowerks HC12 C Compiler
**     Date/Time : 2006-11-11, 14:37
**     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             |  PT0_PWM0_IOC0
**             ----------------------------------------------------
**
**         Timer name                  : PWM0 [8-bit] 
**         Counter                     : PWMCNT0   [236]
**         Mode register               : PWMCTL    [229]
**         Run register                : PWME      [224]
**         Prescaler                   : PWMPRCLK  [227]
**         Compare 1 register          : PWMPER0   [242]
**         Compare 2 register          : PWMDTY0   [248]
**         Flip-flop 1 register        : PWMPOL    [225]
**
**         User handling procedure     : not specified
**
**         Output pin
**
**         Port name                   : T
**         Bit number (in port)        : 0
**         Bit mask of the port        : 1
**         Port data register          : PTT       [576]
**         Port control register       : DDRT      [578]
**
**         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                   : 268 Hz
**           Initial value of            period        pulse width (ratio 5.098%)
**             Xtal ticks              : 7605120       387712
**             microseconds            : 950640        48464
**             milliseconds            : 951           48
**             seconds                 : 1             0
**             seconds (real)          : 0.9506400     0.0484640
**
**     Contents  :
**         SetRatio8 - byte PWM6_SetRatio8(byte Ratio);
**         SetDutyMS - byte PWM6_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 PWM6. */

#include "PWM6.h"

#pragma DATA_SEG PWM6_DATA
#pragma CODE_SEG PWM6_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)
{
  PWMDTY0 = (byte)(((255 * (dword)RatioStore) + 32768) >> 16); /* Calculate new value according to the given ratio */        
}

/*
** ===================================================================
**     Method      :  PWM6_SetRatio8 (bean PWM)
**
**     Description :
**         This method sets a new duty-cycle ratio.
**     Parameters  :
**         NAME       - DESCRIPTION
**         Ratio      - Ratio is expressed as an 8-bit unsigned integer
**                      number. 0 - 255 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 PWM6_SetRatio8(byte Ratio)
{
  RatioStore = (((word)Ratio << 8) + 128); /* 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      :  PWM6_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 951 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 PWM6_SetDutyMS(word Time)
{
  dlong rtval;                         /* Result of two 32-bit numbers multiplication */

  if (Time >= 951) {                   /* Is the given value out of range? */
    return ERR_RANGE;                  /* If yes then error */
  }
  PE_Timer_LngMul((dword)Time,(dword)1156601477,&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      :  PWM6_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 PWM6_Init(void)
{
  PWMCNT0 = 0;
  RatioStore = 3341;                   /* Store initial value of the ratio */
  PWMDTY0 = 13;                        /* Store initial value to the duty-compare register */
  PWMPER0 = 255;                       /* and to the period register */
  /* PWMPRCLK: ??=0,PCKB2=0,PCKB1=0,PCKB0=0,??=0,PCKA2=1,PCKA1=0,PCKA0=1 */
  PWMPRCLK = 5;                        /* Set prescaler register */
  /* PWMSCLA: BIT7=1,BIT6=1,BIT5=1,BIT4=0,BIT3=1,BIT2=0,BIT1=0,BIT0=1 */
  PWMSCLA = 233;                       /* Set scale register */
  PWMCLK_PCLK0 = 1;                    /* Select clock source */
  PWME_PWME0 = 1;                      /* Run counter */
}

/* END PWM6. */

/*
** ###################################################################
**
**     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 + -