找回密码
 立即注册

QQ登录

只需一步,快速开始

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

STM32+RS485网关代码

[复制链接]
跳转到指定楼层
楼主
ID:302293 发表于 2022-5-18 17:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  超级网口,网关代码
  1. #include "main.h"
  2. #include "iwdg.h"
  3. #include "usart.h"
  4. #include "tim.h"
  5. #include "gpio.h"

  6. /* Private includes ----------------------------------------------------------*/
  7. /* USER CODE BEGIN Includes */
  8. #include "delay.h"
  9. #include "buzzer.h"
  10. #include "rs485.h"
  11. #include "netport.h"
  12. #include "relay.h"
  13. #include "w25qxx.h"
  14. #include "led.h"
  15. #include "process.h"
  16. #include "modbustcp_slave.h"
  17. /* USER CODE END Includes */

  18. /* Private typedef -----------------------------------------------------------*/
  19. /* USER CODE BEGIN PTD */

  20. /* USER CODE END PTD */

  21. /* Private define ------------------------------------------------------------*/
  22. /* USER CODE BEGIN PD */
  23. /* USER CODE END PD */

  24. /* Private macro -------------------------------------------------------------*/
  25. /* USER CODE BEGIN PM */

  26. /* USER CODE END PM */

  27. /* Private variables ---------------------------------------------------------*/

  28. /* USER CODE BEGIN PV */

  29. /* USER CODE END PV */

  30. /* Private function prototypes -----------------------------------------------*/
  31. void SystemClock_Config(void);
  32. static void MX_NVIC_Init(void);
  33. /* USER CODE BEGIN PFP */

  34. /* USER CODE END PFP */

  35. /* Private user code ---------------------------------------------------------*/
  36. /* USER CODE BEGIN 0 */
  37.   uint16_t htim22_count=0;
  38. /* USER CODE END 0 */

  39. /**
  40.   * @brief  The application entry point.
  41.   * @retval int
  42.   */
  43. int main(void)
  44. {
  45.   /* USER CODE BEGIN 1 */

  46.   /* USER CODE END 1 */

  47.   /* MCU Configuration--------------------------------------------------------*/

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

  50.   /* USER CODE BEGIN Init */

  51.   /* USER CODE END Init */

  52.   /* Configure the system clock */
  53.   SystemClock_Config();

  54.   /* USER CODE BEGIN SysInit */

  55.   /* USER CODE END SysInit */

  56.   /* Initialize all configured peripherals */
  57.   MX_GPIO_Init();
  58.   MX_LPUART1_UART_Init();
  59.   MX_USART1_UART_Init();
  60.   MX_USART2_UART_Init();
  61. //  MX_IWDG_Init();
  62.   MX_TIM21_Init();
  63.   MX_TIM22_Init();
  64.   MX_TIM2_Init();
  65.         
  66.   /* Initialize interrupts */
  67.   MX_NVIC_Init();
  68.   /* USER CODE BEGIN 2 */

  69.   /* USER CODE END 2 */

  70.   /* Infinite loop */
  71.   /* USER CODE BEGIN WHILE */
  72.   SPI_Flash_ReadID();
  73.   Netport_Reset();
  74.   BUZZER_ON;
  75.   delay_ms(50);
  76.   BUZZER_OFF;
  77.   __HAL_TIM_DISABLE( &htim21 );
  78.   RS485_1_RX;
  79.         
  80.         Sample();
  81.   while (1)
  82.   {
  83.     /* USER CODE END WHILE */

  84.     /* USER CODE BEGIN 3 */
  85.          
  86.           if(netport.sta & 0x8000)
  87.           {
  88.                   if(netport.rx_buff[6] == 0xFF)
  89.                   {
  90.                             //if(Check_CRC16(netport.rx_buff, netport.sta & 0x7FFF))
  91.                                 {
  92.                                         RX_LED_BLINK(NETPORT);
  93.                                         Process_ModbusTCP(MODBUS_TCP, netport.rx_buff, netport.sta&0x7FFF);
  94.                                 }
  95.                   }
  96.                
  97.                 netport.sta = 0;
  98.           }
  99.                
  100.                 if(htim22_count>=50)//2.5s
  101.                         {
  102.                          htim22_count=0;
  103.                          Sample();
  104.                          //HAL_GPIO_TogglePin( GPIOB, GPIO_PIN_7);
  105.                         }
  106.          // Netport_Send((uint8_t *)"123", 3);
  107.           //delay_ms(10);
  108.          
  109.          
  110.         __HAL_IWDG_RELOAD_COUNTER(&hiwdg);
  111.         
  112.   }
  113.   /* USER CODE END 3 */
  114. }

  115. /**
  116.   * @brief System Clock Configuration
  117.   * @retval None
  118.   */
  119. void SystemClock_Config(void)
  120. {
  121.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  122.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  123.   RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};

  124.   /** Configure the main internal regulator output voltage
  125.   */
  126.   __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
  127.   /** Initializes the RCC Oscillators according to the specified parameters
  128.   * in the RCC_OscInitTypeDef structure.
  129.   */
  130.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE;
  131.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  132.   RCC_OscInitStruct.LSIState = RCC_LSI_ON;
  133.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  134.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  135.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLLMUL_8;
  136.   RCC_OscInitStruct.PLL.PLLDIV = RCC_PLLDIV_2;
  137.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  138.   {
  139.     Error_Handler();
  140.   }
  141.   /** Initializes the CPU, AHB and APB buses clocks
  142.   */
  143.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  144.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  145.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  146.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  147.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  148.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  149.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
  150.   {
  151.     Error_Handler();
  152.   }
  153.   PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1|RCC_PERIPHCLK_USART2
  154.                               |RCC_PERIPHCLK_LPUART1;
  155.   PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  156.   PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  157.   PeriphClkInit.Lpuart1ClockSelection = RCC_LPUART1CLKSOURCE_PCLK1;
  158.   if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  159.   {
  160.     Error_Handler();
  161.   }
  162. }

  163. /**
  164.   * @brief NVIC Configuration.
  165.   * @retval None
  166.   */
  167. static void MX_NVIC_Init(void)
  168. {
  169.         /* TIM21_IRQn interrupt configuration */
  170.   HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0);
  171.   HAL_NVIC_EnableIRQ(TIM2_IRQn);
  172.   /* TIM21_IRQn interrupt configuration */
  173.   HAL_NVIC_SetPriority(TIM21_IRQn, 0, 0);
  174.   HAL_NVIC_EnableIRQ(TIM21_IRQn);
  175.   /* USART1_IRQn interrupt configuration */
  176.   HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  177.   HAL_NVIC_EnableIRQ(USART1_IRQn);
  178.   /* USART2_IRQn interrupt configuration */
  179.   HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  180.   HAL_NVIC_EnableIRQ(USART2_IRQn);
  181.   /* LPUART1_IRQn interrupt configuration */
  182.   HAL_NVIC_SetPriority(LPUART1_IRQn, 0, 0);
  183.   HAL_NVIC_EnableIRQ(LPUART1_IRQn);
  184. }

  185. /* USER CODE BEGIN 4 */

  186. /* USER CODE END 4 */

  187. /**
  188.   * @brief  This function is executed in case of error occurrence.
  189.   * @retval None
  190.   */
  191. void Error_Handler(void)
  192. {
  193.   /* USER CODE BEGIN Error_Handler_Debug */
  194.   /* User can add his own implementation to report the HAL error return state */
  195.   __disable_irq();
  196.   while (1)
  197.   {
  198.   }
  199.   /* USER CODE END Error_Handler_Debug */
  200. }

  201. #ifdef  USE_FULL_ASSERT
  202. /**
  203.   * @brief  Reports the name of the source file and the source line number
  204.   *         where the assert_param error has occurred.
  205.   * @param  file: pointer to the source file name
  206.   * @param  line: assert_param error line source number
  207.   * @retval None
  208.   */
  209. void assert_failed(uint8_t *file, uint32_t line)
  210. {
  211.   /* USER CODE BEGIN 6 */
  212.   /* User can add his own implementation to report the file name and line number,
  213.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  214.   /* USER CODE END 6 */
  215. }
  216. #endif /* USE_FULL_ASSERT */
复制代码

Keil代码下载,仅供参考: 485DTU - V1.2.7z (5.14 MB, 下载次数: 32)

评分

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

查看全部评分

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

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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