/**
* @brief This function handles EXTI line0 interrupt.
*/
void EXTI0_IRQHandler(void)
{
  /* USER CODE BEGIN EXTI0_IRQn 0 */
  // Checks if the interrupt has been triggered by a rising edge( first 
  // pressing of button), starts a 2secs timer and make the line falling
  // edge sensitive.
  if ((EXTI-> RTSR) & (GPIO_PIN_0))
  {
    HAL_TIM_Base_Start_IT(&htim6);
    EXTI-> RTSR &= (~GPIO_PIN_0);
    EXTI-> FTSR |= GPIO_PIN_0;
  }
  else
  {
    // If a falling edge is detected( button is released) before the countdown
    // ends, we stop the countdown, reset it and make the line rising
    // edge sensitive again.
    HAL_TIM_Base_Stop_IT(&htim6);
    __HAL_TIM_SET_COUNTER(&htim6,0);
    EXTI-> FTSR &= (~GPIO_PIN_0);
    EXTI-> RTSR |= GPIO_PIN_0;
  }
  /* USER CODE END EXTI0_IRQn 0 */
  HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_0);
  /* USER CODE BEGIN EXTI0_IRQn 1 */

  /* USER CODE END EXTI0_IRQn 1 */
}
示例#2
0
/**
  * @brief  Period elapsed callback in non blocking mode
  * @param  htim : TIM handle
  * @retval None
  */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    __HAL_TIM_SET_COUNTER(htim, 0);

    switch (KB_state) {
    case BTN_Down:
        if ((len_KB_str == 0) || (pKB_str == NULL)) {
            HAL_TIM_Base_Stop_IT(htim);
            return;
        }
        KB_state = BTN_Up;

        char2KBID(*pKB_str);
        SEGGER_RTT_printf(0, "USB send char %c \r\n" , *pKB_str);
        pKB_str++;
        len_KB_str--;
        USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE);

        __HAL_TIM_SET_COUNTER(&TimHandle, 0);
        __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms
        HAL_TIM_Base_Start_IT(&TimHandle);
        break;

    case BTN_Up:
        memset(KB_USBBuf, 0, 9);
        KB_USBBuf[0] = 1;
        USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE);
        if (len_KB_str == 0) {
            pKB_str = NULL;
            HAL_TIM_Base_Stop_IT(htim);
        } else {
            KB_state = BTN_Down;
            __HAL_TIM_SET_COUNTER(&TimHandle, 0);
            __HAL_TIM_SET_AUTORELOAD(&TimHandle, 200-1); // 200ms
            HAL_TIM_Base_Start_IT(&TimHandle);
        }
        break;
    default:
        pKB_str = NULL;
        HAL_TIM_Base_Stop_IT(htim);
        break;
    }
}
示例#3
0
int32_t startRecv(uint32_t* pTempMeasureRslt){
//	GPIO_InitTypeDef GPIO_InitStruct;
	HAL_TIM_IC_Stop_DMA(&TH_RCV_DATA_TIMER_HANDLER, TIM_CHANNEL_1);
	__HAL_TIM_SET_COUNTER(&TH_RCV_DATA_TIMER_HANDLER, 0);
	if (HAL_TIM_IC_Start_DMA(&TH_RCV_DATA_TIMER_HANDLER, TIM_CHANNEL_1, pTempMeasureRslt, MAX_TH_MEASURE_BIT_NUM + 2) != HAL_OK){
		return (-1);
	}
	xSemaphoreTake(TH_tMeasureData_CommCpltHandle, portMAX_DELAY);
//	GPIO_InitStruct.Pin = AM2301_DATA_Pin;
//	GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
//	HAL_GPIO_Init(AM2301_DATA_GPIO_Port, &GPIO_InitStruct);
//	HAL_GPIO_WritePin(AM2301_DATA_GPIO_Port, AM2301_DATA_Pin, GPIO_PIN_SET);
	return 0;
}
示例#4
0
void USB_KB_type(const char *str, uint8_t len) {
	if (pKB_str != NULL) {
		return;
	}
	if (len == 0) {
        return;
	}

	strcpy(KB_strBuf, str);
	KB_state = BTN_Up;
	pKB_str = KB_strBuf;

	char2KBID(*pKB_str++);
	len_KB_str = len - 1;
	USBD_HID_SendReport(&USBD_Device, KB_USBBuf, HID_KB_SIZE);

	__HAL_TIM_SET_COUNTER(&TimHandle, 0);
	__HAL_TIM_SET_AUTORELOAD(&TimHandle, 50-1); // 50ms
	HAL_TIM_Base_Start_IT(&TimHandle);
}
示例#5
0
/**
  * @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 */

}
示例#6
0
/**
  * @brief  SYSTICK callback.
  * @param  None
  * @retval None
  */
