||
//LPC2214。利用cap0.0的捕获中断方式获取PWM的正脉冲长度,通过串口发送
#include "config.h"
uint32 DUTY=0x12345678;
uint8 flag=0;
void DelayNS(uint32 dly)
{ uint32 i;
for(; dly>0; dly--)
for(i=0; i<5000; i++);
}
//串口初始化
# define UART0_BR 115200
void UART0_Init(void)
{ uint16 Div;
U0LCR = 0X83;
Div = (Fpclk >> 4) / UART0_BR;
U0DLL = Div; //Fdiv % 256;
U0DLM = Div>>8; //Fdiv / 256;
U0LCR = 0x03;
}
//定时器0初始化
void Timer0_Init(void)
{
T0PR = 0; //定时器0的时钟不分频
T0CCR = 0x005; //设置CAP1~4上升沿捕获并产生中断
T0TC = 0; //定时器0设置为0
T0TCR = 1;
}
//中断函数,测量DUTY
void __irq Timer0_IRQ(void)
{
uint32 time_0,time_1;
if(flag==0)
{
time_0=T0CR0; //记录CAP0.0上升沿捕获的时间
flag=1; //置标志位
T0CCR=0x006; //设置CAP0.0下降沿捕获并产生中断
}
else
{
time_1=T0CR0; //记录CAP0.0下降沿捕获的时间
flag=0; //置标志位
//获取DUTY值
if((time_1-time_0)<30000)
{ DUTY = (time_1-time_0);
}
T0CCR=0x005; //设置CAP0.0上升沿捕获并产生中断
}
T0IR = 0x10; //一定要清除中断标志位!
VICVectAddr = 0; //一定要有中断函数返回语句
}
//串口发送函数
void SendBuf(uint8 *pdata)
{ uint8 i;
for (i=0; i<4; i++)
{ U0THR = *(pdata++);
while((U0LSR & 0x20) == 0); //判断THRE位是否为1?
}
}
//向量中断初始化
void VICVECT_Init(void)
{ VICIntSelect = 0x00000000;
VICVectCntl0 = 0X20 | 4; //使能cap0中断
VICVectAddr0 = (uint32) Timer0_IRQ;
VICIntEnable = 1<<4;
}
int main()
{ uint8 data[4];
PINSEL0 = 0X00000025; //设置p0.0,p0.1连接到UART0,p0.2连接到cap0.0
UART0_Init();
Timer0_Init();
VICVECT_Init();
while(1)
{ DelayNS(10);
data[0] = DUTY >> 24;
data[1] = DUTY >> 16;
data[2] = DUTY >> 8;
data[3] = DUTY;
SendBuf( data );
}
return(0);
}