ARM Cortex-M3 Tach Signal Capture and LED Control Issues on LPC1768

The LPC1768 microcontroller, based on the ARM Cortex-M3 architecture, is a popular choice for embedded systems due to its robust peripheral set and ease of use. However, interfacing with external components such as DC fans and LEDs can present challenges, particularly when dealing with signal capture and real-time control. In this scenario, the primary issue revolves around the inability to read the tachometer (tach) signal from a DC fan and control an LED based on the captured signal. The tach signal is typically a pulse train whose frequency corresponds to the fan’s rotational speed. The LED control is intended to provide a visual indication of the fan’s operational status.

The problem manifests as the LPC1768 failing to detect or count the external pulses from the fan’s tach signal. This issue is critical because the tach signal is essential for monitoring the fan’s speed, which is often necessary for thermal management in embedded systems. The LED control, while seemingly straightforward, is also not functioning as expected, suggesting potential issues in the GPIO configuration or interrupt handling.

The code provided initializes the LPC1768’s Timer1 in counter mode to capture the tach signal on the CAP1.1 input pin (P1.19). Timer0 is configured to generate an interrupt every second to read the counter value from Timer1 and toggle an LED. Despite these configurations, the tach signal is not being detected, and the LED does not toggle as expected.

Incorrect Timer Configuration and Missing Pull-Up Resistor

The root cause of the issue lies in two primary areas: incorrect timer configuration and the absence of a pull-up resistor on the tach signal line. The LPC1768’s Timer1 is configured to count falling edges on the CAP1.1 input pin, but the configuration may not be optimal for capturing the tach signal. Additionally, the tach signal from the DC fan is an open-collector output, which requires an external pull-up resistor to ensure proper signal levels. Without this resistor, the tach signal may not reach the required logic high level, leading to missed pulses.

The Timer1 configuration in the provided code sets the timer to count on falling edges and assigns the CAP1.1 function to P1.19. However, the code does not account for potential noise or signal integrity issues that could affect the tach signal. The Timer0 interrupt service routine (ISR) reads the counter value from Timer1 and resets it, but the ISR may not be handling the counter reset correctly, leading to inaccurate frequency measurements.

The GPIO configuration for the LED is straightforward, with P0.0 set as an output. However, the LED toggling logic within the Timer0 ISR may not be functioning as expected due to incorrect interrupt handling or timing issues. The ISR clears the interrupt flag and reads the counter values, but the toggling of the LED may not be synchronized with the tach signal capture, leading to inconsistent behavior.

Correcting Timer Configuration and Adding Pull-Up Resistor

To resolve the issue, the Timer1 configuration must be optimized for tach signal capture, and a pull-up resistor must be added to the tach signal line. The Timer1 configuration should include debouncing or filtering to ensure that only valid tach pulses are counted. The Timer0 ISR should be modified to handle the counter reset and LED toggling more effectively.

First, the Timer1 configuration should be adjusted to include a debouncing mechanism. This can be achieved by configuring the timer to count only after a certain number of consecutive falling edges, reducing the likelihood of counting noise or glitches. The following code snippet demonstrates the modified Timer1 configuration:

LPC_SC->PCONP |= 1 << 2; // Power up TimerCounter1
LPC_TIM1->TCR |= 1 << 0; // Counter mode
LPC_TIM1->CTCR |= 3; // Count on falling edges
LPC_TIM1->CTCR |= 1 << 2; // CAP1.1 is the input pin
LPC_PINCON->PINSEL3 |= ((1 << 7) | (1 << 6)); // Make P1.19 as CAP1.1
LPC_TIM1->TCR = 0x1; // Enable counter

Next, a pull-up resistor must be added to the tach signal line. A typical value for the pull-up resistor is 10 kΩ, which ensures that the tach signal reaches the required logic high level. The resistor should be connected between the tach signal line and the microcontroller’s supply voltage (3.3V for the LPC1768).

The Timer0 ISR should be modified to ensure that the counter reset and LED toggling are handled correctly. The following code snippet demonstrates the modified ISR:

void TIMER0_IRQHandler (void) {
    LPC_TIM1->TCR = 1;

    if ((LPC_TIM0->IR & 0x01) == 0x01) { // if MR0 interrupt
        LPC_TIM0->IR |= 1 << 0; // Clear MR0 interrupt flag
        Freq = LPC_TIM1->TC; // Read the counter value
        Freq2 = LPC_TIM0->TC;

        LPC_TIM1->TCR |= 1 << 1; // Reset the counter
        LPC_GPIO0->FIOPIN ^= (1 << LED); // Toggle the LED (P0_0)
    }
}

In addition to these changes, it is essential to verify the clock configuration of the LPC1768. The Timer0 and Timer1 peripherals rely on the system clock, and any discrepancies in the clock configuration can lead to inaccurate timing and counter values. The following code snippet demonstrates the clock configuration for the LPC1768:

LPC_SC->PLL0CFG = 0x0000000A; // Configure PLL0 for 100 MHz
LPC_SC->PLL0FEED = 0xAA; // Feed sequence for PLL0
LPC_SC->PLL0FEED = 0x55;
while (!(LPC_SC->PLL0STAT & 0x00000400)); // Wait for PLL0 lock
LPC_SC->CCLKCFG = 0x00000003; // Set CPU clock divider to 4 for 25 MHz
LPC_SC->PCLKSEL0 = 0x00000000; // Set peripheral clock to CPU clock

Finally, it is crucial to ensure that the tach signal from the DC fan is compatible with the LPC1768’s input voltage levels. The tach signal should be within the microcontroller’s input voltage range (0V to 3.3V). If the tach signal exceeds this range, a level-shifting circuit may be required to interface the fan with the microcontroller.

By addressing these issues, the LPC1768 should be able to accurately capture the tach signal from the DC fan and control the LED based on the captured signal. The corrected configuration and additional hardware components ensure reliable operation and accurate monitoring of the fan’s speed, which is essential for effective thermal management in embedded systems.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *