1

我决定不使用提供的 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 并且做的一切都正确(显然这是不正确的)。非常感谢任何关于我哪里出错的帮助,因为我已经为此绞尽脑汁了很长一段时间并且找不到解决方案。干杯,卢克。

4

1 回答 1

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)
                ;

        }
    }
于 2012-12-31T14:10:12.020 回答