stm32 rs485采用RS485通信,用串口1直接接SP3485可以吗

STM32F407VGT6的485通信程序【SP3485芯片&中断接收】_中华文本库
第1页/共5页
STM32F407VGT6的485通信程序
【SP3485芯片&中断接收】
本例程为STM32F4XX(M4内核)的485通信程序,采用串口1发送和接收数据,中断接收,将接收到的数据重新发送出去。
主函数main.c文件如下:
#include "stm32f4xx.h"
/**********************************************************\ ** 文 件 名:
mian.c ************************************** ** 库 版 本:
STM32F4xx_DSP_StdPeriph_Lib_V1.0.1 ********* ** 工作环境:
RealView MDK-ARM 4.23 ********************* ** 作
曾有根 ************************************* ** 生成日期:
********************************** ** 功
RS485 通过串口1发送,中断接收!将接收到***
的数据通过再次发送出去 ********************* \**********************************************************/
extern void uart_init(void); extern void USART1_SendByte(u8 Data); extern unsigned char UART1_GetByte(u8 GetData); extern void delay(unsigned int dl);
void delay(unsigned int dl) {
unsigned int i,y;
for(i = 0; i & 5000; i++)
for(y = 0; y & y++);
static void led_init(void) {
GPIO_InitTypeDef
GPIO_InitS
/* Enable the GPIO_LED Clock */
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE);
/* Configure the GPIO_LED pin */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 ;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
第1页/共5页
寻找更多 ""stm32rhf407zet6的用法之rs232与rs485通信_stm32吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:24,418贴子:
stm32rhf407zet6的用法之rs232与rs485通信收藏
我这几天用了下rs485,就是用电脑的串口调试助手通过rs232把命令发给单片机,然后再用单片机通过rs485发给罗盘,然后再用rs485接受到的数据通过rs232发到串口调试助手,显示在电脑屏幕上,可是我发的读取角度命令之后,收的是12个0.而且把rs485的A B线拔了结果一样,这是为什么啊?我用的uart5来处理rs232的数据,用uart4来处理rs485数据。ps:该罗盘只要给收到特定的命令字就会自动返回角度值。
望大神帮我解决一下,谢谢
stm32让您能优化架构,电源管理模式,取得超低功耗外设,支持节能型USB功能,立即了解stm32 最新资讯和技术交流!
在附加一些程序段:#include &stdio.h&#include &stm32f4xx.h&#include &app_protocol_can.h&#include &uart_protocol.h&#include &drv_button.h&#include &drv_can.h&#include &drv_delay.h&#include &drv_led.h&#include &drv_uart.h&#include &drv_timer.h&//RS232串口相关定义#define SCOM_BASE
UART5#define SCOM_CLK
RCC_APB1Periph_UART5#define SCOM_RCC_CMD
RCC_APB1#define SCOM_TX_PIN
GPIO_Pin_12#define SCOM_TX_GPIO_PORT
GPIOC#define SCOM_TX_GPIO_CLK
RCC_AHB1Periph_GPIOC#define SCOM_TX_SOURCE
GPIO_PinSource12#define SCOM_TX_AF
GPIO_AF_UART5#define SCOM_RX_PIN
GPIO_Pin_2#define SCOM_RX_GPIO_PORT
GPIOD#define SCOM_RX_GPIO_CLK
RCC_AHB1Periph_GPIOD#define SCOM_RX_SOURCE
GPIO_PinSource2#define SCOM_RX_AF
GPIO_AF_UART5//RS485串口相关定义#define RS_485
UART4#define RS_CLK
RCC_APB1Periph_UART4#define RS_RCC_CMD
RCC_APB1#define RS_TX_PIN
GPIO_Pin_2#define RS_TX_GPIO_PORT
GPIOA#define RS_TX_GPIO_CLK
RCC_AHB1Periph_GPIOA#define RS_TX_SOURCE
GPIO_PinSource2#define RS_TX_AF
GPIO_AF_UART4#define RS_RX_PIN
GPIO_Pin_3#define RS_RX_GPIO_PORT
GPIOA#define RS_RX_GPIO_CLK
RCC_AHB1Periph_GPIOA#define RS_RX_SOURCE
GPIO_PinSource3#define RS_RX_AF
GPIO_AF_UART4// 定义平台电机驱动器地址#define MOTOR_DIRVER_ADDRESS
// 需要根据具体的底盘型号说明书设置//系统时针寄存器变量RCC_ClocksTypeDef
RCC_C//ms计数器volatile unsigned long g_ulMsC//系统初始化函数void app_main_system_init( void );u8//Anolog[4];u8 Uart1_Rx=0;char buff[0x0E];u8 flag=0;void ScomConfig(void);void USART1_IRQHandler(void);//============================================================================// 名称: main// 功能: C主函数// 参数: 无// 返回: 无// 说明: 无//============================================================================// 主函数int main(void){uint8_t rxchar,RXC;ScomConfig();
printf(&Please input char:\r\n&);
{while( USART_GetFlagStatus(SCOM_BASE, USART_FLAG_RXNE) == RESET );
USART_ClearITPendingBit(USART1,USART_IT_RXNE);//清除中断标志rxchar= USART_ReceiveData(SCOM_BASE) & 0xFF;/*if(rxchar==1){
USART_SendData(SCOM_BASE,'6');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'8');drv_delay_ms(1000);USART_SendData(SCOM_BASE,'0');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'4');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'0');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'0');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'0');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'3');drv_delay_ms(1000);
USART_SendData(SCOM_BASE,'0');drv_delay_ms(1000);USART_SendData(SCOM_BASE,'7');drv_delay_ms(1000);//USART_SendData(SCOM_BASE,'\r');}else
*/USART_SendData(RS_485,(uint8_t) rxchar);RXC=USART_ReceiveData(RS_485) & 0xFF;USART_SendData(SCOM_BASE,(uint8_t) RXC);
}}// 配置串口void ScomConfig(void){
GPIO_InitTypeDef GPIO_InitS
USART_InitTypeDef USART_InitS
// 使能RXD和TXD端口外设时钟
RCC_AHB1PeriphClockCmd(SCOM_TX_GPIO_CLK | SCOM_RX_GPIO_CLK, ENABLE);
// 使能串口外设时钟
RCC_APB1PeriphClockCmd(SCOM_CLK, ENABLE);
// 连接口线到串口发送脚
GPIO_PinAFConfig(SCOM_TX_GPIO_PORT, SCOM_TX_SOURCE, SCOM_TX_AF);
// 连接口线到串口接收脚
GPIO_PinAFConfig(SCOM_RX_GPIO_PORT, SCOM_RX_SOURCE, SCOM_RX_AF);
// 配置串口发送脚为复用功能
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = SCOM_TX_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SCOM_TX_GPIO_PORT, &GPIO_InitStructure);
// 配置串口接收脚为复用功能
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Pin = SCOM_RX_PIN;
GPIO_Init(SCOM_RX_GPIO_PORT, &GPIO_InitStructure);
// 配置串口
USART_InitStructure.USART_BaudRate = 9600;
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_N
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(SCOM_BASE, &USART_InitStructure);
// 使能串口
USART_Cmd(SCOM_BASE, ENABLE);}//printf实现int fputc(int ch, FILE *f){
USART_SendData(SCOM_BASE, (uint8_t)ch);
while(USART_GetFlagStatus(SCOM_BASE, USART_FLAG_TXE) == RESET);
}void USART1_IRQHandler(void)
////USART1_printf(USART1,&c&);FlagStatus fl=USART_GetFlagStatus(SCOM_BASE, USART_FLAG_RXNE);if(fl!=RESET){USART_ClearITPendingBit(SCOM_BASE, USART_FLAG_RXNE);//这里接受数据key=USART_ReceiveData(SCOM_BASE) & 0xFF;//USART_SendData(SCOM_BASE,key);if(key==0xFF){Uart1_Rx=0;buff[Uart1_Rx]=Uart1_Rx++;}else{buff[Uart1_Rx]=Uart1_Rx++;}if(Uart1_Rx==14){ flag=1;}}//if(USART_GetFlagStatus(USART1,USART_FLAG_ORE)==SET)//{//USART_ClearFlag(USART1,USART_FLAG_ORE);//USART_ReceiveData(USART1);//}} //============================================================================// 名称: app_main_system_init// 功能: 系统初始化// 参数: 无// 返回: 无// 说明: 无//============================================================================void app_main_system_init( void ){
drv_led_init();
drv_button_init();
// 注意按键初始化为查询模式
drv_delay_init();
drv_timer_init();
drv_can_init();
//初始化定位模块接口串口
//drv_uart_init(UartDbg,UART_BAUDRATE_115200);
//有修改,注释了
//UART_DMA_Configuration();
//有修改,注释了
//初始化系统接口串口,本例程中没有使用此串口,在进行开发板与PC通信时可使用此串口
drv_uart_init(UartSys,UART_BAUDRATE_115200);
//SYSTEM_UART_DMA_Configuration();
//有修改,注释了
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000); }//系统1ms时钟处理函数void SysTickIntHandler(void){
// this will works 49days so not do the run over exe
g_ulMsCounters++;
gpReceive(uartRxBuf,&uartReadIndex,uartWriteIndex);}
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或stm32(1)
功能:通过PC机的串口调试助手发送数据给串口A,串口B接收到串口A发送的数据,再由串口B将接收到的数据返回给PC机的串口调试助手。
PC-&串口A-&串口B-&PC。
实验平台:stm32f407
/*********************************************************************
本平台具有六个串口:
com3 &232 需一个管脚控制DTU
com5 &调试串口 &TTL
*********************************************************************/
本实验用的的串口A对应为com1,串口B对应为com4。
本程序的设计思路:
通过PC机串口调试助手发送数据,当串口A发现有数据过来时,产生串口接收中断,把数据保存到自定义的接收缓冲区中,然后串口接收函数去缓冲区中去取数据保存在发送缓冲区中,再将数据通过串口B发送给串口调试助手。
multiple_serial.h
#ifndef multiple_serial_H
#define multiple_serial_H
#define MAX_RECV_BUF_LEN
#define MAX_RECV_DATA_MASK
#define MAX_COM_COUNT
intint16_t;
intuint16_t;
#define NEXTAI_COM1_NAME&COM1&
#define NEXTAI_COM2_NAME&COM2&
#define NEXTAI_COM3_NAME&COM3&
#define NEXTAI_COM4_NAME&COM4&
#define NEXTAI_COM5_NAME&COM5&
#define NEXTAI_COM6_NAME&COM6&
NEXTAI_COM1_ID=1,
NEXTAI_COM2_ID,
NEXTAI_COM3_ID,
NEXTAI_COM4_ID,
NEXTAI_COM5_ID,
NEXTAI_COM6_ID
struct _NEXTAI_CHANNEL_INFO_
local_com_id;
//nextai_uint8 *
//nextai_uint16 data_read_p;
//nextai_uint16 data_write_p;
used_flag;
}NEXTAI_CHANNEL_INFO;
/*******************************************************************************
* local_channel_data : 通道数据结构
* data_recv_flag : 是否有数据flag,1表示
*******************************************************************************/
struct _NEXTAI_MGR_INFO_
NEXTAI_CHANNEL_INFO local_channel_data[MAX_COM_COUNT];
data_recv_flag;
}NEXTAI_MGR_INFO;
static NEXTAI_MGR_INFO*
local_com_mgr_p=0;
void serial_nvic_Configuration(uint8_t
void serial_gpio_configuration(uint8_t
void serial_rcc_configuration(uint8_t
uint8_t serial_Configuration(uint8_t*comName);
uint8_t serial_send(uint8_t
comID,uint8_t*databuf,uint32_t
uint32_t serial_recv(uint8_t
comID,uint8_t*databuf,uint32_t
void clean_rebuff(void);
multiple_serial.c
#include&multiple_serial.h&
#include&stm32f4xx_gpio.h&
#include&stm32f4xx_usart.h&
#include&string.h&
char uart_buf[MAX_RECV_BUF_LEN]={0};
uint8_t com1_recv_buf[MAX_RECV_BUF_LEN]={0};
int com1_gloabl_p=0;
int com1_read_p=0;
uint8_t com4_recv_buf[MAX_RECV_BUF_LEN]={0};
int com4_gloabl_p=0;
int com4_read_p=0;
uint8_t com5_recv_buf[MAX_RECV_BUF_LEN]={0};
int com5_gloabl_p=0;
int com5_read_p=0;
uint8_t com6_recv_buf[MAX_RECV_BUF_LEN]={0};
int com6_gloabl_p=0;
int com6_read_p=0;
//串口gpio的配置
void serial_gpio_configuration(uint8_t
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD,
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,
Configure USART Tx and Rx as alternate function push-pull */
== NEXTAI_COM1_ID)
GPIO_StructInit(&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,
GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA,
GPIO_PinSource10, GPIO_AF_USART1);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_9;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_10;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_4;
GPIO_Init(GPIOF,&GPIO_InitStructure);
elseif(comID==
NEXTAI_COM4_ID)
GPIO_StructInit(&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,
GPIO_PinSource0, GPIO_AF_UART4);
GPIO_PinAFConfig(GPIOA,
GPIO_PinSource1, GPIO_AF_UART4);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_1;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_0;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_7;
GPIO_Init(GPIOA,&GPIO_InitStructure);
elseif(comID==
NEXTAI_COM5_ID)
GPIO_StructInit(&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,
GPIO_PinSource12, GPIO_AF_UART5);
GPIO_PinAFConfig(GPIOD,
GPIO_PinSource2, GPIO_AF_UART5);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_2;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_12;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
elseif(comID==
NEXTAI_COM6_ID)
GPIO_StructInit(&GPIO_InitStructure);
GPIO_PinAFConfig(GPIOC,
GPIO_PinSource6, GPIO_AF_USART6);
GPIO_PinAFConfig(GPIOC,
GPIO_PinSource7, GPIO_AF_USART6);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_6;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_7;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode=
GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed=
GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType=
GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd=
GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Pin=
GPIO_Pin_8;
GPIO_Init(GPIOC,&GPIO_InitStructure);
//串口时钟的配置
void serial_rcc_configuration(uint8_t
/* Enable USART clock */
if(comID==
NEXTAI_COM1_ID)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,
elseif(comID==
NEXTAI_COM4_ID)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
elseif(comID==
NEXTAI_COM5_ID)
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
elseif(comID==
NEXTAI_COM6_ID)
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6,
//配置各个串口的中断处理函数,优先级相同
void serial_nvic_Configuration(uint8_t
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
if(comID==
NEXTAI_COM1_ID)
NVIC_InitStructure.NVIC_IRQChannel=
USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=
NVIC_Init(&NVIC_InitStructure);
elseif(comID==
NEXTAI_COM4_ID)
NVIC_InitStructure.NVIC_IRQChannel=
UART4_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=
NVIC_Init(&NVIC_InitStructure);
elseif(comID==
NEXTAI_COM5_ID)
NVIC_InitStructure.NVIC_IRQChannel=
UART5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=
NVIC_Init(&NVIC_InitStructure);
elseif(comID==
NEXTAI_COM6_ID)
NVIC_InitStructure.NVIC_IRQChannel=
USART6_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=
NVIC_Init(&NVIC_InitStructure);
/*串口参数配置*/
uint8_t serial_Configuration(uint8_t*comName)
USART_InitTypeDef USART_InitStructure;
if(strcmp(NEXTAI_COM1_NAME,(constchar*)comName)==0)
NEXTAI_COM1_ID;
elseif(strcmp(NEXTAI_COM4_NAME,(constchar*)comName)==0)
NEXTAI_COM4_ID;
elseif(strcmp(NEXTAI_COM5_NAME,(constchar*)comName)==0)
NEXTAI_COM5_ID;
elseif(strcmp(NEXTAI_COM6_NAME,(constchar*)comName)==0)
NEXTAI_COM6_ID;
System Clocks Configuration */
serial_rcc_configuration(comID);
Configure the GPIO ports */
serial_gpio_configuration(comID);
USART_InitStructure.USART_WordLength=
USART_WordLength_8b;
USART_InitStructure.USART_Parity=
USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl=
USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=
USART_Mode_Rx| USART_Mode_Tx;
if(comID==
NEXTAI_COM1_ID)
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_StopBits=
USART_StopBits_1;
USART_Init(USART1,&USART_InitStructure);
elseif(comID==
NEXTAI_COM4_ID)
USART_InitStructure.USART_BaudRate=38400;
USART_InitStructure.USART_StopBits=
USART_StopBits_1;
USART_Init(UART4,&USART_InitStructure);
elseif(comID==
NEXTAI_COM5_ID)
USART_InitStructure.USART_BaudRate=115200;
USART_InitStructure.USART_StopBits=
USART_StopBits_1;
USART_Init(UART5,&USART_InitStructure);
elseif(comID==
NEXTAI_COM6_ID)
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_StopBits=
USART_StopBits_1;
USART_Init(USART6,&USART_InitStructure);
serial_nvic_Configuration(comID);
if(comID==
NEXTAI_COM1_ID)
USART_Cmd(USART1,
USART_ITConfig(USART1,
USART_IT_RXNE, ENABLE);
elseif(comID==
NEXTAI_COM4_ID)
USART_Cmd(UART4,
USART_ITConfig(UART4,
USART_IT_RXNE, ENABLE);
elseif(comID==
NEXTAI_COM5_ID)
USART_Cmd(UART5,
USART_ITConfig(UART5,
USART_IT_RXNE, ENABLE);
elseif(comID==
NEXTAI_COM6_ID)
USART_Cmd(USART6,
USART_ITConfig(USART6,
USART_IT_RXNE, ENABLE);
/*******************************************************************************
* 函数名 : serial_send
* 描 述 : 发送某个端口的数据
* 输 入 : comID: 串口标识号
databuf: 发送数据缓冲区
datalen: 发送数据长度
* 输 出 : None
* 返 回 : None
*******************************************************************************/
uint8_t serial_send(uint8_t
comID,uint8_t*databuf,uint32_t
nextai_timer =0;
if(comID==
NEXTAI_COM1_ID)
GPIO_SetBits(GPIOF,
GPIO_Pin_4);
//清除标志位,否则第1位数据会丢失
USART_GetFlagStatus(USART1,
USART_FLAG_TC);
i& datalen; i++)
需添加超时检测
USART_SendData(USART1,databuf[i]);
while(USART_GetFlagStatus(USART1,
USART_FLAG_TC)== RESET)
nextai_timer++;
if(nextai_timer&&8)
GPIO_ResetBits(GPIOF,
GPIO_Pin_4);
elseif(comID==
NEXTAI_COM4_ID)
GPIO_SetBits(GPIOA,
GPIO_Pin_7);
//清除标志位,否则第1位数据会丢失
USART_GetFlagStatus(UART4,
USART_FLAG_TC);
i& datalen; i++)
USART_SendData(UART4,databuf[i]);
while(USART_GetFlagStatus(UART4,
USART_FLAG_TC)== RESET)
nextai_timer++;
if(nextai_timer&&8)
GPIO_ResetBits(GPIOA,
GPIO_Pin_7);
elseif(comID==
NEXTAI_COM5_ID)
//清除标志位,否则第1位数据会丢失
USART_GetFlagStatus(UART5,USART_FLAG_TC);
i& datalen; i++)
while(USART_GetFlagStatus(UART5,
USART_FLAG_TC)== RESET);
USART_SendData(UART5,databuf[i]);
while(USART_GetFlagStatus(UART5,
USART_FLAG_TC)== RESET)
nextai_timer++;
if(nextai_timer&&8)
elseif(comID==
NEXTAI_COM6_ID)
GPIO_SetBits(GPIOC,
GPIO_Pin_8);
//清除标志位,否则第1位数据会丢失
USART_GetFlagStatus(USART6,USART_FLAG_TC);
i& datalen; i++)
USART_SendData(USART6,databuf[i]);
while(USART_GetFlagStatus(USART6,
USART_FLAG_TC)== RESET)
nextai_timer++;
if(nextai_timer&&8)
GPIO_ResetBits(GPIOC,
GPIO_Pin_8);
/*******************************************************************************
* 函数名 : serial_recv
* 描 述 : 接收一定长度的数据(从内存中读取数据)
* 输 入 : comID: 串口标识号
databuf: 接收数据缓冲区
datalen: 需要接收数据长度
* 输 出 : None
* 返 回 : 实际接收到的数据长度
*******************************************************************************/
uint32_t serial_recv(uint8_t
comID,uint8_t*databuf,uint32_t
nextai_indx=0,
NEXTAI_MGR_INFO*me=
local_com_mgr_p;
if((comID&
NEXTAI_COM6_ID)||(comID&
NEXTAI_COM1_ID))
0xffffffff;
nextai_indx=
comID- NEXTAI_COM1_ID;
if(me-&local_channel_data[nextai_indx].used_flag==0)
0xffffffff;
if(!(me-&data_recv_flag&(1&&
nextai_indx)))
0xffffffff;
if(comID==
NEXTAI_COM1_ID)
if(com1_read_p==
com1_gloabl_p)
databuf[i]=
com1_recv_buf[com1_read_p];
com1_read_p++;
com1_read_p&=
MAX_RECV_DATA_MASK;
i++;
j++);
me-&data_recv_flag=
me-&data_recv_flag&(~(1&&
nextai_indx));
if(comID==
NEXTAI_COM4_ID)
if(com4_read_p==
com4_gloabl_p)
databuf[i]=
com4_recv_buf[com4_read_p];
com4_read_p++;
com4_read_p&=
MAX_RECV_DATA_MASK;
i++;
j++);
me-&data_recv_flag=
me-&data_recv_flag&(~(1&&
nextai_indx));
if(comID==
NEXTAI_COM5_ID)
if(com5_read_p==
com5_gloabl_p)
databuf[i]=
com5_recv_buf[com5_read_p];
com5_read_p++;
com5_read_p&=
MAX_RECV_DATA_MASK;
i++;
j++);
me-&data_recv_flag=
me-&data_recv_flag&(~(1&&
nextai_indx));
if(comID==
NEXTAI_COM6_ID)
if(com6_read_p==
com6_gloabl_p)
databuf[i]=
com6_recv_buf[com6_read_p];
com6_read_p++;
com6_read_p&=
MAX_RECV_DATA_MASK;
i++;
j++);
me-&data_recv_flag=
me-&data_recv_flag&(~(1&&
nextai_indx));
//中断处理函数只是将发送过来的数据保存到内存
void USART1_IRQHandler(void)
NEXTAI_MGR_INFO*me=
local_com_mgr_p;
if(USART_GetITStatus(USART1,
USART_IT_RXNE)!= RESET)
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
com1_recv_buf[com1_gloabl_p]=
USART_ReceiveData(USART1);
com1_gloabl_p++;
com1_gloabl_p&=
MAX_RECV_DATA_MASK;
me-&data_recv_flag|=0x01;
//void UART5_IRQHandler(void)
uint8_t temp;
if(USART_GetITStatus(UART5,
USART_IT_RXNE)!= RESET)
temp= USART_ReceiveData(UART5);
USART_SendData(UART5,temp);
void UART4_IRQHandler(void)
NEXTAI_MGR_INFO*me=
local_com_mgr_p;
if(USART_GetITStatus(UART4,
USART_IT_RXNE)!= RESET)
com4_recv_buf[com4_gloabl_p++]=
USART_ReceiveData(UART4);
com4_gloabl_p&=
MAX_RECV_DATA_MASK;
me-&data_recv_flag|=0x08;
void UART5_IRQHandler(void)
NEXTAI_MGR_INFO *me = local_com_mgr_p;
if(USART_GetITStatus(UART5, USART_IT_RXNE)
com5_recv_buf[com5_gloabl_p++] = USART_ReceiveData(UART5);
com5_gloabl_p &= MAX_RECV_DATA_MASK;
me-&data_recv_flag |= 0x10;
void USART6_IRQHandler(void)
NEXTAI_MGR_INFO*me=
local_com_mgr_p;
if(USART_GetITStatus(USART6,
USART_IT_RXNE)!= RESET)
com6_recv_buf[com6_gloabl_p++]=
USART_ReceiveData(USART6);
com6_gloabl_p&=
MAX_RECV_DATA_MASK;
me-&data_recv_flag|=0x20;
#include&multiple_serial.h&
#include&stm32f4xx_gpio.h&
#include&stm32f4xx_usart.h&
#include&string.h&
#include&stdio.h&
int main()
char*COM1=&COM1&;
char*COM4=&COM4&;
serial_Configuration((uint8_t*)COM1);
serial_Configuration((uint8_t*)COM4);
char uart_buf[64];
char printf_buf[128]={0};
serial_recv(1, uart_buf,64);
//延时让serial_recv函数接收完PC机串口调试助手发来的全部数据
for(i=10000;i!=0;i--)
for(j=3000;j!=0;j--);
if(flag&0)
sprintf((char*)printf_buf,&receive
data len is %d\n&, flag);//将字符串写入printf_buf中
serial_send(4,
printf_buf, strlen((char*)printf_buf));//发送printf_buf中的内容到串口
serial_send(4,
uart_buf, flag+1);
测试结果如下图所示:
刚才改了下程序:
#include&multiple_serial.h&
#include&stm32f4xx_gpio.h&
#include&stm32f4xx_usart.h&
#include&string.h&
#include&stdio.h&
int main()
char*COM1=&COM1&;
char*COM4=&COM4&;
char*COM5=&COM5&;
char*COM6=&COM6&;
serial_Configuration((uint8_t*)COM1);
serial_Configuration((uint8_t*)COM6);
char uart_buf[64];
&&&&&&&&int i=0,j;
char printf_buf[128]={0};
&&&&&&&&uint8_t flag=0;
//从串口调试助手发送数据,串口A接收到数据后产生中断,在中断函数中将数据保存到com6_recv_buf中,
//然后通过serial_recv函数将数据从com6_recv_buf中保存到接收uart_buf中,最后调用serial_send回传给串口调试助手。
serial_recv(1, uart_buf,64);
if(flag&0)
serial_send(4,
uart_buf, flag+1);
测试结果如下图所示:
通过发送和接收的数据对比,发现程序设计的还是比较稳定的,没有出现数据丢失的现象。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:784次
排名:千里之外}

我要回帖

更多关于 stm32 rs485电路 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信