lpcware

LPC1114 Capture input please help

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by exgreyfox on Tue May 21 07:42:00 MST 2013
Hello,

Basically what I want to do is feed a square wave of varying frequency over time into the CT16B1_CAP0 input of my LPC1114 and have it be read. I have written some code to do this but I do not understand what I am seeing in the CR0 register when debugging. I am using a function generator to stick a 100Hz 3.3volt square wave into the CT16B1_CAP0 input and am monitoring the CR0 register as I am stepping through debugging. As soon as I remove the square wave from the CAP0 input I can see the CR0 register stopping to update. So I know its capturing my input, however, I cannot make any sense of what values are being written to the CR0 register. Here is my code:

In main.c

#include "driver_config.h"
#include "target_config.h"

#include "gpio.h"
#include "timer16.h"
#include "timer32.h"

int main (void) {

/* Init the system */
SystemInit();
/* Init the GPIO */
GPIOInit();
//initialize 16-bit COUNTER/TIMER 0
init_timer16(1, TIME_INTERVAL);
enable_timer16(1);

  while (1)
  {
     }

  return 0;
}


In timer16.c I have modified TIMER16_1_IRQHandler and init_timer16 to look like this:

void TIMER16_1_IRQHandler(void)
{
  if ( LPC_TMR16B1->IR & 0x1 )
  { 
  LPC_TMR16B1->IR = 1;        // clear interrupt flag
  timer16_1_counter++;
  }
  if ( LPC_TMR16B1->IR & (0x1<<4) )
  {
LPC_TMR16B1->TC = 0;//reset timer
timer16_1_capture = LPC_TMR16B1->CR0;//read period
LPC_TMR16B1->IR = 0x1<<4;/* clear interrupt flag */
  }
  return;
}


void init_timer16(uint8_t timer_num, uint16_t TimerInterval)
{
  if ( timer_num == 0 )
  {
    /* Some of the I/O pins need to be clearfully planned if
    you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
    LPC_IOCON->PIO0_2           &= ~0x07;/*  Timer0_16 I/O config */
    LPC_IOCON->PIO0_2           |= 0x02;/* Timer0_16 CAP0 */
    LPC_IOCON->PIO0_8           &= ~0x07;
    LPC_IOCON->PIO0_8           |= 0x02;/* Timer0_16 MAT0 */
    LPC_IOCON->PIO0_9           &= ~0x07;
    LPC_IOCON->PIO0_9           |= 0x02;/* Timer0_16 MAT1 */
#ifdef __JTAG_DISABLED
    LPC_IOCON->JTAG_TCK_PIO0_10 &= ~0x07;
    LPC_IOCON->JTAG_TCK_PIO0_10 |= 0x03;/* Timer0_16 MAT2 */
#endif

    timer16_0_counter = 0;
timer16_0_capture = 0;

    LPC_TMR16B0->PR  = MHZ_PRESCALE; /* set prescaler to get 1 M counts/sec */
    LPC_TMR16B0->MR0 = TIME_INTERVALmS * 10; /* Set up 10 mS interval */
#if TIMER_MATCH
LPC_TMR16B0->EMR &= ~(0xFF<<4);
LPC_TMR16B0->EMR |= ((0x3<<4)|(0x3<<6));
#else
/* Capture 0 on rising edge, interrupt enable. */
LPC_TMR16B0->CCR = (0x1<<0)|(0x1<<2);
#endif
    LPC_TMR16B0->MCR = 3;/* Interrupt and Reset on MR0 and MR1 */

    /* Enable the TIMER0 Interrupt */
    NVIC_EnableIRQ(TIMER_16_0_IRQn);
  }
  else if ( timer_num == 1 )
  {
    /* Some of the I/O pins need to be clearfully planned if
    you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
    LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
    LPC_IOCON->PIO1_8           &= ~0x2F;/*  Timer1_16 I/O config */
    LPC_IOCON->PIO1_8           |= 0x01;/* Timer1_16 CAP0 */
    LPC_IOCON->PIO1_9           &= ~0x07;
    LPC_IOCON->PIO1_9           |= 0x01;/* Timer1_16 MAT0 */
    LPC_IOCON->PIO1_10          &= ~0x07;
    LPC_IOCON->PIO1_10          |= 0x02;/* Timer1_16 MAT1 */

    timer16_1_counter = 0;
    timer16_1_capture = 0;
    LPC_TMR16B1->PR  = MHZ_PRESCALE; /* set prescaler to get 1 M counts/sec */
    LPC_TMR16B1->MR0 = TIME_INTERVALmS * 10; /* Set up 10 mS interval */
#if TIMER_MATCH
    LPC_TMR16B1->EMR &= ~(0xFF<<4);
    LPC_TMR16B1->EMR |= ((0x3<<4)|(0x3<<6));
#else
    LPC_TMR16B1->CTCR = 0;                           //use timer mode

    /* Capture 0 on rising edge, interrupt enable. */
    LPC_TMR16B1->CCR = (0x1<<0)|(0x1<<2);
#endif
    LPC_TMR16B1->MCR = 3;/* Interrupt and Reset on MR0 and MR1 */

    /* Enable the TIMER1 Interrupt */
    NVIC_EnableIRQ(TIMER_16_1_IRQn);
  }
  return;
}



I am not sure how the pre-scale value works so maybe that is what is confusing me. Here is a sample of the hex values I'm seeing in the CR0 register with a 100Hz 3.3 volt square wave on the CT16B1_CAP0 input.

0x184
0x2288
0xc7
0xe04
0x17bc


Any help is greatly appreciated

Outcomes