#include "LPC13xx.h" /* LPC13xx Peripheral Registers */
#include "gpio.h"
#include "uart.h"
#include "config.h"
#include "stdio.h"
#define EN_CT16B1 (1<<8)
volatile unsigned int timer_up; //timer high
volatile unsigned int timer_down; //timer low
volatile unsigned int timer_count; //timer difference
extern volatile uint32_t UARTCount;
extern volatile uint8_t UARTBuffer[BUFSIZE];
void timer_config(void)
{
LPC_SYSCON->SYSAHBCLKCTRL |= (EN_CT16B1); //enable clock CT16B1
//init i/o CT16B1_CAP0
LPC_IOCON->PIO1_8 = (1<<0) | (1<<4); //CT16B1_CAP0 & pullup
LPC_TMR16B1->CTCR = 0; //use timer mode
LPC_TMR16B1->CCR = 7; //capture rising & falling edge, enable interrupt
LPC_TMR16B1->TCR = 1; //start timer
NVIC_EnableIRQ(TIMER_16_1_IRQn);
}
void TIMER16_1_IRQHandler(void)
{
LPC_TMR16B1->TC = 0; //reset counter
if(LPC_GPIO1->DATA & (1<<8) )
{
timer_down = LPC_TMR16B1->CR0;
UARTSend("H",1);
GPIOSetValue( 0, 7, 1 ); //LED_ON;
}
else
{
timer_up = LPC_TMR16B1->CR0;
UARTSend("L",1);
GPIOSetValue( 0, 7, 0 ); //LED_OFF;
timer_count = (timer_up - timer_down);
printf ("Timer16B1 Count : %d\n", timer_count);
}
LPC_TMR16B1->IR |= (1<<4); //reset interrupt
}
int main (void)
{
UARTInit(115200);
GPIOInit();
//GPIOSetDir( PORT2, 1, 0 );
//GPIOSetInterrupt( PORT2, 1, 0, 1, 1 ); // original 'extint' example - input pin
//GPIOIntEnable( PORT2, 1 );
//GPIOSetDir( PORT1, 8, 0 ); // Not needed to Setup Port1_8 as input.. done in TimerConfig.
//GPIOSetInterrupt( PORT1, 8, 0, 1, 1 );
//GPIOIntEnable( PORT1, 8 );
GPIOSetDir( 0, 7, 1 ); // set LPCExpresso LED as echo of input pulse stream
GPIOSetValue( 0, 7, 0 );
timer_config();
UARTSend("Pulse Length Counter", 20);
while (1)
{
if ( UARTCount != 0 ) // 'whatever I type in Hyper terminal is echoed back.'
{
LPC_UART->IER = IER_THRE | IER_RLS; /* Disable RBR */
UARTSend( (uint8_t *)UARTBuffer, UARTCount );
UARTCount = 0;
LPC_UART->IER = IER_THRE | IER_RLS | IER_RBR; /* Re-enable RBR */
}
}
}
|