📄 bsp_piezo.c
字号:
#include <includes.h>
#define TCR_CNT_EN 0x00000001
#define TCR_RESET 0x00000002
#define TCR_PWM_EN 0x00000008
#define PWMMR0R 1 << 1
#define PWMENA6 1 << 14
#define LER0_EN 1 << 0
#define LER6_EN 1 << 6
CPU_INT32U Piezo_Frequency;
CPU_INT08U Piezo_Duration;
/*********************************************************************************************************
* PWW_PiezoInit()
*
* Description : Inniatialize and enable a singled edge PWM signal of 1Khz at P1.26
*
* Argument(s) : none.
*
* Return(s) : none.
*********************************************************************************************************
*/
void PWW_PiezoInit (void)
{
CPU_INT32U pclk_freq;
CPU_INT32U rld_cnts;
Piezo_Duration=0;
/* VIC timer #0 Initialization */
// VICIntSelect &= ~(1 << VIC_TIMER0); /* Configure the timer interrupt as an IRQ source */
// VICVectAddr4 = (CPU_INT32U)Tmr_TickISR_Handler; /* Set the vector address */
// VICIntEnable = (1 << VIC_TIMER0); /* Enable the timer interrupt source */
PINSEL3|=0x00200000; /* (1) enable P1.26 as PWM1(6) and as an output */
pclk_freq = BSP_CPU_PclkFreq(PCLK_PWM1); /* Get the peripheral clock frequency */
rld_cnts = pclk_freq / 1000; /* Calculate the # of counts necessary for the OS ticker */
PWM1TCR = TCR_RESET; /* Counter Reset */
PWM1PR = 0x00; /* count frequency:Fpclk */
PWM1MR0 = rld_cnts; /*PWW 0 controls the frequency of the waveform= 1000 Hz */
PWM1MR6 = rld_cnts / 2; /*PWW 6 controls the duty cycle: 50 %*/
PWM1MCR = PWMMR0R; // reset on MR0
// PWM1IR = 0x07FF; //reset all interrupt flags
// PWM1CCR = 0x0000; // turn off capture mode
//PWM1PCR = 0x00004040; // PWM6 enabled and selected
PWM1PCR = PWMENA6; /*Single edge and PWM6 output enabled*/
PWM1LER = LER0_EN | LER6_EN; /*Enable Latchs for MR0 and MR6 registers*/
/* (4) start the PWM1 module in PWM mode */
//PWM1TCR = TCR_CNT_EN | TCR_PWM_EN ;
}
/******************************************************************************
** Function name: PWM_Set
**
** Descriptions: PWM cycle setup
**
** parameters: PWM cycle
** Returned value:
**
******************************************************************************/
void Piezo_Set(CPU_INT32U cycle){
CPU_INT32U pclk_freq;
CPU_INT32U rld_cnts;
pclk_freq = BSP_CPU_PclkFreq(PCLK_PWM1); /* Get the peripheral clock frequency */
rld_cnts = pclk_freq / cycle; /* Calculate the # of counts necessary for the OS ticker */
PWM1MR0 = rld_cnts; /*PWW 0 controls the frequency of the waveform */
PWM1MR6 = rld_cnts / 2; /*PWW 6 controls the duty cycle: 50 %*/
/* The LER will be cleared when the Match 0 takes place, in order to
load and execute the new value of match registers, PWMLERs need to
reloaded. */
PWM1LER = LER0_EN | LER6_EN;
}
/******************************************************************************
** Function name: PWM_Start
**
** Descriptions: Enable PWM by setting the PCR, PTCR registers
**
** parameters: duration (seconds)
** Returned value:
**
******************************************************************************/
CPU_INT08U Piezo_Start(CPU_INT08U duration){
/* All single edge, all enable */
if (Piezo_Duration ==0){
Piezo_Duration = duration;
PWM1TCR = TCR_RESET;
PWM1PCR = PWMENA6;
PWM1TCR = TCR_CNT_EN | TCR_PWM_EN; /* counter enable, PWM enable */
return DEF_TRUE;
}else{
return DEF_FALSE;
}
}
/******************************************************************************
** Function name: PWM_Stop
**
** Descriptions: Stop the PWM channel
**
** parameters: channel number
** Returned value: None
**
******************************************************************************/
void Piezo_Stop( void ){
PWM1PCR = 0;
PWM1TCR = 0x00; /* Stop all PWMs */
}
/******************************************************************************
** Function name: PiezoDurationHandler
**
** Descriptions: Piezo Setup includes a duration, this routine is
** supossed to be called every second, to decrease the
** timer and stop the PWM signal.
**
** parameters:
** Returned value: None
**
******************************************************************************/
void PiezoDurationHandler()
{
if(Piezo_Duration >0)
Piezo_Duration--;
if(Piezo_Duration==0){
Piezo_Stop();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -