OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 620|回复: 1

关于stm32的温控风扇代码问题

[复制链接]

1

主题

1

帖子

0

精华

新手入门

积分
11
金钱
11
注册时间
2019-10-29
在线时间
5 小时
发表于 2019-11-9 20:45:10 | 显示全部楼层 |阅读模式
1金钱
编译出错,不知道怎么改。

/**
  ******************************************************************************
  * @file           : main.c
  * @brief          : Main program body
  ******************************************************************************
  ** This notice applies to any and all portions of this file
  * that are not between comment pairs USER CODE BEGIN and
  * USER CODE END. Other portions of this file, whether
  * inserted by the user or by software development tools
  * are owned by their respective copyright owners.
  *
  * COPYRIGHT(c) 2018 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 "main.h"
#include "stm32f1xx_hal.h"

/* USER CODE BEGIN Includes */
#include "stmflash.h"
#include "STM32_LCD1602.h"
#include "DS18B20.h"

//--------------------------------------------------> Key
#define Key1  HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_12)
#define Key2  HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_13)
#define Key3  HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_14)
unsigned char Key1_flag=0;
unsigned char Key2_flag=0;
unsigned char Key3_flag=0;
unsigned char sec1=0;
unsigned char sec2=0;

//--------------------------------------------------> Display
unsigned char State=0;
unsigned char s0=0;

//--------------------------------------------------> DS18B20
unsigned char Read_DS18B20=1;
int DS18B20_Temp=0;
unsigned char DS18B20_Temp_H1=0;
unsigned char DS18B20_Temp_H2=0;
unsigned char DS18B20_Temp_H3=0;  
//--------------------------------------------------> Timer3
unsigned int Time3_ms=0;

//--------------------------------------------------> Memory
unsigned char memory_flag=0;

//--------------------------------------------------> LED
#define LED0_0  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_RESET)
#define LED0_1  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_10,GPIO_PIN_SET)

#define LED1_0  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_RESET)
#define LED1_1  HAL_GPIO_WritePin(GPIOB,GPIO_PIN_11,GPIO_PIN_SET)


//
unsigned char cold=1;


/* USER CODE END Includes */

/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim3;

/* USER CODE BEGIN PV */
/* Private variables ---------------------------------------------------------*/

void Police() // DS18B20_Temp DS18B20_Temp_H1
{
  if(DS18B20_Temp>DS18B20_Temp_H3*10)
        {
                LED0_0;
                LED1_0;
                TIM3->CCR3 = 65535;               
                cold=1;
        }
        else if(DS18B20_Temp>DS18B20_Temp_H2*10&&DS18B20_Temp<=DS18B20_Temp_H3*10)
        {
                LED0_1;
                LED1_0;
                TIM3->CCR3 = 300;               
                cold=1;
        }       
        else if(DS18B20_Temp>DS18B20_Temp_H1*10&&DS18B20_Temp<=DS18B20_Temp_H2*10)
        {
                LED0_0;
                LED1_1;
    if(cold==1)
                {
                        cold=0;
                  TIM3->CCR3 = 65535;
                        HAL_Delay(800);
                }
                TIM3->CCR3 = 200;               
        }
  else        
        {
                LED0_1;
                LED1_1;  
                TIM3->CCR3 = 0;               
                cold=1;
        }
       
       
       
}
void Key_Dispose() //按键处理函数
{
   if(!Key1)  
         {
                 if(Key1_flag)
                 {
                   Key1_flag=0;
                   State=(State+1)%4;
                 }
         }
         else Key1_flag=1;

   if(!Key2)  
         {
                   if(Key2_flag||sec1==0)
                         {
                                 Key2_flag=0;
                                 switch(State)
                                 {
                                   case 1: if(DS18B20_Temp_H1+1<DS18B20_Temp_H2)DS18B20_Temp_H1++;  break;
                                         case 2: if(DS18B20_Temp_H2+1<DS18B20_Temp_H3)DS18B20_Temp_H2++;  break;
                                         case 3: if(DS18B20_Temp_H3<99)DS18B20_Temp_H3++;                 break;
                                 }
                         }
         }
         else
          {
            if(Key2_flag==0)  { Key2_flag=1; memory_flag=1; }
                  sec1=2;
          }

               
  if(!Key3)  
         {
                        if(Key3_flag||sec2==0)
                         {
                                 Key3_flag=0;
                                 switch(State)
                                 {
                                   case 1: if(DS18B20_Temp_H1>0)DS18B20_Temp_H1--;                  break;
                                         case 2: if(DS18B20_Temp_H2-1>DS18B20_Temp_H1)DS18B20_Temp_H2--;  break;
                                         case 3: if(DS18B20_Temp_H3-1>DS18B20_Temp_H2)DS18B20_Temp_H3--;  break;
                                 }
                         }
         }
         else
          {
                         if(Key3_flag==0)  {Key3_flag=1;memory_flag=1;}
                   sec2=2;
          }


}





