在Tiva C中按下开关时闪烁LED

时间:2019-10-05 18:53:02

标签: c arm embedded microcontroller

我试图使PF0和PF4指示灯在按下开关时闪烁。但这根本不会打开任何指示灯。

有人告诉我我需要使用两个端口,我不明白为什么,因为只能使用一个端口(在本例中为端口D),但是建议我也使用端口K( ?)。
董事会是Tiva C TM4c1294nctpd

#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_memmap.h"
#include "driverlib/debug.h"
#include "driverlib/gpio.h"
#include "driverlib/sysctl.h"
#include <inc/tm4c1294ncpdt.h>

uint32_t SW1,SW2; //

int main(void) {

    while(1){
    SYSCTL_RCGCGPIO_R=0X1100; // Enable port D
    GPIO_PORTD_DIR_R=0X03;      //enable the GPIO pin PN0, 
    GPIO_PORTD_DEN_R=0X03;  
    GPIO_PORTK_AHB_DIR_R=0;
    GPIO_PORTK_AHB_DEN_R=0X03;
    GPIO_PORTK_AHB_PUR_R=0X01;

    SW1 = GPIO_PORTD_DATA_R&0x10;     // read PF4 into SW1
    SW2 = GPIO_PORTD_DATA_R&0x01;     // read PF0 into SW2
    if (!SW1 && !SW2) { // both pressed
        GPIO_PORTD_DATA_R = 0x04;
    } else if (!SW1) { // SW1 pressed
        GPIO_PORTD_DATA_R = 0x02;
    } else if (!SW2) { // SW2 pressed
        GPIO_PORTD_DATA_R = 0x08;
    } else { // neither
        GPIO_PORTD_DATA_R = 0x00;
    }
  }

}

1 个答案:

答案 0 :(得分:1)

  • 您仅启用了D0和D1,但似乎正在使用D0,D1,D2,D3和D4。
  • 您已将D0和D1设置为输出,但似乎正在使用D1,D2,D3作为输出。
  • 您已将D0设置为输出,但尝试将其读取为输入。
  • 如果不使用PORTK,则完全不相关。
  • RCGCGPIO为您根本没有使用的PORTN和PORTJ启用时钟。

我不熟悉该器件,只是简要阅读了数据手册,但是如果输入/输出代码本身正确,则PORTD时钟,方向和数字使能配置应如下所述。

SYSCTL_RCGCGPIO_R = 0x0008; // Enable port D clock
GPIO_PORTD_DIR_R = 0x0E;    // D4, D0 input, D1 to D3 output.
GPIO_PORTD_DEN_R = 0x1F;    // Enable D0 to D4  

这些初始化设置仅需要一次-循环之前,而不是在内部

int main(void) 
{
    SYSCTL_RCGCGPIO_R = 0x0008; // Enable port D
    GPIO_PORTD_DIR_R = 0x0E;    // D4, D0 input, D1 to D3 output.
    GPIO_PORTD_DEN_R = 0x1F;    // Enable D0 to D4  

    for(;;)
    {
        uint32_t SW1 = GPIO_PORTD_DATA_R & 0x10;  // read PD4 into SW1
        uint32_t SW2 = GPIO_PORTD_DATA_R & 0x01;  // read PD0 into SW2

        if (!SW1 && !SW2)  // both pressed
        {
            GPIO_PORTD_DATA_R = 0x04;
        } 
        else if (!SW1)     // SW1 pressed
        { 
            GPIO_PORTD_DATA_R = 0x02;
        } 
        else if (!SW2)     // SW2 pressed
        {
            GPIO_PORTD_DATA_R = 0x08;
        } 
        else               // neither 
        { 
            GPIO_PORTD_DATA_R = 0x00;
        }
    }
}

经过深思:

大概是复制粘贴的代码中的注释表明董事会可能是EK-TM4C1294XL。在这种情况下,LED被称为 D1,D2,D3,D4(D为二极管 ,而不是_PORTD),但分别位于GPIO PN1,PN0,PF4和PF0上,并且开关在PJ0和PJ1上。

在这种情况下,也许以下会更成功:

int main(void) 
{
    SYSCTL_RCGCGPIO_R |= (1<<5 | 1<<8 | 1<<12); // Enable port F, J and N clocks
    GPIO_PORTN_DIR_R |= 0x03;   // PN1 = LED0, PN0 = LED1 (Outputs)
    GPIO_PORTN_DEN_R |= 0x03;   // Enable PN0 and PN1  
    GPIO_PORTF_DIR_R |= 0x11;   // PF4 = LED3, PF0 = LED4 (Outputs)
    GPIO_PORTF_DEN_R |= 0x11;   // Enable PF0 and PF4  

    GPIO_PORTJ_DIR_R &= ~0x03;   // PJ0 = SW1, PJ1 = SW2 (Inputs)
    GPIO_PORTJ_DEN_R &= ~0x03;   // Enable PJ0 and PJ4  

    for(;;)
    {
        uint32_t SW1 = GPIO_PORTJ_DATA_R & 0x01;  // read PJ0 into SW1
        uint32_t SW2 = GPIO_PORTJ_DATA_R & 0x02;  // read PJ1 into SW2

        if (!SW1 && !SW2)  // both pressed
        {
            GPIO_PORTF_DATA_R = 0x01;  // LED4
        } 
        else if (!SW1)     // SW1 pressed
        { 
            GPIO_PORTF_DATA_R = 0x10;  // LED3
        } 
        else if (!SW2)     // SW2 pressed
        {
            GPIO_PORTN_DATA_R = 0x01;  // LED2
        } 
        else               // neither 
        { 
            GPIO_PORTN_DATA_R = 0x02;  // LED1
        }
    }
}

这仍然是无效的,因为该代码只能打开LED指示灯-它不尊重可能连接到端口F和N上其他引脚的其他硬件;您需要添加代码以对设置的LED的相应引脚进行读-修改-写操作,还需要清除其他三个引脚。我把它留给你-它超出了最初的问题。