gpt4 book ai didi

c - Keil 中的 Traffic Light for Tiva 系列 - c 编程

转载 作者:行者123 更新时间:2023-11-30 16:13:32 36 4
gpt4 key购买 nike

该程序应该检测哪个开关打开,然后将交通灯更改为红色、黄色或绿色。我陷入困境,我不知道我的初始化是否适合端口 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 - Keil 中的 Traffic Light for Tiva 系列 - c 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58016935/

36 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com