Написал библиотеку к 7_сегментному индикатору которая имеет функцию плавного мигания. Задействовал при этом TIM1.
Для счета импульсов задействовал энкодер на TIM2.
сама по себе она работает пока не считываю значение encoder_counter = TIM2->CNT; .
в прерывании любого таймера . поле этого мигание дисплея пропадает...
коментиш или кидаеш строчку опроса в main.c все работает ...
помогите пожалуйста ... немогу понять почему так ...
можно ли в прерывании одного таймера обращаться к другому таймеру ?
Заранее благодарен за любую подсказку )
ссылка на проект https://fex.net/#!023745203742
stm32f1xx_it.c
Код: Выделить всё
/**
******************************************************************************
* @file stm32f1xx_it.c
* @brief Interrupt Service Routines.
******************************************************************************
*
* COPYRIGHT(c) 2017 STMicroelectronics
*
* Redistribution and use in source and binary forms, with or without modification,
* are permitted provided that the following conditions are met:
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. Neither the name of STMicroelectronics nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "stm32f1xx_hal.h"
#include "stm32f1xx.h"
#include "stm32f1xx_it.h"
#include "display_7seg.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
/* External variables --------------------------------------------------------*/
extern TIM_HandleTypeDef htim1;
extern TIM_HandleTypeDef htim2;
extern TIM_HandleTypeDef htim4;
extern uint8_t digit_counter;
extern int encoder_counter;
extern uint8_t digit_on_period;
uint8_t statt;
uint8_t flash_rescaller ;
extern uint8_t Digit_mask_State;
/******************************************************************************/
/* Cortex-M3 Processor Interruption and Exception Handlers */
/******************************************************************************/
/**
* @brief This function handles Non maskable interrupt.
*/
void NMI_Handler(void)
{
/* USER CODE BEGIN NonMaskableInt_IRQn 0 */
/* USER CODE END NonMaskableInt_IRQn 0 */
/* USER CODE BEGIN NonMaskableInt_IRQn 1 */
/* USER CODE END NonMaskableInt_IRQn 1 */
}
/**
* @brief This function handles Hard fault interrupt.
*/
void HardFault_Handler(void)
{
/* USER CODE BEGIN HardFault_IRQn 0 */
/* USER CODE END HardFault_IRQn 0 */
while (1)
{
}
/* USER CODE BEGIN HardFault_IRQn 1 */
/* USER CODE END HardFault_IRQn 1 */
}
/**
* @brief This function handles Memory management fault.
*/
void MemManage_Handler(void)
{
/* USER CODE BEGIN MemoryManagement_IRQn 0 */
/* USER CODE END MemoryManagement_IRQn 0 */
while (1)
{
}
/* USER CODE BEGIN MemoryManagement_IRQn 1 */
/* USER CODE END MemoryManagement_IRQn 1 */
}
/**
* @brief This function handles Prefetch fault, memory access fault.
*/
void BusFault_Handler(void)
{
/* USER CODE BEGIN BusFault_IRQn 0 */
/* USER CODE END BusFault_IRQn 0 */
while (1)
{
}
/* USER CODE BEGIN BusFault_IRQn 1 */
/* USER CODE END BusFault_IRQn 1 */
}
/**
* @brief This function handles Undefined instruction or illegal state.
*/
void UsageFault_Handler(void)
{
/* USER CODE BEGIN UsageFault_IRQn 0 */
/* USER CODE END UsageFault_IRQn 0 */
while (1)
{
}
/* USER CODE BEGIN UsageFault_IRQn 1 */
/* USER CODE END UsageFault_IRQn 1 */
}
/**
* @brief This function handles System service call via SWI instruction.
*/
void SVC_Handler(void)
{
/* USER CODE BEGIN SVCall_IRQn 0 */
/* USER CODE END SVCall_IRQn 0 */
/* USER CODE BEGIN SVCall_IRQn 1 */
/* USER CODE END SVCall_IRQn 1 */
}
/**
* @brief This function handles Debug monitor.
*/
void DebugMon_Handler(void)
{
/* USER CODE BEGIN DebugMonitor_IRQn 0 */
/* USER CODE END DebugMonitor_IRQn 0 */
/* USER CODE BEGIN DebugMonitor_IRQn 1 */
/* USER CODE END DebugMonitor_IRQn 1 */
}
/**
* @brief This function handles Pendable request for system service.
*/
void PendSV_Handler(void)
{
/* USER CODE BEGIN PendSV_IRQn 0 */
/* USER CODE END PendSV_IRQn 0 */
/* USER CODE BEGIN PendSV_IRQn 1 */
/* USER CODE END PendSV_IRQn 1 */
}
/**
* @brief This function handles System tick timer.
*/
void SysTick_Handler(void)
{
/* USER CODE BEGIN SysTick_IRQn 0 */
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
HAL_SYSTICK_IRQHandler();
/* USER CODE BEGIN SysTick_IRQn 1 */
/* USER CODE END SysTick_IRQn 1 */
}
/******************************************************************************/
/* STM32F1xx Peripheral Interrupt Handlers */
/* Add here the Interrupt Handlers for the used peripherals. */
/* For the available peripheral interrupt handler names, */
/* please refer to the startup file (startup_stm32f1xx.s). */
/******************************************************************************/
/**
* @brief This function handles RCC global interrupt.
*/
void RCC_IRQHandler(void)
{
/* USER CODE BEGIN RCC_IRQn 0 */
/* USER CODE END RCC_IRQn 0 */
/* USER CODE BEGIN RCC_IRQn 1 */
/* USER CODE END RCC_IRQn 1 */
}
/**
* @brief This function handles EXTI line2 interrupt.
*/
void EXTI2_IRQHandler(void)
{
/* USER CODE BEGIN EXTI2_IRQn 0 */
/* USER CODE END EXTI2_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_2);
/* USER CODE BEGIN EXTI2_IRQn 1 */
/* USER CODE END EXTI2_IRQn 1 */
}
/**
* @brief This function handles EXTI line3 interrupt.
*/
void EXTI3_IRQHandler(void)
{
/* USER CODE BEGIN EXTI3_IRQn 0 */
/* USER CODE END EXTI3_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
/* USER CODE BEGIN EXTI3_IRQn 1 */
/* USER CODE END EXTI3_IRQn 1 */
}
/**
* @brief This function handles EXTI line4 interrupt.
*/
void EXTI4_IRQHandler(void)
{
/* USER CODE BEGIN EXTI4_IRQn 0 */
/* USER CODE END EXTI4_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_4);
/* USER CODE BEGIN EXTI4_IRQn 1 */
/* USER CODE END EXTI4_IRQn 1 */
}
/**
* @brief This function handles EXTI line[9:5] interrupts.
*/
void EXTI9_5_IRQHandler(void)
{
/* USER CODE BEGIN EXTI9_5_IRQn 0 */
/* USER CODE END EXTI9_5_IRQn 0 */
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_5);
HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
/* USER CODE BEGIN EXTI9_5_IRQn 1 */
/* USER CODE END EXTI9_5_IRQn 1 */
}
/**
* @brief This function handles TIM1 update interrupt.
*/
void TIM1_UP_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_UP_IRQn 0 */
flash_rescaller++;
if (flash_rescaller>3)
{
TIM1->CCR1 = digit_on_period;
if (statt == 1)
{
digit_on_period++;
if(digit_on_period>99)
{
statt=0;
}
}
else
{
digit_on_period--;
if(digit_on_period<10)
{
statt=1;
}
};
flash_rescaller = 0;
}
write_digit_led_buffer (digit_counter);
digit_counter = caunter_digit (digit_counter);
/* USER CODE END TIM1_UP_IRQn 0 */
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_UP_IRQn 1 */
/* USER CODE END TIM1_UP_IRQn 1 */
}
/**
* @brief This function handles TIM1 capture compare interrupt.
*/
void TIM1_CC_IRQHandler(void)
{
/* USER CODE BEGIN TIM1_CC_IRQn 0 */
flasing_light_display (Digit_mask_State);
/* USER CODE END TIM1_CC_IRQn 0 */
HAL_TIM_IRQHandler(&htim1);
/* USER CODE BEGIN TIM1_CC_IRQn 1 */
/* USER CODE END TIM1_CC_IRQn 1 */
}
/**
* @brief This function handles TIM2 global interrupt.
*/
void TIM2_IRQHandler(void)
{
/* USER CODE BEGIN TIM2_IRQn 0 */
/* USER CODE END TIM2_IRQn 0 */
HAL_TIM_IRQHandler(&htim2);
/* USER CODE BEGIN TIM2_IRQn 1 */
/* USER CODE END TIM2_IRQn 1 */
}
/**
* @brief This function handles TIM4 global interrupt.
*/
void TIM4_IRQHandler(void)
{
/* USER CODE BEGIN TIM4_IRQn 0 */
encoder_counter = TIM2->CNT; // WTF!???!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
digit_decodder_number (encoder_counter);
/* USER CODE END TIM4_IRQn 0 */
HAL_TIM_IRQHandler(&htim4);
/* USER CODE BEGIN TIM4_IRQn 1 */
/* USER CODE END TIM4_IRQn 1 */
}
/* *********************** (C) COPYRIGHT STMicroelectronics *****END OF FILE*** */
}
isplay_7seg.h
Код: Выделить всё
/**
******************************************************************************
* File Name : display_7seg.h
* Description : This file contains the common defines of the application
******************************************************************************
*/
#ifndef __DISPLAY_7SEG_H
#define __DISPLAY_7SEG_H
#include "stm32f1xx_hal.h"
typedef enum
{
ALL = (uint8_t)0x3F,
X_1 = (uint8_t)0x02,
X_2 = (uint8_t)0x04,
X_3 = (uint8_t)0x08,
X_4 = (uint8_t)0x10,
X_5 = (uint8_t)0x01,
X_6 = (uint8_t)0x20,
NON = (uint8_t)0x00
}Digit_mask_TypeDef;
/*****************************************************************
void TIM1_UP_IRQHandler(void)
{
write_digit_led_buffer (digit_counter);
digit_counter = caunter_digit (digit_counter);
HAL_TIM_IRQHandler(&htim1);
}
*****************************************************************
void TIM1_CC_IRQHandler(void)
{
flasing_light_display (Digit_mask_State); ->> Digit_mask_TypeDef;
HAL_TIM_IRQHandler(&htim1);
}
*****************************************************************
TIM1->CCR1 = digit_on_period; luminosity display 0...100
счетчик плавного мигания
void TIM4_IRQHandler(void) //1000interups/1sec
{
flash_rescaller++;
if (flash_rescaller>3)
{
TIM1->CCR1 = digit_on_period;
if (statt == 1)
{
digit_on_period++;
if(digit_on_period>99)
{
statt=0;
}
}
else
{
digit_on_period--;
if(digit_on_period<10)
{
statt=1;
}
};
flash_rescaller = 0;
}
HAL_TIM_IRQHandler(&htim4);
}
******************************************************************
*/
void segmen_decodder_write (uint8_t x);
void segmen_clear (void);
void off_comon_catode (Digit_mask_TypeDef State );
void write_digit_led_buffer(uint8_t N_digit);
void digit_decodder_number (uint16_t x);
int caunter_digit ( uint8_t counter);
void flasing_light_display (uint8_t State);
void write_flasing_light(Digit_mask_TypeDef State );
#endif /* __DISPLAY_7SEG_H */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
display_7seg.c
Код: Выделить всё
/*
******************************************************************************
* File Name : display_7seg.c
* Description : Main program body
******************************************************************************
*/
#include "stm32f1xx_hal.h"
#include "display_7seg.h"
uint8_t digits[] =
//0 1 2 3 4 5 6 7 8 9 A b C d E F G r - " y
{0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5E,0x79,0x71,0x3d,0x50,0x40,0x22,0x6e,0x00 };
//буфер для символов
uint8_t Led_Buffer[] ={0x01,0x02,0x03,0x04,0x05,0x06};
//буффер для вывода на екран
uint8_t digit_on_period = 0; //0....100
extern uint8_t digit_counter ;
uint8_t Digit_mask_State = 0 ;
void segmen_decodder_write (uint8_t x)
{
uint8_t temp_seg ;
temp_seg=digits[x] ;
// #define BitIsSet(reg, bit) ((reg & (1<<(bit))) != 0)
//пример: if(BitIsSet(PORTB,2)) {...}
//если бит установлен
HAL_GPIO_WritePin(SEG_A_GPIO_Port , SEG_A_Pin, ((digits[x] & (1<<(0))) != 0));
HAL_GPIO_WritePin(SEG_B_GPIO_Port , SEG_B_Pin, ((digits[x] & (1<<(1))) != 0));
HAL_GPIO_WritePin(SEG_C_GPIO_Port , SEG_C_Pin, ((digits[x] & (1<<(2))) != 0));
HAL_GPIO_WritePin(SEG_D_GPIO_Port , SEG_D_Pin, ((digits[x] & (1<<(3))) != 0));
HAL_GPIO_WritePin(SEG_E_GPIO_Port , SEG_E_Pin, ((digits[x] & (1<<(4))) != 0));
HAL_GPIO_WritePin(SEG_F_GPIO_Port , SEG_F_Pin, ((digits[x] & (1<<(5))) != 0));
HAL_GPIO_WritePin(SEG_G_GPIO_Port , SEG_G_Pin, ((digits[x] & (1<<(6))) != 0));
HAL_GPIO_WritePin(SEG_H_GPIO_Port , SEG_H_Pin, ((digits[x] & (1<<(7))) != 0));
}
void segmen_clear (void)
{
HAL_GPIO_WritePin(SEG_A_GPIO_Port , SEG_A_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_B_GPIO_Port , SEG_B_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_C_GPIO_Port , SEG_C_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_D_GPIO_Port , SEG_D_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_E_GPIO_Port , SEG_E_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_F_GPIO_Port , SEG_F_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_G_GPIO_Port , SEG_G_Pin, GPIO_PIN_RESET);
HAL_GPIO_WritePin(SEG_H_GPIO_Port , SEG_H_Pin, GPIO_PIN_RESET);
}
void flasing_light_display (uint8_t State )
{
if ((State & (1<<(digit_counter))) != 0)
{
segmen_clear ();
off_comon_catode(ALL);
}
}
void write_flasing_light(Digit_mask_TypeDef State )
{
Digit_mask_State = State;
}
void off_comon_catode(Digit_mask_TypeDef State )
{
HAL_GPIO_WritePin(X_1_GPIO_Port, X_1_Pin , ((State & (1<<(0))) == 0));
HAL_GPIO_WritePin(X_2_GPIO_Port, X_2_Pin , ((State & (1<<(1))) == 0));
HAL_GPIO_WritePin(X_3_GPIO_Port, X_3_Pin , ((State & (1<<(2))) == 0));
HAL_GPIO_WritePin(X_4_GPIO_Port, X_4_Pin , ((State & (1<<(3))) == 0));
HAL_GPIO_WritePin(X_5_GPIO_Port, X_5_Pin , ((State & (1<<(4))) == 0));
HAL_GPIO_WritePin(X_6_GPIO_Port, X_6_Pin , ((State & (1<<(5))) == 0));
}
void write_digit_led_buffer(uint8_t N_digit)
{
off_comon_catode (ALL);
uint8_t temp = N_digit;
switch (N_digit)
{
case 0:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_6_GPIO_Port, X_6_Pin , GPIO_PIN_SET);
break;
}
case 1:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_5_GPIO_Port, X_5_Pin , GPIO_PIN_SET);
break;
}
case 2:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_4_GPIO_Port, X_4_Pin , GPIO_PIN_SET);
break;
}
case 3:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_3_GPIO_Port, X_3_Pin , GPIO_PIN_SET);
break ;
}
case 4:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_2_GPIO_Port, X_2_Pin , GPIO_PIN_SET);
break;
}
case 5:
{
segmen_decodder_write (Led_Buffer[temp]);
HAL_GPIO_WritePin(X_1_GPIO_Port, X_1_Pin , GPIO_PIN_SET);
break;
}
default:
{
off_comon_catode (ALL);
break;
};
};
}
//функция которая считает розряды
int caunter_digit ( uint8_t counter)
{
if ( counter < 4 )
{
counter++;
}
else{counter=0;};
return counter;
}
void digit_decodder_number (uint16_t x)
{
uint16_t temp,res;
temp=x;
res=temp/10000; //Calculate 1000-s
Led_Buffer[0]= res;
temp=temp-res*10000;
res=temp/1000; //Calculate 1000-s
Led_Buffer[1]= res;
temp=temp-res*1000;
res=temp/100; //Calculate 100-s
Led_Buffer[2]=res;
temp=temp-res*100;
res=temp/10; //Calculaate 10-s
Led_Buffer[3]=res;
temp=temp-res*10;
Led_Buffer[4]=temp; //Calculate 1-s
}