【R7F0C809】TAU 输出PWM驱动LED闪起来
[复制链接]
【R7F0C809】TAU 输出PWM驱动LED闪起来
PWM输出驱动LED做 检测触发显示提示用!,LED亮度要不断改变
硬件接口:P15口 /TOO1
注意:目前输出IO驱动LED(非上拉),所以要禁止N 方式输出
一、主函数
初始化硬件后,自由运行
INTERVAL_TIMER_ini();
/* TAU0 Timer Init */
TAU0_PWM_Init();
/* Enable Interrupt */
EI();
/* Start Timer, i.e. PWM output */
TAU0_Start();
while(1)
{
}
二、初始化PWM
void TAU0_PWM_Init(void)
{
TAU0EN = 1; /* supplies input clock */
TPS0 = 0x22; /* CK01=fclk, CK00=fclk/2^15 20M/32767=610HZ*/
/* Stop all channels *///TT03=TT02=TT01=TT00=1
TTH0 =0x02U;
TT0 = 0x03U;
TT0 |= 0x04U;
TT0 |= 0x08U;
//--------------------------------------------------------------
/* 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 */
TMIF01 = 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; //Specifying level 3 (low priority)
/* Set INTTM01 low priority */
TMPR101 = 1U;
TMPR001 = 1U;
/* Set INTTM02 low priority */
TMPR102 = 1U;
//TMPR002 = 1U;
/* Set INTTM03 low priority */
TMPR103 = 1U;
//TMPR003 = 1U;
//---------------------------------------------------------------
/* Channel 0 is used as master channel for PWM output function */
TMR00H = 0x00;//_00_TAU_CLOCK_SELECT_CKM0 | _00_TAU_CLOCK_MODE_CKS | _00_TAU_TRIGGER_SOFTWARE;
TMR00L = 0x01U ;//_01_TAU_MODE_PWM_MASTER;
DI();
TDR00H = 0x0FU;//
TDR00L = 0xFFU;//C34F 49999
EI();
TO0 &= (uint8_t)~0x01U; //TO00=0 Timer output value is “0”.
TOE0 &= (uint8_t)~0x01U;//TOE00 ,Disable output of timer
//---------------------------------------------------------------
/* Channel 1 is used as slave channel for PWM output function */
TMR01H = 0x04U;
TMR01L = 0x09U;
DI();
TDR01H = 0x00U;//2710 //10000
TDR01L = 0x00U;
EI();
TOM0 |= 0x02U; //Format of Timer Output Mode Register 0 (TOM0)
TOL0 &= (uint8_t)~0x02U;
TO0 &= ((uint8_t)~0x03U );//TO01=TO00=0 Timer output value is “0”.
TOE0 |= (0x03U );//TOE01=TOE00=1 Enable output of timer.
//----------------------------------------------------------------
/* Channel 2 is used as slave channel for PWM output function */
TMR02H = 0x04U;
TMR02L = 0x09U;
DI();
TDR02H = 0x00U;//2710 //10000
TDR02L = 0x00U;
EI();
TOM0 |= 0x04U; //Format of Timer Output Mode Register 0 (TOM0)
TOL0 &= (uint8_t)~0x04U;
TO0 &= ((uint8_t)~0x04U );//TO02=0 Timer output value is “0”.
TOE0 |= (0x04U );//TOE02 Enable output of timer.
/* Set TO01 pin */
PMC1 &= 0x00U;
P1 &= 0x00U;
PM1 &= 0x00U;
}
三、开启定时器功能
void TAU0_Start(void)
{
TMIF00 = 0U; /* clear INTTM00 interrupt flag */
TMMK00 = 0U; /* enable INTTM00 interrupt */
TMIF01 = 0U; /* clear INTTM01 interrupt flag */
TMMK01 = 0U; /* enable INTTM01 interrupt */
/*
TMIF02 = 0U; // clear INTTM02 interrupt flag
TMMK02 = 0U; // enable INTTM02 interrupt
TMIF03 = 0U; // clear INTTM03 interrupt flag
TMMK03 = 0U; // enable INTTM03 interrupt
*/
TOE0 |= 0x03U; // enable CH1 CH0 output
/*
TOE0 |= 0x04U; //CH2
TOE0 |= 0x08U; //CH3
*/
TS0 = 0x03U; // Trigger CH0 start
/*
TS0 |= 0x04U; // Trigger CH2 start
TS0 |= 0x08U; // Trigger CH3 start
*/
}
四、间隔定时器
不断改变PWM中的占空比值 cnt[1],从小变大,在从大变小
__interrupt void Interrupt_INTIT(void)
{
//P00 OUTPUT LED
P0 ^= 0b00000001U;
//PWM
if(pwm_data[0].dir==0)
{
pwm_data[0].inc++;
if(pwm_data[0].inc>pwm_data[0].max)
{
pwm_data[0].dir=1;
}
}
else//pwm_data[0].dir=1
{
pwm_data[0].inc--;
if(pwm_data[0].inc==pwm_data[0].min)
{
pwm_data[0].dir=0;
//---------------------------------
if(pwm_data[0].max>100)
{
pwm_data[0].max=pwm_data[0].max-500;
pwm_data[0].min=pwm_data[0].min-20;
}
else
{
pwm_data[0].max=MAX_PWM;
pwm_data[0].min=MIN_PWM;
}
}
}
cnt[1]=pwm_data[0].inc;
}
五、在PWM中断中更新占空值
__interrupt void Interrupt_INTTM01(void) /*Stepper motor commutation operation*/
{
TDR01H = (uint8_t)(cnt[1] >> 8);
TDR01L = (uint8_t)cnt[1];
}
|