Проблемы с назначением портов для панели запуска tivia TM4 C, которые не могут быть подключены к компонентам на макете - PullRequest
1 голос
/ 02 апреля 2020

Я работаю с блокнотом TM4 C и мне нужно назначить порты E и B в качестве входов и выходов, чтобы иметь возможность управлять простым светом traffi c, построенным на макете. На плате имеется три светодиода, а также две кнопки: при нажатии btn1 светодиоды чередуются между красным - зеленым - желтым - обратно на красный, при нажатии btn2 текущий индикатор начинает гореть sh , когда оба нажаты, светодиоды работают. Я пытался использовать другие порты и добился успеха (вот почему я знаю, что это не проблема с оборудованием), но указанные требования относятся к портам B [2: 0] в качестве выходов и E [1: 0] в качестве входов, и я не вижу, что я делаю неправильно в коде. Когда я загружаю код на плату, единственным функционирующим компонентом является красный светодиод на макете (который является исходным состоянием автомата-автомата), но нажатие любой кнопки ничего не дает. Примечание. Порт B [2: 0] подключен к аноду соответствующего светодиода (2 - красный, 1 - желтый, 0 - зеленый), а катод каждого подключен к собственному резистору, который подключен к земле.

#include <stdint.h>
#include "tm4c123gh6pm.h"

// ***** 1. Assign switches *****
#define LIGHT                    (*((volatile unsigned long *)0x4000501C)) // Port B Pins 3, 2, 1
    // Port B base: 4000.5000 + x0004 (bit 0: 4*(2^0)) + x0008 (bit 1: 4*(2^1)) = x 0010 (bit 2: 4*(2^2))
#define SENSOR                   (*((volatile unsigned long *)0x4002400C)) // Port E Pins 0 and 1
// Port E base: 4002.4000 + x0004 (bit 0: 4*(2^0)) + x0008 (bit 1: 4*(2^1))

// ***** 2. Pre-processor Directives Section *****
//Port B used for LEDs
#define GPIO_PORTB_DATA_R       (*((volatile unsigned long *)0x400053FC))
#define GPIO_PORTB_DIR_R        (*((volatile unsigned long *)0x40005400))
#define GPIO_PORTB_AFSEL_R      (*((volatile unsigned long *)0x40005420))
#define GPIO_PORTB_DEN_R        (*((volatile unsigned long *)0x4000551C))
#define GPIO_PORTB_AMSEL_R      (*((volatile unsigned long *)0x40005528))
#define GPIO_PORTB_PCTL_R       (*((volatile unsigned long *)0x4000552C))

//Port E used for switches
#define GPIO_PORTE_DATA_R       (*((volatile unsigned long *)0x400243FC))
#define GPIO_PORTE_DIR_R        (*((volatile unsigned long *)0x40024400))
#define GPIO_PORTE_AFSEL_R      (*((volatile unsigned long *)0x40024420))
#define GPIO_PORTE_DEN_R        (*((volatile unsigned long *)0x4002451C))
#define GPIO_PORTE_AMSEL_R      (*((volatile unsigned long *)0x40024528))
#define GPIO_PORTE_PCTL_R       (*((volatile unsigned long *)0x4002452C))

#define SYSCTL_RCGC2_R          (*((volatile unsigned long *)0x400FE108))

// ***** 3. Global Declarations Section *****
// FUNCTION PROTOTYPES: Each subroutine defined
void PortB_Init(void);
void PortE_Init(void);
void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void);  // Enable interrupts
void Delay(unsigned int amp);

// ***** 4. Subroutines Section *****
// Data structure for FSM
struct State {
    uint32_t Out;
    uint32_t Time;
    uint32_t Next[4]; // 2 inputs, four combinations
};
typedef const struct State STyp;
#define rOn     0
#define rOff    1
#define yOn     2
#define yOff    3
#define gOn     4
#define gOff    5
STyp FSM[6]={
           // 00  01   10   11
 {0x04,100, {rOn,gOn,rOff,rOff}},  // State rOn | 0x08 = PortB Pin 2 ON
 {0x00,100, {rOn,rOn,rOn, gOn}},     // State rOff
 {0x02,100, {yOn,rOn,yOff,yOff}},  // State yOn | 0x02 = PortB Pin 1 ON
 {0x00,100, {yOn,yOn,yOn, rOn}},     // State yOff
 {0x01,100, {gOn,yOn,gOff,gOff}},  // state gOn | 0x40 = PortB Pin 0 ON
 {0x00,100, {gOn,gOn,gOn, yOn}}    // state gOff
};

int main(void){ 
    uint8_t cs;     // Index to the current state 
    uint8_t Input;  // Index to the inputs

    // Initialize GPIO on Ports B, E
    PortB_Init();        // Call initialization of port PB0 PB1 PB2
    PortE_Init();        // Call initialization of port PE0 PE1
    EnableInterrupts();  // The grader uses interrupts

    // Initial state: Red LED lit
    // Set delay for 100ms
    Delay(10);
    cs = rOn;  // Assign current state to be red on

    while(1){
        LIGHT = FSM[cs].Out;
        Delay(FSM[cs].Time);
        Input = SENSOR >> 2;
        cs = FSM[cs].Next[Input];       
   }
}

void PortE_Init(void){ volatile unsigned long delay; // Port E INPUT
    SYSCTL_RCGC2_R |= 0x00000010;      // E clock
    delay = SYSCTL_RCGC2_R;            // delay         
    GPIO_PORTE_AMSEL_R &= ~0x03;       // disable analog function  
    GPIO_PORTE_PCTL_R  &= ~0x000000FF; // GPIO clear PCTL bits for PE1 and PE0
    GPIO_PORTE_DIR_R   &= ~0x03;       // PE1 and PE0 (turn off inputs) 
    GPIO_PORTE_AFSEL_R &= ~0x03;       // no alternate function       
    GPIO_PORTE_DEN_R   |=  0x03;       // enable digital pins PE1 and PE0        
}

void PortB_Init(void){ volatile unsigned long delay; // Port B OUTPUT
    SYSCTL_RCGC2_R |= 0x00000002;      // B clock
    delay = SYSCTL_RCGC2_R;            // delay     
    GPIO_PORTB_AMSEL_R &= ~0x07;       // disable analog function
    GPIO_PORTB_PCTL_R  &= ~0x00000FFF; // GPIO clear PCTL bits for PB2, PB1, and PB0
    GPIO_PORTB_DIR_R   |=  0x07;       // PB2, PB1, and PB0 (turn on/SET outputs)   
    GPIO_PORTB_AFSEL_R &= ~0x07;       // no alternate function       
    GPIO_PORTB_DEN_R   |=  0x07;       // enable digital pins PB2, PB1, and PB0        
}

void Delay(unsigned int amp) {
    volatile uint32_t time;
    time = ((727240*200/91)*2/1000) * amp;  
    while(time) {
        time--;
    }
}
...