void HAL_SYSTICK_Callback(void)
{
    uint8_t *buf;
    uint16_t Temp_X, Temp_Y = 0x00;
    uint16_t NewARR_X, NewARR_Y = 0x00;

    if (DemoEnterCondition != 0x00)
    {
        buf = USBD_HID_GetPos();
        if((buf[1] != 0) ||(buf[2] != 0))
        {
            USBD_HID_SendReport (&hUSBDDevice,
                                 buf,
                                 4);
        }
        Counter ++;
        if (Counter == 10)
        {
            /* Reset Buffer used to get accelerometer values */
            Buffer[0] = 0;
            Buffer[1] = 0;

            /* Disable All TIM4 Capture Compare Channels */
            HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_1);
            HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_2);
            HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_3);
            HAL_TIM_PWM_Stop(&htim4, TIM_CHANNEL_4);

            /* Read Acceleration*/
            BSP_ACCELERO_GetXYZ(Buffer);

            /* Set X and Y positions */
            X_Offset = Buffer[0];
            Y_Offset = Buffer[1];

            /* Update New autoreload value in case of X or Y acceleration*/
            /* Basic acceleration X_Offset and Y_Offset are divide by 40 to fir with ARR range */
            NewARR_X = TIM_ARR - ABS(X_Offset/40);
            NewARR_Y = TIM_ARR - ABS(Y_Offset/40);

            /* Calculation of Max acceleration detected on X or Y axis */
            Temp_X = ABS(X_Offset/40);
            Temp_Y = ABS(Y_Offset/40);
            MaxAcceleration = MAX_AB(Temp_X, Temp_Y);

            if(MaxAcceleration != 0)
            {

                /* Reset CNT to a lowest value (equal to min CCRx of all Channels) */
                __HAL_TIM_SET_COUNTER(&htim4,(TIM_ARR-MaxAcceleration)/2);

                if (X_Offset < ThreadholdAcceleroLow)
                {

                    /* Sets the TIM4 Capture Compare for Channel1 Register value */
                    /* Equal to NewARR_X/2 to have duty cycle equal to 50% */
                    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, NewARR_X/2);

                    /* Time base configuration */
                    __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X);

                    /* Enable TIM4 Capture Compare Channel1 */
                    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);

                }
                else if (X_Offset > ThreadholdAcceleroHigh)
                {

                    /* Sets the TIM4 Capture Compare for Channel3 Register value */
                    /* Equal to NewARR_X/2 to have duty cycle equal to 50% */
                    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, NewARR_X/2);

                    /* Time base configuration */
                    __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_X);

                    /* Enable TIM4 Capture Compare Channel3 */
                    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_3);

                }
                if (Y_Offset > ThreadholdAcceleroHigh)
                {

                    /* Sets the TIM4 Capture Compare for Channel2 Register value */
                    /* Equal to NewARR_Y/2 to have duty cycle equal to 50% */
                    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2,NewARR_Y/2);

                    /* Time base configuration */
                    __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_Y);

                    /* Enable TIM4 Capture Compare Channel2 */
                    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_2);

                }
                else if (Y_Offset < ThreadholdAcceleroLow)
                {

                    /* Sets the TIM4 Capture Compare for Channel4 Register value */
                    /* Equal to NewARR_Y/2 to have duty cycle equal to 50% */
                    __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_4, NewARR_Y/2);

                    /* Time base configuration */
                    __HAL_TIM_SET_AUTORELOAD(&htim4, NewARR_Y);

                    /* Enable TIM4 Capture Compare Channel4 */
                    HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_4);

                }
            }
            Counter = 0x00;
        }
    }
}
示例#7
0
/** \brief 温度控制硬件初始化
 *
 * \return void
 */
