📄 pwm.c
字号:
{
EnEvent = TRUE; /* Set the flag "events enabled" */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM_DisableEvent (bean PWMMC)
**
** Description :
** Disable the events. Method is available only if interrupt
** service/event is enabled.
** Parameters : None
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_SPEED - This device does not work in
** the active speed mode
** ===================================================================
*/
byte PWM_DisableEvent(void)
{
EnEvent = FALSE; /* Set the flag "events disabled" */
return ERR_OK; /* OK */
}
/*
** ===================================================================
** Method : PWM_InterruptOnReload (bean PWMMC)
**
** Description :
** This method is internal. It is used by Processor Expert only.
** ===================================================================
*/
#pragma interrupt alignsp saveall
void PWM_InterruptOnReload(void)
{
getReg(PWM_PMCTL); /* Read PWM Control Register with PWMF set */
clrRegBit(PWM_PMCTL,PWMF); /* Reset interrupt request flag */
if (EnEvent) { /* Are the events enabled? */
PWM_OnReload(); /* Invoke user event */
}
}
/*
** ===================================================================
** Method : PWM_Init (bean PWMMC)
**
** 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 PWM_Init(void)
{
/* PWM_PMICCR: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,ICC2=0,ICC1=0,ICC0=0 */
setReg16(PWM_PMICCR, 0);
/* PWM_PMCTL: LDFQ=0,HALF=1,IPOL2=0,IPOL1=0,IPOL0=0,PRSC=0,PWMRIE=1,PWMF=0,??=0,??=0,LDOK=0,PWMEN=0 */
setReg16(PWM_PMCTL, 2080);
/* PWM_PMFCTL: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,FIE3=0,FMODE3=0,FIE2=0,FMODE2=0,FIE1=0,FMODE1=0,FIE0=0,FMODE0=0 */
setReg16(PWM_PMFCTL, 0);
/* PWM_PMDISMAP1: DISMAP=0 */
setReg16(PWM_PMDISMAP1, 0);
/* PWM_PMDISMAP2: ??=0,??=0,??=0,??=0,??=0,??=0,??=0,??=0,DISMAP=0 */
setReg16(PWM_PMDISMAP2, 0);
/* PWM_PMOUT: PAD_EN=0,??=0,OUTCTL=0,??=0,??=0,OUT=0 */
setReg16(PWM_PMOUT, 0);
/* PWM_PMCCR: ENHA=1,nBX=0,MSK=0,??=0,??=0,VLMODE=0,??=0,SWP45=0,SWP23=0,SWP01=0 */
setReg16(PWM_PMCCR, 32768);
/* PWM_PMCCR: ENHA=1,nBX=1,MSK=0,??=0,??=0,VLMODE=0,??=0,SWP45=0,SWP23=0,SWP01=0 */
setReg16(PWM_PMCCR, 49152);
EnEvent = TRUE; /* Enable events */
EnUser = TRUE; /* Enable device */
/* PWM_PMSRC: ??=0,??=0,CINV5=0,CINV4=0,CINV3=0,CINV2=0,CINV1=0,CINV0=0,SRC2=0,SRC1=0,SRC0=0 */
setReg16(PWM_PMSRC, 0);
/* PWM_PMDEADTM0: ??=0,??=0,??=0,??=0,PWMDT0=32 */
setReg16(PWM_PMDEADTM0, 32);
/* PWM_PMDEADTM1: ??=0,??=0,??=0,??=0,PWMDT1=32 */
setReg16(PWM_PMDEADTM1, 32);
/* PWM_PWMVAL0: VAL=800 */
setReg16(PWM_PWMVAL0, 800);
/* PWM_PWMVAL1: VAL=800 */
setReg16(PWM_PWMVAL1, 800);
/* PWM_PWMVAL2: VAL=800 */
setReg16(PWM_PWMVAL2, 800);
/* PWM_PWMVAL3: VAL=800 */
setReg16(PWM_PWMVAL3, 800);
/* PWM_PWMVAL4: VAL=800 */
setReg16(PWM_PWMVAL4, 800);
/* PWM_PWMVAL5: VAL=800 */
setReg16(PWM_PWMVAL5, 800);
/* PWM_PWMCM: ??=0,CM=1600 */
setReg16(PWM_PWMCM, 1600);
/* PWM_PMCFG: ??=0,DBG_EN=0,WAIT_EN=0,EDG=0,??=0,TOPNEG45=1,TOPNEG23=1,TOPNEG01=1,??=0,BOTNEG45=1,BOTNEG23=1,BOTNEG01=1,INDEP45=0,INDEP23=0,INDEP01=0,WP=0 */
setReg16(PWM_PMCFG, 1904);
HWEnDi(); /* Enable/disable device according to status flags */
}
/*
** ===================================================================
** Method : PWM_SetPeriod (bean PWMMC)
**
** Description :
** Setting PWM modulo register. The value is loaded after
** calling Load() method.
** Parameters :
** NAME - DESCRIPTION
** period - Value for setting PWM period
** register in range 15 bit (12-bit on HC08 family).
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_RANGE - Parameter period is out of
** range
** ===================================================================
*/
byte PWM_SetPeriod(word period)
{
if (period<32768) {
setReg(PWM_PWMCM,period); /* Set up period-modulo register */
}
else {
return ERR_RANGE;
}
return ERR_OK;
}
/*
** ===================================================================
** Method : PWM_SetDuty (bean PWMMC)
**
** Description :
** Setting duty(value) register of selected channel. The
** value is loaded after calling Load() method.
** Parameters :
** NAME - DESCRIPTION
** channel - channel 0-5
** duty - Duty value for selected channel.
** Writing a number less than or equal to 0
** causes the PWM to be off for the entire
** PWM period. Writing a number greater
** than or equal to the 15 bit (12-bit on
** HC08MR32 CPU) modulus causes the PWM to
** be on for the entire PWM period.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_NOTAVAIL - Channel is disabled
** ERR_RANGE - Parameter channel is out of
** range
** ===================================================================
*/
byte PWM_SetDuty(byte channel,int duty)
{
switch (channel) {
case 0 :
setReg(PWM_PWMVAL0,duty); /* Store value to the duty-compare register 0 */
break;
case 1 :
setReg(PWM_PWMVAL1,duty); /* Store value to the duty-compare register 1 */
break;
case 2 :
setReg(PWM_PWMVAL2,duty); /* Store value to the duty-compare register 2 */
break;
case 3 :
setReg(PWM_PWMVAL3,duty); /* Store value to the duty-compare register 3 */
break;
case 4 :
setReg(PWM_PWMVAL4,duty); /* Store value to the duty-compare register 4 */
break;
case 5 :
setReg(PWM_PWMVAL5,duty); /* Store value to the duty-compare register 5 */
break;
default: return ERR_RANGE;
}
return ERR_OK;
}
/*
** ===================================================================
** Method : PWM_SetDutyPercent (bean PWMMC)
**
** Description :
** Setting duty(value) in percent of selected channel. The
** value is loaded after calling Load() method.
** Parameters :
** NAME - DESCRIPTION
** channel - channel 0-5
** duty - Duty value in pecents for selected
** channel.
** Returns :
** --- - Error code, possible codes:
** ERR_OK - OK
** ERR_NOTAVAIL - Channel is disabled
** ERR_RANGE - Parameter channel is out of
** range
** ===================================================================
*/
byte PWM_SetDutyPercent(byte channel,byte duty)
{
register word dutyreg;
if (duty>100)
return ERR_RANGE;
dutyreg = (word)((dword)getReg(PWM_PWMCM)*duty/100); /* Calculate real duty from percent value */
switch (channel) {
case 0 :
setReg(PWM_PWMVAL0,dutyreg); /* Store percent duty value to the duty-compare register 0 */
break;
case 1 :
setReg(PWM_PWMVAL1,dutyreg); /* Store percent duty value to the duty-compare register 1 */
break;
case 2 :
setReg(PWM_PWMVAL2,dutyreg); /* Store percent duty value to the duty-compare register 2 */
break;
case 3 :
setReg(PWM_PWMVAL3,dutyreg); /* Store percent duty value to the duty-compare register 3 */
break;
case 4 :
setReg(PWM_PWMVAL4,dutyreg); /* Store percent duty value to the duty-compare register 4 */
break;
case 5 :
setReg(PWM_PWMVAL5,dutyreg); /* Store percent duty value to the duty-compare register 5 */
break;
default: return ERR_RANGE;
}
return ERR_OK;
}
/*
** ===================================================================
** Method : PWM_Load (bean PWMMC)
**
** Description :
** Apply last setting of the methods SetDuty, SetDutyPercent,
** SetPeriod and SetPrescaler.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
/*
void PWM_Load(void)
** This method is implemented as a macro. See PWM.h file. **
*/
/*
** ===================================================================
** Method : PWM_OutputPadEnable (bean PWMMC)
**
** Description :
** Method enables output pads.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
/*
void PWM_OutputPadEnable(void)
** This method is implemented as a macro. See PWM.h file. **
*/
/*
** ===================================================================
** Method : PWM_OutputPadDisable (bean PWMMC)
**
** Description :
** Method disables output pads.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
/*
void PWM_OutputPadDisable(void)
** This method is implemented as a macro. See PWM.h file. **
*/
/* END PWM. */
/*
** ###################################################################
**
** This file was created by UNIS Processor Expert 2.98 [03.79]
** for the Freescale 56800 series of microcontrollers.
**
** ###################################################################
*/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -