久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2513|回復: 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
復制代碼




分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 特一级毛片 | www网站在线观看 | 日韩视频91| 久久亚洲春色中文字幕久久久 | 亚洲福利一区 | 成人免费影院 | 国产成人免费视频网站高清观看视频 | 中文久久| 九九久久这里只有精品 | 欧美综合在线观看 | 精品福利在线视频 | 欧美一区中文字幕 | 国产我和子的乱视频网站 | 亚洲精品在线播放 | 午夜性视频 | 亚洲一区国产精品 | 波多野结衣先锋影音 | 亚洲黄色国产 | 一区二区三区四区视频 | 久久er99热精品一区二区 | 一级做受毛片免费大片 | 成人免费一区二区三区视频网站 | 免费99视频 | 国产精品美女一区二区 | 国产一区二区日韩 | 在线观看成人小视频 | av网站在线播放 | 一级一级一级毛片 | 日韩在线免费电影 | 中文字幕免费视频 | 日韩一区和二区 | 午夜免费影视 | 狠狠亚洲| 四虎永久在线精品免费一区二 | 日本色综合 | avav在线看| 在线91| 在线 丝袜 欧美 日韩 制服 | 精品网站999 | 亚洲一视频 | av黄色在线 |