0% found this document useful (0 votes)
5 views7 pages

Expt 2

The document contains code snippets for controlling GPIO pins on a microcontroller using HAL library functions. It includes three main functionalities: flashing all pins on and off simultaneously, alternating the state of specific pins, and creating a rolling effect by sequentially activating each pin. The code also includes initialization functions for UART and GPIO, as well as error handling mechanisms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Expt 2

The document contains code snippets for controlling GPIO pins on a microcontroller using HAL library functions. It includes three main functionalities: flashing all pins on and off simultaneously, alternating the state of specific pins, and creating a rolling effect by sequentially activating each pin. The code also includes initialization functions for UART and GPIO, as well as error handling mechanisms.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NAME: Siddhesh Bhivare

PRN: 202301070005

BATCH: A1

1. FLASH CODE:
ALL ON AND OFF AT SAME TIME

Code:

#include "main.h"

UART_HandleTypeDef huart2;

void SystemClock_Config(void);

static void MX_GPIO_Init(void);

static void MX_USART2_UART_Init(void);

int main(void)

HAL_Init();

SystemClock_Config();

MX_GPIO_Init();

MX_USART2_UART_Init();

while (1)

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |


GPIO_PIN_3, GPIO_PIN_SET);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0 | GPIO_PIN_1 | GPIO_PIN_2 |


GPIO_PIN_3, GPIO_PIN_RESET);

HAL_Delay(500);
}

void SystemClock_Config(void)

RCC_OscInitTypeDef RCC_OscInitStruct = {0};

RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

HAL_RCC_PWR_CLK_ENABLE();

HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);

RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;

RCC_OscInitStruct.HSIState = RCC_HSI_ON;

RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;

RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI;

RCC_OscInitStruct.PLL.PLLM = 16;

RCC_OscInitStruct.PLL.PLLN = 336;

RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV4;

RCC_OscInitStruct.PLL.PLLQ = 4;

if (HAL_RCC_OscConfig(CRCC_OscInitStruct) != HAL_OK)

Error_Handler();

RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

if (HAL_RCC_ClockConfig(CRCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

Error_Handler();

static void MX_USART2_UART_Init(void)

huart2.Instance = USART2;

huart2.Init.BaudRate = 115200;

huart2.Init.WordLength = UART_WORDLENGTH_8B;

huart2.Init.StopBits = UART_STOPBITS_1;

huart2.Init.Parity = UART_PARITY_NONE;

huart2.Init.Mode = UART_MODE_TX_RX;

huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;

huart2.Init.OverSampling = UART_OVERSAMPLING_16;

if (HAL_UART_Init(Chuart2) != HAL_OK)

Error_Handler();

static void MX_GPIO_Init(void)

{
GPIO_InitTypeDef GPIO_InitStruct = {0};

HAL_RCC_GPIOC_CLK_ENABLE();

HAL_RCC_GPIOH_CLK_ENABLE();

HAL_RCC_GPIOA_CLK_ENABLE();

HAL_RCC_GPIOB_CLK_ENABLE();

HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3,
GPIO_PIN_RESET);

HAL_GPIO_WritePin(LD2_GPIO_Port, LD2_Pin, GPIO_PIN_RESET);

GPIO_InitStruct.Pin = B1_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(B1_GPIO_Port, CGPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_3;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(GPIOC, CGPIO_InitStruct);

GPIO_InitStruct.Pin = GPIO_PIN_2;

GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

GPIO_InitStruct.Pull = GPIO_NOPULL;

HAL_GPIO_Init(GPIOC, CGPIO_InitStruct);
GPIO_InitStruct.Pin = LD2_Pin;

GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

GPIO_InitStruct.Pull = GPIO_NOPULL;

GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

HAL_GPIO_Init(LD2_GPIO_Port, CGPIO_InitStruct);

void Error_Handler(void)

disable_irq();

while (1)

#ifdef USE_FULL_ASSERT

void assert_failed(uint8_t *file, uint32_t line)

#endif
2. Alternate On and Off

while (1)

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_2, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1 | GPIO_PIN_3, GPIO_PIN_RESET);

HAL_Delay(500);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0 | GPIO_PIN_2, GPIO_PIN_RESET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1 | GPIO_PIN_3, GPIO_PIN_SET);

HAL_Delay(500);

OUTPUT:
3. Rolling effect

while (1)

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, GPIO_PIN_RESET);

HAL_DELAY(1000);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_1, GPIO_PIN_RESET);

HAL_DELAY(1000);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_2, GPIO_PIN_RESET);

HAL_DELAY(1000);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_SET);

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_3, GPIO_PIN_RESET);

HAL_DELAY(1000);

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy