Alexan E / blinky

Description: blinky example from NXP code bundle for LPC11Uxx. No mbed library used

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers timer16.c Source File

timer16.c

00001 /****************************************************************************
00002  *   $Id:: timer16.c 9190 2012-02-16 20:59:45Z nxp41306                     $
00003  *   Project: NXP LPC11Uxx 16-bit timer example
00004  *
00005  *   Description:
00006  *     This file contains 16-bit timer code example which include timer 
00007  *     initialization, timer interrupt handler, and related APIs for 
00008  *     timer setup.
00009 *
00010 ****************************************************************************
00011 * Software that is described herein is for illustrative purposes only
00012 * which provides customers with programming information regarding the
00013 * products. This software is supplied "AS IS" without any warranties.
00014 * NXP Semiconductors assumes no responsibility or liability for the
00015 * use of the software, conveys no license or title under any patent,
00016 * copyright, or mask work right to the product. NXP Semiconductors
00017 * reserves the right to make changes in the software without
00018 * notification. NXP Semiconductors also make no representation or
00019 * warranty that such application will be suitable for the specified
00020 * use without further testing or modification.
00021 
00022 * Permission to use, copy, modify, and distribute this software and its 
00023 * documentation is hereby granted, under NXP Semiconductors' 
00024 * relevant copyright in the software, without fee, provided that it 
00025 * is used in conjunction with NXP Semiconductors microcontrollers.  This 
00026 * copyright, permission, and disclaimer notice must appear in all copies of 
00027 * this code.
00028 
00029 ****************************************************************************/
00030 
00031 #include "LPC11Uxx.h"
00032 #include "timer16.h"
00033 #include "nmi.h"
00034 
00035 volatile uint32_t timer16_0_counter[2] = {0,0};
00036 volatile uint32_t timer16_1_counter[2] = {0,0};
00037 volatile uint32_t timer16_0_capture[2] = {0,0};
00038 volatile uint32_t timer16_1_capture[2] = {0,0};
00039 volatile uint32_t timer16_0_period = 0;
00040 volatile uint32_t timer16_1_period = 0;
00041 
00042 /*****************************************************************************
00043 ** Function name:        delayMs
00044 **
00045 ** Descriptions:        Start the timer delay in milo seconds
00046 **                        until elapsed
00047 **
00048 ** parameters:            timer number, Delay value in milo second             
00049 **                         
00050 ** Returned value:        None
00051 ** 
00052 *****************************************************************************/
00053 void delayMs(uint8_t timer_num, uint32_t delayInMs)
00054 {
00055   if (timer_num == 0)
00056   {
00057     /*
00058     * setup timer #0 for delay
00059     */
00060     LPC_CT16B0->TCR = 0x02;        /* reset timer */
00061     LPC_CT16B0->PR  = 0x00;        /* set prescaler to zero */
00062     LPC_CT16B0->MR0 = delayInMs * (SystemCoreClock / 1000);
00063     LPC_CT16B0->IR  = 0xff;        /* reset all interrrupts */
00064     LPC_CT16B0->MCR = 0x04;        /* stop timer on match */
00065     LPC_CT16B0->TCR = 0x01;        /* start timer */
00066     /* wait until delay time has elapsed */
00067     while (LPC_CT16B0->TCR & 0x01);
00068   }
00069   else if (timer_num == 1)
00070   {
00071     /*
00072     * setup timer #1 for delay
00073     */
00074     LPC_CT16B1->TCR = 0x02;        /* reset timer */
00075     LPC_CT16B1->PR  = 0x00;        /* set prescaler to zero */
00076     LPC_CT16B1->MR0 = delayInMs * (SystemCoreClock / 1000);
00077     LPC_CT16B1->IR  = 0xff;        /* reset all interrrupts */
00078     LPC_CT16B1->MCR = 0x04;        /* stop timer on match */
00079     LPC_CT16B1->TCR = 0x01;        /* start timer */
00080     /* wait until delay time has elapsed */
00081     while (LPC_CT16B1->TCR & 0x01);
00082   }
00083   return;
00084 }
00085 
00086 /******************************************************************************
00087 ** Function name:        TIMER_0_IRQHandler
00088 **
00089 ** Descriptions:        Timer/CounterX and CaptureX interrupt handler
00090 **
00091 ** parameters:            None
00092 ** Returned value:        None
00093 ** 
00094 ******************************************************************************/
00095 extern "C" void TIMER16_0_IRQHandler(void)
00096 {
00097   if ( LPC_CT16B0->IR & (0x1<<0) )
00098   {
00099     LPC_CT16B0->IR = 0x1<<0;            /* clear interrupt flag */
00100     timer16_0_counter[0]++;
00101   }
00102   if ( LPC_CT16B0->IR & (0x1<<1) )
00103   {
00104     LPC_CT16B0->IR = 0x1<<1;            /* clear interrupt flag */
00105     timer16_0_counter[1]++;
00106   }
00107   if ( LPC_CT16B0->IR & (0x1<<4) )
00108   {
00109     LPC_CT16B0->IR = 0x1<<4;        /* clear interrupt flag */
00110     timer16_0_capture[0]++;
00111   }
00112   if ( LPC_CT16B0->IR & (0x1<<5) )
00113   {
00114     LPC_CT16B0->IR = 0x1<<5;        /* clear interrupt flag */
00115     timer16_0_capture[1]++;
00116   }
00117   return;
00118 }
00119 
00120 /******************************************************************************
00121 ** Function name:        TIMER_1_IRQHandler
00122 **
00123 ** Descriptions:        Timer/CounterX and CaptureX interrupt handler
00124 **
00125 ** parameters:            None
00126 ** Returned value:        None
00127 ** 
00128 ******************************************************************************/
00129 extern "C" void TIMER16_1_IRQHandler(void)
00130 {
00131   if ( LPC_CT16B1->IR & (0x1<<0) )
00132   {  
00133     LPC_CT16B1->IR = 0x1<<0;            /* clear interrupt flag */
00134     timer16_1_counter[0]++;
00135   }
00136   if ( LPC_CT16B1->IR & (0x1<<1) )
00137   {  
00138     LPC_CT16B1->IR = 0x1<<1;            /* clear interrupt flag */
00139     timer16_1_counter[1]++;
00140   }
00141   if ( LPC_CT16B1->IR & (0x1<<4) )
00142   {
00143     LPC_CT16B1->IR = 0x1<<4;        /* clear interrupt flag */
00144     timer16_1_capture[0]++;
00145   }
00146   if ( LPC_CT16B1->IR & (0x1<<5) )
00147   {
00148     LPC_CT16B1->IR = 0x1<<5;        /* clear interrupt flag */
00149     timer16_1_capture[1]++;
00150   }
00151   return;
00152 }
00153 
00154 /******************************************************************************
00155 ** Function name:        enable_timer
00156 **
00157 ** Descriptions:        Enable timer
00158 **
00159 ** parameters:            timer number: 0 or 1
00160 ** Returned value:        None
00161 ** 
00162 ******************************************************************************/
00163 void enable_timer16(uint8_t timer_num)
00164 {
00165   if ( timer_num == 0 )
00166   {
00167     LPC_CT16B0->TCR = 1;
00168   }
00169   else
00170   {
00171     LPC_CT16B1->TCR = 1;
00172   }
00173   return;
00174 }
00175 
00176 /******************************************************************************
00177 ** Function name:        disable_timer
00178 **
00179 ** Descriptions:        Disable timer
00180 **
00181 ** parameters:            timer number: 0 or 1
00182 ** Returned value:        None
00183 ** 
00184 ******************************************************************************/
00185 void disable_timer16(uint8_t timer_num)
00186 {
00187   if ( timer_num == 0 )
00188   {
00189     LPC_CT16B0->TCR = 0;
00190   }
00191   else
00192   {
00193     LPC_CT16B1->TCR = 0;
00194   }
00195   return;
00196 }
00197 
00198 /******************************************************************************
00199 ** Function name:        reset_timer
00200 **
00201 ** Descriptions:        Reset timer
00202 **
00203 ** parameters:            timer number: 0 or 1
00204 ** Returned value:        None
00205 ** 
00206 ******************************************************************************/
00207 void reset_timer16(uint8_t timer_num)
00208 {
00209   uint32_t regVal;
00210 
00211   if ( timer_num == 0 )
00212   {
00213     regVal = LPC_CT16B0->TCR;
00214     regVal |= 0x02;
00215     LPC_CT16B0->TCR = regVal;
00216   }
00217   else
00218   {
00219     regVal = LPC_CT16B1->TCR;
00220     regVal |= 0x02;
00221     LPC_CT16B1->TCR = regVal;
00222   }
00223   return;
00224 }
00225 
00226 /******************************************************************************
00227 ** Function name:        Set_timer_capture
00228 **
00229 ** Descriptions:        set timer capture based on LOC number.
00230 **
00231 ** parameters:            timer number and location number
00232 ** Returned value:        None
00233 ** 
00234 ******************************************************************************/
00235 void set_timer16_capture(uint8_t timer_num, uint8_t location )
00236 {
00237   if ( timer_num == 0 )
00238   {
00239     /*  Timer0_16 I/O config */
00240     if ( location == 0 )
00241     {
00242       LPC_IOCON->PIO1_16           &= ~0x07;
00243       LPC_IOCON->PIO1_16           |= 0x02;        /* Timer0_16 CAP0 */
00244 //      LPC_IOCON->PIO1_17           &= ~0x07;
00245 //      LPC_IOCON->PIO1_17           |= 0x01;        /* Timer0_16 CAP1 */
00246     }
00247     else if ( location == 1 )
00248     {
00249       LPC_IOCON->PIO0_2            &= ~0x07;
00250       LPC_IOCON->PIO0_2            |= 0x02;        /* Timer0_16 CAP0 */
00251     }
00252     else
00253     {
00254       while ( 1 );                /* Fatal location number error */
00255     }
00256   }
00257   else
00258   {
00259     /*  Timer1_16 I/O config */
00260     if ( location == 0 )
00261     {
00262       LPC_IOCON->PIO0_20           &= ~0x07;    /*  Timer1_16 I/O config */
00263       LPC_IOCON->PIO0_20           |= 0x01;        /* Timer1_16 CAP0 */
00264 //      LPC_IOCON->PIO1_18           &= ~0x07;
00265 //      LPC_IOCON->PIO1_18           |= 0x01;        /* Timer1_16 CAP1 */
00266     }
00267     else
00268     {
00269       while ( 1 );                /* Fatal location number error */
00270     }
00271   }    
00272   return;
00273 }
00274 
00275 /******************************************************************************
00276 ** Function name:        Set_timer_match
00277 **
00278 ** Descriptions:        set timer match based on LOC number.
00279 **
00280 ** parameters:            timer number, match enable, and location number 
00281 ** Returned value:        None
00282 ** 
00283 ******************************************************************************/
00284 void set_timer16_match(uint8_t timer_num, uint8_t match_enable, uint8_t location)
00285 {
00286   if ( timer_num == 0 )
00287   {
00288     if ( match_enable & 0x01 )
00289     {
00290       /*  Timer0_16 I/O config */
00291       if ( location == 0 )
00292       {
00293         LPC_IOCON->PIO0_8           &= ~0x07;    
00294         LPC_IOCON->PIO0_8           |= 0x02;        /* Timer0_16 MAT0 */
00295       }
00296       else if ( location == 1 )
00297       {
00298         LPC_IOCON->PIO1_13           &= ~0x07;
00299         LPC_IOCON->PIO1_13           |= 0x02;        /* Timer0_16 MAT0 */
00300       }
00301     }
00302     if ( match_enable & 0x02 )
00303     {
00304       /*  Timer0_16 I/O config */
00305       if ( location == 0 )
00306       {
00307         LPC_IOCON->PIO0_9           &= ~0x07;
00308         LPC_IOCON->PIO0_9           |= 0x02;        /* Timer0_16 MAT1 */
00309       }
00310       else if ( location == 1 )
00311       {
00312         LPC_IOCON->PIO1_14           &= ~0x07;
00313         LPC_IOCON->PIO1_14           |= 0x02;        /* Timer0_16 MAT1 */
00314       }
00315     }
00316     if ( match_enable & 0x04 )
00317     {
00318       /*  Timer0_16 I/O config */
00319       if ( location == 0 )
00320       {
00321 #ifdef __SWD_DISABLED
00322         LPC_IOCON->SWCLK_PIO0_10    &= ~0x07;
00323         LPC_IOCON->SWCLK_PIO0_10    |= 0x03;        /* Timer0_16 MAT2 */
00324 #endif    
00325       }
00326       else if ( location == 1 )
00327       {
00328         LPC_IOCON->PIO1_15           &= ~0x07;
00329         LPC_IOCON->PIO1_15           |= 0x02;        /* Timer0_16 MAT0 */
00330       }
00331     }
00332   }
00333   else if ( timer_num == 1 )
00334   {
00335     if ( match_enable & 0x01 )
00336     {
00337       /*  Timer1_16 I/O config */
00338       if ( location == 0 )
00339       {
00340         LPC_IOCON->PIO0_21           &= ~0x07;
00341         LPC_IOCON->PIO0_21           |= 0x01;        /* Timer1_16 MAT0 */
00342       }
00343     }
00344     if ( match_enable & 0x02 )
00345     {
00346       /*  Timer1_16 I/O config */
00347       if ( location == 0 )
00348       {
00349         LPC_IOCON->PIO0_22           &= ~0x07;
00350         LPC_IOCON->PIO0_22           |= 0x02;        /* Timer1_16 MAT1 */
00351       }
00352       else if ( location == 1 )
00353       {
00354         LPC_IOCON->PIO1_23           &= ~0x07;
00355         LPC_IOCON->PIO1_23           |= 0x01;        /* Timer1_16 MAT0 */
00356       }
00357     }
00358   }
00359   return;        
00360 }
00361 
00362 /******************************************************************************
00363 ** Function name:        init_timer
00364 **
00365 ** Descriptions:        Initialize timer, set timer interval, reset timer,
00366 **                        install timer interrupt handler
00367 **
00368 ** parameters:            timer number and timer interval
00369 ** Returned value:        None
00370 ** 
00371 ******************************************************************************/
00372 void init_timer16(uint8_t timer_num, uint32_t TimerInterval) 
00373 {
00374   if ( timer_num == 0 )
00375   {
00376     /* Some of the I/O pins need to be clearfully planned if
00377     you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
00378     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
00379 
00380     LPC_CT16B0->MR0 = TimerInterval;
00381     LPC_CT16B0->MR1 = TimerInterval;
00382 #if TIMER_MATCH
00383     timer16_0_counter[0] = 0;
00384     timer16_0_counter[1] = 0;
00385     set_timer16_match(timer_num, 0x07, 0);
00386     LPC_CT16B0->EMR &= ~(0xFF<<4);
00387     LPC_CT16B0->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8));
00388 #else
00389     timer16_0_capture[0] = 0;
00390     timer16_0_capture[1] = 0;
00391     set_timer16_capture(timer_num, 0);
00392     /* Capture 0 and 1 on rising edge, interrupt enable. */
00393     LPC_CT16B0->CCR = (0x5<<0)|(0x5<<3);
00394 #endif
00395     LPC_CT16B0->MCR = (0x3<<0)|(0x3<<3);  /* Interrupt and Reset on MR0 and MR1 */
00396         
00397     /* Enable the TIMER0 Interrupt */
00398 #if NMI_ENABLED
00399     NVIC_DisableIRQ(TIMER_16_0_IRQn);
00400     NMI_Init( TIMER_16_0_IRQn );
00401 #else
00402     NVIC_EnableIRQ(TIMER_16_0_IRQn);
00403 #endif
00404   }
00405   else if ( timer_num == 1 )
00406   {
00407     /* Some of the I/O pins need to be clearfully planned if
00408     you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
00409     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
00410     LPC_CT16B1->MR0 = TimerInterval;
00411     LPC_CT16B1->MR1 = TimerInterval;
00412 #if TIMER_MATCH
00413     timer16_1_counter[0] = 0;
00414     timer16_1_counter[1] = 0;
00415     set_timer16_match(timer_num, 0x07, 0);
00416     LPC_CT16B1->EMR &= ~(0xFF<<4);
00417     LPC_CT16B1->EMR |= ((0x3<<4)|(0x3<<6)|(0x3<<8));
00418 #else
00419     timer16_1_capture[0] = 0;
00420     timer16_1_capture[1] = 0;
00421     set_timer16_capture(timer_num, 0);
00422     /* Capture 0 and 1 on rising edge, interrupt enable. */
00423     LPC_CT16B1->CCR = (0x5<<0)|(0x5<<3);
00424 #endif
00425     LPC_CT16B1->MCR = (0x3<<0)|(0x3<<3);  /* Interrupt and Reset on MR0 and MR1 */
00426 
00427     /* Enable the TIMER1 Interrupt */
00428 #if NMI_ENABLED
00429     NVIC_DisableIRQ(TIMER_16_1_IRQn);
00430     NMI_Init( TIMER_16_1_IRQn );
00431 #else
00432     NVIC_EnableIRQ(TIMER_16_1_IRQn);
00433 #endif
00434   }
00435   return;
00436 }
00437 
00438 /******************************************************************************
00439 ** Function name:        init_timer16PWM
00440 **
00441 ** Descriptions:        Initialize timer as PWM
00442 **
00443 ** parameters:            timer number, period and match enable:
00444 **                        match_enable[0] = PWM for MAT0 
00445 **                        match_enable[1] = PWM for MAT1
00446 **                        match_enable[2] = PWM for MAT2
00447 **            
00448 ** Returned value:    None
00449 ** 
00450 ******************************************************************************/
00451 void init_timer16PWM(uint8_t timer_num, uint32_t period, uint8_t match_enable, uint8_t cap_enabled)
00452 {    
00453   disable_timer16(timer_num);
00454 
00455   if (timer_num == 1)
00456   {
00457     /* Some of the I/O pins need to be clearfully planned if
00458     you use below module because JTAG and TIMER CAP/MAT pins are muxed. */
00459     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<8);
00460         
00461     /* Setup the external match register */
00462     LPC_CT16B1->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(2<<EMC0)|(1<<3)|(match_enable);
00463         
00464     /* Setup the outputs */
00465     /* If match0 is enabled, set the output */
00466     set_timer16_match(timer_num, match_enable, 0 );
00467         
00468     /* Enable the selected PWMs and enable Match3 */
00469     LPC_CT16B1->PWMC = (1<<3)|(match_enable);
00470         
00471     /* Setup the match registers */
00472     /* set the period value to a global variable */
00473     timer16_1_period     = period;
00474     LPC_CT16B1->MR3     = timer16_1_period;
00475     LPC_CT16B1->MR0    = timer16_1_period/2;
00476     LPC_CT16B1->MR1    = timer16_1_period/2;
00477     LPC_CT16B1->MR2    = timer16_1_period/2;
00478         
00479     /* Set match control register */
00480     LPC_CT16B1->MCR = 1<<10;// | 1<<9;                /* Reset on MR3 */
00481         
00482     if (cap_enabled)
00483     {
00484       /* Use location 0 for test. */
00485       set_timer16_capture( timer_num, 0 );
00486       LPC_CT16B1->IR = 0xF;                            /* clear interrupt flag */
00487             
00488       /* Set the capture control register */
00489       LPC_CT16B1->CCR = 7;
00490             
00491     }
00492     /* Enable the TIMER1 Interrupt */
00493     NVIC_EnableIRQ(TIMER_16_1_IRQn);
00494   }
00495   else
00496   {
00497     LPC_SYSCON->SYSAHBCLKCTRL |= (1<<7);
00498         
00499     /* Setup the external match register */
00500     LPC_CT16B0->EMR = (1<<EMC3)|(1<<EMC2)|(1<<EMC1)|(1<<EMC0)|(1<<3)|(match_enable);
00501         
00502     /* Setup the outputs */
00503     /* If match0 is enabled, set the output */
00504     set_timer16_match(timer_num, match_enable, 0 );
00505             
00506     /* Enable the selected PWMs and enable Match3 */
00507     LPC_CT16B0->PWMC = (1<<3)|(match_enable);
00508         
00509     /* Setup the match registers */
00510     /* set the period value to a global variable */
00511     timer16_0_period = period;
00512     LPC_CT16B0->MR3 = timer16_0_period;
00513     LPC_CT16B0->MR0 = timer16_0_period/2;
00514     LPC_CT16B0->MR1 = timer16_0_period/2;
00515     LPC_CT16B0->MR2 = timer16_0_period/2;
00516         
00517     /* Set the match control register */
00518     LPC_CT16B0->MCR = 1<<10;                /* Reset on MR3 */
00519         
00520     /* Enable the TIMER1 Interrupt */
00521     NVIC_EnableIRQ(TIMER_16_0_IRQn);
00522   }
00523 }
00524 
00525 /******************************************************************************
00526 ** Function name:        pwm16_setMatch
00527 **
00528 ** Descriptions:        Set the pwm16 match values
00529 **
00530 ** parameters:            timer number, match numner and the value
00531 **
00532 ** Returned value:        None
00533 ** 
00534 ******************************************************************************/
00535 void setMatch_timer16PWM (uint8_t timer_num, uint8_t match_nr, uint32_t value)
00536 {
00537   if (timer_num)
00538   {
00539     switch (match_nr)
00540     {
00541       case 0:
00542         LPC_CT16B1->MR0 = value;
00543       break;
00544       case 1: 
00545         LPC_CT16B1->MR1 = value;
00546       break;
00547       case 2:
00548         LPC_CT16B1->MR2 = value;
00549       break;
00550       case 3: 
00551         LPC_CT16B1->MR3 = value;
00552       break;
00553       default:
00554         break;
00555     }    
00556   }
00557   else 
00558   {
00559     switch (match_nr)
00560     {
00561       case 0:
00562         LPC_CT16B0->MR0 = value;
00563       break;
00564       case 1: 
00565         LPC_CT16B0->MR1 = value;
00566       break;
00567       case 2:
00568         LPC_CT16B0->MR2 = value;
00569       break;
00570       case 3: 
00571         LPC_CT16B0->MR3 = value;
00572       break;
00573       default:
00574       break;
00575     }    
00576   }
00577 }
00578 
00579 /******************************************************************************
00580 **                            End Of File
00581 ******************************************************************************/