首页 > 解决方案 > Keil 中用于 Tiva Series-c 编程的红绿灯

问题描述

该程序假设检测打开了哪个开关,并从那里将交通灯更改为红色、黄色或绿色。我被卡住了,我不知道我的初始化是否适合端口 A 和端口 E。我们只假设更改 DEN 和 DIR。当我在调试时运行它时,什么也没有发生。应该从闯红灯开始。

// Input/Output:
//   PE2 - Red
//   PE1 - Yellow
//   PE0 - Green
//   PA3 - South
//   PA2 - West

// Preprocessor Directives
#include <stdint.h>
#include "tm4c123gh6pm.h"

// Global Variables
uint8_t In0, In1;
uint8_t Out;

// Function Prototypes - Each subroutine defined
void Delay(void);

int main(void) { 
    // Initialize GPIO on Ports A, E
    unsigned long int delay;
    SYSCTL_RCGC2_R |= 0x11; // enables clock for e and a
    delay = SYSCTL_RCGC2_R;
    GPIO_PORTE_AMSEL_R = 0x00; //diables analog
    GPIO_PORTE_AFSEL_R = 0x00;  //disable alternate function
    GPIO_PORTE_PCTL_R = 0x00000000;  //enable GPIO
    GPIO_PORTE_DEN_R = 0x07; // Ports E0-2
    GPIO_PORTE_DIR_R = 0x07; //inputs PE0-2

    GPIO_PORTA_AMSEL_R = 0x00;  //disables analog
    GPIO_PORTA_AFSEL_R = 0x00;  //disable alternate function
    GPIO_PORTA_PCTL_R = 0x00000000; //enable GPIO
    GPIO_PORTA_DEN_R = 0x0C;  // Ports A2 and A3
    GPIO_PORTA_DIR_R = ~0x0C; //outputs PA2 and PA3

    // Initial state: Red LED lit
  Out = (GPIO_PORTE_DATA_R & 0x04); //red starting


    while(1) {
        In0 = GPIO_PORTA_DATA_R&0x08 ; // Read value of south
        In1 = GPIO_PORTA_DATA_R&0x04 ;  // read west
        // Check the following conditions and set Out appropriately:
        //   If south is enabled and red LED is on, then red LED turns off and green LED turns on.
        //   If west is enabled and green LED is on, then green LED turns off and yellow LED turns on.
        //   If yellow LED is on, then yellow LED turns off and red LED turns on.
        if ((In0 == 0x08) & ((Out&0x04) == 0x04)){ // south and red
            GPIO_PORTE_DATA_R = 0x01; //green light turns on
            Delay();
            Out = GPIO_PORTE_DATA_R;

        }
        if ((In1== 0x04) & ((Out&0x01) == 0x01)){ //west and green
            GPIO_PORTE_DATA_R = 0x02;  //yellow light turns on
            Delay();
            Out = GPIO_PORTE_DATA_R;
        }
        if ((Out&0x02) == 0x02){ //if yellow
            GPIO_PORTE_DATA_R = 0x04;  //red light turns on
            Delay();
            Out = GPIO_PORTE_DATA_R;
        }
        Out = GPIO_PORTE_DATA_R;

    //  ??? = Out; // Update LEDs based on new value of Out
    }
}

// Subroutine to wait about 0.1 sec
// Inputs: None
// Outputs: None
// Notes: the Keil simulation runs slower than the real board
void Delay(void) {
    volatile uint32_t time;
  time = 727240*200/91;  // 0.1sec
  while(time) {
    time--;
  }
}

标签: carmkeil

解决方案


你确定直接修改寄存器吗?尝试仔细检查你是否在寄存器中翻转了正确的位。还要检查你写的这些十六进制值是正确的。用万用表测量提到的 GPIO 上的电压,它是否按预期改变?如果没有,那么您的电路有错误,或者 inits()。


推荐阅读