// // Initialize the oled screen // uint8_t ssd1306_Init(void) { // Wait for the screen to boot HAL_Delay(100); /* Init LCD */ ssd1306_WriteCommand(0xAE); //display off ssd1306_WriteCommand(0x20); //Set Memory Addressing Mode ssd1306_WriteCommand(0x10); //00,Horizontal Addressing Mode;01,Vertical Addressing Mode;10,Page Addressing Mode (RESET);11,Invalid ssd1306_WriteCommand(0xB0); //Set Page Start Address for Page Addressing Mode,0-7 ssd1306_WriteCommand(0xC8); //Set COM Output Scan Direction ssd1306_WriteCommand(0x00); //---set low column address ssd1306_WriteCommand(0x10); //---set high column address ssd1306_WriteCommand(0x40); //--set start line address ssd1306_WriteCommand(0x81); //--set contrast control register ssd1306_WriteCommand(0xFF); ssd1306_WriteCommand(0xA1); //--set segment re-map 0 to 127 ssd1306_WriteCommand(0xA6); //--set normal display ssd1306_WriteCommand(0xA8); //--set multiplex ratio(1 to 64) ssd1306_WriteCommand(0x3F); // ssd1306_WriteCommand(0xA4); //0xa4,Output follows RAM content;0xa5,Output ignores RAM content ssd1306_WriteCommand(0xD3); //-set display offset ssd1306_WriteCommand(0x00); //-not offset ssd1306_WriteCommand(0xD5); //--set display clock divide ratio/oscillator frequency ssd1306_WriteCommand(0xF0); //--set divide ratio ssd1306_WriteCommand(0xD9); //--set pre-charge period ssd1306_WriteCommand(0x22); // ssd1306_WriteCommand(0xDA); //--set com pins hardware configuration ssd1306_WriteCommand(0x12); ssd1306_WriteCommand(0xDB); //--set vcomh ssd1306_WriteCommand(0x20); //0x20,0.77xVcc ssd1306_WriteCommand(0x8D); //--set DC-DC enable ssd1306_WriteCommand(0x14); // ssd1306_WriteCommand(0xAF); //--turn on SSD1306 panel // Clear screen ssd1306_Fill(Black); // Flush buffer to screen ssd1306_UpdateScreen(); // Set default values for screen object SSD1306.CurrentX = 0; SSD1306.CurrentY = 0; SSD1306.Initialized = 1; return 1; }
/** * @brief The application entry point. * * @retval None */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* USER CODE BEGIN SysInit */ /* USER CODE END SysInit */ /* Initialize all configured peripherals */ MX_GPIO_Init(); MX_I2C1_Init(); MX_TIM3_Init(); //MX_IWDG_Init(); MX_TIM14_Init(); MX_TIM16_Init(); MX_TIM17_Init(); /* USER CODE BEGIN 2 */ HAL_TIM_Encoder_Start(&htim3, TIM_CHANNEL_ALL); HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM3_IRQn); HAL_TIM_Base_Start_IT(&htim3); // Seconds Counter startup duty HAL_TIM_Base_Start_IT(&htim14); HAL_TIM_Base_Stop_IT(&htim14); HAL_TIM_Base_Start_IT(&htim16); HAL_TIM_Base_Start_IT(&htim17); HAL_GPIO_WritePin(GPIOA, LED_RED_PIN, GPIO_PIN_RESET); ssd1306_Init(); current_cursor = exposition; /* USER CODE END 2 */ /* Infinite loop */ /* USER CODE BEGIN WHILE */ while (1) { /* USER CODE END WHILE */ /* USER CODE BEGIN 3 */ //HAL_IWDG_Refresh(&hiwdg); if(__HAL_RCC_GET_SYSCLK_SOURCE() == RCC_SYSCLKSOURCE_STATUS_HSE) HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN, GPIO_PIN_SET); else HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN, GPIO_PIN_RESET); if (update_screen_flag) { ssd1306_Fill(Black); ssd1306_UpdateScreen(); update_screen_flag = 0; } if (seconds_counter > STOP_TIME_SEC) //if more than 10 s need to stop { // Entering STOP Mode Procedure seconds_counter = 0; GPIO_InitTypeDef GPIO_InitStruct; ssd1306_WriteCommand(0xAE); // OLED Off GPIO_InitStruct.Pin = ENC_BUTTON_PIN; GPIO_InitStruct.Mode = GPIO_MODE_EVT_RISING_FALLING; GPIO_InitStruct.Pull = GPIO_PULLDOWN; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); HAL_NVIC_SetPriority(EXTI4_15_IRQn, 0, 0); HAL_NVIC_EnableIRQ(EXTI4_15_IRQn); HAL_GPIO_WritePin(GPIOA, LED_GREEN_PIN|LED_RED_PIN|GATE_PIN|FOCUS_PIN, GPIO_PIN_RESET); HAL_PWR_EnterSTOPMode(PWR_LOWPOWERREGULATOR_ON, PWR_STOPENTRY_WFE); // Exit from STOP Mode procedure SystemClock_Config(); ssd1306_WriteCommand(0xAF); // OLED On GPIO_InitStruct.Pin = ENC_BUTTON_PIN; GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_PULLDOWN; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); current_state = menu_navigation; } Menu(); if (current_state == running_timer || current_state == running_interval) { if(gate_flag) { // Reset Gate & Focus HAL_GPIO_WritePin(GPIOA, FOCUS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, GATE_PIN, GPIO_PIN_RESET); HAL_TIM_Base_Stop_IT(&htim14); HAL_Delay(500); __HAL_TIM_SET_COUNTER(&htim14, 0); HAL_TIM_Base_Start_IT(&htim14); gate_flag = 0; // If not last shot, go to Interval state if(tmp_num_shots != 0) { tmp_int_minutes = set_int_minutes; tmp_int_sec = set_int_sec; tmp_exp_minutes = set_exp_minutes; tmp_exp_sec = set_exp_sec; current_state = running_interval; HAL_GPIO_WritePin(GPIOA, LED_RED_PIN,GPIO_PIN_RESET); // 1s LED Off } else { HAL_TIM_Base_Stop_IT(&htim14); __HAL_TIM_SET_COUNTER(&htim14, 0); HAL_GPIO_WritePin(GPIOA, LED_RED_PIN,GPIO_PIN_RESET); // 1s LED Off tmp_exp_minutes = set_exp_minutes; tmp_exp_sec = set_exp_sec; current_state = menu_navigation; update_screen_flag = 1; } } } if (exiting_run) { HAL_TIM_Base_Stop_IT(&htim16); HAL_Delay(1000); exiting_run = 0; HAL_TIM_Base_Start_IT(&htim16); HAL_GPIO_WritePin(GPIOA, FOCUS_PIN, GPIO_PIN_RESET); HAL_GPIO_WritePin(GPIOA, GATE_PIN, GPIO_PIN_RESET); } } /* USER CODE END 3 */ }