#include <NXP\iolpc2148.h>
#include <stdio.h>
#include <intrinsics.h> //中断头文件
#include "pll.h"
void Timer0_int(void);
const unsigned char TABLE[3]={1,4,8}; //占空比改变的值放到一个数组里
/**************************延时**************************/
void Delay(unsigned int dly)
{
unsigned int i;
for(; dly>0; dly--)
for(i=0; i<5000; i++);
}
//主函数
void main(void)
{
char i;
PLL_Init();
PINSEL1 = (0x03<<12); //MAT0.0连接到管脚
T0EMR = 0x01; //高电平
T0EMR = 0x01<<4; //匹配低电平
T0PR = 0; //9+1个Fpclk,TC增加一次
T0MCR = 0x18; //当MR1和TC相等时,TC复位,产生中断
T0MR1 = 15000000; //PWM的时钟频率为APB时钟频率的1/1000
T0MR0 = T0MR1/10; //占空比为90%
T0TCR = 0x02; //定时计数器复位
T0TCR = 0x01; //定时器使能
VICIntSelect = 0x00;
VICVectCntl0 = 0x20|VIC_TIMER0;
VICVectAddr0 = (unsigned int)Timer0_int;
VICIntEnable = (1<<VIC_TIMER0);
__enable_interrupt();
while(1)
{
for(i=0;i<3;i++)
{
T0MR0 = T0MR1*TABLE[i]/10;//改变占空比
Delay(1000);
}
}
}
//定时器1中断
void Timer0_int(void)
{
// T0IR = 0x02;
T0EMR= 0x01|0x01<<4; //高电平
VICVectAddr = 0;
}
//irq 中断入口
#pragma vector=IRQV
__irq __arm void irq_handler (void)
{
void (*interrupt_function)();
unsigned int vector;
vector = VICVectAddr; //获得中断向量.
interrupt_function = (void(*)())vector;
if(interrupt_function != NULL)
{
interrupt_function(); //调用向量中断函数.
}
else
{
VICVectAddr = 0; //清除VIC中的中断.
}
}
//fiq中断入口
__fiq __arm void fiq_handler (void)
{
while(1);
}
T0IR中断寄存器一直保持着TM0中断复位状态,进入不到中断服务程序 |