Stellaris Launchpad,lm4f120h5qr,计时器无法正常工作,可以倒数但不能计数

时间:2012-12-06 12:15:40

标签: timer arm

我决定不使用提供的stellarisware功能,并试图弄清楚如何让我的Timer工作。此时,我所要做的就是将Timer0 A计数到指定值(存储在TIMER0_TAILR_R中)。

现在的程序将从指定值开始倒计时(通过使TIMER0_TAMR_R | = 0x1),但在计数模式下,无论TIMER0_TAILR_R中存储的值如何,指定的LED都以恒定速率(缓慢)闪烁。

#include "lm4f120h5qr.h"

int main(void){
    SYSCTL_RCGCGPIO_R |= 0x20; // clock gate to port F
    GPIO_PORTF_DIR_R |= 0x7 << 1; // Set Led's as outputs
    GPIO_PORTF_DEN_R |= 0x7 << 1;// for LED's

    GPIO_PORTF_DEN_R |= 0x1; // For timer
    GPIO_PORTF_PCTL_R |= 0x7; //select T0CCP0
    SYSCTL_RCGC1_R |= 0x1 << 16; 
    SYSCTL_RCGCTIMER_R |= 0x1;

    TIMER0_CTL_R = 0;
    TIMER0_CFG_R  = 0;
    TIMER0_CFG_R  |= 0x4;
    TIMER0_TAMR_R |= 0x11; // set mode
    TIMER0_TAPR_R |= 0xF; // prescale
    TIMER0_TAILR_R = 0x1F1; // load value

    while(1){
        GPIO_PORTF_DATA_R |= 0x1 << 2;
        TIMER0_CTL_R |= 0x1;
        while(TIMER0_CTL_R & 0x1);
        GPIO_PORTF_DATA_R &= ~(0x1 << 2);
        TIMER0_CTL_R |= 0x1;
        while(TIMER0_CTL_R & 0x1);

    }
}

据我所知,我已经正确初始化了Timer并正确地做了一切(显然这是不正确的)。非常感谢任何关于我出错的地方的帮助,因为我已经绞尽脑汁已经有一段时间了,无法找到解决方案。 干杯, 路加。

1 个答案:

答案 0 :(得分:0)

根据数据表11.3(p660),具有向上和向下计数的代码:

   #include "lm4f120h5qr.h"

    int main(void) {
        SYSCTL_RCGCGPIO_R |= 0x20; // clock gate to port F
        GPIO_PORTF_DIR_R |= 0x7 << 1; // Set Led's as outputs
        GPIO_PORTF_DEN_R |= 0x7 << 1; // for LED's

        GPIO_PORTF_DEN_R |= 0x1; // For timer
        GPIO_PORTF_PCTL_R |= 0x7; //select T0CCP0
        SYSCTL_RCGC1_R |= 0x1 << 16;
        SYSCTL_RCGCTIMER_R |= 0x1;

        TIMER0_CTL_R = 0;
        TIMER0_CFG_R = 0;
        TIMER0_CFG_R |= 0x4;

        // up
        TIMER0_TAMR_R = 0x11; // set mode
        TIMER0_TAPR_R = 0xF00; // prescale

        // down
        //TIMER0_TAMR_R = 0x1; // set mode
        //TIMER0_TAPR_R = 0x0F; // prescale

        TIMER0_TAILR_R = 0x1F1; // load value

        while (1) {
            GPIO_PORTF_DATA_R |= 0x1 << 2;
            TIMER0_CTL_R |= 0x1;
            while (TIMER0_CTL_R & 0x1)
                ;
            GPIO_PORTF_DATA_R &= ~(0x1 << 2);
            TIMER0_CTL_R |= 0x1;
            while (TIMER0_CTL_R & 0x1)
                ;

        }
    }