📄 tio0_pwm.c
字号:
/*""FILE COMMENT""*****************************************************
* M32R C Programming Rev. 1.00
* < Sample Program for TIO0 PWM output mode >
*
* Copyright (c) 2003 Renesas Technology Corporation
* And Renesas Solutions Corporation
* All Rights Reserved
*********************************************************************/
/************************************************************************/
/* Include file */
/************************************************************************/
#include "..\inc\sfr32170_pragma.h"
/************************************************************************/
/* Definition of external reference */
/************************************************************************/
extern void DisInt( void ); /* Interrupt disable function */
extern void EnInt( void ); /* Interrupt enable function */
/************************************************************************/
/* Function prototype declaration */
/************************************************************************/
void TIO0_PWM_init( void ); /* Initialize TIO0 PWM output mode */
unsigned long TIO0_PWM_out( unsigned short, unsigned short ); /* Start TIO0 PWM output */
/************************************************************************/
/* Define macro */
/************************************************************************/
#define OK 1
#define NG 0
/*** PWM(TIO0) ***/
/* 0123 4567 */
#define TIO03_ClkSrc (unsigned char)0x00 /* 0000 0000B */
/* |||| ||++---- Select clock bus 0 */
/* ++++ ++------ don't care */
/* 0123 4567 89AB CDEF */
#define TIO0_MASK (unsigned short)0x000f /* 0000 0000 0000 1111B */
#define TIO0_PWM (unsigned short)0x0003 /* 0000 0000 0000 0011B */
/* |+++- Set TIO0 PWM mode */
/* +---- TIO0 enable: unselected */
/* 0123 4567 89AB CDEF */
#define FF11_TIO0M (unsigned short)0x0100 /* 0000 0001 0000 0000B */
/* +----------- FF11 source : TIO0 unselected */
/*""FUNC COMMENT""*******************************************************
* Function name: TIO0_PWM_init()
*-----------------------------------------------------------------------
* Description : Initial settings necessary to drive TIO0 in PWM mode
* : - While driving TIO0 in PWM mode, this function outputs PWM waveform from TO11
* : - The count source used for this operation is clock bus 0
*-----------------------------------------------------------------------
* Argument : -
*-----------------------------------------------------------------------
* Returns : -
*-----------------------------------------------------------------------
* Notes : The prescaler, clock bus, etc. are set separately from the above
* : Must be executed while interrupts are disabled
*""FUNC COMMENT END""***************************************************/
void TIO0_PWM_init()
{
unsigned char temp;
/*** Initializing P103 (TO11) output (low-level output) ***/
FFS0 &= ~FF11_TIO0M; /* FF11 source : TIO0 selected */
FFP0 = ~FP11; /* Enable F/F11 rewrite */
FFD0 = 0x0000; /* F/F11 low (0) output (inverted to high during timer operation) */
P10MOD |= 0x10; /* Select TO11 (TIO0 output) for output */
/*** PWM setting (TIO0) ***/
TIO03CR1 = TIO03_ClkSrc; /* Select TIO0 clock source */
TIO03CR0 = ( TIO03CR0 & ~TIO0_MASK) | TIO0_PWM; /* Set TIO0 PWM */
temp = TIOIR0;
temp |= ( TIOIS3 | TIOIS2 | TIOIS1) | TIOIM0; /* Disable TIO0 interrupt */
TIOIR0 = temp;
}
/*""FUNC COMMENT""*******************************************************
* Function name: TIO0_PWM_out()
*-----------------------------------------------------------------------
* Description : Drive TIO1 in PWM mode
*-----------------------------------------------------------------------
* Argument : unsigned short cycle PWM period
* : unsigned short duty High duty (0~100%:0x0000~0x0100)
*-----------------------------------------------------------------------
* Returns : Terminated normally 1
* : Terminated abnormally 0
* : - PWM period = 0
*-----------------------------------------------------------------------
* Notes : PWM periodFor 0/100% duty cycle, OFF/ON processing is performed in port mode.
*""FUNC COMMENT END""***************************************************/
unsigned long TIO0_PWM_out( unsigned short cycle, unsigned short duty)
{
unsigned long Hwidth;
if( cycle == 0) { /* Determine PWM period */
return( NG);
}
/*** Calculating high-level output time ***/
Hwidth = duty * cycle;
Hwidth >>= 8;
DisInt(); /* Disable TIO0 interrupt */
/*** duty 0% ***/
if(( duty == 0) ||
( Hwidth == 0)) {
TIOPRO = ~TIO0PRO; /* Enable TIO0 enable protect rewrite */
TIOCEN = 0x0000; /* Stop TIO0 count (PWM stop) */
P10DATA &= ~0x10; /* P103 low(0) output */
P10DIR |= 0x10; /* P103 output */
P10MOD &= ~0x10; /* Disable TO11 (TIO0 output) against output .. Port P103 output selected */
}
/*** duty 100% ***/
else if(( duty >= 0x100) ||
( Hwidth >= cycle)) {
TIOPRO = ~TIO0PRO; /* Enable TIO0 enable protect rewrite */
TIOCEN = 0x0000; /* Stop TIO0 count (PWM stop) */
P10DATA |= 0x10; /* P103 High(0) output */
P10DIR |= 0x10; /* P103 output */
P10MOD &= ~0x10; /* Disable TO11 (TIO0 output) against output .. Port P103 output selected */
}
/*** PWM output ***/
else {
TIO0RL1 = cycle - (unsigned short)Hwidth - 1;
TIO0RL0 = (unsigned short)Hwidth - 1;
if(( TIOCEN & TIO0CEN) == 0) { /* Start output from 0% or 100% state */
FFP0 = ~FP11; /* Enable F/F11 rewrite */
FFD0 = 0x0000; /* F/F11 low (0) output (inverted to high during timer operation) */
TIOPRO = ~TIO0PRO; /* Enable TIO0 enable protect rewrite */
TIOCEN = 0xffff; /* Start TIO0 count (PWM start) */
/*** Preventing glitch generated by a prescaler equivalent delay ***/
while(( FFD0 & FD11) == 0); /* Start timer and wait until F/F11 goes high */
}
P10MOD |= 0x10; /* Select TO11 (TIO0 output) for output */
}
EnInt(); /* Enable interrupt */
return( OK);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -