该程序假定要检测打开了什么开关,然后从那里将交通灯更改为红色,黄色或绿色。我被卡住了,我不知道我的初始化是否适合端口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--;
  }
}

最佳答案

您确定要直接修改寄存器吗?请尝试仔细检查是否要翻转寄存器中的正确位。还要检查您编写的这些十六进制值正确无误。用万用表测量上述GPIO上的电压,它是否按预期变化?如果不是,则说明您的电路有错误或inits()。

关于c - Teil Series中的Keil交通灯-c编程,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/58016935/

10-11 00:21