void hw_TemperatureManageInit(void)
{
    ///ADC配置,ADC1,in10,in11,in12,in13
    GPIO_InitTypeDef    GPIO_InitStruct;
    TIM_OC_InitTypeDef  TIM_InitStruct;
    ADC_ChannelConfTypeDef sConfig;

    ADC_HandleStruct.Instance                       = ADC1;

    if (HAL_ADC_DeInit(&ADC_HandleStruct) != HAL_OK)
    {
        return;
    }

    ADC_HandleStruct.Init.ScanConvMode              = ADC_SCAN_ENABLE;
    ADC_HandleStruct.Init.ContinuousConvMode        = ENABLE;
    ADC_HandleStruct.Init.DiscontinuousConvMode     = DISABLE;
    ADC_HandleStruct.Init.DataAlign                 = ADC_DATAALIGN_RIGHT;
    ADC_HandleStruct.Init.ExternalTrigConv          = ADC_SOFTWARE_START;
    ADC_HandleStruct.Init.NbrOfConversion           = HEATER_COUNT;

    init_BeforeADC();
    HAL_ADC_Init(&ADC_HandleStruct);


    sConfig.Channel = ADC_CHANNEL_10;                        ///Channel_Extruder_0
    sConfig.Rank = 1;
    sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
    HAL_ADC_ConfigChannel(&ADC_HandleStruct, &sConfig);

#ifdef HEATBED_ENABLED
    sConfig.Channel = ADC_CHANNEL_11;                        ///Channel_bed
    sConfig.Rank = 2;
    sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
    HAL_ADC_ConfigChannel(&ADC_HandleStruct, &sConfig);
#endif
#ifdef EXTRUDER_2_ENABLED
    sConfig.Channel = ADC_CHANNEL_12;                        ///Channel_Extruder_1
    sConfig.Rank = 3;
    sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
    HAL_ADC_ConfigChannel(&ADC_HandleStruct, &sConfig);
#endif
#ifdef EXTRUDER_3_ENABLED
    sConfig.Channel = ADC_CHANNEL_13;                        ///Channel_Extruder_2
    sConfig.Rank = 4;
    sConfig.SamplingTime = ADC_SAMPLETIME_71CYCLES_5;
    HAL_ADC_ConfigChannel(&ADC_HandleStruct, &sConfig);
#endif
    HAL_ADCEx_Calibration_Start(&ADC_HandleStruct);

    ///ADC时钟配置  采样周期100ms
    __HAL_RCC_TIM6_CLK_ENABLE();
    TIM_ADC_HandleStruct.Instance           = TIM6;
    TIM_ADC_HandleStruct.Init.Period        = 1000 - 1;
    TIM_ADC_HandleStruct.Init.Prescaler     = (uint32_t) (SystemCoreClock/2/10000) - 1;
    TIM_ADC_HandleStruct.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    TIM_ADC_HandleStruct.Init.CounterMode   = TIM_COUNTERMODE_UP;
    HAL_TIM_Base_Init(&TIM_ADC_HandleStruct);
    __HAL_TIM_SET_COUNTER(&TIM_ADC_HandleStruct, 0);

    HAL_NVIC_SetPriority(TIM6_IRQn, 4, 0);
    HAL_NVIC_EnableIRQ(TIM6_IRQn);

    ///加热PWM配置  PB6,7,8,9
    __HAL_RCC_GPIOB_CLK_ENABLE();

    GPIO_InitStruct.Pin = GPIO_PIN_6;
#ifdef HEATBED_ENABLED
    GPIO_InitStruct.Pin |= GPIO_PIN_7;
#endif
#ifdef EXTRUDER_2_ENABLED
    GPIO_InitStruct.Pin |= GPIO_PIN_8;
#endif
#ifdef EXTRUDER_3_ENABLED
    GPIO_InitStruct.Pin |= GPIO_PIN_9;
#endif
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;             ///内部不做上下拉电阻
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

    ///PWM时钟配置
    __HAL_RCC_TIM4_CLK_ENABLE();
    TIM_PWM_HandleStruct.Instance = TIM4;
    TIM_PWM_HandleStruct.Init.Prescaler = (uint32_t) (SystemCoreClock/2/250000) - 1;
    TIM_PWM_HandleStruct.Init.Period = 254;         ///总共255
    TIM_PWM_HandleStruct.Init.ClockDivision = 0;
    TIM_PWM_HandleStruct.Init.CounterMode = TIM_COUNTERMODE_UP;
    HAL_TIM_PWM_Init(&TIM_PWM_HandleStruct);

    TIM_InitStruct.OCMode = TIM_OCMODE_PWM1;
    TIM_InitStruct.OCPolarity = TIM_OCPOLARITY_HIGH;
    TIM_InitStruct.OCFastMode = TIM_OCFAST_DISABLE;
    TIM_InitStruct.Pulse = 0;

    HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_1);
