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

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2470|回復(fù): 0
收起左側(cè)

求大神添加CRC16程序 完成標(biāo)準(zhǔn)的MODBUS通信,本人實(shí)測該代碼的數(shù)據(jù)透傳有效 !

[復(fù)制鏈接]
ID:415061 發(fā)表于 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發(fā)送使能
  8. #define uart6_EN_0        GPIO_ResetBits(GPIOB, GPIO_Pin_15); //485接收使能

  9. #define uart3_EN_1        GPIO_SetBits(GPIOD, GPIO_Pin_10); //485發(fā)送使能
  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時(shí)鐘
  29.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);//使能USART3時(shí)鐘
  30.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART6時(shí)鐘

  31.         
  32.         
  33.         
  34.         /* ---------------串口3 IO口----------------- */
  35.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); //PD8復(fù)用為串口3
  36.         GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); //PD9復(fù)用為串口3
  37.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;//GPIO8與GPIO9
  38.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //復(fù)用功能
  39.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
  40.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽復(fù)用輸出
  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復(fù)用為串口6
  55.         GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//PD7復(fù)用為串口6

  56.         GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIO6與GPIO7
  57.         GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;        //復(fù)用功能
  58.         GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;        //速度100MHz
  59.         GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;      //推挽復(fù)用輸出
  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                //字節(jié)長度=8比特
  74.         - One Stop Bit                                //一個(gè)停止位
  75.         - No parity                                        //無校驗(yàn)碼位
  76.         - Hardware flow control disabled (RTS and CTS signals)//禁止硬件流控制
  77.         - Receive and transmit enabled        //接受發(fā)送已啟動
  78.         */
  79.         USART_InitStructure.USART_BaudRate = 9600;//串口的波特率設(shè)置為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;//設(shè)置串口既可以發(fā)送也可以接收數(shù)據(jù)
  86.         USART_Init(USART3, &USART_InitStructure);//設(shè)置串口3
  87.         USART_Init(USART6, &USART_InitStructure);//設(shè)置串口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.         ;                 //中斷里實(shí)現(xiàn)串口收發(fā),數(shù)據(jù)處理
  104.         }
  105. }

  106. #if 0   /*串口6調(diào)通,收什么發(fā)什么*/
  107. uint8_t uart6_RxBuffer[500];//設(shè)置數(shù)據(jù)緩沖
  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.                 //數(shù)據(jù)處理
  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.                         //開啟接收,禁止發(fā)送
  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.                 //數(shù)據(jù)處理
  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.                         //開啟接收,禁止發(fā)送
  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收到數(shù)據(jù)從串口6發(fā)送,串口6收到數(shù)據(jù)從串口3發(fā)送 */
  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 { /* 用戶數(shù)據(jù)處理寫在此處 */








  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; //開啟接收,禁止發(fā)送
  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 { /* 用戶數(shù)據(jù)處理寫在此處 */








  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; //開啟接收,禁止發(fā)送
  224.                 }
  225.                 else{
  226.                         USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
  227.                 }
  228.                 USART_ClearFlag(USART3, USART_FLAG_TC);
  229.         }
  230. }
  231. #endif
復(fù)制代碼




回復(fù)

使用道具 舉報(bào)

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

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 国产黑丝av | 日韩一区二区不卡 | 99在线观看视频 | 亚洲久视频 | 成人免费看黄网站在线观看 | 亚洲成人精选 | 色在线免费视频 | 久久成人国产 | 亚洲风情在线观看 | 久操av在线 | 日本不卡一区二区三区在线观看 | 久久一区二区av | 国产7777| 国产精久久久久久久妇剪断 | av中文字幕在线 | 国产欧美一区二区三区在线看 | 日韩精品一区二区三区免费视频 | 国产欧美精品区一区二区三区 | 亚洲欧美日韩国产综合 | 欧美激情啪啪 | 中文字幕在线第二页 | 午夜合集 | 日韩欧美国产一区二区 | 国产日韩欧美 | 久久久精彩视频 | 亚洲一区二区视频 | 一级大黄色片 | 亚洲成人免费观看 | 成人高清视频在线观看 | 综合伊人 | 久久精品免费观看 | 亚洲午夜三级 | 精品一区二区三区在线观看 | 99久久中文字幕三级久久日本 | 国产精品成人一区二区 | 99re视频在线免费观看 | 久久国产欧美日韩精品 | 国产1区2区在线观看 | 久久一区二区三区四区 | 日本人做爰大片免费观看一老师 | 久久91视频 |