Input frequency measurement using LPC2148
Today I am going to discuss 'how to measure input frequency using LPC2148'. This is helpful in case when u don't have an oscillator and still want to measure frequency.
The process is very straight forward, you have to measure the number of times input pin goes high and low in 1 second. This measure will give you the frequency.
HOW TO
1.) Set a pin as input and make sure you connect a pull-up register to it. Pull-up is necessary otherwise pin will not go to high state.
2.) Give in the frequency input to the pin and display the frequency on the serial terminal via UART.
Some Insight into the Code:-
IODIR1 |= (1<<18); // will select the Port1.18 as input
while (1)
{
if (!(IOPIN1&(1<<18))) // if the pin is low
{
while (!(IOPIN1&(1<<18))) // wait until the pin is low
}
else if (IOPIN1&(1<<18)) // if the pin is high
{
while (IOPIN1&(1<<18)) // wait until the pin is high
}
counter++; // increment the counter by 1
freq = increment/2;
counter = 0;
}
CODE:-
#include <lpc214x.h>
#include <stdint.h>
#include "pll.h"
#include "timer0.h"
#include "uart0.h"
#include <stdio.h>
static uint32_t increment = 0;
uint32_t time, freq;
char converted[6];
int main ()
{
PINSEL1 = 0;
IODIR0|= (0<<18); // set P0.18 as input
pll_init (); // initialsise pll clocks
VPBDIV = 0x00; // set pclk = 15 MHz
timer0_init (15000000); // initialize timer with 1 sec base
Uart0_init (); // initialize uart
while (1)
{
T0TC=0; // set 0 to TC counter
T0TCR=(1<<1); //reset timer
T0TCR=(1<<0); // enable timer
while (T0TC<1) // while loop for a delay of 1 second
{
if (!(IOPIN0&(1<<18))) // if the pin is low
{
while(!(IOPIN0&(1<<18))); // wait until the pin is low
}
else if ((IOPIN0&(1<<18))) // if the pin is high
{
while (IOPIN0&(1<<18)); // wait until the pin is high
}
increment++; // increment the counter
}
freq = increment/2;
increment = 0;
sprintf (converted, "%u", freq);
Uart0_Tx_string (converted); // send via UART
Uart0_Tx_string (" Hz \n");
while (*converted)
{
int i =0;
converted[i++] = 0;
}
}
}
The process is very straight forward, you have to measure the number of times input pin goes high and low in 1 second. This measure will give you the frequency.
HOW TO
1.) Set a pin as input and make sure you connect a pull-up register to it. Pull-up is necessary otherwise pin will not go to high state.
2.) Give in the frequency input to the pin and display the frequency on the serial terminal via UART.
Some Insight into the Code:-
IODIR1 |= (1<<18); // will select the Port1.18 as input
while (1)
{
if (!(IOPIN1&(1<<18))) // if the pin is low
{
while (!(IOPIN1&(1<<18))) // wait until the pin is low
}
else if (IOPIN1&(1<<18)) // if the pin is high
{
while (IOPIN1&(1<<18)) // wait until the pin is high
}
counter++; // increment the counter by 1
freq = increment/2;
counter = 0;
}
CODE:-
#include <lpc214x.h>
#include <stdint.h>
#include "pll.h"
#include "timer0.h"
#include "uart0.h"
#include <stdio.h>
static uint32_t increment = 0;
uint32_t time, freq;
char converted[6];
int main ()
{
PINSEL1 = 0;
IODIR0|= (0<<18); // set P0.18 as input
pll_init (); // initialsise pll clocks
VPBDIV = 0x00; // set pclk = 15 MHz
timer0_init (15000000); // initialize timer with 1 sec base
Uart0_init (); // initialize uart
while (1)
{
T0TC=0; // set 0 to TC counter
T0TCR=(1<<1); //reset timer
T0TCR=(1<<0); // enable timer
while (T0TC<1) // while loop for a delay of 1 second
{
if (!(IOPIN0&(1<<18))) // if the pin is low
{
while(!(IOPIN0&(1<<18))); // wait until the pin is low
}
else if ((IOPIN0&(1<<18))) // if the pin is high
{
while (IOPIN0&(1<<18)); // wait until the pin is high
}
increment++; // increment the counter
}
freq = increment/2;
increment = 0;
sprintf (converted, "%u", freq);
Uart0_Tx_string (converted); // send via UART
Uart0_Tx_string (" Hz \n");
while (*converted)
{
int i =0;
converted[i++] = 0;
}
}
}
That's it. You can change the frequency input in the clock and see the output in the debugger.
Subscribe for more tutorials. Check out my youtube channel Controllers tech
Download full code with simulation HERE
Input frequency measurement using LPC2148
Reviewed by Controllerstech
on
June 24, 2017
Rating:
No comments: