stm32.2

/* 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 "tim.h"
#include "usart.h"
#include "gpio.h"
#include "fsmc.h"

/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdlib.h"
#include "lcd.h"
#include <string.h>

#include <stdio.h>
//#include "24cxx.h"
extern UART_HandleTypeDef huart1; 
-----------
#define ADDR_24LCxx_Write 0xA0
#define ADDR_24LCxx_Read 0xA1
#define BufferSize 8
uint8_t WriteBuffer[BufferSize]={0x31,0x32,0x33,0x34,0x35,0x36,0x37,0x38},ReadBuffer[BufferSize];
uint8_t writett[1];
uint8_t write_tem[2];
uint8_t readtt[1];
uint8_t read_tem[2];
uint8_t i=0xaa;
const uint8_t g_text_buf[] = {"STM32 IIC TEST"};
 short temperature;
#define TEXT_SIZE       sizeof(g_text_buf)  /* TEXT×Ö·û´®³¤¶È */
    
uint8_t tmp=1;
 
uint8_t cnt[1]={1};
uint8_t datatemp[TEXT_SIZE];
uint16_t pwm=0;   //
/* USER CODE END Includes */

/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */

int fputc(int ch, FILE *f)
{
  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);
  return ch;
}
 
int fgetc(FILE *f)
{
  uint8_t ch = 0;
  HAL_UART_Receive(&huart1, &ch, 1, 0xffff);
  return ch;
}
/* USER CODE END PTD */

/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */

/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */

/* USER CODE END PM */

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* ÊäÈ벶»ñ״̬£¨g_timxchy_cap_sta£©
   bit 7 £º0£¬Ã»Óгɹ¦²¶»ñ£»1£¬³É¹¦²¶»ñµ½Ò»´Î
   bit 6 £º0£¬»¹Ã»²¶»ñµ½¸ßµçƽ£»1£¬ÒѾ­²¶»ñµ½¸ßµçƽ
   bit 0~5 £º²¶»ñ¸ßµçƽºóÒç³öµÄ´ÎÊý£¬×î¶àÒç³ö63´Î£¬ËùÒÔ×²¶»ñÖµ = 63*65536 + 65536 = 4194303
             °´1¦Ìs¼ÇÒ»¸öÊý£¬×Òç³öʱ¼ä¼´4194303¦Ìs£¬Ô¼4.19Ãë
*/
uint8_t g_timxchy_cap_sta = 0;
uint16_t g_timxchy_cap_val = 0;        //¼Ç¼²¶»ñµ½µÍµçƽʱ¼Ä´æÆ÷ÖеÄÖµ£¬¼´CCRx2

uint32_t temp = 0;          //´æ·ÅÒç³öʱ¼ä   

uint8_t start_flag=0;
/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
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
  */
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_USART1_UART_Init();
  MX_FSMC_Init();
  MX_TIM3_Init();
  MX_TIM5_Init();
  MX_TIM2_Init();
  MX_TIM4_Init();
  /* USER CODE BEGIN 2 */
  lcd_init();
    HAL_TIM_Base_Start_IT(&htim2);
    //HAL_TIM_Base_Start_IT(&htim4);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_2);
    __HAL_TIM_ENABLE_IT(&htim5, TIM_IT_UPDATE);
    HAL_TIM_IC_Start_IT(&htim5, TIM_CHANNEL_1); 
    lcd_show_string(80, 40, 240, 32, 32, "20210555121", RED);
    lcd_show_string(90, 80, 240, 24, 24, "Hello wenkai", RED);
    lcd_show_string(10, 160, 110, 24, 24, "High_time:", RED);    
    lcd_show_xnum(130, 160, 0,2, 24, 0, BLACK);
    lcd_show_string(160, 160, 110, 24, 24, "us", RED);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);
    HAL_Delay(1000);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);
    HAL_Delay(1000);
      
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
            if(start_flag)
            {
                while(pwm< 1000)
                {
                    pwm++;
                    __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, pwm);               
                    HAL_Delay(2);
                }
                while (pwm)
                {
                    pwm--;
                    __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, pwm);
                    HAL_Delay(2);
                }
                HAL_Delay(10);
            }
  }
  /* USER CODE END 3 */
}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{
  RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  /** Initializes the CPU, AHB and APB busses clocks 
  */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    Error_Handler();
  }
  /** Initializes the CPU, AHB and APB busses 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_DIV2;
  RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  {
    Error_Handler();
  }
}

/* USER CODE BEGIN 4 */
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
    HAL_Delay(20);
    if(GPIO_Pin==GPIO_PIN_2)
        {
            if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_2)==0)
            {
                if(start_flag==0)
                {
                    start_flag=1;
                }
            }
        }
        
        if(GPIO_Pin==GPIO_PIN_4)
        {
            if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_4)==0)
            {
                HAL_TIM_Base_Start_IT(&htim4);                
            }
        }
        if(GPIO_Pin==GPIO_PIN_3)
        {
            if(HAL_GPIO_ReadPin(GPIOE,GPIO_PIN_3)==0)
            {
                    if(start_flag==1)
                {
                    start_flag=0;
                }
            }
        }
}


