📄 basic.c
字号:
/******************************************************************************
* COPYRIGHT (c) MOTOROLA 2003
* FILE NAME: basic.c
*
* PURPOSE: demonstration program for PWM generation using timer module.
*
* DOCUMENTATION SOURCE: EKB Applications
*
* TARGET DEVICE: MC9S12C32
*
* COMPILER: Metrowerks VERSION: MOT_V3.0
*
* AUTHOR: G.More/M.Gallop LOCATION: EKB LAST EDIT DATE: 1/10/03
*
* UPDATE HISTORY
* REV AUTHOR DATE DESCRIPTION OF CHANGE
* --- ------ -------- ---------------------
* 1.0 G.More/M.Gallop 25/03/03 Initial Coding
* 2.0 G.More 1/10/03 Simple PWM generation example.
*
******************************************************************************/
/*===========================================================================*/
/* Motorola reserves the right to make changes without further notice to any */
/* product herein to improve reliability, function, or design. Motorola does */
/* not assume any liability arising out of the application or use of any */
/* product, circuit, or software described herein; neither does it convey */
/* any license under its patent rights nor the rights of others. Motorola */
/* products are not designed, intended, or authorized for use as components */
/* in systems intended for surgical implant into the body, or other */
/* applications intended to support life, or for any other application in */
/* which the failure of the Motorola product could create a situation where */
/* personal injury or death may occur. Should Buyer purchase or use Motorola */
/* products for any such intended or unauthorized application, Buyer shall */
/* indemnify and hold Motorola and its officers, employees, subsidiaries, */
/* affiliates, and distributors harmless against all claims costs, damages, */
/* and expenses, and reasonable attorney fees arising out of, directly or */
/* indirectly, any claim of personal injury or death associated with such */
/* unintended or unauthorized use, even if such claim alleges that Motorola */
/* was negligent regarding the design or manufacture of the part. Motorola */
/* and the Motorola logo* are registered trademarks of Motorola Ltd. */
/*****************************************************************************/
#define BUSCLOCK_KHZ 24000 /* use care! this value is used to calibrate PWM */
#define TIMERPRESCALER 1
/* include files */
#include "basic.h"
/* global variables */
unsigned long Period[1] = 0, Mark[1] = 0, Space[1] = 0;
/******************************************************************************
Function Name : main
Engineer : G.More / M.Gallop
Date : 25/03/03
Parameters : NONE
Returns : NONE
Notes : main routine
******************************************************************************/
void
main(void)
{
Pim.perh.byte = (PERH7); /* set pull up devices on port H pin 7 */
SetPLL(6,4); /* configures bus clock at 24MHz using PLL - !needs correct loop filter components fitted! */
ConfigureTimer(); /* calls timer setup routine */
ConfigureChannel(0,50,125); /* set up channel 0 for 50% duty and 125kHz clock */
EnableInterrupts; /* clear CCR interrupt mask */
while(1) /* forever loop */
{
if((Pim.pth.bit.pth7 == 1) && (Timer.tie.bit.c0i == 0)) /* if control signal = enable and timer is disabled */
{
/* start generating PWM then hand over to ISR */
Timer.tc[0].word = (Timer.tcnt.word + 10); /* set up timer compare for known latency */
Timer.tctl2.bit.ol0 = 1; /* set pin action to rising edge on first compare */
Timer.tie.bit.c0i = 1; /* enable interrupt for this timer channel */
}
}
}
/******************************************************************************
Function Name : ConfigureChannels
Engineer : G.More / M.Gallop
Date : 25/03/03
Parameters : dutyCycle (percentage), frequency (kHz), channel
Returns :
Notes : beware of frequency limitations in relation to BUSCLOCK and
TIMERPRESCALER value (may overflow 16-bit variable). No error
checking is implemented in this function.
******************************************************************************/
void ConfigureChannel (tU08 channel, tU08 dutyCycle, tU16 frequency_khz)
{
Period[channel] = ((BUSCLOCK_KHZ / TIMERPRESCALER) / frequency_khz);
Mark[channel] = ((Period[channel] * dutyCycle ) / 100);
Space[channel] = (Period[channel] - Mark[channel]);
}
/******************************************************************************
Function Name : SetPLL
Engineer : A.McKechan / G.More / C.Culshaw
Date : 31/5/00
Parameters : multiplier, divider
Returns : NONE
******************************************************************************/
void SetPLL(tU08 multiplier, tU08 divider)
{
Crg.clksel.bit.pllsel = 0; /* select crystal as bus clock */
if(!multiplier && !divider) /* if multiplier and divider are 0 */
{
Crg.synr.byte = 0; /* set synr (multiplier) to 0 */
Crg.refdv.byte = 0; /* set refdv (divider) to 0 */
Crg.pllctl.bit.pllon = 0; /* turn PLL off */
}
else
{
Crg.pllctl.bit.pllon = 1; /* turn on the PLL */
Crg.synr.byte = multiplier-1; /* set synr (multiplier) */
Crg.refdv.byte = divider-1; /* set refdv (divider) */
while(!Crg.crgflg.bit.lock) /* wait for PLL to lock */
{
}
Crg.clksel.bit.pllsel = 1; /* select the PLL as the bus clock */
}
}
/******************************************************************************
Function Name : ConfigureTimer
Engineer : G.More / M.Gallop
Date : 25/03/03
Parameters : NONE
Returns : NONE
Notes : sets up the timer for one channel of PWM output.
******************************************************************************/
void
ConfigureTimer(void)
{
Timer.tios.byte = (IOS0); /* set channel one as output compare */
Timer.tctl2.byte = (OM0); /* set mode to follow output level bit */
Timer.tscr2.byte = (TIMERPRESCALER); /* set the prescaler to 0 - use care! */
Timer.tscr1.byte = (TEN|TFFCA); /* enable timer and set fast flags clear all */
}
/*****************************************************************************/
/* Start of interrupt service routines */
/* placed into non-banked memory segment as vectors are only 2 bytes */
#pragma CODE_SEG NON_BANKED
/******************************************************************************
Function Name : _TimerCompare0ISR
Engineer : G.More / M.Gallop
Date : 25/03/03
Parameters : NONE
Returns : NONE
******************************************************************************/
#pragma TRAP_PROC
void
_TimerCompare0ISR(void)
{
if (Timer.tctl2.bit.ol0 == 1) /* if channel is set to generate rising edge */
{
Timer.tc[0].word += Mark[0]; /* set up timer compare for mark time */
Timer.tctl2.bit.ol0 = 0; /* set pin action to falling edge */
/* disable here to drive low on PWM stop */
if (Pim.pth.bit.pth7 != 1) /* if control signal = disable */
{
Timer.tie.bit.c0i = 0; /* disable interrupt */
}
}
else
{
Timer.tc[0].word += Space[0]; /* set up timer compare for space time */
Timer.tctl2.bit.ol0 = 1; /* set pin action to rising edge */
/* disable here to drive high on PWM stop */
}
}
/******************************************************************************
Function Name : _dummyISR
Engineer : M.Gallop
Date : 31/5/00
Parameters : NONE
Returns : NONE
******************************************************************************/
#pragma TRAP_PROC
void
_dummyISR( void )
{
while(1) /* endless loop */
{;
}
}
/* End of interrupt service routines */
/* reset code segment to default */
#pragma CODE_SEG DEFAULT
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -