Version 1.1
This commit is contained in:
34
Core/Inc/DS_Button.h
Normal file
34
Core/Inc/DS_Button.h
Normal file
@@ -0,0 +1,34 @@
|
||||
#ifndef DS_BUTTON_H
|
||||
#define DS_BUTTON_H
|
||||
|
||||
#include "stm32f0xx_hal.h"
|
||||
#include <stdbool.h>
|
||||
|
||||
typedef struct DS_Button
|
||||
{
|
||||
GPIO_TypeDef* Port;
|
||||
uint16_t Pin;
|
||||
bool Pressed, Released, PressedLong, PressedLongLong;
|
||||
uint32_t PrevTick, PressStartTick, PressStartTickLong;
|
||||
uint8_t Storage;
|
||||
|
||||
bool FallingEdge, RisingEdge;
|
||||
}DS_Button;
|
||||
|
||||
void DS_ButtonInit(DS_Button* Button, GPIO_TypeDef* Port, uint16_t Pin);
|
||||
|
||||
void DS_ButtonUpdate(DS_Button* Button);
|
||||
|
||||
bool DS_ButtonPressed(DS_Button* Button);
|
||||
|
||||
bool DS_ButtonReleased(DS_Button* Button);
|
||||
|
||||
bool DS_Button_PressedLong(DS_Button* Button);
|
||||
|
||||
bool DS_Button_PressedLongLong(DS_Button* Button);
|
||||
|
||||
bool DS_ButtonRisingEdge(DS_Button* Button);
|
||||
|
||||
bool DS_ButtonFallingEdge(DS_Button* Button);
|
||||
|
||||
#endif //DS_BUTTON_H
|
||||
35
Core/Inc/DS_MAX7219.h
Normal file
35
Core/Inc/DS_MAX7219.h
Normal file
@@ -0,0 +1,35 @@
|
||||
#ifndef DS_MAX7219_H
|
||||
#define DS_MAX7219_H
|
||||
|
||||
#include "stm32f0xx_hal.h"
|
||||
#include "stdbool.h"
|
||||
|
||||
typedef struct DS_MAX7219
|
||||
{
|
||||
SPI_HandleTypeDef* SPI;
|
||||
char DisplaData[8];
|
||||
uint8_t Dots;
|
||||
uint8_t DecodeMode, Intensivity, ScanLimit;
|
||||
bool EnableDecodeMode;
|
||||
GPIO_TypeDef* PORT_CS;
|
||||
uint16_t Pin_CS;
|
||||
}DS_MAX7219;
|
||||
|
||||
|
||||
void DS_MAX7219_Init(DS_MAX7219* Display, SPI_HandleTypeDef* SPI, GPIO_TypeDef* PORT_CS, uint16_t Pin_CS);
|
||||
|
||||
void DS_MAX7219_EnableDots(DS_MAX7219* Display);
|
||||
|
||||
void DS_MAX7219_UpdateDisplay(DS_MAX7219* Display);
|
||||
|
||||
void DS_MAX7219_ConfigureDisplay(DS_MAX7219* Display, uint8_t DecodeMode, uint8_t Intensivity, uint8_t ScanLimit);
|
||||
|
||||
void DS_MAX7219_Print(DS_MAX7219* Display, int64_t Number);
|
||||
|
||||
void __DS_MAX7219_SendConfigData(DS_MAX7219* Display);
|
||||
|
||||
uint8_t __DS_MAX7219_ConvertSymbol(char Symbol);
|
||||
|
||||
|
||||
|
||||
#endif// DS_MAX7219_H
|
||||
165
Core/Src/DS_Button.c
Normal file
165
Core/Src/DS_Button.c
Normal file
@@ -0,0 +1,165 @@
|
||||
|
||||
#include "DS_Button.h"
|
||||
|
||||
void DS_ButtonInit(DS_Button* Button, GPIO_TypeDef *Port, uint16_t Pin)
|
||||
{
|
||||
Button->Port = Port;
|
||||
Button->Pin = Pin;
|
||||
Button->Pressed = false;
|
||||
Button->Released = false;
|
||||
Button->PrevTick = 0;
|
||||
Button->Storage = 5;
|
||||
Button->PressedLong = false;
|
||||
Button->PressStartTick = 0;
|
||||
|
||||
Button->FallingEdge = false;
|
||||
Button->RisingEdge = false;
|
||||
}
|
||||
|
||||
void DS_ButtonUpdate(DS_Button *Button)
|
||||
{
|
||||
//создаём переменную
|
||||
//устанавливаем для неё границы от 0 до 10
|
||||
//если кнопка нажата - увеличиваем значение переменной, пока не доберемся до максимального (10)
|
||||
//если отпущена - уменьшаем, стремясь к нулю
|
||||
//опрос состояния делаем не чаще чем 1 раз в 10 миллисекунд
|
||||
//первое изменение делаем большим - 5 (имитация гистерезиса)
|
||||
//по достижении нуля - устанавливаем флаг что кнопка отпущена
|
||||
//по достижении 10 - флаг кнопка нажата
|
||||
|
||||
uint32_t CurrentTick = HAL_GetTick();
|
||||
if ((CurrentTick - Button->PrevTick)<10)
|
||||
return;
|
||||
|
||||
Button->PrevTick = CurrentTick;
|
||||
|
||||
|
||||
GPIO_PinState PinState = HAL_GPIO_ReadPin(Button->Port, Button->Pin);
|
||||
if (PinState == GPIO_PIN_SET)
|
||||
{
|
||||
if(Button->Storage == 0)
|
||||
Button->Storage +=5;
|
||||
else if(Button->Storage<=10)
|
||||
Button->Storage++;
|
||||
|
||||
if(Button->PressStartTick == 0)
|
||||
Button->PressStartTick = CurrentTick;
|
||||
else if ((CurrentTick - Button->PressStartTick)>1000)
|
||||
Button->PressedLong = true;
|
||||
|
||||
if (Button->PressStartTickLong ==0)
|
||||
{
|
||||
Button->PressStartTickLong = CurrentTick;
|
||||
}
|
||||
else if((CurrentTick - Button->PressStartTickLong)>5000)
|
||||
Button->PressedLongLong = true;
|
||||
|
||||
|
||||
}
|
||||
else
|
||||
{
|
||||
if(Button->Storage == 10)
|
||||
Button->Storage -=5;
|
||||
else if(Button->Storage > 0)
|
||||
Button->Storage--;
|
||||
|
||||
Button->PressStartTick = 0;
|
||||
Button->PressedLong = false;
|
||||
|
||||
Button->PressStartTickLong = 0;
|
||||
Button->PressedLongLong = false;
|
||||
}
|
||||
|
||||
switch (Button->Storage)
|
||||
{
|
||||
case 0:
|
||||
|
||||
if (Button->Released == false)
|
||||
{
|
||||
Button->FallingEdge = true;
|
||||
}
|
||||
|
||||
Button->Pressed = false;
|
||||
Button->Released = true;
|
||||
break;
|
||||
case 10:
|
||||
|
||||
if (Button->Pressed == false)
|
||||
{
|
||||
Button->RisingEdge = true;
|
||||
}
|
||||
|
||||
Button->Pressed = true;
|
||||
Button->Released = false;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
bool DS_ButtonPressed(DS_Button *Button)
|
||||
{
|
||||
if (Button->Pressed)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_ButtonReleased(DS_Button *Button)
|
||||
{
|
||||
if(Button->Released)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_Button_PressedLong(DS_Button *Button)
|
||||
{
|
||||
if(Button->PressedLong)
|
||||
{
|
||||
Button->PressedLong = false;
|
||||
Button->PressStartTick = Button->PressStartTick + 500;
|
||||
return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_Button_PressedLongLong(DS_Button *Button)
|
||||
{
|
||||
if(Button->PressedLongLong)
|
||||
{
|
||||
Button->PressStartTick = 0;
|
||||
Button->PressedLongLong = false;
|
||||
Button->PressStartTickLong = Button->PressStartTickLong + 500;
|
||||
return true;
|
||||
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_ButtonRisingEdge(DS_Button *Button)
|
||||
{
|
||||
if (Button->RisingEdge)
|
||||
{
|
||||
Button->RisingEdge = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
bool DS_ButtonFallingEdge(DS_Button *Button)
|
||||
{
|
||||
if (Button->FallingEdge)
|
||||
{
|
||||
Button->FallingEdge = false;
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
229
Core/Src/DS_MAX7219.c
Normal file
229
Core/Src/DS_MAX7219.c
Normal file
@@ -0,0 +1,229 @@
|
||||
#include "DS_MAX7219.h"
|
||||
|
||||
void DS_MAX7219_Init(DS_MAX7219* Display, SPI_HandleTypeDef* SPI, GPIO_TypeDef* PORT_CS, uint16_t Pin_CS)
|
||||
{
|
||||
HAL_Delay(500);
|
||||
Display->SPI = SPI;
|
||||
Display->Intensivity = 8;
|
||||
Display->ScanLimit = 7;
|
||||
Display->DecodeMode = 0;
|
||||
Display->EnableDecodeMode = true;
|
||||
Display->PORT_CS = PORT_CS;
|
||||
Display->Pin_CS = Pin_CS;
|
||||
|
||||
|
||||
Display->DisplaData[0]=
|
||||
Display->DisplaData[1]=
|
||||
Display->DisplaData[2]=
|
||||
Display->DisplaData[3]=
|
||||
Display->DisplaData[4]=
|
||||
Display->DisplaData[5]=
|
||||
Display->DisplaData[6]=
|
||||
Display->DisplaData[7]=' ';
|
||||
|
||||
|
||||
|
||||
__DS_MAX7219_SendConfigData(Display);
|
||||
DS_MAX7219_UpdateDisplay(Display);
|
||||
}
|
||||
|
||||
|
||||
void DS_MAX7219_Print(DS_MAX7219* Display, int64_t Number)
|
||||
{
|
||||
if(Number<0)
|
||||
{
|
||||
Display->DisplaData[0] = '-';
|
||||
Number = Number * -1;
|
||||
}
|
||||
else
|
||||
Display->DisplaData[0] = ' ';
|
||||
|
||||
|
||||
uint8_t t = Number/1000000;
|
||||
|
||||
if(t==0)
|
||||
Display->DisplaData[1] = ' ';
|
||||
else if(t<10)
|
||||
{
|
||||
Display->DisplaData[1] = t;
|
||||
Number = Number - t*1000000;
|
||||
}
|
||||
else
|
||||
{
|
||||
Display->DisplaData[0]=
|
||||
Display->DisplaData[1]=
|
||||
Display->DisplaData[2]=
|
||||
Display->DisplaData[3]=
|
||||
Display->DisplaData[4]=
|
||||
Display->DisplaData[5]=
|
||||
Display->DisplaData[6]=
|
||||
Display->DisplaData[7]='9';
|
||||
}
|
||||
|
||||
|
||||
t = Number / 100000;
|
||||
if(t==0)
|
||||
Display->DisplaData[2] = ' ';
|
||||
else
|
||||
{
|
||||
Display->DisplaData[2] = t;
|
||||
Number = Number - t*100000;
|
||||
}
|
||||
|
||||
t = Number / 10000;
|
||||
if(t==0)
|
||||
Display->DisplaData[3] = ' ';
|
||||
else
|
||||
{
|
||||
Display->DisplaData[3] = t;
|
||||
Number = Number - t*10000;
|
||||
}
|
||||
|
||||
t = Number / 1000;
|
||||
if(t==0)
|
||||
Display->DisplaData[4] = ' ';
|
||||
else
|
||||
{
|
||||
Display->DisplaData[4] = t;
|
||||
Number = Number - t*1000;
|
||||
}
|
||||
|
||||
t = Number / 100;
|
||||
if(t==0)
|
||||
Display->DisplaData[5] = ' ';
|
||||
else
|
||||
{
|
||||
Display->DisplaData[5] = t;
|
||||
Number = Number - t*100;
|
||||
}
|
||||
|
||||
t = Number / 10;
|
||||
if(t==0)
|
||||
Display->DisplaData[6] = ' ';
|
||||
else
|
||||
{
|
||||
Display->DisplaData[6] = t;
|
||||
Number = Number - t*10;
|
||||
}
|
||||
|
||||
Display->DisplaData[7] = Number;
|
||||
|
||||
}
|
||||
|
||||
void DS_MAX7219_UpdateDisplay(DS_MAX7219* Display)
|
||||
{
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
HAL_GPIO_WritePin(Display->PORT_CS, Display->Pin_CS, GPIO_PIN_RESET);
|
||||
uint16_t Data = (i+1<<8) + __DS_MAX7219_ConvertSymbol(Display->DisplaData[7-i]);
|
||||
HAL_SPI_Transmit(Display->SPI, (uint8_t*) &Data, 1 , 100);
|
||||
HAL_GPIO_WritePin(Display->PORT_CS, Display->Pin_CS, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void DS_MAX7219_ConfigureDisplay(DS_MAX7219* Display, uint8_t DecodeMode, uint8_t Intensivity, uint8_t ScanLimit)
|
||||
{
|
||||
Display->DecodeMode = DecodeMode;
|
||||
|
||||
if(Intensivity>0xF)
|
||||
Display->Intensivity = 0xF;
|
||||
else
|
||||
Display->Intensivity = Intensivity;
|
||||
|
||||
if(ScanLimit>0x7)
|
||||
Display->ScanLimit = 0x7;
|
||||
else
|
||||
Display->ScanLimit = ScanLimit;
|
||||
|
||||
|
||||
__DS_MAX7219_SendConfigData(Display);
|
||||
}
|
||||
|
||||
void __DS_MAX7219_SendConfigData(DS_MAX7219* Display)
|
||||
{
|
||||
uint16_t Data[5];
|
||||
//0 - DecodeMode 0x9;
|
||||
//1 - Intensivity 0xA;
|
||||
//2 - ScanLimit 0xB;
|
||||
//3 - Shutdown mode 0xC;
|
||||
//4 - Display Test 0xF;
|
||||
|
||||
if(Display->EnableDecodeMode)
|
||||
Data[0] = 0x9FF;
|
||||
else
|
||||
Data[0] = 0x900;
|
||||
|
||||
Data[1] = 0xA00 + Display->Intensivity;
|
||||
Data[2] = 0xB00 + Display->ScanLimit;
|
||||
Data[3] = 0xC00 + 1;
|
||||
Data[4] = 0xF00 + 0;
|
||||
|
||||
for (uint8_t i = 0; i < 5; i++)
|
||||
{
|
||||
HAL_GPIO_WritePin(Display->PORT_CS, Display->Pin_CS, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(Display->SPI, (uint8_t*) &Data[i], 1, 100);
|
||||
HAL_GPIO_WritePin(Display->PORT_CS, Display->Pin_CS, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
uint8_t __DS_MAX7219_ConvertSymbol(char Symbol)
|
||||
{
|
||||
switch (Symbol)
|
||||
{
|
||||
// case '0':
|
||||
// return 0;
|
||||
// break;
|
||||
// case '1':
|
||||
// return 1;
|
||||
// break;
|
||||
// case '2':
|
||||
// return 3;
|
||||
// break;
|
||||
// case '3':
|
||||
// return 3;
|
||||
// break;
|
||||
// case '4':
|
||||
// return 4;
|
||||
// break;
|
||||
// case '5':
|
||||
// return 5;
|
||||
// break;
|
||||
// case '6':
|
||||
// return 6;
|
||||
// break;
|
||||
// case '7':
|
||||
// return 7;
|
||||
// break;
|
||||
// case '8':
|
||||
// return 8;
|
||||
// break;
|
||||
// case '9':
|
||||
// return 9;
|
||||
// break;
|
||||
case '-':
|
||||
return 10;
|
||||
break;
|
||||
case 'E':
|
||||
return 11;
|
||||
break;
|
||||
case 'H':
|
||||
return 12;
|
||||
break;
|
||||
case 'L':
|
||||
return 13;
|
||||
break;
|
||||
case 'P':
|
||||
return 14;
|
||||
break;
|
||||
case ' ':
|
||||
return 15;
|
||||
break;
|
||||
|
||||
default:
|
||||
break;
|
||||
}
|
||||
|
||||
return Symbol;
|
||||
}
|
||||
110
Core/Src/main.c
110
Core/Src/main.c
@@ -21,7 +21,8 @@
|
||||
|
||||
/* Private includes ----------------------------------------------------------*/
|
||||
/* USER CODE BEGIN Includes */
|
||||
|
||||
#include "DS_MAX7219.h"
|
||||
#include "DS_Button.h"
|
||||
/* USER CODE END Includes */
|
||||
|
||||
/* Private typedef -----------------------------------------------------------*/
|
||||
@@ -91,62 +92,14 @@ int main(void)
|
||||
MX_SPI1_Init();
|
||||
/* USER CODE BEGIN 2 */
|
||||
|
||||
DS_MAX7219 Display;
|
||||
DS_MAX7219_Init(&Display, &hspi1, CS_GPIO_Port, CS_Pin);
|
||||
|
||||
DS_Button ButtonUP, ButtonDown;
|
||||
DS_ButtonInit(&ButtonUP, Button1_GPIO_Port, Button1_Pin);
|
||||
DS_ButtonInit(&ButtonDown, Button2_GPIO_Port, Button2_Pin);
|
||||
|
||||
//регистр данные шеснадцатеричное представление регистра
|
||||
//0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 0xC
|
||||
//0 0 0 0 1 1 1 1 0 0 0 0 0 0 0 0 1 0хF
|
||||
|
||||
|
||||
|
||||
//Алгоритм
|
||||
//1. вывести микросхему из режима выключения
|
||||
//2. заставить выводить все 8 цифр
|
||||
//3. Установить яркость на половину
|
||||
//4. Во все регистры цифр записать данные. Цифры от 1 до 8
|
||||
|
||||
// 0x0C + 0x01
|
||||
// 0x0B + 0x07
|
||||
// 0x0A + 0x08
|
||||
// регистры 1-8
|
||||
|
||||
uint16_t Data[1];
|
||||
|
||||
Data[0] = 0xC01;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
|
||||
Data[0] = 0xF00;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
Data[0] = 0xB07;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
Data[0] = 0xA08;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
Data[0] = 0x9FF;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
|
||||
for (uint8_t i = 0; i < 8; i++)
|
||||
{
|
||||
Data[0] = 0x100 + (i<<8) + i;
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_RESET);
|
||||
HAL_SPI_Transmit(&hspi1, (uint8_t*)Data, 1, 100);
|
||||
HAL_GPIO_WritePin(CS_GPIO_Port, CS_Pin, GPIO_PIN_SET);
|
||||
}
|
||||
|
||||
|
||||
int64_t DisplayData = 0;
|
||||
|
||||
/* USER CODE END 2 */
|
||||
|
||||
@@ -154,6 +107,53 @@ int main(void)
|
||||
/* USER CODE BEGIN WHILE */
|
||||
while (1)
|
||||
{
|
||||
|
||||
DS_ButtonUpdate(&ButtonUP);
|
||||
DS_ButtonUpdate(&ButtonDown);
|
||||
|
||||
|
||||
if(DS_ButtonRisingEdge(&ButtonUP))
|
||||
{
|
||||
DisplayData++;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
if(DS_ButtonRisingEdge(&ButtonDown))
|
||||
{
|
||||
DisplayData--;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
if(DS_Button_PressedLong(&ButtonUP))
|
||||
{
|
||||
DisplayData = DisplayData + 10;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
if(DS_Button_PressedLong(&ButtonDown))
|
||||
{
|
||||
DisplayData = DisplayData - 10;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
if(DS_Button_PressedLongLong(&ButtonUP))
|
||||
{
|
||||
DisplayData = DisplayData + 100;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
if(DS_Button_PressedLongLong(&ButtonDown))
|
||||
{
|
||||
DisplayData = DisplayData - 100;
|
||||
DS_MAX7219_Print(&Display, DisplayData);
|
||||
DS_MAX7219_UpdateDisplay(&Display);
|
||||
}
|
||||
|
||||
/* USER CODE END WHILE */
|
||||
|
||||
/* USER CODE BEGIN 3 */
|
||||
|
||||
Reference in New Issue
Block a user