void Display()     //显示函数
{
        if(State==0)
        {
                LCD1602_write(0,0x80);
                LCD1602_writebyte((unsigned char *)"   Temperature  ");
                LCD1602_write(0,0xC0);
                LCD1602_writebyte((unsigned char *)"      ");
    LCD1602_write(1,0x30+DS18B20_Temp/100%10);
          LCD1602_write(1,0x30+DS18B20_Temp/10%10);
                LCD1602_writebyte((unsigned char *)".");
          LCD1602_write(1,0x30+DS18B20_Temp%10);
          LCD1602_write(1,0xdf);
                LCD1602_writebyte((unsigned char *)"C      ");
               
        }
        else
        {
                if(State<3)  
                {
                                  LCD1602_write(0,0x80);
                                        LCD1602_writebyte((unsigned char *)"Temp1:");
                                        if(State==1&&s0==1)  LCD1602_writebyte((unsigned char *)"  ");
                                                else
                                                {
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H1/10%10);
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H1%10);
                                                }
                                        LCD1602_write(1,0xdf);
                                        LCD1602_writebyte((unsigned char *)"C        ");
                                        LCD1602_write(0,0xC0);
                                        LCD1602_writebyte((unsigned char *)"Temp2:");
                                        if(State==2&&s0==1)  LCD1602_writebyte((unsigned char *)"  ");
                                                else
                                                {
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H2/10%10);
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H2%10);
                                                }
                                        LCD1602_write(1,0xdf);
                                        LCD1602_writebyte((unsigned char *)"C         ");
                }
                else
                {
                                  LCD1602_write(0,0x80);
                                        LCD1602_writebyte((unsigned char *)"Temp3:");
                                        if(State==3&&s0==1)  LCD1602_writebyte((unsigned char *)"  ");
                                                else
                                                {
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H3/10%10);
                                                        LCD1602_write(1,0x30+DS18B20_Temp_H3%10);
                                                }
                                        LCD1602_write(1,0xdf);
                                        LCD1602_writebyte((unsigned char *)"C        ");
                                        LCD1602_write(0,0xC0);
                                        LCD1602_writebyte((unsigned char *)"                  ");
                }
               
                       
        }


}       

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) //定时器回调
{
    if(htim==&htim3)
     {
        Time3_ms++;
                          if(Time3_ms%40==0)
                                {
                                  Key_Dispose();
                                }
                          if(Time3_ms%250==0)s0=s0^0x01;
                                if(Time3_ms%1000==0)
                                {
                                  if(sec1!=0) sec1--;
                                        if(sec2!=0) sec2--;
                                }                                       
                                if(Time3_ms>=2000)
                                {
                                        Time3_ms=0;
                                        Read_DS18B20=1;
                                }       
     }
                 
}


void memory() //存储函数
{
  if(memory_flag==1)  
        {
          memory_flag=0;
                Write_flsh_byte(0x0800F000,DS18B20_Temp_H1);
                Write_flsh_byte(0x08010000,DS18B20_Temp_H2);
                Write_flsh_byte(0x08011000,DS18B20_Temp_H3);

        }
}

void read_memory() //读取存储
{
    DS18B20_Temp_H1=readFlash(0x0800F000);
          DS18B20_Temp_H2=readFlash(0x08010000);
          DS18B20_Temp_H3=readFlash(0x08011000);
          if(DS18B20_Temp_H3>99||(DS18B20_Temp_H1>=DS18B20_Temp_H2)||(DS18B20_Temp_H2>=DS18B20_Temp_H3))
                {
                  DS18B20_Temp_H1=10;
                        DS18B20_Temp_H2=20;
                        DS18B20_Temp_H3=30;
                       
                }
}

/* USER CODE END PV */

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM3_Init(void);                                    
void HAL_TIM_MspPostInit(TIM_HandleTypeDef *htim);                          
/* USER CODE BEGIN PFP */
/* Private function prototypes -----------------------------------------------*/

/* USER CODE END PFP */

/* USER CODE BEGIN 0 */





/* USER CODE END 0 */

/**
  * @brief  The application entry point.
  *
  * @retval None
  */
int main(void)
{
  /* USER CODE BEGIN 1 */
  uint8_t i, DS18B20ID[8];
  /* 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_TIM3_Init();
  /* USER CODE BEGIN 2 */
  read_memory();  //调用读取
  HAL_Delay(100);
        LCD1602_cls();  //液晶初始化
       
       
  HAL_TIM_Base_Start_IT(&htim3); //启动定时器 通用定时器 3
        HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_3);

  DS18B20_Init();
  do
        {
          DS18B20_Temp=DS18B20_Get_Temp();
        }while(DS18B20_Temp==850);


  /* USER CODE END 2 */

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

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */
                memory();
          Display();
                Police() ;  
                if(Read_DS18B20==1)
                {
                        Read_DS18B20=0;
                        DS18B20_Temp=DS18B20_Get_Temp();
                }


               
  }
  /* USER CODE END 3 */

}