#ifdef HEATBED_ENABLED
    HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_2);
#endif
#ifdef EXTRUDER_2_ENABLED
    HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_3);
#endif
#ifdef EXTRUDER_3_ENABLED
    HAL_TIM_PWM_ConfigChannel(&TIM_PWM_HandleStruct, &TIM_InitStruct, TIM_CHANNEL_4);
#endif
}
示例#8
0
文件: motor.c 项目: kefalakis/STM32
void EncReset(TIM_HandleTypeDef *motor){
	__HAL_TIM_SET_COUNTER(motor,0);
}
/* 过零点触发中断 回调函数 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  
  if (GPIO_Pin != ZERO) 
    return;
  
  bool wave_is_rise;
  // 读Pin,判断是上升沿还是下降沿    
  if (HAL_GPIO_ReadPin(PORT_ZERO, ZERO) == GPIO_PIN_RESET) {
    wave_is_rise = true;
  }
  else {
    wave_is_rise = false;
  }
  
  
  // t 顺序: 1 3 4 6
  
  // 上升沿
  if (wave_is_rise) {
    t4 = __HAL_TIM_GET_COUNTER(&htim3);
    // 上升沿是否valid?
    //      if (!it_is_valid(t4_prev, t4)) {
    //          t4_prev = t4;
    //          r++;
    //          return;
    //      }
    //r++;
  }
  else {
    // 下降沿
    t6 = __HAL_TIM_GET_COUNTER(&htim3);
    // 下降沿是否valid?
    //      if (!it_is_valid(t6_prev, t6)) {
    //          t6_prev = t6;
    //          f++;
    //          return;
    //      }
    //f++;
  }
  
  if (wave_is_rise)  // 只在波谷计算,否则退出
    return;
  
  is_lower_blow = true;  // 设置波谷flag
  is_lower_exchange = true;
  is_lower_feed = true;
  
  /* 下降沿时计算下一个零点 */ 
  // t2
  if (t3 < t1)
    t2 = (0xffff + t1 + t3) / 2;
  else
    t2 = (t1 + t3) / 2;
  
  // t5
  if (t6 < t4)
    t5 = (0xffff + t4 + t6) / 2;
  else
    t5 = (t4 + t6) / 2;
  
  // 交流电源周期
  if (t5 < t2)
    T1 = 0xffff + t5 - t2;
  else
    T1 = t5 - t2;
  // 下一个零点
  if (t5 < t4)
    t7 = t5 + 0xffff - t4 + (T1 / 4);  /*  计算不对 ! */
  else
    t7 = t5 - t4 + (T1 / 4);
  
