标题:
求大神添加CRC16程序 完成标准的MODBUS通信,本人实测该代码的数据透传有效 !
[打印本页]
作者:
17351371760
时间:
2018-11-13 17:06
标题:
求大神添加CRC16程序 完成标准的MODBUS通信,本人实测该代码的数据透传有效 !
#include "stm32f4xx.h"
#include "misc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_crc.h"
#define uart6_EN_1 GPIO_SetBits(GPIOB, GPIO_Pin_15); //485发送使能
#define uart6_EN_0 GPIO_ResetBits(GPIOB, GPIO_Pin_15); //485接收使能
#define uart3_EN_1 GPIO_SetBits(GPIOD, GPIO_Pin_10); //485发送使能
#define uart3_EN_0 GPIO_ResetBits(GPIOD, GPIO_Pin_10); //485接收使能
void NVIC_Config(void) {
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Config(void) {
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //使能GPIOD时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE);//使能USART3时钟
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);//使能USART6时钟
/* ---------------串口3 IO口----------------- */
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); //PD8复用为串口3
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); //PD9复用为串口3
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;//GPIO8与GPIO9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD8、PD9
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PD10 控制485_2的输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD, &GPIO_InitStructure);
/* --------------------------------------------- */
/* ---------------串口6 IO口----------------- */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//PD6复用为串口6
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//PD7复用为串口6
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; //GPIO6与GPIO7
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽复用输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOD,&GPIO_InitStructure); //初始化PD6、PD7
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15; //PB15控制485_1的输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //速度100MHz
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽输出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //上拉
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* --------------------------------------------- */
/* USART configured as follow: //串口配置如下:
- BaudRate = 115200 baud //波特率:115200
- Word Length = 8 Bits //字节长度=8比特
- One Stop Bit //一个停止位
- No parity //无校验码位
- Hardware flow control disabled (RTS and CTS signals)//禁止硬件流控制
- Receive and transmit enabled //接受发送已启动
*/
USART_InitStructure.USART_BaudRate = 9600;//串口的波特率设置为9600bps
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
/*-------------------------------------*/
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//设置串口既可以发送也可以接收数据
USART_Init(USART3, &USART_InitStructure);//设置串口3
USART_Init(USART6, &USART_InitStructure);//设置串口6
USART_ITConfig(USART6, USART_IT_TC, ENABLE);
USART_ITConfig(USART6, USART_IT_IDLE, ENABLE);
USART_ITConfig(USART6, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_TC, ENABLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_Cmd(USART6, ENABLE);
USART_Cmd(USART3, ENABLE);
uart6_EN_0;
uart3_EN_0;
}
int main(void) {
NVIC_Config();
USART_Config();
while (1) {
; //中断里实现串口收发,数据处理
}
}
#if 0 /*串口6调通,收什么发什么*/
uint8_t uart6_RxBuffer[500];//设置数据缓冲
uint8_t uart6_TxCounter = 0;
uint16_t uart6_RxCounter = 0;
void USART3_IRQHandler(void) {
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET){
uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART3);
}
if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
USART_ReceiveData(USART3); //清中断
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);
//数据处理
uart6_EN_1;
USART_ITConfig(USART3, USART_IT_TC, ENABLE);
uart6_TxCounter = 0;
USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);
}
if (USART_GetITStatus(USART3, USART_IT_TC) != RESET){
USART_SendData(USART3, uart6_RxBuffer[uart6_TxCounter++]);
if (uart6_TxCounter > uart6_RxCounter){
//开启接收,禁止发送
uart6_EN_0;
uart6_RxCounter = 0;
USART_ITConfig(USART3, USART_IT_TC, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
}
}
}
uint8_t uart3_RxBuffer[500];
uint8_t uart3_TxCounter = 0;
uint16_t uart3_RxCounter = 0;
void USART3_IRQHandler(void) {
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
}
if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
USART_ReceiveData(USART3); //清中断
USART_ITConfig(USART3, USART_IT_RXNE, DISABLE); //禁止接收
USART_ITConfig(USART3, USART_IT_IDLE, DISABLE);
//数据处理
uart3_EN_1;
USART_ITConfig(USART3, USART_IT_TC, ENABLE);
uart3_TxCounter = 0;
USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);
}
if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
USART_SendData(USART3, uart3_RxBuffer[uart3_TxCounter++]);
if (uart3_TxCounter > uart3_RxCounter) {
//开启接收,禁止发送
uart3_EN_0;
uart3_RxCounter = 0;
USART_ITConfig(USART3, USART_IT_TC, DISABLE);
USART_ITConfig(USART3, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART3, USART_IT_IDLE, ENABLE);
}
}
}
#endif
/* 串口3收到数据从串口6发送,串口6收到数据从串口3发送 */
#if 1
uint8_t uart6_RxBuffer[500];
uint8_t uart6_TxBuffer[500];
uint8_t uart6_TxSize = 0;
uint8_t uart6_TxCounter = 0;
uint8_t uart6_RxCounter = 0;
uint8_t uart3_RxBuffer[500];
uint8_t uart3_TxBuffer[500];
uint8_t uart3_TxSize = 0;
uint8_t uart3_TxCounter = 0;
uint8_t uart3_RxCounter = 0;
void src_cpy2_des(uint8_t* src, uint8_t* des, uint8_t len) {
while (len--) *des++ = *src++;
}
void USART6_IRQHandler(void) {
if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) {
uart6_RxBuffer[uart6_RxCounter++] = USART_ReceiveData(USART6);
}
if (USART_GetITStatus(USART6, USART_IT_IDLE) != RESET) {
USART_ReceiveData(USART6); //清中断
uart3_EN_1;
uart3_TxCounter = 0;
uart3_TxSize = uart6_RxCounter;
src_cpy2_des(uart6_RxBuffer, uart3_TxBuffer, uart3_TxSize);
do { /* 用户数据处理写在此处 */
} while (0);
USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
uart6_RxCounter = 0;
}
if (USART_GetITStatus(USART6, USART_IT_TC) != RESET) {
if (uart6_TxCounter > uart6_TxSize) {
uart6_EN_0; //开启接收,禁止发送
}
else{
USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);
}
USART_ClearFlag(USART6, USART_FLAG_TC);
}
}
void USART3_IRQHandler(void) {
if (USART_GetITStatus(USART3, USART_IT_RXNE) != RESET) {
uart3_RxBuffer[uart3_RxCounter++] = USART_ReceiveData(USART3);
}
if (USART_GetITStatus(USART3, USART_IT_IDLE) != RESET) {
USART_ReceiveData(USART3); //清中断
uart6_EN_1;
uart6_TxCounter = 0;
uart6_TxSize = uart3_RxCounter;
src_cpy2_des(uart3_RxBuffer, uart6_TxBuffer, uart6_TxSize);
do { /* 用户数据处理写在此处 */
} while (0);
USART_SendData(USART6, uart6_TxBuffer[uart6_TxCounter++]);
uart3_RxCounter = 0;
}
if (USART_GetITStatus(USART3, USART_IT_TC) != RESET) {
if (uart3_TxCounter > uart3_TxSize) {
uart3_EN_0; //开启接收,禁止发送
}
else{
USART_SendData(USART3, uart3_TxBuffer[uart3_TxCounter++]);
}
USART_ClearFlag(USART3, USART_FLAG_TC);
}
}
#endif
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1