LPC1768: How to Read Tach signal for DC fan

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

LPC1768: How to Read Tach signal for DC fan

1,170 Views
rsapple_007
Contributor I

Hi,

Can anybody help me out on reading a Tach signal from DC fan and controlling an LED on/off.

iam a nee bee to this.

Thanks in advance

Labels (1)
0 Kudos
4 Replies

1,006 Views
soledad
NXP Employee
NXP Employee

Hi, 

You can use the one of the LPC1768 Timers as a frequency counter. The idea is to count the number of cycles of the input in one second. You need one counter that keeps counting the rising/falling edges of the input signal then read the value of this counter once in a second and then clear it.  

Below you can find pseudocode: 

1. Initialization of the counter

LPC_SC->PCONP |= 1 << 2; //Power up TimerCounter1

LPC_TIM1->TCR |= 1 << 0; // Counter mode
LPC_TIM1->CTCR |= 2; // Count on falling edges
LPC_TIM1->CTCR |= 1 << 2; // CAP1.1 is the input pin for which the input signal needs to be connected.

LPC_PINCON->PINSEL3 |= 3 << 6; // Make P1.19 as CAP1.1

 LPC_TIM1->TCR |= 1 << 1; // Reset the counter
LPC_TIM1->TCR |= 1 << 0; // Start counter


2. Initialization of the timer which generates interrupt once in a second
LPC_SC->PCONP |= 1 << 1; //Power up Timer0
LPC_SC->PCLKSEL0 |= 3 << 2; // Clock for timer = CCLK/8

LPC_TIM0->MR0 = 12499999; // Assuming that clk freq = 100 MHz, this value is calculated to generate an interrupt once in a second.

LPC_TIM0->MCR |= 1 << 0; // Interrupt on Match0 compare
LPC_TIM0->TCR |= 1 << 1; // Reset Timer0

NVIC_EnableIRQ(TIMER0_IRQn); // Enable timer interrupt

LPC_TIM0->TCR |= 1 << 0; // Start timer


3. Timer interrupt routine

In this routine we need to read the value of the counter if the interrupt was generated by MR0 and then we need to clear the counter.

inputFreq = LPC_TIM1->TC; // Read the counter value
LPC_TIM1->TCR |= 1 << 1; // Reset the counter

I hope this helps, 

Regards 

Soledad

0 Kudos

1,006 Views
rsapple_007
Contributor I

Hi,

Please see my code i have formed with your suggestion, but i unable to read external pulse (connected a TACH output fan), only internal timer value is able to read.

Please help to resolve the issue

#include <lpc17xx.h>
#include "uart.h"
#include "delay.h"

#define LED 0

volatile uint32_t Freq, Freq2;

int main (void)
{
UART0_Init(9600);
LPC_SC->PCONP |= ( 1 << 15 ); // power up GPIO
LPC_GPIO0->FIODIR |= 1 << LED; // puts P0.0 into output mode.


//Initialization of the counter on P1.19 as CAP1.1
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 for which the input signal needs to be connected.
LPC_PINCON->PINSEL3 |= ((1 << 7) | (1 << 6));; // Make P1.19 as CAP1.1

LPC_TIM1->TCR = 0x1; // Enable counter

//Initialization of the timer which generates interrupt once in a second
LPC_SC->PCONP |= 1 << 1; //Power up Timer0

LPC_TIM0->MR0 = 12499999; // Assuming that clk freq = 100 MHz, this value is calculated to generate an interrupt once in a second.
LPC_TIM0->MCR = 3; //interrupt and reset control

NVIC_EnableIRQ(TIMER0_IRQn); //enable timer0 interrupt

LPC_TIM0->TCR = 1; //enable Timer0

while(1)
{
UART0_TxFloatNumber(Freq);
UART0_Printf("\n");
DELAY_ms(500);
UART0_TxFloatNumber(Freq2);
UART0_Printf("\n");
}

}


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) */

}

}

0 Kudos

1,006 Views
soledad
NXP Employee
NXP Employee

Hi, 

Please check the following examples and tutorial. Let me know if this helps. 

http://www.ocfreaks.com/lpc1768-timer-input-capture-frequency-counter-tutorial/

Have a nice day!

Regards

Sol

0 Kudos

1,006 Views
rsapple_007
Contributor I

Thank you for your help,

i will try your advice.

0 Kudos