//  counter++;
//  if (counter >=299) {
//    //printf("t4:%d,t5:%d,t6:%d,t7:%d,T1:%d\r\n", t4,t5,t6,t7,T1);
//    printf("delay:%d\r\n", t7 + TOTAL_TIME - triac_cur.fan_smoke_delay * 100);
//    counter = 0;
//  }      
  
  // 更新数值
  t1 = t4;
  t2 = t5;
  t3 = t6;
  
  // 计数器复位
  __HAL_TIM_SET_COUNTER (&htim1, 0);
  __HAL_TIM_SET_COUNTER (&htim4, 0);
  
  // 检查Q_Triac队列,更新输出的控制状态
  if (Q_TriacHandle != NULL) {
    
    // 读取值
    osEvent evt = osMessageGet(Q_TriacHandle, 0);
    if (evt.status == osEventMessage) {        
      triac_cur = *((Triac *)evt.value.p);   
      
      // 更新送料duty
      on_count = triac_cur.feed_duty * 100;
      off_count = 1000 - on_count;  // 10s = 10000ms 10000 / 10 = 1000
      
      // 返还内存
      osStatus status = osPoolFree(pool_TriacHandle, evt.value.p);
      if (status != osOK)
        printf("Free Triac memory error:%x\r\n", status);
    }
  }
  
  // 设置period
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, t7 + TOTAL_TIME - triac_cur.fan_smoke_delay * 100);  // 80÷100×10×1000 = 80×100
  
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, t7 + TOTAL_TIME - triac_cur.fan_exchange_delay * 100);
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, t7);
  
  /* 输出: 条件判断  */
  
  HAL_StatusTypeDef status;
  
  //排烟通道
  if (triac_cur.fan_smoke_is_on) {
    status = HAL_TIM_OC_Start_IT(&htim4, TIM_CHANNEL_1);
    if (status != HAL_OK)
      printf("time4 c1 failed at zero:%x\r\n", status);
  }
  
  //循环通道
  if (triac_cur.fan_exchange_is_on) {
    status = HAL_TIM_OC_Start_IT(&htim4, TIM_CHANNEL_2);
    if (status != HAL_OK)
      printf("time4 c2 failed:%x\r\n", status);
  }
  
  //送料通道 ON计数 OFF计数
  if (triac_cur.feed_is_on) {
    // 送料
    if (running_feed) {
      if (on_cnt < on_count) {
        // 输出 送料: On        
        HAL_TIM_OC_Start_IT(&htim4, TIM_CHANNEL_3);
        on_cnt++;
      }
      else {
        on_cnt = 0;
        off_cnt = 0;
        running_feed = false;
        waiting_feed = true;
      }
      
    }
    // 不送料
    if (waiting_feed) {
      off_cnt ++;
      if (off_cnt >= off_count) {
        on_cnt = 0;
        off_cnt = 0;
        running_feed = true;
        waiting_feed = false;
        
      }
      
    }
  }
// 
}
示例#10
0
void startcounter(void){
	__HAL_TIM_SET_COUNTER(&htim1,0);
	HAL_TIM_Base_Start_IT(&htim1);
}
示例#11
0
/* 过零点触发中断 回调函数 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
  
  if (GPIO_Pin != ZERO) 
    return;
  
  bool rising_edge;
  // 读Pin,判断是上升沿还是下降沿    
  if (HAL_GPIO_ReadPin(PORT_ZERO, ZERO) == GPIO_PIN_SET) {
    rising_edge = true;
  }
  else {
    rising_edge = false;
  }
  
  // t 顺序: 1 3 4 6
  
  // 上升沿
  if (rising_edge) {
    t6 = __HAL_TIM_GET_COUNTER(&htim3);
  }
  else {
    // 下降沿
    t4 = __HAL_TIM_GET_COUNTER(&htim3);
  }

  if (!rising_edge)  // 只在波谷计算,否则退出
    return;
  
/*----------------------------------------------------------------------------*/
    
  is_lower_blow = true;  // 设置波谷flag
  is_lower_exchange = true;
  is_lower_feed = true;
  
  /* 下降沿时计算下一个零点 */ 
  // t1 t3 t4 t6
  if ((t1<t3) && (t3<t4) && (t4<t6)) {  
    t2 = (t1+t3) / 2; 
    t5 = (t4+t6) / 2;
  }
  else if(t3<t1) {
    t2 = (t1+t3+0xffff) / 2;
    t5 = (t4+t6)/2 + 0xffff;
  }
  else if(t4<t3) {
    t2 = (t1+t3) / 2;
    t5 = (t4+t6)/2 + 0xffff;
  }
  else if(t6<t4) {
    t2 = (t1+t3) / 2;
    t5 = (t4+t6+0xffff) / 2;
  }
  
    T1 = t5 - t2;
    
    // 下一个零点    
  if (t6<t5) {
    t7 = t5 + T1/4 - t6 -0xffff;
  }
  else {
    t7 = t5 + T1/4 - t6;
  }  

  //  debug
