找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3051|回复: 1
打印 上一主题 下一主题
收起左侧

stm32f103c8T6,+DS18B20+DHT11cubemax版本程序

[复制链接]
跳转到指定楼层
楼主
ID:258945 发表于 2019-6-2 16:42 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
修改一下IO口直接可以用,串口1打印

单片机源程序如下:
  1. /* USER CODE END Header */

  2. /* Includes ------------------------------------------------------------------*/
  3. #include "main.h"

  4. /* Private includes ----------------------------------------------------------*/
  5. /* USER CODE BEGIN Includes */
  6. #include "ds18b20.h"
  7. #include "hal_temp_hum.h"
  8. /* USER CODE END Includes */

  9. /* Private typedef -----------------------------------------------------------*/
  10. /* USER CODE BEGIN PTD */

  11. /* USER CODE END PTD */

  12. /* Private define ------------------------------------------------------------*/
  13. /* USER CODE BEGIN PD */

  14. /* USER CODE END PD */

  15. /* Private macro -------------------------------------------------------------*/
  16. /* USER CODE BEGIN PM */

  17. /* USER CODE END PM */

  18. /* Private variables ---------------------------------------------------------*/
  19. UART_HandleTypeDef huart1;

  20. /* USER CODE BEGIN PV */

  21. /* USER CODE END PV */

  22. /* Private function prototypes -----------------------------------------------*/
  23. void SystemClock_Config(void);
  24. static void MX_GPIO_Init(void);
  25. static void MX_USART1_UART_Init(void);
  26. /* USER CODE BEGIN PFP */

  27. /* USER CODE END PFP */

  28. /* Private user code ---------------------------------------------------------*/
  29. /* USER CODE BEGIN 0 */

  30. /* USER CODE END 0 */

  31. /**
  32.   * @brief  The application entry point.
  33.   * @retval int
  34.   */
  35. int main(void)
  36. {
  37.   /* USER CODE BEGIN 1 */
  38.         short  T;
  39.         uint8_t temperature;
  40.         uint8_t humidity;
  41.   /* USER CODE END 1 */
  42.   

  43.   /* MCU Configuration--------------------------------------------------------*/

  44.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  45.   HAL_Init();

  46.   /* USER CODE BEGIN Init */
  47.        
  48.   /* USER CODE END Init */

  49.   /* Configure the system clock */
  50.   SystemClock_Config();

  51.   /* USER CODE BEGIN SysInit */

  52.   /* USER CODE END SysInit */

  53.   /* Initialize all configured peripherals */
  54.   MX_GPIO_Init();
  55.   MX_USART1_UART_Init();
  56.   /* USER CODE BEGIN 2 */
  57.         DS18B20_Init();
  58.         dht11Init();
  59.   /* USER CODE END 2 */

  60.   /* Infinite loop */
  61.   /* USER CODE BEGIN WHILE */
  62.         if(!dht11Init())
  63.         {
  64.         printf("dht11int_success");
  65.         }
  66.         else
  67.         printf("dht11int_err");
  68.   while (1)
  69.   {
  70.     /* USER CODE END WHILE */

  71.     /* USER CODE BEGIN 3 */
  72.                 T=DS18B20_Get_Temp();
  73.                 dht11Read(&temperature,&humidity);
  74.                 printf("串口测试");
  75.                 printf("T=%d.%d\n",T/10,T/1%10);
  76.                 printf("temperature=%d,humidity=%d",temperature,humidity);
  77.                 HAL_Delay(5000);
  78.   }
  79.   /* USER CODE END 3 */
  80. }

  81. /**
  82.   * @brief System Clock Configuration
  83.   * @retval None
  84.   */
  85. void SystemClock_Config(void)
  86. {
  87.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  88.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

  89.   /** Initializes the CPU, AHB and APB busses clocks
  90.   */
  91.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  92.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  93.   RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  94.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  95.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
  96.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL16;
  97.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  98.   {
  99.     Error_Handler();
  100.   }
  101.   /** Initializes the CPU, AHB and APB busses clocks
  102.   */
  103.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  104.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  105.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  106.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  107.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  108.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  109.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  110.   {
  111.     Error_Handler();
  112.   }
  113. }

  114. /**
  115.   * @brief USART1 Initialization Function
  116.   * @param None
  117.   * @retval None
  118.   */
  119. static void MX_USART1_UART_Init(void)
  120. {

  121.   /* USER CODE BEGIN USART1_Init 0 */

  122.   /* USER CODE END USART1_Init 0 */

  123.   /* USER CODE BEGIN USART1_Init 1 */

  124.   /* USER CODE END USART1_Init 1 */
  125.   huart1.Instance = USART1;
  126.   huart1.Init.BaudRate = 115200;
  127.   huart1.Init.WordLength = UART_WORDLENGTH_8B;
  128.   huart1.Init.StopBits = UART_STOPBITS_1;
  129.   huart1.Init.Parity = UART_PARITY_NONE;
  130.   huart1.Init.Mode = UART_MODE_TX_RX;
  131.   huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  132.   huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  133.   if (HAL_UART_Init(&huart1) != HAL_OK)
  134.   {
  135.     Error_Handler();
  136.   }
  137.   /* USER CODE BEGIN USART1_Init 2 */

  138.   /* USER CODE END USART1_Init 2 */

  139. }

  140. /**
  141.   * @brief GPIO Initialization Function
  142.   * @param None
  143.   * @retval None
  144.   */
  145. static void MX_GPIO_Init(void)
  146. {
  147.   GPIO_InitTypeDef GPIO_InitStruct = {0};

  148.   /* GPIO Ports Clock Enable */
  149.   __HAL_RCC_GPIOB_CLK_ENABLE();
  150.   __HAL_RCC_GPIOA_CLK_ENABLE();

  151.   /*Configure GPIO pin Output Level */
  152.   HAL_GPIO_WritePin(GPIOB, DHT11_Pin|DS18B20_Pin, GPIO_PIN_RESET);

  153.   /*Configure GPIO pins : DHT11_Pin DS18B20_Pin */
  154.   GPIO_InitStruct.Pin = DHT11_Pin|DS18B20_Pin;
  155.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  156.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  157.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  158.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  159. }

  160. /* USER CODE BEGIN 4 */
  161. /* printf函数重映射 */
  162. #ifdef __GNUC__
  163.   /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
  164.      set to 'Yes') calls __io_putchar() */
  165.   #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
  166. #else
  167.   #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
  168. #endif /* __GNUC__ */

  169. /**
  170.   * @brief  Retargets the C library printf function to the USART.
  171.   * @param  None
  172.   * @retval None
  173.   */
  174. PUTCHAR_PROTOTYPE
  175. {
  176.   /* Place your implementation of fputc here */
  177.   /* e.g. write a character to the EVAL_COM1 and Loop until the end of transmission */
  178.         HAL_UART_Transmit(&huart1,(uint8_t *)&ch,1,1);       

  179.   return ch;
  180. }
  181. /* USER CODE END 4 */

  182. /**
  183.   * @brief  This function is executed in case of error occurrence.
  184.   * @retval None
  185.   */
  186. void Error_Handler(void)
  187. {
  188.   /* USER CODE BEGIN Error_Handler_Debug */
  189.   /* User can add his own implementation to report the HAL error return state */

  190.   /* USER CODE END Error_Handler_Debug */
  191. }

  192. #ifdef  USE_FULL_ASSERT
  193. /**
  194.   * @brief  Reports the name of the source file and the source line number
  195.   *         where the assert_param error has occurred.
  196.   * @param  file: pointer to the source file name
  197.   * @param  line: assert_param error line source number
  198.   * @retval None
  199.   */
  200. void assert_failed(uint8_t *file, uint32_t line)
  201. {
  202.   /* USER CODE BEGIN 6 */
  203.   /* User can add his own implementation to report the file name and line number,
  204.      tex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  205.   /* USER CODE END 6 */
  206. }
  207. #endif /* USE_FULL_ASSERT */

  208. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
复制代码

所有资料51hei提供下载:
usart.7z (2.9 MB, 下载次数: 150)


评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶2 踩
回复

使用道具 举报

沙发
ID:91165 发表于 2020-2-25 19:01 | 只看该作者
下载几个18b20对比一下
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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