gpio command for turning off not functioning inside "if"

Status
Not open for further replies.

yefj

Advanced Member level 5
Joined
Sep 12, 2019
Messages
1,505
Helped
1
Reputation
2
Reaction score
5
Trophy points
38
Activity points
9,114
Hello, i have done blinking led prigram and it worked fine.(EFM32LG)
Afterwards i have built a uart combined program to turn ON and OFF my LED by letters sent from MATLAB.
Before going into the main thing i noticed a behavior in which when i define the pin by using
Code:
 GPIO_PinModeSet(LED_PORT_E,15,gpioModePushPull,0);

It turns the LED ON although i did not ask it to,so i have put this definition inside the IF to turn it ON on the first recieved "g" command.

So i wont get the green LED to turn ON for no reason.



The main problem is for some reason when i send 'h' command to execute the follwing line and turn OFF the green LED,nothing happens.
Code:
 GPIO_PinOutClear(LED_PORT_E,15);
The full code shown bellow.
Where did i go wrong?

Thanks.

Code:
#include "em_device.h"
#include "em_cmu.h"
#include "em_gpio.h"
#include "em_usart.h"
#include "em_chip.h"
//////////////////////////////////////
#include <stdint.h>
#include <stdbool.h>
#include "em_emu.h"
#include "bsp.h"
#include "bsp_trace.h"
///////////////////////////////////////
#define LED_PORT_E    gpioPortE
#define LED_PIN_E     15

#define LED_PORT_A    gpioPortA
#define LED_PIN_A     15



#define BUFFER_SIZE 1
char buffer[BUFFER_SIZE];

volatile uint32_t msTicks; /* counts 1ms timeTicks */

void Delay(uint32_t dlyTicks);
void SysTick_Handler(void)
{
  msTicks++;       /* increment counter necessary in Delay()*/
}
void Delay(uint32_t dlyTicks)
{
  uint32_t curTicks;

  curTicks = msTicks;
  while ((msTicks - curTicks) < dlyTicks) ;
}
/**************************************************************************//**
* @brief  Main function
*****************************************************************************/
int main(void)
{


  int i,j;

  // Chip errata
  CHIP_Init();

  // Enable oscillator to GPIO and USART1 modules
  CMU_ClockEnable(cmuClock_GPIO, true);
  CMU_ClockEnable(cmuClock_USART1, true);

// BSP_TraceProfilerSetup();

    /* Setup SysTick Timer for 1 msec interrupts  */
    if (SysTick_Config(CMU_ClockFreqGet(cmuClock_CORE) / 1000)) {
      while (1) ;
    }

    /* Initialize LED driver */



  // set pin modes for UART TX and RX pins
  GPIO_PinModeSet(gpioPortC, 1, gpioModeInput, 0);
  GPIO_PinModeSet(gpioPortC, 0, gpioModePushPull, 1);


  // Initialize USART asynchronous mode and route pins
  USART_InitAsync_TypeDef init = USART_INITASYNC_DEFAULT;
  //init.parity=usartEvenParity;
  USART_InitAsync(USART1, &init);

  USART1->ROUTE |= USART_ROUTE_TXPEN | USART_ROUTE_RXPEN;



  while (1)
  {

//E-green
//A-red


          GPIO_PinOutSet(LED_PORT_E,15);

          USART_Tx(USART1,buffer[0]);
          GPIO_PinOutClear(LED_PORT_E,15);


          if (USART_Rx(USART1) == 'g')
          {
              GPIO_PinModeSet(LED_PORT_E,15,gpioModePushPull,0);
              USART_Tx(USART1,'D');USART_Tx(USART1,'O');USART_Tx(USART1,'N');USART_Tx(USART1,'E');
          }
          if (USART_Rx(USART1) == 'h')
          {
              GPIO_PinOutClear(LED_PORT_E,15);
              USART_Tx(USART1,'D');USART_Tx(USART1,'O');USART_Tx(USART1,'N');USART_Tx(USART1,'E');
          }
          if (USART_Rx(USART1) == 'r')
                   {
                       GPIO_PinModeSet(LED_PORT_A,15,gpioModePushPull,0);
                       USART_Tx(USART1,'D');USART_Tx(USART1,'O');USART_Tx(USART1,'N');USART_Tx(USART1,'E');
                   }
          if (USART_Rx(USART1) == 't')
                             {
              GPIO_PinOutClear(LED_PORT_A,15);
                                 USART_Tx(USART1,'D');USART_Tx(USART1,'O');USART_Tx(USART1,'N');USART_Tx(USART1,'E');
                             }

  }
}
 

You don't understand the operation of USART_Rx(USART1). It's waiting endlessly for the next character. Hence when you type 'g', the CPU will wait at any of the USART_Rx(USART1) calls, but not necessarily at that responding on 'g'.
 

Hello,Yes i didnt understand the full operation USART_Rx.
When i send 'g' it responds and turns ON the green LED, then you say its waiting for something to let me send another character.
My code also responds to the "r" command and turns on the Red LED but it egnore completely the 't' tu turn off the red LED.
After i send 'g' what should be done in order to "unfreeze" it from this waiting state and let me send the 'h' character to turn off the green LED?
Thanks.
 
Last edited:

You should have only one USART_Rx(USART1) call in your while(1) loop that is reading the typed character to a variable. Then perform all decisions using the variable value. Or have a single switch statement:


Code C - [expand]
1
2
3
4
5
6
switch ( USART_Rx(USART1))
{
    case 'g':
 
    case 'h'
}

 
Reactions: yefj

    yefj

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…