//  counter++;
//  if (counter >= 149) {
//    Zero *z;
//    z = (Zero*)osPoolAlloc(pool_ZeroHandle);
//    if (z!=NULL) {
//      z->t1 = t1;
//      z->t3 = t3;
//      z->t4 = t4;
//      z->t6 = t6;
//      z->t7 = t7;
//      z->T1 = T1;
//      osMessagePut(Q_ErrorHandle, (uint32_t)z, 0);
//    }
//    
//    counter = 0;
//  }  
//  
  
  // 更新数值
  t1 = t4;
  t3 = t6;
    
  /*--------------------------------------------------------------------------*/
  
  // 只处理下半波
  
  if (t7 == 0) {
    t7 = 220;
  }
  
  is_lower_blow = true;  // 设置波谷flag
  is_lower_exchange = true;
  is_lower_feed = true;
  
  //uint32_t compare = 0;
  
  // 检查Q_Triac队列,更新输出的控制状态
  if (Q_TriacHandle != NULL) {    
    // 读取值
    osEvent evt = osMessageGet(Q_TriacHandle, 0);
    if (evt.status == osEventMessage) {  
      Triac *p = (Triac *)evt.value.p;  
      
      triac_cur.fan_exchange_delay = p->fan_exchange_delay; 
      triac_cur.fan_exchange_is_on = p->fan_exchange_is_on;
      triac_cur.fan_smoke_is_on = p->fan_smoke_is_on;
      triac_cur.fan_smoke_power = p->fan_smoke_power;
      triac_cur.feed_by_manual = p->feed_by_manual;
      triac_cur.feed_duty = p->feed_duty;
      triac_cur.feed_full = p->feed_full;
      triac_cur.feed_is_on = p->feed_is_on;
      
      // 更新送料duty
      if (triac_cur.feed_duty != 0) {
        // 500×triac_cur.feed_duty÷100
        on_count = 5 * triac_cur.feed_duty;  
        off_count = 500 - on_count;  // 10s = 10000ms 10000 / 20 = 500
      }
       
      // 返还内存
      osStatus status = osPoolFree(pool_TriacHandle, evt.value.p);
      if (status != osOK)
        printf("Free Triac memory error:%x\r\n", status);
    }
  }
  
/*----------------------------------------------------------------------------*/
  
  
  // 设置period
  if (triac_cur.fan_smoke_power == 0) { 
    triac_cur.fan_smoke_power = 60;
  }
  
  // 最低延时2.5ms,最高延时6ms
  uint32_t delay = t7 + triac_cur.fan_smoke_power;  
  if (delay>8999) {
    delay = last_delay;    
  }
  else {
    last_delay = delay;
  }
  
  
    // 计数器复位
  __HAL_TIM_SET_COUNTER (&htim2, 0);
  __HAL_TIM_SET_COUNTER (&htim4, 0);
  
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, delay); 
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_2, t7+T1/4);
  __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_3, t7+T1/4);
  
  
  /* 输出: 条件判断  */  

  //排烟通道
  if (triac_cur.fan_smoke_is_on) {
     __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC1);
  }
    
  //循环通道
  if (triac_cur.fan_exchange_is_on) {
    __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC2);
  }
    
  //送料通道
  if ((triac_cur.feed_by_manual || triac_cur.feed_full) && (!triac_cur.feed_is_on)) {
    __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3);
  }
  //送料通道 ON计数 OFF计数
  if (triac_cur.feed_is_on && (!triac_cur.feed_by_manual)) {  // 0
    // 送料
    if (feed_run) {
      __HAL_TIM_ENABLE_IT(&htim4, TIM_IT_CC3);      
      count_feed ++;
      if (count_feed >= on_count) {        
        count_feed = 0;
        feed_run = false;
      }
      
    } 
    else {
      // 不送料
      count_feed ++;
      if (count_feed >= off_count) {
        count_feed = 0;
        feed_run = true;        
      }
    }  
  } // if 0
  
}
示例#12
0
文件: tim.c 项目: vpcola/stm32f4
void TIM4_ResetCount(void)
{
	__HAL_TIM_SET_COUNTER(&htim4, 0);
}