标签:
//p2.0 pwm1.1
void ALS_Init(void) //初始化
{
LPC_SC->PCONP |= (1<<1)|(1<<2)|(1<<15);//打开时钟
LPC_SC->PCLKSEL0 &= ~(3<<2); //时钟25M
LPC_SC->PCLKSEL0 &= ~(3<<4);
//p2.0
LPC_PINCON->PINSEL4 &= ~(0X03L<<0); //GPIO
LPC_PINCON->PINMODE4 &= ~(0X03L<<0); //使能上拉
LPC_PINCON->PINMODE_OD2 &= ~(0X01<<1); //推挽正常模式
P2dir(0) = 1;//输出
LPC_TIM1->TCR |= (1<<1); //复位定时器
LPC_TIM1->CTCR = 0x00; //定时器模式
LPC_TIM1->PC = 0X00;
LPC_TIM1->PR = 25-1; //预分频,计数频率1m
LPC_TIM1->MR0 = 20; //25*100 2500 频率10K
LPC_TIM1->MR1 = 100;
LPC_TIM1->MCR |= ((1<<0)|(1<<3)|(1<<4)); //MR0与TC值匹配时将产生复位 产生中断 MR1匹配时产生中断 不复位
LPC_TIM1->IR |= 0XFF; //清所有中断标志位
LPC_TIM1->TCR = (1<<0); //启动定时器
NVIC_EnableIRQ(TIMER1_IRQn);
}
void ALS_Set_Duty(u8 duty) //设置占空比
{
LPC_TIM1->TCR = 0; //暂停定时器
if(duty >= 100)duty = 99;
if(duty == 0)duty = 1;
LPC_TIM1->MR0 = duty;
LPC_TIM1->TCR = (1<<0); //启动定时器
}
void ALS_Stop(void)
{
LPC_TIM1->TCR = 0; //暂停定时器
P2low(0) = 1; //电平恒定
}
void TIMER1_IRQHandler(void)
{
NVIC_ClearPendingIRQ(TIMER1_IRQn);
if( LPC_TIM1->IR &0x01) //mr0中断
{
P2low(0) = 1;
LPC_TIM1->IR |= (1<<0); //清中断标志
}
else if(LPC_TIM1->IR &0x02)
{
P2high(0) = 1;
LPC_TIM1->IR |= (1<<1); //清中断标志
}
}
标签:
原文地址:http://www.cnblogs.com/dengxiaojun/p/4338686.html