// STM32 Nucleo-L031K6 HAL Blink + printf() example
// Simulation: https://wokwi.com/projects/367244067477216257
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stm32l0xx_hal.h>
#define delay 1
#define delayc 1
//#define delayseg 10
// ST Nucleo Green user LED (PB3)
#define LED_PORT GPIOB
#define LED_PORT1 GPIOA
#define LED_PIN GPIO_PIN_3
#define LED_PIN1 GPIO_PIN_4
#define LED_PIN2 GPIO_PIN_5
#define LED_PIN3 GPIO_PIN_11
#define LED_PIN4 GPIO_PIN_8
#define LED_PIN5 GPIO_PIN_1
#define LED_PIN6 GPIO_PIN_6
//////////////////////////////////////////////////
#define LED_PIN7 GPIO_PIN_0 ////D3
#define LED_PIN8 GPIO_PIN_7 ////D4
#define LED_PIN9 GPIO_PIN_12 ////D2
#define LED_PIN10 GPIO_PIN_0 //////anlog a0 port a for output for dig1
#define LED_PORT_CLK_ENABLE __HAL_RCC_GPIOB_CLK_ENABLE
#define LED_PORT1_CLK_ENABLE __HAL_RCC_GPIOA_CLK_ENABLE
#define VCP_TX_Pin GPIO_PIN_2
#define VCP_RX_Pin GPIO_PIN_15
int i,dig1,dig2,dig3,dig4;
uint32_t previousMillis,previousMillis1;
#define interval 300
UART_HandleTypeDef huart2;
void SystemClock_Config(void);
static void MX_USART2_UART_Init(void);
static void MX_TIM2_Init(void);
TIM_HandleTypeDef htim2;
void splitmath(){
dig1 = i/1000;
dig2 = ((i/100)%10);
dig3 = ((i/10)%10);
dig4 = (i%10);
}
void osSystickHandler(void)
{
}
void initGPIO()
{
GPIO_InitTypeDef GPIO_Config;
LED_PORT_CLK_ENABLE();
LED_PORT1_CLK_ENABLE();
///////////////////////////////////////////// port B
GPIO_Config.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_Config.Pull = GPIO_NOPULL;
GPIO_Config.Speed = GPIO_SPEED_FREQ_HIGH;
GPIO_Config.Pin = LED_PIN | LED_PIN1 | LED_PIN2 | LED_PIN5
| LED_PIN6 | LED_PIN7 | LED_PIN7 | LED_PIN8;
HAL_GPIO_Init(LED_PORT, &GPIO_Config);
///////////////////////////////////////////// port A
GPIO_Config.Pin = LED_PIN3 | LED_PIN4 |LED_PIN9 | LED_PIN10;
HAL_GPIO_Init(LED_PORT1, &GPIO_Config);
//////////////////////////////////////////// input config
}
int segmentNumber[10] = {
0x3f, // formate for 7 segnment send in hex from bin
0x06,
0x5b,
0x4f,
0x66,
0x6d,
0x7d,
0x07,
0x7f,
0x67
};
void Sevensegment(int i){
HAL_GPIO_WritePin(LED_PORT, LED_PIN, ((i>>0)&0x01));
HAL_GPIO_WritePin(LED_PORT, LED_PIN1, ((i>>1)&0x01));
HAL_GPIO_WritePin(LED_PORT, LED_PIN2, ((i>>2)&0x01));
HAL_GPIO_WritePin(LED_PORT1, LED_PIN3, ((i>>3)&0x01));
HAL_GPIO_WritePin(LED_PORT1, LED_PIN4, ((i>>4)&0x01));
HAL_GPIO_WritePin(LED_PORT, LED_PIN6, ((i>>5)&0x01));
HAL_GPIO_WritePin(LED_PORT, LED_PIN5, ((i>>6)&0x01));
}
int main(void)
{
HAL_Init();
SystemClock_Config();
initGPIO();
MX_USART2_UART_Init();
MX_TIM2_Init();
// printf("Hello, %s!\n", "world");
//HAL_GPIO_TogglePin(LED_PORT, LED_PIN);
while (1){
if (HAL_GetTick()- previousMillis >= interval){
// save the last time you blinked the LED
// printf("millis :%d\n",HAL_GetTick());
previousMillis = HAL_GetTick();
if(i>=0){
i++;
}
else{
i--;
}
if(i==9999){
i=0;
}
}
dig1 = i/1000;
dig2 = ((i/100)%10);
dig3 = ((i/10)%10);
dig4 = (i%10);
if (HAL_GetTick()- previousMillis1 >= interval){
printf("Number: %d\n", i);
previousMillis1 = HAL_GetTick();
}
Sevensegment(segmentNumber[dig1]);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN10, GPIO_PIN_RESET);///a0
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN10, GPIO_PIN_SET);//a0
Sevensegment(segmentNumber[dig2]);
HAL_GPIO_WritePin(LED_PORT, LED_PIN8, GPIO_PIN_RESET);////d4
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT, LED_PIN8, GPIO_PIN_SET);////d4
Sevensegment(segmentNumber[dig3]);
HAL_GPIO_WritePin(LED_PORT, LED_PIN7, GPIO_PIN_RESET);///d3
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT, LED_PIN7, GPIO_PIN_SET);///d3
Sevensegment(segmentNumber[dig4]);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN9, GPIO_PIN_RESET);//d2
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN9, GPIO_PIN_SET);//d2
//}
/*
//for(i=98;i>=1;i--){
//HAL_Delay(300);
dig1 = i/1000;
dig2 = ((i/100)%10);
dig3 = ((i/10)%10);
dig4 = (i%10);
printf("R Number: %d\n", i);
Sevensegment(segmentNumber[dig1]);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN10, GPIO_PIN_RESET);///a0
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN10, GPIO_PIN_SET);//a0
Sevensegment(segmentNumber[dig2]);
HAL_GPIO_WritePin(LED_PORT, LED_PIN8, GPIO_PIN_RESET);////d4
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT, LED_PIN8, GPIO_PIN_SET);////d4
Sevensegment(segmentNumber[dig3]);
HAL_GPIO_WritePin(LED_PORT, LED_PIN7, GPIO_PIN_RESET);///d3
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT, LED_PIN7, GPIO_PIN_SET);///d3
Sevensegment(segmentNumber[dig4]);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN9, GPIO_PIN_RESET);//d2
HAL_Delay(delay);
HAL_GPIO_WritePin(LED_PORT1, LED_PIN9, GPIO_PIN_SET);//d2
*/
}
return 0;
}
/*
void writes(int a,int b,int c,int d){
HAL_GPIO_WritePin(LED_PORT1,LED_PIN10,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN7,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN8,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN9,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN1,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN2,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN3,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN4,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN6,GPIO_PIN_SET);
HAL_Delay(delay);
///////////abcdefg off
HAL_GPIO_WritePin(LED_PORT,LED_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN1,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN2,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN3,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN4,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN5,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN6,GPIO_PIN_RESET);
}
*/
void segnment_NO_OFF(void){
HAL_GPIO_WritePin(LED_PORT,LED_PIN7,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN8,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN9,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN1,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN2,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN3,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN4,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN5,GPIO_PIN_SET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN6,GPIO_PIN_SET);
// HAL_Delay(delay);
segnment_OFF();
}
void segnment_OFF(void){
HAL_GPIO_WritePin(LED_PORT,LED_PIN7,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN8,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN9,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN10,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN1,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN2,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN3,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT1,LED_PIN4,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN5,GPIO_PIN_RESET);
HAL_GPIO_WritePin(LED_PORT,LED_PIN6,GPIO_PIN_RESET);
}
void segnment(int i){
switch(i){
case 0:
zero();
zero_DIGI2();
break;
case 1:
one();
break;
case 2:
two();
two_DIG2();
break;
case 3:
three();
three_DIGI2();
break;
case 4:
four();
four_DIGI2();
break;
case 5:
five();
five_DIGI2();
break;
case 6:
six();
six_DIGI2();
break;
case 7:
seven();
seven_DIGI2();
break;
case 8:
eight();
eight_DIGI2();
break;
case 9:
nine();
nine_DIGI2();
break;
default:
segnment_OFF();
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** 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.PLLMUL = RCC_PLLMUL_4;
RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
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_PLLCLK;
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_1) != HAL_OK)
{
Error_Handler();
}
PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
{
Error_Handler();
}
}
/**
@brief USART2 Initialization Function
@param None
@retval None
*/
static void MX_USART2_UART_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
/**USART2 GPIO Configuration
PA2 ------> USART2_TX
PA15 ------> USART2_RX
*/
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = VCP_TX_Pin | VCP_RX_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.Alternate = GPIO_AF4_USART2;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
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;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
Error_Handler();
}
__HAL_RCC_USART2_CLK_ENABLE();
}
void Error_Handler(void)
{
/* User can add his own implementation to report the HAL error return state */
}
// The following makes printf() write to USART2:
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
int _write(int file, uint8_t *ptr, int len)
{
switch (file)
{
case STDOUT_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
case STDERR_FILENO:
HAL_UART_Transmit(&huart2, ptr, len, HAL_MAX_DELAY);
break;
default:
return -1;
}
return len;
}
Loading
st-nucleo-l031k6
st-nucleo-l031k6