找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2137|回复: 0
收起左侧

求大神添加CRC16程序 完成标准的MODBUS通信,本人实测该代码的数据透传有效 !

[复制链接]
ID:415061 发表于 2018-11-13 17:06 | 显示全部楼层 |阅读模式
  1. #include "stm32f4xx.h"
  2. #include "misc.h"
  3. #include "stm32f4xx_gpio.h"
  4. #include "stm32f4xx_rcc.h"
  5. #include "stm32f4xx_usart.h"
  6. #include "stm32f4xx_crc.h"
  7. #define uart6_EN_1        GPIO_SetBits(GPIOB, GPIO_Pin_15); //485发送使能
  8. #define uart6_EN_0        GPIO_ResetBits(GPIOB, GPIO_Pin_15); //485接收使能

  9. #define uart3_EN_1        GPIO_SetBits(GPIOD, GPIO_Pin_10); //485发送使能
  10. #define uart3_EN_0        GPIO_ResetBits(GPIOD, GPIO_Pin_10); //485接收使能

  11. void NVIC_Config(void) {
  12.         NVIC_InitTypeDef NVIC_InitStructure;

  13.         /* Enable the USARTx Interrupt */
  14.         NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
  15.         NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  16.         NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  17.         NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  18.         NVIC_Init(&NVIC_InitStructure);

  19.         NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
  20.         NVIC_Init(&NVIC_InitStructure);
  21. }

  22. void USART_Config(void) {
  23.         USART_InitTypeDef USART_InitStructure;
  24.         GPIO_InitTypeDef GPIO_InitStructure;

  25.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  26.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  27.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
  28.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能GPIOD时钟
  29.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);//使能USART3时钟
  30.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART6时钟

  31.         
  32.         
  33.         
  34.         /* ---------------串口3 IO口----------------- */
  35.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); //PD8复用为串口3
  36.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); //PD9复用为串口3
  37.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;//GPIO8与GPIO9
  38.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //复用功能
  39.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
  40.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
  41.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;        //上拉
  42.         GPIO_Init(GPIOD,&GPIO_InitStructure);              //初始化PD8、PD9

  43.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;              //PD10 控制485_2的输入
  44.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;           //输出
  45.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;            //速度100MHz
  46.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;          //推挽输出
  47.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;            //上拉
  48.         GPIO_Init(GPIOD, &GPIO_InitStructure);
  49.         /* --------------------------------------------- */
  50.         
  51.         
  52.         
  53.         /* ---------------串口6 IO口----------------- */
  54.         GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//PD6复用为串口6
  55.         GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//PD7复用为串口6

  56.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIO6与GPIO7
  57.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //复用功能
  58.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
  59.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽复用输出
  60.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;                //上拉
  61.         
  62.         GPIO_Init(GPIOD,&GPIO_InitStructure);                                 //初始化PD6、PD7
  63.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;              //PB15控制485_1的输入
  64.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;           //输出
  65.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;            //速度100MHz
  66.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;          //推挽输出
  67.         GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;            //上拉
  68.         GPIO_Init(GPIOB, &GPIO_InitStructure);
  69.         /* --------------------------------------------- */

  70.         /* USART configured as follow:  //串口配置如下:
  71.                                                                                                 
  72.         - BaudRate = 115200 baud        //波特率:115200
  73.         - Word Length = 8 Bits                //字节长度=8比特
  74.         - One Stop Bit                                //一个停止位
  75.         - No parity                                        //无校验码位
  76.         - Hardware flow control disabled (RTS and CTS signals)//禁止硬件流控制
  77.         - Receive and transmit enabled        //接受发送已启动
  78.         */
  79.         USART_InitStructure.USART_BaudRate = 9600;//串口的波特率设置为9600bps
  80.         USART_InitStructure.USART_WordLength = USART_WordLength_8b;
  81.         USART_InitStructure.USART_StopBits = USART_StopBits_1;
  82.         USART_InitStructure.USART_Parity = USART_Parity_No;
  83.         USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  84.         /*-------------------------------------*/
  85.         USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置串口既可以发送也可以接收数据
  86.         USART_Init(USART3, &USART_InitStructure);//设置串口3
  87.         USART_Init(USART6, &USART_InitStructure);//设置串口6

  88.         USART_ITConfig(USART6, USART_IT_TC, ENABLE);
  89.         USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);
  90.         USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);

  91.         USART_ITConfig(USART3, USART_IT_TC, ENABLE);
  92.         USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
  93.         USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);

  94.         USART_Cmd(USART6, ENABLE);
  95.         USART_Cmd(USART3, ENABLE);
  96.         uart6_EN_0;
  97.         uart3_EN_0;
  98. }


  99. int main(void) {
  100.         NVIC_Config();
  101.         USART_Config();

  102.         while (1) {
  103.         ;                 //中断里实现串口收发,数据处理
  104.         }
  105. }

  106. #if 0   /*串口6调通,收什么发什么*/
  107. uint8_t uart6_RxBuffer[500];//设置数据缓冲
  108. uint8_t uart6_TxCounter = 0;
  109. uint16_t uart6_RxCounter = 0;
  110. void USART3_IRQHandler(void) {
  111.         if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
  112.                 uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART3);
  113.         }

  114.         if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
  115.                 USART_ReceiveData(USART3); //清中断
  116.                 USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
  117.                 USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);

  118.                 //数据处理
  119.                 uart6_EN_1;
  120.                 USART_ITConfig(USART3, USART_IT_TC, ENABLE);
  121.                 uart6_TxCounter = 0;
  122.                 USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);
  123.         }

  124.         if (USART_GetITStatus(USART3, USART_IT_TC) != RESET){
  125.                 USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);

  126.                 if (uart6_TxCounter > uart6_RxCounter){
  127.                         //开启接收,禁止发送
  128.                         uart6_EN_0;
  129.                         uart6_RxCounter = 0;
  130.                         USART_ITConfig(USART3, USART_IT_TC, DISABLE);
  131.                         USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  132.                         USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
  133.                 }
  134.         }
  135. }

  136. uint8_t uart3_RxBuffer[500];
  137. uint8_t uart3_TxCounter = 0;
  138. uint16_t uart3_RxCounter = 0;
  139. void USART3_IRQHandler(void) {
  140.         if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
  141.                 uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
  142.         }

  143.         if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
  144.                 USART_ReceiveData(USART3); //清中断
  145.                 USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
  146.                 USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);

  147.                 //数据处理
  148.                 uart3_EN_1;
  149.                 USART_ITConfig(USART3, USART_IT_TC, ENABLE);
  150.                 uart3_TxCounter = 0;
  151.                 USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);
  152.         }

  153.         if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
  154.                 USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);

  155.                 if (uart3_TxCounter > uart3_RxCounter) {
  156.                         //开启接收,禁止发送
  157.                         uart3_EN_0;
  158.                         uart3_RxCounter = 0;
  159.                         USART_ITConfig(USART3, USART_IT_TC, DISABLE);
  160.                         USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
  161.                         USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
  162.                 }
  163.         }
  164. }
  165. #endif

  166. /* 串口3收到数据从串口6发送,串口6收到数据从串口3发送 */
  167. #if 1
  168. uint8_t uart6_RxBuffer[500];
  169. uint8_t uart6_TxBuffer[500];
  170. uint8_t uart6_TxSize = 0;
  171. uint8_t uart6_TxCounter = 0;
  172. uint8_t uart6_RxCounter = 0;

  173. uint8_t uart3_RxBuffer[500];
  174. uint8_t uart3_TxBuffer[500];
  175. uint8_t uart3_TxSize = 0;
  176. uint8_t uart3_TxCounter = 0;
  177. uint8_t uart3_RxCounter = 0;

  178. void src_cpy2_des(uint8_t* src, uint8_t* des, uint8_t len) {
  179.         while (len--) *des++ = *src++;
  180. }

  181. void USART6_IRQHandler(void) {
  182.         if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) {
  183.                 uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART6);
  184.         }

  185.         if (USART_GetITStatus(USART6, USART_IT_IDLE) != RESET) {
  186.                 USART_ReceiveData(USART6); //清中断

  187.                 uart3_EN_1;
  188.                 uart3_TxCounter = 0;
  189.                 uart3_TxSize = uart6_RxCounter;
  190.                 src_cpy2_des(uart6_RxBuffer, uart3_TxBuffer, uart3_TxSize);
  191.                 do { /* 用户数据处理写在此处 */








  192.                 } while (0);
  193.                 USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);

  194.                 uart6_RxCounter = 0;
  195.         }

  196.         if (USART_GetITStatus(USART6, USART_IT_TC) != RESET) {
  197.                 if (uart6_TxCounter > uart6_TxSize) {
  198.                         uart6_EN_0; //开启接收,禁止发送
  199.                 }
  200.                 else{
  201.                         USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);
  202.                 }
  203.                 USART_ClearFlag(USART6, USART_FLAG_TC);
  204.         }
  205. }

  206. void USART3_IRQHandler(void) {
  207.         if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
  208.                 uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
  209.         }

  210.         if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
  211.                 USART_ReceiveData(USART3); //清中断

  212.                 uart6_EN_1;
  213.                 uart6_TxCounter = 0;
  214.                 uart6_TxSize = uart3_RxCounter;
  215.                 src_cpy2_des(uart3_RxBuffer, uart6_TxBuffer, uart6_TxSize);
  216.                 do { /* 用户数据处理写在此处 */








  217.                 } while (0);
  218.                 USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);

  219.                 uart3_RxCounter = 0;
  220.         }

  221.         if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
  222.                 if (uart3_TxCounter > uart3_TxSize) {
  223.                         uart3_EN_0; //开启接收,禁止发送
  224.                 }
  225.                 else{
  226.                         USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
  227.                 }
  228.                 USART_ClearFlag(USART3, USART_FLAG_TC);
  229.         }
  230. }
  231. #endif
复制代码




回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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