#include "chip.h"
#include <stdio.h>
#define FLOW_PIN_PORT 1
#define FLOW_PIN_NUM 8
#define FLOW_IRQ_NUM PIN_INT0_IRQn // Using PININT0
#define FLOW_CH PININTCH0
volatile uint32_t pulse_count = 0;
/* Interrupt Service Routine for flow sensor */
void PIN_INT0_IRQHandler(void) {
if (Chip_PININT_GetFallStates(LPC_PININT) & (1 << FLOW_CH)) {
pulse_count++; // Count pulses
Chip_PININT_ClearIntStatus(LPC_PININT, FLOW_CH);
}
}
int main(void) {
SystemCoreClockUpdate();
// Initialize GPIO
Chip_GPIO_Init(LPC_GPIO_PORT);
Chip_IOCON_PinMuxSet(LPC_IOCON, FLOW_PIN_PORT, FLOW_PIN_NUM,
(IOCON_FUNC0 | IOCON_MODE_PULLUP));
Chip_GPIO_SetPinDIRInput(LPC_GPIO_PORT, FLOW_PIN_PORT, FLOW_PIN_NUM);
// Map pin to interrupt channel (channel 0)
Chip_INMUX_PinIntSel(0, FLOW_PIN_PORT, FLOW_PIN_NUM);
// Configure falling-edge interrupt
Chip_PININT_ClearIntStatus(LPC_PININT, FLOW_CH);
Chip_PININT_SetPinModeEdge(LPC_PININT, FLOW_CH);
Chip_PININT_EnableIntLow(LPC_PININT, FLOW_CH);
// Enable NVIC for PIN_INT0
NVIC_ClearPendingIRQ(FLOW_IRQ_NUM);
NVIC_EnableIRQ(FLOW_IRQ_NUM);
while (1) {
uint32_t start_count = pulse_count;
// crude 1 second delay loop
for (volatile uint32_t i = 0; i < (SystemCoreClock/1000 * 1000); i++);
uint32_t pulses_per_sec = pulse_count - start_count;
float flow_rate = (float)pulses_per_sec / 7.5f; // L/min (depends on sensor spec!)
printf("Flow Rate: %.2f L/min, Total pulses: %lu\n",
flow_rate, pulse_count);
}
}