A1210/Core/Src/main.c

465 lines
9.5 KiB
C

/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>&copy; Copyright (c) 2023 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adc.h"
#include "dma.h"
#include "spi.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
#include "OLED_Fonts.h"
#include "string.h"
#include <math.h>
#include <stdbool.h>
#include <stdlib.h>
#include "my.h"
#include "ssd1306.h"
__IO uint16_t repeat = 0;
volatile int needRefresh; ///
typedef enum
{
CURSOR = Black,
TEXT = White
}typeTEXT;
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
//0,2; 1; 2; 10 ôâ÷
//0,1; 0,3; 1; 3; 10; 30; 100 ôí÷
typedef struct {
int kPr;
int kExit;
int FVCH;
int FNCH;
} set_type;
char pString[16] = {0};
char* FloatToASCII(float Val, int32_t RoundTo)
{
uint8_t idx = 0;
int32_t i;
float value, F, I;
uint32_t rnd = 0;
uint32_t Tmp32 = 0;
uint32_t Int32 = 0;
uint32_t Frac32 = 0;
uint32_t _I32 = 0;
uint32_t _F32 = 0;
uint32_t d = 0, dd = 0;
char tmparray[32];
volatile bool minus = false;
memset(tmparray, 0, 32);
memset(pString, 0, 16);
value = Val;
if(value < 0)
minus = true;
if((Val >= 10000.00000f)/* || (Val <= 0)*/)
{
if(Val <= 0)
{
pString[0] = '0';
pString[1] = '~';
pString[2] = '0';
pString[3] = '\0';
}
else
{
pString[0] = '1';
pString[1] = '0';
pString[2] = '0';
pString[3] = '0';
pString[4] = '0';
pString[5] = '\0';
}
return pString;
}
value = ABS(Val);
rnd = ABS(RoundTo);
if(RoundTo < 0)
{
F = modff(value, &I);
while(rnd)
{
F *= 10.0f;
rnd--;
d++;
}
F = roundf(F);
dd = 1;
for(i = 0; i < d; i++)
dd *= 10;
if((uint32_t) F == dd)
{
F = 0;
I += 1;
}
}
else
{
I = roundf(value);
F = 0.0f;
}
Int32 = (uint32_t) I;
Frac32 = (uint32_t) F;
_I32 = Int32;
_F32 = Frac32;
idx = 0;
tmparray[idx++] = '\0';
if((Int32 == 0) && (Frac32 == 0))
{
if(RoundTo < 0)
{
for(i = 0; i < abs(RoundTo); i++)
tmparray[idx++] = '0';
tmparray[idx++] = '~';
}
tmparray[idx++] = '0';
}
else
{
if(RoundTo < 0)
{
for(i = 0; i < abs(RoundTo); i++)
{
Tmp32 = Frac32;
Frac32 /= 10;
Tmp32 = Tmp32 - Frac32 * 10;
tmparray[idx++] = '0' + Tmp32;
}
tmparray[idx++] = '~';
}
if(Int32 != 0)
{
while(Int32)
{
Tmp32 = Int32;
Int32 /= 10;
Tmp32 = Tmp32 - Int32 * 10;
tmparray[idx++] = '0' + Tmp32;
}
}
else
tmparray[idx++] = '0';
if(minus)
{
if((_I32 != 0) || (_F32 != 0))
tmparray[idx++] = '-';
}
}
for(i = 0; i < idx; i++)
pString[i] = tmparray[idx - i - 1];
return pString;
}
uint16_t readkey(void)
{
uint16_t key = 0;
if(HAL_GPIO_ReadPin(OK_BTN_GPIO_Port, OK_BTN_Pin) == GPIO_PIN_RESET)
key |= OK_BTN_Pin;
if(HAL_GPIO_ReadPin(UP_BTN_GPIO_Port, UP_BTN_Pin) == GPIO_PIN_RESET)
key |= UP_BTN_Pin;
if(HAL_GPIO_ReadPin(DN_BTN_GPIO_Port, DN_BTN_Pin) == GPIO_PIN_RESET)
key |= DN_BTN_Pin;
if(HAL_GPIO_ReadPin(ON_GPIO_Port, ON_Pin) == GPIO_PIN_RESET)
key |= ON_Pin;
return key;
}
uint16_t kbhit(void)
{
volatile uint16_t key;
static uint32_t dreb = 0; //äðåáåçã
static uint8_t first = 1; //ïðèçíàê ïåðâîãî ìîìåíòà íàæàòèÿ êíîïêè
static uint16_t rep = 50;
do
{
key = readkey();
if(!key)
{
if(dreb)
dreb--;
else first = 1;
}
else
{
if(dreb < 3)
dreb++;
}
}
while((dreb > 0) && (dreb < 3));
if(!key) //åñëè êíîïêè îòïóùåíû
{
first = 1; //äîñòîâåðíî êíîïêè îòïóùåíû
return 0; //âåðíóòü 0
}
else //èíà÷å
{
if(first) //îïðåäåëèòü êíîïêó
{ //òîëüêî îäèí ðàç
first = 0; //çà íàæàòèå
repeat = rep = 32;
return key; //âåðíóòü â ãëàâíóþ ôóíêöèþ ñîñòîÿíèå êëàâèàòóðû
}
else
{
if(repeat)
return 0; //èìèòàöèÿ ñâîáîäíîé êëàâèàòóðû
else
{
if(rep > 0)
rep >>= 1;
else rep = 32;
repeat = rep;
// if(key == KEY_RESET_Pin)
// return 0;
return key;
}
}
}
}
const float exitVar[] = {0.1, 0,2, 0,5, 1, 2, 5, 10, 20, 50, 100, 200, 500};
const float fvchVar[] = {0.2, 1, 2, 10};
const float fnchVar[] = {0.1, 0.3, 1, 3, 10, 30, 100};
const char string11[] = "Rgh";
const char string12[] = "gRk/vc2";
const char string21[] = "Ds[jl";
const char string22[] = "vD/vc2";
const char string31[] = "ADX";
const char string32[] = "Uw";
const char string41[] = "AYX";
const char string42[] = "rUw";
const char string51[] = "Gthtuheprf!!!";
int main(void)
{
set_type set;
set.kPr = 22222;
set.kExit =22222;
set.FVCH=22222;
set.FNCH=22222;
HAL_Init();
SystemClock_Config();
MX_FLASH_Init();
MX_GPIO_Init();
MX_DMA_Init();
MX_ADC1_Init();
MX_TIM3_Init();
MX_SPI1_Init();
MX_TIM7_Init();
MX_USART1_UART_Init();
/////Ìîé êîä çäåñü
ssd1306_Init();
DrawLogo();
HAL_Delay(100);
ssd1306_Fill(Black);
ssd1306_UpdateScreen();
ssd1306_SetCursor(0, 7);
while (1)
{
FontSet(Segoe_UI_Rus_8);
OLED_DrawStr(string11, 1, 10, TEXT);
OLED_DrawStr(FloatToASCII(2.45f, -3), 40, 10, TEXT);
// OLED_DrawStr(FloatToASCII(2.45f, 0), 40, 10, TEXT);
// OLED_DrawNum(set.kPr, 40, 10, TEXT);
OLED_DrawStr(string12, 80, 10, TEXT);
////////////////////////////
OLED_DrawStr(string21, 1, 20, TEXT);
OLED_DrawNum(set.kExit, 40, 20, CURSOR);
OLED_DrawStr(string22, 80, 20, TEXT);
////////////////////////////
OLED_DrawStr(string31, 2, 30, TEXT);
OLED_DrawNum(set.FVCH, 40, 30, TEXT);
OLED_DrawStr(string32, 80, 30, TEXT);
//////////////
OLED_DrawStr(string41, 2, 40, TEXT);
OLED_DrawNum(set.FNCH, 40, 40, TEXT);
OLED_DrawStr(string42, 80, 40, TEXT);
//////////////
OLED_DrawStr(string51, 30, 50, TEXT);
//OLED_DrawChar("n", 4, 4, TEXT);
//if(readkey()==OK_BTN_Pin) OLED_DrawStr("jr", 255, 8, White);
//if(readkey()==DN_BTN_Pin) OLED_DrawStr("dybp", 255, 20, White);
//if(readkey()==UP_BTN_Pin) OLED_DrawStr("ddth[", 255, 32, White);
//if(readkey()==ON_Pin) OLED_DrawStr("drk", 255, 44, White);
//HAL_Delay(300);
// ssd1306_Fill(Black);
//; //
//OLED_DrawStr("Ds[jl", 255, 20, White); //
//OLED_DrawStr("adx", 255, 32, White); // Modbus
//OLED_DrawStr("ayx", 255, 44, White);
needRefresh=1;
}
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE3);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
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 = 8;
RCC_OscInitStruct.PLL.PLLN = 60;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 2;
RCC_OscInitStruct.PLL.PLLR = 5;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLRCLK;
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();
}
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t *file, uint32_t line)
{
/* USER CODE BEGIN 6 */
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/