gp timer pwm and width capture.c

来自「ADI 公司的DSP ADSP21262 EZ-KIT LITE开发板的全部源代」· C语言 代码 · 共 84 行

C
84
字号
#include <signal.h>

/* Register Definitions */
#define TMSTAT  (0x1400)   /* GP Timer 0 Status register  */
#define TM0CTL  (0x1401)   /* GP Timer 0 Control register */
#define TM0CNT  (0x1402)   /* GP Timer 0 Count register   */
#define TM0PRD  (0x1403)   /* GP Timer 0 Period register  */
#define TM0W    (0x1404)   /* GP Timer 0 Width register   */
#define TM1CTL  (0x1409)   /* GP Timer 1 Control register */
#define TM1CNT  (0x140A)   /* GP Timer 1 Count register   */
#define TM1PRD  (0x140B)   /* GP Timer 1 Period register  */
#define TM1W    (0x140C)   /* GP Timer 1 Width register   */
#define SRU_PIN0         (0x2460)
#define SRU_PBEN0        (0x2478)
#define SRU_EXT_MISCB    (0x2471)

/* Bit Definitions */
#define TIMODEPWM 0x00000001
#define TIMODEW   0x00000002
#define PULSE     0x00000004
#define PRDCNT    0x00000008
#define IRQEN     0x00000010
#define TIM0EN    0x00000100
#define TIM1EN    0x00000400
#define GPTMR1I   0x00000010
#define TIM1IRQ   0x00000002

/* SRU definitions */
#define TIMER0_Od       0x2C
#define TIMER0_Oe       0x14
#define PBEN_HIGH_Of    0x01

/* Bit positions */
#define TIMER1_I        5

// period and width will hold the values programmed into TM0PRD and TM0W
// after the timer interrupt is serviced.
int period, width;
int interrupted=0;

void timer_interrupt(int sig_int)
{
    period = * (volatile int *) TM1PRD;
    width = * (volatile int *) TM1W;
}

/* Main code section */
void main()
{

     interrupt(SIG_GPTMR1,timer_interrupt);  /* Set up and enable Timer 0 in PWM Out mode*/
    /* Route Timer 0 Output to DAI Pin 1 via SRU */
    * (volatile int *) SRU_PIN0 = TIMER0_Od;

    /* Enable DAI Pin 1 as an output */
    * (volatile int *) SRU_PBEN0 = PBEN_HIGH_Of;

    * (volatile int *) TM0CTL = TIMODEPWM|     /* PWM Out Mode */
                                PULSE|         /* Positive edge is active */
                                PRDCNT;        /* Count to end of period */

    * (volatile int *) TM0PRD = 0xFF;       /* Timer 0 period = 255 */

    * (volatile int *) TM0W = 0x3F;         /* Timer 0 Pulse width = 15 */

    * (volatile int *) TMSTAT = TIM0EN;            /* enable timer 0 */

    /* --------------End of Timer 0 Setup-------------------- */

    /* Set up and enable Timer 1 in Width Capture mode */
    /* Use the output of Timer 0 as the input to Timer 1 */
    /* Route Timer 0 Output to Timer 1 Input via SRU */
    * (volatile int *) SRU_EXT_MISCB = (TIMER0_Oe<<TIMER1_I);

    * (volatile int *) TM1CTL = TIMODEW|       /* PWM Out Mode */
                                PULSE|         /* Positive edge is active */
                                IRQEN|         /* Enable Timer 1 Interrupt */
                                PRDCNT;        /* Count to end of period */

    * (volatile int *) TMSTAT = TIM1EN;            /* enable timer 1 */
    
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?