|
设计思路:
一、蜂鸣器:频率为40KHz,周期25us,占空比50%,P70为蜂鸣器开关,P31为PWM输出。
二、程 序:根据联动通道设置,主机通道为0,从机通道为3,PWM输出引脚为TO03。
三、原理图:
四、程序代码:
#define Port_Beep_OUT P7.0
#define Port_Beep_PWM P3.1
PM3.1 = 0; P3.1 = 0; //Buzzer PWM Out
PM7.0 = 0; P7.0 = 0; //Buzzer Switch
void R_TAU0_Create(void)
{
TAU0EN = 1U; /* supplies input clock */
TPS0 = _0000_TAU_CKM0_FCLK_0 | _0000_TAU_CKM1_FCLK_0 | _0000_TAU_CKM2_FCLK_1 | _0000_TAU_CKM3_FCLK_8;
/* Stop all channels */
TT0 = _0001_TAU_CH0_STOP_TRG_ON | _0002_TAU_CH1_STOP_TRG_ON | _0004_TAU_CH2_STOP_TRG_ON | _0008_TAU_CH3_STOP_TRG_ON | _0200_TAU_CH1_H8_STOP_TRG_ON | _0800_TAU_CH3_H8_STOP_TRG_ON;
/* Mask channel 0 interrupt */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
/* Mask channel 1 interrupt */
TMMK01 = 1U; /* disable INTTM01 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
/* Mask channel 1 higher 8 bits interrupt */
TMMK01H = 1U; /* disable INTTM01H interrupt */
TMIF01H = 0U; /* clear INTTM01H interrupt flag */
/* Mask channel 2 interrupt */
TMMK02 = 1U; /* disable INTTM02 interrupt */
TMIF02 = 0U; /* clear INTTM02 interrupt flag */
/* Mask channel 3 interrupt */
TMMK03 = 1U; /* disable INTTM03 interrupt */
TMIF03 = 0U; /* clear INTTM03 interrupt flag */
/* Mask channel 3 higher 8 bits interrupt */
TMMK03H = 1U; /* disable INTTM03H interrupt */
TMIF03H = 0U; /* clear INTTM03H interrupt flag */
/* Set INTTM00 low priority */
TMPR100 = 1U;
TMPR000 = 1U;
/* Set INTTM03 low priority */
TMPR103 = 1U;
TMPR003 = 1U;
/* Channel 0 is used as master channel for PWM output function */
TMR00 = _0000_TAU_CLOCK_SELECT_CKM0 | _0000_TAU_CLOCK_MODE_CKS | _0000_TAU_COMBINATION_MASTER | _0000_TAU_TRIGGER_SOFTWARE | _0001_TAU_MODE_PWM_MASTER;
TDR00 = 0x031F; //799,周期25us
TO0 &= ~_0001_TAU_CH0_OUTPUT_VALUE_1;
TOE0 &= ~_0001_TAU_CH0_OUTPUT_ENABLE;
/* Channel 3 is used as slave channel for PWM output function */
TMR03 = _0000_TAU_CLOCK_SELECT_CKM0 | _0000_TAU_CLOCK_MODE_CKS | _0000_TAU_COMBINATION_SLAVE | _0400_TAU_TRIGGER_MASTER_INT | _0009_TAU_MODE_PWM_SLAVE;
TDR03 = 0x0190; //400,占空比50%
TOM0 |= _0008_TAU_CH3_OUTPUT_COMBIN;
TOL0 &= ~_0008_TAU_CH3_OUTPUT_LEVEL_L;
TO0 |= _0008_TAU_CH3_OUTPUT_VALUE_1;
TOE0 |= _0008_TAU_CH3_OUTPUT_ENABLE;
/* Set TO03 pin */
P3 &= 0xFDU;
PM3 &= 0xFDU;
}
void R_TAU0_Channel0_Start(void)
{
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
TMMK00 = 0U; /* enable INTTM00 interrupt */
TMIF03 = 0U; /* clear INTTM03 interrupt flag */
TMMK03 = 0U; /* enable INTTM03 interrupt */
TOE0 |= _0008_TAU_CH3_OUTPUT_ENABLE;
TS0 |= _0001_TAU_CH0_START_TRG_ON | _0008_TAU_CH3_START_TRG_ON;
}
void R_TAU0_Channel0_Stop(void)
{
TT0 |= _0001_TAU_CH0_STOP_TRG_ON | _0008_TAU_CH3_STOP_TRG_ON;
TOE0 &= ~_0008_TAU_CH3_OUTPUT_ENABLE;
/* Mask channel 0 interrupt */
TMMK00 = 1U; /* disable INTTM00 interrupt */
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
/* Mask channel 1 interrupt */
TMMK03 = 1U; /* disable INTTM03 interrupt */
TMIF03 = 0U; /* clear INTTM03 interrupt flag */
}
#pragma interrupt INTTM00 r_tau0_channel0_interrupt
__interrupt static void r_tau0_channel0_interrupt(void)
{
}
#pragma interrupt INTTM03 r_tau0_channel3_interrupt
__interrupt static void r_tau0_channel3_interrupt(void)
{
}
void main(void)
{
hdwinit();
R_MAIN_UserInit();
R_TAU0_Channel0_Start();
while(1)
{
Port_Beep_OUT = 1; //打开蜂鸣器,让他一直响。
}
}
|
|