Actions

EmSys

TM4C123 GPTM with IO Pins Programs

From EdWiki

GPTM with IO Pins Programs

  1. /*
  2.  * GPTM Sample Programs
  3.  */
  4.  
  5. #include <stdint.h>
  6. #include <stdio.h>
  7. #include "inc/tm4c123gh6pm.h"
  8.  
  9. void Timer0Capture_Init(void);
  10. int Timer0A_periodCapture(void);
  11.  
  12. void Timer3A_CountCapture_Init(void);
  13. int Timer3A_CountCapture(void);
  14.  
  15. /* Functions to initialize Timer0A for edge-time capture mode to
  16.  * measure the period of a square-wave input signal
  17.  *
  18.  * square wave signal should be fed to PB6 pin.
  19.  * Make sure it is 3.3 to 5V peak-to-peak.
  20.  * Initialize Timer0A in edge-time mode to capture rising edges.
  21.  * The input pin of Timer0A is PB6.
  22.  *
  23. */
  24. void Timer0Capture_Init(void)
  25. {
  26.     SYSCTL_RCGCTIMER_R |= 1;        /* enable clock to Timer Block 0 */
  27.     SYSCTL_RCGC2_R |= 2;            /* enable clock to PORTB */
  28.  
  29.     GPIO_PORTB_DIR_R &= ~0x40;      /* make PB6 an input pin */
  30.     GPIO_PORTB_DEN_R |= 0x40;       /* make PB6 as digital pin */
  31.     GPIO_PORTB_AFSEL_R |= 0x40;     /* use PB6 alternate function */
  32.     GPIO_PORTB_PCTL_R &= ~0x0F000000; /* configure PB6 for T0CCP0 */
  33.     GPIO_PORTB_PCTL_R |= 0x07000000;
  34.  
  35.     TIMER0_CTL_R &= ~1;             /* disable timer0A during setup */
  36.     TIMER0_CFG_R = 4;               /* 16-bit timer mode */
  37.     TIMER0_TAMR_R = 0x17;           /* up-count, edge-time, capture mode */
  38.     TIMER0_CTL_R &= ~0x0C;          /* capture the rising edge */
  39.     TIMER0_CTL_R |= 1;              /* enable timer0A */
  40. }
  41.  
  42. /*
  43.  * This function captures two consecutive rising edges of a periodic signal
  44.  * from Timer Block 0 Timer A and returns the time difference
  45.  * (the period of the signal).
  46. */
  47. int Timer0A_periodCapture(void)
  48. {
  49.     int lastEdge, thisEdge;
  50.  
  51.     /* capture the first rising edge */
  52.     TIMER0_ICR_R = 4;               /* clear timer0A capture flag */
  53.     while((TIMER0_RIS_R & 4) == 0)
  54.         ;                           /* wait till captured */
  55.     lastEdge = TIMER0_TAR_R;        /* save the timestamp */
  56.  
  57.     /* capture the second rising edge */
  58.     TIMER0_ICR_R = 4;               /* clear timer0A capture flag */
  59.     while((TIMER0_RIS_R & 4) == 0)
  60.         ;                           /* wait till captured */
  61.     thisEdge = TIMER0_TAR_R;        /* save the timestamp */
  62.  
  63.     return (thisEdge - lastEdge) & 0x00FFFFFF; /* return the time difference */
  64. }
  65.  
  66. /*
  67.  * Initialize Timer3A to capture rising edge in edge-count mode.
  68.  * The input pin of Timer3A is PB2.
  69.  *
  70.  */
  71. void Timer3A_CountCapture_Init(void)
  72. {
  73.     SYSCTL_RCGCTIMER_R |= 8;     /* enable clock to Timer Block 3 */
  74.     SYSCTL_RCGC2_R |= 2;      	 /* enable clock to PORTB */
  75.  
  76.     GPIO_PORTB_DIR_R &= ~0x04;   /* make PB2 an input pin */
  77.     GPIO_PORTB_DEN_R |= 0x04;    /* make PB2 a digital pin */
  78.     GPIO_PORTB_AFSEL_R |= 0x04;  /* enable alternate function on PB2 */
  79.     GPIO_PORTB_PCTL_R &= ~0x00000F00;  /* configure PB2 as T3CCP0 pin */
  80.     GPIO_PORTB_PCTL_R |= 0x00000700;
  81.  
  82.     TIMER3_CTL_R &= ~1;          /* disable TIMER3A during setup */
  83.     TIMER3_CFG_R = 4;            /* configure as 16-bit timer mode */
  84.     TIMER3_TAMR_R = 0x13;        /* up-count, edge-count, capture mode */
  85.     TIMER3_TAMATCHR_R = 0xFFFF;  /* set the count limit */
  86.     TIMER3_TAPMR_R = 0xFF;         /* to 0xFFFFFF with prescaler */
  87.     TIMER3_CTL_R &= ~0xC;        /* capture the rising edge */
  88.     TIMER3_CTL_R |= 1;           /* enable timer3A */
  89. }
  90.  
  91. int Timer3A_CountCapture(void)
  92. {
  93.     return TIMER3_TAR_R;
  94. }
  95.  
  96. int main (void)
  97. {
  98.     int period;
  99.  
  100.     Timer0Capture_Init();
  101.  
  102.     while(1) {
  103.         period = Timer0A_periodCapture();
  104.         printf("%d\n\r", period);
  105.     }
  106. }