July 26, 2024

Complete the given task Day1 (GPIO)

Untitled

//In main.c file
while (1)
  {
	  if(!(HAL_GPIO_ReadPin(GPIOC,SW_Pin)))
	  {
		  HAL_GPIO_WritePin(GPIOA,LED_Pin,1);//HAL is liabray write means writting value of digital pin (GPIO port,GPIO pin no, Value to NO)
		  HAL_Delay(100);
	  }
	  else
	  {
		  HAL_GPIO_WritePin(GPIOA,LED_Pin,0);
		  HAL_Delay(100);
	  }
  }
//in main.c file 
// Variable to keep track of the LED state
uint8_t ledState = 0;
  
  while (1)
  {    // Read the state of the switch
	    if(HAL_GPIO_ReadPin(GPIOC, switch_Pin) == GPIO_PIN_RESET) // Assuming active-low switch
	    {
	      // Toggle the LED state
	      ledState = !ledState;

	      // Set the LED pin according to the ledState variable
	      HAL_GPIO_WritePin(GPIOA, led_Pin , ledState ? GPIO_PIN_SET : GPIO_PIN_RESET);

	      // Wait until the button is released
	      while(HAL_GPIO_ReadPin(GPIOC, switch_Pin) == GPIO_PIN_RESET);

	      // Debounce delay
	      HAL_Delay(100);
	    }
	  }
  }

Untitled

  while (1)
  {
  	  if(!(HAL_GPIO_ReadPin(GPIOC,switch1_Pin)))
  		  {
  			  HAL_GPIO_WritePin(GPIOA,led1_Pin,1);
  			  HAL_Delay(100);
  		  }
  	  else if(!(HAL_GPIO_ReadPin(GPIOB,switch2_Pin)))
  	  {
  		  HAL_GPIO_WritePin(GPIOA,led2_Pin,1);
  		  HAL_Delay(100);
  	  }
  	  else if(!(HAL_GPIO_ReadPin(GPIOB,switch3_Pin)))
  	  {
  		  HAL_GPIO_WritePin(GPIOA,led3_Pin,1);
  		  HAL_Delay(100);
  	  }

  	  else
  		  {
  			  HAL_GPIO_WritePin(GPIOA,led1_Pin,0);
  			  HAL_GPIO_WritePin(GPIOA,led2_Pin,0);
  			  HAL_GPIO_WritePin(GPIOA,led3_Pin,0);
  			  HAL_Delay(100);
  		  }
    }
while (1)
  {
  	  if(!(HAL_GPIO_ReadPin(GPIOC,switch1_Pin)))
  		  {
  			  HAL_GPIO_WritePin(GPIOA,led1_Pin,1);
  			  HAL_Delay(100);
  		  }
  	  else if(!(HAL_GPIO_ReadPin(GPIOB,switch2_Pin)))
  	  {
  		  HAL_GPIO_WritePin(GPIOA,led1_Pin,1);
  		  HAL_GPIO_WritePin(GPIOA,led2_Pin,1);
  		  HAL_Delay(100);
  	  }
  	  else if(!(HAL_GPIO_ReadPin(GPIOB,switch3_Pin)))
  	  {
  		  HAL_GPIO_WritePin(GPIOA,led1_Pin,1);
  		  HAL_GPIO_WritePin(GPIOA,led2_Pin,1);
  		  HAL_GPIO_WritePin(GPIOA,led3_Pin,1);
  		  HAL_Delay(100);
  	  }

  	  else
  		  {
  			  HAL_GPIO_WritePin(GPIOA,led1_Pin,0);
  			  HAL_GPIO_WritePin(GPIOA,led2_Pin,0);
  			  HAL_GPIO_WritePin(GPIOA,led3_Pin,0);
  			  HAL_Delay(100);
  		  }
    }
  while (1)
  {
  	  if(!(HAL_GPIO_ReadPin(GPIOC,switch1_Pin)))
  		  {
  			  HAL_GPIO_WritePin(GPIOA,led1_Pin,1);
  			  HAL_Delay(100);
  		  }
  	  else if(!(HAL_GPIO_ReadPin(GPIOB,switch2_Pin)))
  	  {
  		  HAL_GPIO_WritePin(GPIOA,led1_Pin,0);
  		  HAL_Delay(100);
  	  }
  }

Untitled

uint8_t switchPressCount = 0;

while (1)
  {
	  // Read the state of the switch
	      if (HAL_GPIO_ReadPin(GPIOC, switch1_Pin) == GPIO_PIN_RESET) // Assuming active-low switch
	      {
	        // Increment the switch press count
	        switchPressCount++;

	        // Turn on the corresponding LED based on the switch press count
	        if (switchPressCount == 1)
	        {
	          HAL_GPIO_WritePin(GPIOA, led1_Pin, GPIO_PIN_SET);
	        }
	        else if (switchPressCount == 2)
	        {
	          HAL_GPIO_WritePin(GPIOA, led2_Pin, GPIO_PIN_SET);
	        }
	        else if (switchPressCount == 3)
	        {
	          HAL_GPIO_WritePin(GPIOA, led3_Pin, GPIO_PIN_SET);
	        }

	        // Wait until the button is released
	        while (HAL_GPIO_ReadPin(GPIOC, switch1_Pin) == GPIO_PIN_RESET);

	        // Turn off all LEDs after releasing the switch
	        HAL_GPIO_WritePin(GPIOA, led1_Pin, GPIO_PIN_RESET);
	        HAL_GPIO_WritePin(GPIOA, led2_Pin, GPIO_PIN_RESET);
	        HAL_GPIO_WritePin(GPIOA, led3_Pin, GPIO_PIN_RESET);

	        // Reset the switch press count after the third press
	        if (switchPressCount == 3)
	        {
	          switchPressCount = 0;
	        }
	        else if (switchPressCount == 4)
	        {
	        	switchPressCount=0;
	        }

	        // Debounce delay
	        HAL_Delay(100);
	      }

  }
uint8_t switchPressCount = 0;

  while (1)
  {
	  // Read the state of the switch
	      if (HAL_GPIO_ReadPin(GPIOC, switch1_Pin) == GPIO_PIN_RESET) // Assuming active-low switch
	      {
	        // Increment the switch press count
	        switchPressCount++;

	        // Turn on the corresponding LED based on the switch press count
	        if (switchPressCount == 1)
	        {
	          HAL_GPIO_WritePin(GPIOA, led1_Pin, GPIO_PIN_SET);
	        }
	        else if (switchPressCount == 2)
	        {
	          HAL_GPIO_WritePin(GPIOA, led2_Pin, GPIO_PIN_SET);
	        }
	        else if (switchPressCount == 3)
	        {
	          HAL_GPIO_WritePin(GPIOA, led3_Pin, GPIO_PIN_SET);
	          switchPressCount=0;
	        }

	        // Wait until the button is released
	        while (HAL_GPIO_ReadPin(GPIOC, switch1_Pin) == GPIO_PIN_RESET);

	        // Debounce delay
	        HAL_Delay(100);
	      }

  }