[SIZE=2][COLOR=#7f0055] [B][SIZE=2][COLOR=#7f0055]#include [/COLOR][/SIZE][/B][/COLOR][/SIZE][LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"LPC13xx.h"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* LPC13xx Peripheral Registers */[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f] [LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#include[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"gpio.h"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff] [LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#include[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"uart.h"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff] [LEFT][/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]#include[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"config.h"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#2a00ff] [/COLOR][/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extern[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]volatile[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint32_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] UARTCount;[/SIZE] [LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]extern[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]volatile[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint8_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] UARTBuffer[BUFSIZE];[/SIZE] [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/*****************************************************************************[/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f]** Main Function main()[/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f]******************************************************************************/[/COLOR][/SIZE] [/COLOR][/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]int[/LEFT] [/LEFT] [/COLOR][/SIZE][/COLOR][/SIZE][/B][LEFT][SIZE=2][B]main[/B] ([/SIZE][B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]void[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2])[/SIZE] [LEFT][SIZE=2]{[/SIZE][/LEFT] [LEFT][SIZE=2]UARTInit(115200);[/SIZE] [SIZE=2]GPIOInit();[/SIZE] [SIZE=2]GPIOSetDir( PORT2, 1, 0 );[/SIZE] [SIZE=2]GPIOSetInterrupt( PORT2, 1, 0, 1, 1 );[/SIZE][/LEFT] [LEFT][SIZE=2]GPIOIntEnable( PORT2, 1 );[/SIZE][/LEFT] [LEFT][SIZE=2]GPIOSetDir( LED_PORT, LED_BIT, 1 );[/SIZE] [SIZE=2]GPIOSetValue( LED_PORT, LED_BIT, LED_OFF );[/SIZE][/LEFT] [LEFT][SIZE=2][U]UARTSend([/U][/SIZE][U][SIZE=2][COLOR=#2a00ff][SIZE=2][COLOR=#2a00ff]"Pulse Length Counter"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2], 20);[/SIZE][/U][/LEFT] [LEFT][SIZE=2] [/SIZE] [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]while[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] (1)[/SIZE] [SIZE=2]{[/SIZE] [B][SIZE=2][COLOR=#7f0055][SIZE=2][COLOR=#7f0055]if[/COLOR][/SIZE][/COLOR][/SIZE][/B][SIZE=2] ( UARTCount != 0 ) [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]// whatever I type in Hyper terminal is echoed back.[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f] [/COLOR][/SIZE][SIZE=2]{[/SIZE] [SIZE=2]LPC_UART->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]IER[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = IER_THRE | IER_RLS; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Disable RBR */[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f] [/COLOR][/SIZE][SIZE=2]UARTSend( ([/LEFT] [/LEFT] [/SIZE][LEFT][SIZE=2][COLOR=#005032][SIZE=2][COLOR=#005032]uint8_t[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] *)UARTBuffer, UARTCount );[/SIZE] [LEFT][SIZE=2]UARTCount = 0;[/SIZE] [SIZE=2]LPC_UART->[/SIZE][SIZE=2][COLOR=#0000c0][SIZE=2][COLOR=#0000c0]IER[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] = IER_THRE | IER_RLS | IER_RBR; [/SIZE][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/* Re-enable RBR */[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][COLOR=#3f7f5f] [/COLOR][/SIZE][SIZE=2]}[/SIZE] [SIZE=2]}[/SIZE] [SIZE=2]}[/SIZE][/LEFT] [LEFT][SIZE=2][COLOR=#3f7f5f][SIZE=2][COLOR=#3f7f5f]/*********************************************************************************[/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f]** End Of File[/COLOR][/SIZE] [SIZE=2][COLOR=#3f7f5f]*********************************************************************************/[/COLOR][/SIZE][/LEFT] [/COLOR][/SIZE] |
//set CT32B1_CAP0 LPC_IOCON->JTAG_TMS_PIO1_0 &= 0x00; LPC_IOCON->JTAG_TMS_PIO1_0 |= ((0x01<<3)| 0x03);//CT32B1_CAP0, pull-down |
#include <stdio.h> // Function __write() ->printf int __write (int iFileHandle, uint8_t *pcBuffer, uint8_t iLength) { UARTSend(pcBuffer,iLength); return iLength; } |
volatile unsigned char flag_low=0; volatile unsigned char flag_high=0; ISR... { ... timer_down = LPC_TMR16B1->CR0; flag_low=1; ... timer_up = LPC_TMR16B1->CR0; flag_high=1; ... } while(1) { ... if (flag_low & flag_high) { flag_low =0; flag_high =0; ...calculate and printf() } ... } |
#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 */ } } } |
#include "LPC13xx.h" /* LPC13xx Peripheral Registers */ #include "gpio.h" #include "uart.h" #include "config.h" #define EN_CT16B1 (1<<8) volatile unsigned int timer_up; //timer high volatile unsigned int timer_down; //timer low extern volatile uint32_t UARTCount; extern volatile uint8_t UARTBuffer[BUFSIZE]; void TIMER16_1_IRQHandler(void) { LPC_TMR16B1->TC = 0; //reset counter if(LPC_GPIO1->DATA & (1<<8) ) { timer_down = LPC_TMR16B1->CR0; UARTSend("High",4); GPIOSetValue( 0, 7, 1 ); //LED_ON; } else { timer_up = LPC_TMR16B1->CR0; UARTSend("Low",3); GPIOSetValue( 0, 7, 0 ); //LED_OFF; } LPC_TMR16B1->IR |= (1<<4); //reset interrupt } int main (void) { UARTInit(115200); GPIOInit(); GPIOSetDir( PORT1, 8, 0 ); // setting P1_8 as input GPIOSetInterrupt( PORT1, 8, 0, 1, 1 ); GPIOIntEnable( PORT1, 8 ); GPIOSetDir( 0, 7, 1 ); // Setting the LED port as output ( to echo input ) GPIOSetValue( 0, 7, 0 ); //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); //} //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 */ } } } |
#define EN_CT16B1 (1<<8) volatile unsigned int timer_up; //timer high volatile unsigned int timer_down; //timer low void TIMER16_1_IRQHandler(void) { LPC_TMR16B1->TC = 0; //reset counter if(LPC_GPIO1->DATA & (1<<8) ) { timer_down = LPC_TMR16B1->CR0; // LED_ON; } else { timer_up = LPC_TMR16B1->CR0; // LED_OFF; } LPC_TMR16B1->IR |= (1<<4); //reset interrupt } void timer_config(void) { LPC_SYSCON->SYSAHBCLKCTRL |= (EN_CT16B1);//enable clock CT16B1 //init io 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); } |