Skip to content

Instantly share code, notes, and snippets.

@d-boz-wtwh
Created October 22, 2019 20:46
Show Gist options
  • Save d-boz-wtwh/35de7b800181876c205b26d705d51d72 to your computer and use it in GitHub Desktop.
Save d-boz-wtwh/35de7b800181876c205b26d705d51d72 to your computer and use it in GitHub Desktop.
Microcontroller Projects
/****************************************************
Dated: 03 November 2018
Property off: www.microcontroller-project.com
Written by : Usman Ali Butt (usa_butt@hotmail.com)
****************************************************/
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f1xx_hal.h"
#include "gpio.h"
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
int main(void)
{
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* Configure the system clock */
SystemClock_Config();
/* Initialize all configured peripherals */
MX_GPIO_Init();
while (1)
{
//Check - If their is no light or not sufficient light to break the LDR potential
//Barrier / Photoresistor in Reverse bias
if(HAL_GPIO_ReadPin(LdrPin_GPIO_Port, LdrPin_Pin)==GPIO_PIN_RESET)
HAL_GPIO_WritePin(Bulb_GPIO_Port, Bulb_Pin,GPIO_PIN_SET); //Switch on the bulb - Activate relay
else
//If their is sufficient light and light dependent resistor is conducting than
//Switch off the bulb
HAL_GPIO_WritePin(Bulb_GPIO_Port, Bulb_Pin,GPIO_PIN_RESET);
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct;
RCC_ClkInitTypeDef RCC_ClkInitStruct;
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.HSICalibrationValue = 16;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Initializes the CPU, AHB and APB busses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
/**Configure the Systick interrupt time
*/
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
/**Configure the Systick
*/
HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);
/* SysTick_IRQn interrupt configuration */
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}
void _Error_Handler(char *file, int line)
{
while(1)
{
}
}
#ifdef USE_FULL_ASSERT
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment