废话不多说,直奔主题。请看代码:
定时器中断函数:
void timer1_interrupt() ///125uS中断一次
{
///os_time_dly(1000);//延时函数
//timer_ctrl(timer1_hl,TIMER_CMD_SET_CNT,0,NULL);
time1_test_flag++;
if(time1_test_flag==8) ///假设中断时间是125微妙,那么加8次就等于1毫秒
{
time1_test_flag=0;
TEST1_PORT_ON();
//printf("SYS_CLK:%d",clock_get_sys_freq());
//printf("1\r\n");
}
else
{
TEST1_PORT_OFF();
// printf("2222222\r\n");
}
//printf("time1_test_flag:%d\r\n",time1_test_flag);
}
定时器初始化函数:
s32 time1_init(void)
{
s32 ret;
__timer_param timer_parm;
timer1_hl = timer_open(TIMER1,1);
if(NULL == timer1_hl)
{
printf("timer_open err");
ret = TIMER_DRV_OPEN_ERR;
return ret;
}
timer_parm.work_mode = TIMER_WORK_MODE_COUNTER;
//timer_parm.tick_time = 1000;//(1ms)
ret = timer_init_api(timer1_hl,&timer_parm);
if(ret != TIMER_NO_ERR)
{
printf("timer_init err = %x\n",ret);
return ret;
}
ret = timer_start(timer1_hl);
if(ret != TIMER_NO_ERR)
{
printf("timer_start err = %x\n",ret);
return ret;
}
/// JL_TIMER1->CON = 0x0051;///4*2分频
JL_TIMER1->CON = 0x0001;
//JL_TIMER1->CNT = 0;
JL_TIMER1->PRD = 7500;///60000/60M == 0.001秒
///7500/60M == 125us
ret = timer_reg_isr_callback_fun(timer1_hl,4,timer1_interrupt);
if(ret != TIMER_NO_ERR)
{
printf("timer_reg_isr_callback_fun err = %x\n",ret);
return ret;
}
return ret;
}
定时器的时钟周期是60M
JL_TIMER1->PRD这个寄存器的赋值可影响定时器中断时间
计算方式请看代码的注释。
做好这些,然后你就会发现定时器再也不是1ms中断一次了,可以做到微妙级了。
下面是PWM输出的周期和占空比设置,原理大致类似:
void PWM2_init(u16 led_pwm_cnt)
{
JL_IOMAP->CON1 &= ~(BIT(11)|BIT(12)|BIT(13));
JL_IOMAP->CON1 |= BIT(12);
JL_PORTC->DIR &= ~BIT(5);
JL_PORTC->PU |= BIT(5);
JL_PORTC->PD |= BIT(5);
JL_PORTC->DIE |= BIT(5);
JL_TIMER2->CNT = 0;
JL_TIMER2->PWM = led_pwm_cnt;//led_pwm_cnt/3600*100%==占空比为%
JL_TIMER2->PRD = 3600;//周期频率 3600/60M == 60us
JL_TIMER2->CON =BIT(0); //BIT(8)|BIT(0);
}
OK,以上请自行实测,多动手。Good Luck.