/**
  * @brief System Clock Configuration
  * @retval None
  */
void SystemClock_Config(void)
{

  RCC_OscInitTypeDef RCC_OscInitStruct;
  RCC_ClkInitTypeDef RCC_ClkInitStruct;

    /**Initializes the CPU, AHB and APB busses clocks
    */
  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  RCC_OscInitStruct.HSICalibrationValue = 16;
  RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

    /**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(__FILE__, __LINE__);
  }

    /**Configure the Systick interrupt time
    */
  HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

    /**Configure the Systick
    */
  HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  /* SysTick_IRQn interrupt configuration */
  HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
}

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 63;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 0;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  HAL_TIM_MspPostInit(&htim3);

}

/** Configure pins as
        * Analog
        * Input
        * Output
        * EVENT_OUT
        * EXTI
*/
static void MX_GPIO_Init(void)
{

  GPIO_InitTypeDef GPIO_InitStruct;

  /* GPIO Ports Clock Enable */
  __HAL_RCC_GPIOB_CLK_ENABLE();
  __HAL_RCC_GPIOA_CLK_ENABLE();

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_RESET);

  /*Configure GPIO pin Output Level */
  HAL_GPIO_WritePin(GPIOB, GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_15, GPIO_PIN_SET);

  /*Configure GPIO pins : PB1 PB10 PB11 PB15 */
  GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_15;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /*Configure GPIO pins : PB12 PB13 PB14 */
  GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14;
  GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

}

/* USER CODE BEGIN 4 */

/* USER CODE END 4 */

/**
  * @brief  This function is executed in case of error occurrence.
  * @param  file: The file name as string.
  * @param  line: The line in file as a number.
  * @retval None
  */
void _Error_Handler(char *file, int line)
{
  /* USER CODE BEGIN Error_Handler_Debug */
  /* User can add his own implementation to report the HAL error return state */
  while(1)
  {
  }
  /* 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****/


Rebuild target 'STM32'
assembling startup_stm32f103xb.s...
compiling main.c...
..\HARDWARE\LCD1602\STM32_LCD1602.h(80): warning:  #1295-D: Deprecated declaration LCD1602_cls - give arg types
  void LCD1602_cls();               //初始化液晶
../Src/main.c(333): warning:  #177-D: variable "i" was declared but never referenced
    uint8_t i, DS18B20ID[8];
../Src/main.c(333): warning:  #177-D: variable "DS18B20ID" was declared but never referenced
    uint8_t i, DS18B20ID[8];
../Src/main.c: 3 warnings, 0 errors
compiling stm32f1xx_it.c...
compiling stm32f1xx_hal_msp.c...
compiling stm32f1xx_hal_gpio_ex.c...
compiling stm32f1xx_hal_tim.c...
compiling stm32f1xx_hal_tim_ex.c...
compiling stm32f1xx_hal.c...
compiling stm32f1xx_hal_rcc.c...
compiling stm32f1xx_hal_rcc_ex.c...
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c(132): error:  #268: declaration may not appear after executable statement in block
      FlagStatus       pwrclkchanged = RESET;
../Drivers/STM32F1xx_HAL_Driver/Src/stm32f1xx_hal_rcc_ex.c: 0 warnings, 1 error
compiling stm32f1xx_hal_gpio.c...
compiling stm32f1xx_hal_dma.c...
compiling stm32f1xx_hal_cortex.c...
compiling stm32f1xx_hal_pwr.c...
compiling stm32f1xx_hal_flash.c...
compiling stm32f1xx_hal_flash_ex.c...
compiling system_stm32f1xx.c...
compiling STM32_LCD1602.c...
..\HARDWARE\LCD1602\STM32_LCD1602.h(80): warning:  #1295-D: Deprecated declaration LCD1602_cls - give arg types
  void LCD1602_cls();               //初始化液晶
..\HARDWARE\LCD1602\STM32_LCD1602.c: 1 warning, 0 errors
compiling stmflash.c...
..\HARDWARE\STMFLASH\stmflash.c(26): error:  #268: declaration may not appear after executable statement in block
      FLASH_EraseInitTypeDef f;
..\HARDWARE\STMFLASH\stmflash.c(31): error:  #268: declaration may not appear after executable statement in block
      uint32_t PageError = 0;
..\HARDWARE\STMFLASH\stmflash.c: 0 warnings, 2 errors
compiling DS18B20.c...
"STM32\STM32.axf" - 3 Error(s), 4 Warning(s).
Target not created

正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165524
金钱
165524
注册时间
2010-12-1
在线时间
2116 小时
发表于 2019-11-10 21:36:15 | 显示全部楼层
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-5-26 10:19

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表