void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
      if(htim->Instance == TIM5)
    {
        if((g_timxchy_cap_sta & 0x80) == 0)             //»¹Î´³É¹¦²¶»ñÒ»´Î¸ßµçƽÂö¿í
        {
            if(g_timxchy_cap_sta & 0x40)                //ÒѾ­²¶»ñµ½¸ßµçƽ
            {
                if((g_timxchy_cap_sta & 0x3f) == 0x3f)           //¸ßµçƽÂö¿í³¬³öÉ趨ֵ
                {
                    /*
                      Çå³ý¶¨Ê±Æ÷5Ô­ÏȵÄÉèÖ㬲¢½«Æ䲶»ñ¸ÄΪÉÏÉýÑز¶»ñ½øÐÐÏÂÒ»ÂÖÅжÏ
                    */
                    TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1);
                    TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
                    g_timxchy_cap_sta |= 0x80;          //±ê¼ÇÒѾ­³É¹¦²¶»ñµ½Ò»´Î¸ßµçƽÂö¿í
                    g_timxchy_cap_val = 0xffff;         //¸ßµçƽÂö¿íÒѳ¬³öÉ趨ֵ£¬ËùÒÔ½«valÉ趨Ϊ×î´ó
                }
                else        //ÿÒç³öÒ»´Î½øÐÐ++
                {
                    g_timxchy_cap_sta++;
                }
            }
        }
    }
            if(htim->Instance == TIM2)
            {

                    if(g_timxchy_cap_sta & 0x80)
                    {
                            temp = g_timxchy_cap_sta & 0x3f;        //»ñÈ¡Òç³ö´ÎÊý
                            temp *= 65536;                          //ÿÒç³öÒ»´Î´ú±í¼ÆÊý65536
                            temp += g_timxchy_cap_val;              //×îÖÕ¼ÓÉÏ×îºó¼ÇÊýÖµ¼´Îª¸ßµçƽ×Üʱ¼ä
                            printf("HIGH:%d us\r\n", temp);
                            g_timxchy_cap_sta = 0;                  //¿ªÆôÏÂÒ»´Î²¶»ñ
                          lcd_show_xnum(130, 160, temp,8, 24, 0, BLACK);
                        lcd_show_string(230, 160, 110, 24, 24, "us", RED);
                    }
            }
            if(htim->Instance == TIM4)
            {
                 HAL_GPIO_TogglePin(GPIOE, GPIO_PIN_5);
            }
}

// ½øÈë¸Ãº¯Êý±êÃ÷ÒѾ­·¢ÉúÁËÒ»´Î²¶»ñÖжϣ¬¼´²¶×½µ½ÉÏÉýÑØ»òϽµÑØ
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
    if(htim->Instance == TIM5)
    {
        if((g_timxchy_cap_sta & 0x80) == 0)         //»¹Î´³É¹¦²¶»ñ¸ßµçƽ
        {
            if(g_timxchy_cap_sta & 0x40)            //ÉÏ´Îͨ¹ý²¶»ñµ½ÉÏÉýÑؽ«g_timxchy_cap_staλ6ÖÃ1ºó²¢¸ü¸ÄΪϽµÑز¶×½,±íÃ÷Õâ´Î½øÈ벶»ñµ½Ï½µÑØ
            {
                g_timxchy_cap_sta |= 0x80;          //±ê¼ÇÒѾ­³É¹¦²¶»ñµ½Ò»´Î¸ßµçƽÂö¿í
                g_timxchy_cap_val = HAL_TIM_ReadCapturedValue(htim, TIM_CHANNEL_1);     //»ñÈ¡µ±Ç°µÄ²¶»ñÖµ
                /*
                  Çå³ý¶¨Ê±Æ÷5Ô­ÏȵÄÉèÖ㬲¢½«Æ䲶»ñ¸ÄΪÉÏÉýÑز¶»ñ½øÐÐÏÂÒ»ÂÖÅжÏ
                */
                TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1);
                TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING);
                
            }
            else                                    //»¹Î´¿ªÊ¼£¬µÚÒ»´Î²¶»ñÉÏÉýÑØ
            {
                g_timxchy_cap_sta = 0;              //ÇåÁã
                g_timxchy_cap_val = 0;
                g_timxchy_cap_sta |= 0x40;          //±ê¼Ç²¶»ñµ½ÉÏÉýÑØ
                
                /*³õʼ»¯²Ù×÷
                  ²¶»ñµ½ÉÏÉýÑغó£¬Ê§Äܶ¨Ê±Æ÷5£¬²¢½«Æä¼ÆÊý¹éÁ㣬ÒòΪҪʹÓö¨Ê±Æ÷5À´½øÐмÆÊýÅжÏÂö¿í
                  Çå³ý¶¨Ê±Æ÷5Ô­ÏȵÄÉèÖ㬲¢½«Æ䲶»ñ¸ÄΪϽµÑز¶»ñ
                  ÖØÐÂʹÄܶ¨Ê±Æ÷5
                */
                __HAL_TIM_DISABLE(htim);
                __HAL_TIM_SET_COUNTER(htim, 0);
                TIM_RESET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1);
                TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING);
                __HAL_TIM_ENABLE(htim);
            }
        }
    }

}
/* 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 */

  /* 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,
     tex: 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****/

  • 4
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值