lpcware

LPC timer interrupts

Discussion created by lpcware Employee on Jun 15, 2016
Latest reply on Jun 15, 2016 by lpcware
Content originally posted in LPCWare by audriusmerfeldas on Sun Sep 06 12:50:52 MST 2015
I am very new at programming but using examples i am developing me own code. I am trying to set 16 bit timmer for increasing variable 'count' that I am using to display digit on 7 segment indicator. I am trying to get timer interrupt and at that moment increase variable 'count', but now it doesn't seem to work. Please find the code below:



#include "LPC11xx.h"                        /* LPC11xx definitions */
#include "timer16.h"
#include "clkconfig.h"
#include "gpio.h"

#include <cr_section_macros.h>
#include <NXP/crp.h>

__CRP const unsigned int CRP_WORD = CRP_NO_CRP ;

#define LED_ON 0
#define LED_OFF 1

int main (void) {

  GPIOInit();
  long count;

  // Set port for LED to output
  // GPIOSetDir( LED_PORT, LED_BIT, 1 );
  GPIOSetDir( 2, 2, 1 );
  GPIOSetDir( 0, 7, 1 );
  GPIOSetDir( 0, 6, 1 );
  GPIOSetDir( 1, 8, 1 );
  GPIOSetDir( 3, 1, 1 );
  GPIOSetDir( 3, 2, 1 );
  GPIOSetDir( 3, 0, 1 );
  // Set port for input
  // GPIOSetDir( LED_PORT, LED_BIT, 0 );
  GPIOSetDir( 1, 10, 0 );

  // Initial variable values
  count=0;


  //Set up 16 bit timer
  LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);                                  //enable clock signal to 16 bit timer0 (sec 3.5.14)
  LPC_TMR16B0->PR           = 0x0;                                      //set prescaler max value, not used here (sec 18.7.4)
  LPC_TMR16B0->MCR          = 0x10;                                     //set for reset on counter match (sec 18.7.6)
  LPC_TMR16B0->CCR          = 0;                                        //set to timer mode (sec 18.7.11)
  LPC_TMR16B0->MR1          = 0x64;                                     //set value for period (sec 18.7.7)
  LPC_TMR16B0->TCR          |= 0x3;                                     //enable and reset counter (sec 18.7.2)
  LPC_TMR16B0->TCR          &= ~(0x2);                                  //clear reset bit (sec 18.7.2)

  while (1)                                   // Loop forever
  {

   // 7 SEGMENT INDICATOR CODES FOR DIFFERENT NUMBERS
   //1
      if (count==1) {
      GPIOSetValue( 2, 2, LED_OFF );
      GPIOSetValue( 0, 7, LED_OFF );
      GPIOSetValue( 0, 6, LED_OFF );
      GPIOSetValue( 1, 8, LED_ON );
      GPIOSetValue( 3, 0, LED_OFF );
      GPIOSetValue( 3, 1, LED_ON );
      GPIOSetValue( 3, 2, LED_OFF );
      }
   //2
      if (count==2) {
      GPIOSetValue( 2, 2, LED_ON );
      GPIOSetValue( 0, 7, LED_OFF );
      GPIOSetValue( 0, 6, LED_ON );
      GPIOSetValue( 1, 8, LED_ON );
      GPIOSetValue( 3, 0, LED_ON );
      GPIOSetValue( 3, 1, LED_OFF );
      GPIOSetValue( 3, 2, LED_ON );
      }
   //3
      if (count==3) {
      GPIOSetValue( 2, 2, LED_ON );
      GPIOSetValue( 0, 7, LED_OFF );
      GPIOSetValue( 0, 6, LED_ON );
      GPIOSetValue( 1, 8, LED_ON );
       GPIOSetValue( 3, 0, LED_ON );
       GPIOSetValue( 3, 1, LED_ON );
       GPIOSetValue( 3, 2, LED_OFF );
      }
   //4
      if (count==4) {
      GPIOSetValue( 2, 2, LED_ON );
      GPIOSetValue( 0, 7, LED_ON );
        GPIOSetValue( 0, 6, LED_OFF );
        GPIOSetValue( 1, 8, LED_ON );
      GPIOSetValue( 3, 0, LED_OFF );
       GPIOSetValue( 3, 1, LED_ON );
       GPIOSetValue( 3, 2, LED_OFF );
      }
   //5
      if (count==5) {
      GPIOSetValue( 2, 2, LED_ON );
       GPIOSetValue( 0, 7, LED_ON );
       GPIOSetValue( 0, 6, LED_ON );
       GPIOSetValue( 1, 8, LED_OFF );
       GPIOSetValue( 3, 0, LED_ON );
        GPIOSetValue( 3, 1, LED_ON );
       GPIOSetValue( 3, 2, LED_OFF );
      }
   //6
      if (count==6) {
      GPIOSetValue( 2, 2, LED_ON );
       GPIOSetValue( 0, 7, LED_ON );
      GPIOSetValue( 0, 6, LED_ON );
       GPIOSetValue( 1, 8, LED_OFF );
       GPIOSetValue( 3, 0, LED_ON );
      GPIOSetValue( 3, 1, LED_ON );
       GPIOSetValue( 3, 2, LED_ON );
      }
   //7
      if (count==7) {
      GPIOSetValue( 2, 2, LED_OFF );
       GPIOSetValue( 0, 7, LED_OFF );
      GPIOSetValue( 0, 6, LED_ON );
       GPIOSetValue( 1, 8, LED_ON );
        GPIOSetValue( 3, 0, LED_OFF );
        GPIOSetValue( 3, 1, LED_ON );
        GPIOSetValue( 3, 2, LED_OFF );
      }
   //8
      if (count==8) {
      GPIOSetValue( 2, 2, LED_ON );
       GPIOSetValue( 0, 7, LED_ON );
       GPIOSetValue( 0, 6, LED_ON );
       GPIOSetValue( 1, 8, LED_ON );
      GPIOSetValue( 3, 0, LED_ON );
       GPIOSetValue( 3, 1, LED_ON );
        GPIOSetValue( 3, 2, LED_ON );
      }
   //9
      if (count==9) {
      GPIOSetValue( 2, 2, LED_ON );
        GPIOSetValue( 0, 7, LED_ON );
       GPIOSetValue( 0, 6, LED_ON );
        GPIOSetValue( 1, 8, LED_ON );
      GPIOSetValue( 3, 0, LED_ON );
        GPIOSetValue( 3, 1, LED_ON );
        GPIOSetValue( 3, 2, LED_OFF );
      }
      if (count==10) {count=0;}
   //0
      if (count==0) {
      GPIOSetValue( 2, 2, LED_OFF );
       GPIOSetValue( 0, 7, LED_ON );
       GPIOSetValue( 0, 6, LED_ON );
       GPIOSetValue( 1, 8, LED_ON );
       GPIOSetValue( 3, 0, LED_ON );
       GPIOSetValue( 3, 1, LED_ON );
       GPIOSetValue( 3, 2, LED_ON );
          }

  }

void TIMER16_0_IRQHandler(void)
{
if(LPC_TMR16B0->IR & (1<<4))            //capture interrupt?
{
  LPC_TMR16B0->TC = 0;                    //reset timer
  count++;
  LPC_TMR16B0->IR |= (1<<4);            //reset capture interrupt
}                                         //end capture interrupt
}

}

Outcomes