Square Wave Generator using LPC17xx (LPC1769) tutorial.

 This tutorial is about configuring the timer interrupt to generate a square wave whose frequency can be selected by GPIO select pins. In this tutorial we are using LPC 1769 as the controller. If you are new to ARM programming you can refer other tutorials.

LPC176x/5x General Purpose Input/Output (GPIO) Programming
LPC176x/5x Timer Programming
LPC176x/5x Analog-to-Digital Converter (ADC)
Sine Wave Generator using DAC with LPC17xx (LPC175x/LPC176x/LPC1769)


 We are not going to explain each and every step but to give a brief idea about the basic configuration of registers. The frequency of the square wave can be selected based on three select lines. The first step is to select three GPIO pins as input to accept the select input. Also we need to use one of the pins as the output pin to give pulse waveform.


LPC_GPIO0->FIODIR=0x0;
LPC_GPIO1->FIODIR=0x1;
>

 Next step is the configuration of timer registers. Different steps involved are.

1. Reset the timer by configuring TCR.
2. Set Prescale value in PR
3. Set the count value in Match register(MR)
4. Set appropriate value in MCR.
5. Enable the timer by configuring TCR.
6. Enable timer interrupt if needed.

LPC_TIM0->TCR = 0x02; /* reset timer */
LPC_TIM0->PR = 0x00; /* set prescaler to zero */
LPC_TIM0->MR0 = 2400; /* set the match register value */
LPC_TIM0->IR = 0xff; /* reset all interrrupts */
LPC_TIM0->MCR = 3; /* configure MCR register based on our requirement */
LPC_TIM0->TCR = 0x01; /* start timer */

Don’t forget to enable the timer interrupt.

 NVIC_EnableIRQ(TIMER0_IRQn); /* Enable interrupt */
 When the timer counter value matches with the match register value, the timer interrupt routine executed.

 LPC_TIM0->IR = 0xff; /* reset all interrrupts */
 LPC_GPIO1->FIOPIN=~(LPC_GPIO1->FIOPIN); /*Toggle the output pin */

 The frequency can be selected by setting the select line inputs. In this tutorial we are using the pins 4,5 and 6 of port0.


/*
=============================================================
Name : squarewavegenerator.c
Author : $(author)
Version :
Copyright : $(copyright)
Description : main definition
==============================================================
*/

#ifdef __USE_CMSIS
#include "LPC17xx.h"
#endif

#include
#include

void initTimer();

int main(void)
{
SystemInit();
initTimer();
NVIC_EnableIRQ(TIMER0_IRQn); /* Enable interrupt */
LPC_GPIO0->FIODIR=0x0;
LPC_GPIO1->FIODIR=0x1;

while(1)
{
int select=((LPC_GPIO0->FIOPIN)>>4)&0x07;

if(select == 0)
{
LPC_TIM0->TCR = 0x02; /* reset timer */
LPC_TIM0->MR0 = (12000000 /1000);
LPC_TIM0->TCR =(1<<0); /* enable timer */
}

else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 2000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 3000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 4000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 5000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 6000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 7000);
LPC_TIM0->TCR =(1<<0);
}
else if(select == 1)
{
LPC_TIM0->TCR = (1<<(1));
LPC_TIM0->MR0 = (12000000 / 8000);
LPC_TIM0->TCR =(1<<0);
}
}
return 0;
}
void TIMER0_IRQHandler (void)
{
LPC_TIM0->IR = 0xff; /* reset all interrrupts */
LPC_GPIO1->FIOPIN=~(LPC_GPIO1->FIOPIN);
}
void initTimer()
{
LPC_TIM0->TCR = 0x02; /* reset timer */
LPC_TIM0->PR = 0x00; /* set prescaler to zero */
LPC_TIM0->MR0 = 2400;
LPC_TIM0->IR = 0xff; /* reset all interrrupts */
LPC_TIM0->MCR = 3;
LPC_TIM0->TCR = 0x01; /* start timer */
}

.................................................................................................
Related Programs:
ARM General Purpose Input Output
ARM Timer Programming
ARM ADC Programming
ARM Square wave Generator
ARM Sine wave Generator