This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C1231H6PMI turn on LED by switch



Hello,

I have some problems about switch,

1. I'm using TM4C1231H6PMI with IAR Embedded Workbench IDE. I'm trying to use SW PB1 to change the LED mode which has on, off and flash. If I press the SW first time, the LED will turn on, second time will turn off, third time will flash every 1 second by timer. How can I do without interrupt ?

2. Now I'm learning about switch, I don't understand why when I run the code, at the first time while loop the value of  

MAP_GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_1) is 2, and GPIO_PIN_1 has no value. Then MAP_GPIOPinRead will be 0, and after I press the switch it still be 0. I don't know where is the problem.

My code as follow,

int
main(void)
{ 
    MAP_SysCtlClockSet(SYSCTL_SYSDIV_1 | SYSCTL_USE_OSC | SYSCTL_OSC_MAIN |
                   SYSCTL_XTAL_16MHZ);
    
    
    // 
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_UART0);
    
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOA);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);
    MAP_SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);                
    // 
    MAP_GPIOPinConfigure(GPIO_PA0_U0RX);
    MAP_GPIOPinConfigure(GPIO_PA1_U0TX);
    // 
    HWREG(GPIO_PORTF_BASE + 0x520) = 0x4C4F434B;
    HWREG(GPIO_PORTF_BASE + 0x524) = 0x00FF;
    HWREG(GPIO_PORTF_BASE + 0x420) = 0x0000;
    HWREG(GPIO_PORTF_BASE + 0x51C) = 0x001F;
    // 
    MAP_GPIOPinTypeUART(GPIO_PORTA_BASE, GPIO_PIN_0 | GPIO_PIN_1);
    
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTA_BASE, GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7);
    MAP_GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 | GPIO_PIN_3 | GPIO_PIN_4);
    
    MAP_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_1 | GPIO_PIN_0);
    // 
    MAP_UARTConfigSetExpClk(UART0_BASE, MAP_SysCtlClockGet(), 115200,
                        (UART_CONFIG_WLEN_8 | UART_CONFIG_STOP_ONE |
                         UART_CONFIG_PAR_NONE));
    
    // Key
    MAP_GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);
    MAP_GPIOPinTypeGPIOInput(GPIO_PORTB_BASE, GPIO_PIN_1);


    uint32_t Key1status;
    
    while(1)
    {
        Key1status = MAP_GPIOPinRead(GPIO_PORTB_BASE, GPIO_PIN_1);
        
        if (Key1status == GPIO_PIN_1)
        {
            MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, GPIO_PIN_4);
        }
        else
        {
            MAP_GPIOPinWrite(GPIO_PORTA_BASE, GPIO_PIN_4, 0);
        }
    }
}

  • Yang Edison said:
    and GPIO_PIN_1 has no value

    Not true. GPIO_PIN_1 is a macro that has exactly the value 2.

    Your key1status variable will always reflect the button status at the moment that your code passed by the GPIOPinRead line. If you are running it on debug mode, step by step, that might be getting you confused.

    By the way, on most hardware, status 0 means button pressed, and status NOT ZERO means lifted. Check that as well.

    Regarding your goal to have three different states in your command, that's perfectly feasible. Just use another control variable to hold the state, and change its value everytime you press and release the button. You will need to add some logic to be sure that a quick spike of the button signal doesn't change your status several times (go read about debouncing).

  • Hello Yang

    To be able to detect a key press a pull up or a pull down must be connected to the IO. In this case you plan to detect a 1. so it would mean that the switch has a pull up connected on one leg and the other leg is connected to the IO via a weaker pull down. On the switch press the Circuit gets completed allowing the stronger Pull up to present sufficient voltage above Vih to the GPIO. Is that how the construction of the switch has been done?
  • Hi Bruno,
    Thanks for the answer, it really help me so much.
  • Glad I could help.
    If you have time later, make a quick comment here on how you solved the issue, so that others can learn from your steps!
    Cheers.
  • Hi Amit
    I measured my switch leg but it just 0.04V, I thought it has some problems, I'm still finding it. Thanks for the response.
  • Hello Yang,

    What about the Pull Up resistance on the switch when the key press is done? Can you please share the schematics of the external switch?
  • Hi Amit,
    I don't really understand the first question, but I think maybe the problem is system doesn't offer enough voltage to the switch. I just solved the problem by adding a circuit with 10K resistance and connect to the sys 3.3V. Finally it works!
    I don't have the schematics of the switch, there is only one type switch in my office.
  • Hello Yang

    So you could have drawn the circuit on a paper and attached the snapshot. As I thought there were missing resistors.
  • Hello Amit,

    It's the circuit. I'm now using SW2(PD4).

  • Hello Yang

    So R174 needed to be populated and the connector pin 6 needs to have a 3.3V. Only then the MCU_Photoshot_Key_N will be High (R12 needs to be populated as well) when the key is not pressed and will be low when the key is pressed.
  • Amit,

    Poster does have the pin configured as an input, and the pad set as weak pull up:

    GPIOPadConfigSet(GPIO_PORTB_BASE, GPIO_PIN_1, GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU);

    Hence, provided that both R12 and R174 are populated, there is no need for a 3V3 line on J2-6 - the internal pull up will take care of keeping the signal high when the button is not pressed. Apparently the connector is to be used as an external switch.

    Regards,

    Bruno
  • Hello Bruno,

    Yes, indeed. I did overlook the point when the schematic was not shown. With NC's in the schematic there is not much that can be thought other than the circuit is not complete.