Stm32RS485串口通信——中断接收发送数据
2014-11-18 13:25
323 查看
main.c源码:
/* *说明: *PA9:USART1_TX;PA10:USART1_RX */ #include "stm32f10x.h" #include "stm32f10x_rcc.h" #include "stm32f10x_gpio.h" #include "stm32f10x_usart.h" #include "stm32f10x_exti.h" #include "system_stm32f10x.h" #include "misc.h" void RCC_Configuration(void); void GPIO_Configuration(void); void USART_Configuration(void); void NVIC_Configuration(void); int main() { SystemInit();//72m RCC_Configuration(); GPIO_Configuration(); USART_Configuration(); NVIC_Configuration(); GPIO_ResetBits(GPIOE,GPIO_Pin_5); while(1); } void RCC_Configuration(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOE|RCC_APB2Periph_AFIO,ENABLE); RCC_APB1PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); } void GPIO_Configuration(void) { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //CS_485 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出 GPIO_Init(GPIOE, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //管脚2 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure); //TX初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //管脚3 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure); //RX初始化 } void USART_Configuration(void)//串口初始化函数 { //串口参数初始化 USART_InitTypeDef USART_InitStructure; //串口设置恢复默认参数 //初始化参数设置 USART_InitStructure.USART_BaudRate = 9600; //波特率9600 USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字长8位 USART_InitStructure.USART_StopBits = USART_StopBits_1; //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;//打开Rx接收和Tx发送功能 USART_Init(USART2,&USART_InitStructure); //初始化 USART_ITConfig(USART2,USART_IT_TXE,ENABLE); //允许串口1发送中断。 USART_ITConfig(USART2,USART_IT_RXNE,ENABLE); //允许串口1接收中断。 USART_Cmd(USART2,ENABLE); //启动串口 USART_ClearFlag(USART2,USART_FLAG_TC); //发送完成标志位 } void NVIC_Configuration(void)//配置中断优先级 { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); }stm32f10x_it.c的中断函数接收发送数据:
#include "..\include\stm32f10x.h" #include "..\include\stm32f10x_it.h" #include "..\include\stm32f10x_gpio.h" #include "..\include\stm32f10x_usart.h" void delay_ms(u16 time) { u16 i=0; while(time--) { i=12000; while(i--); } } void USART2_IRQHandler(void) { u8 RX_dat; if(USART_GetITStatus(USART2,USART_IT_RXNE)==SET)//USART_IT_RXNE:接收中断 { RX_dat=USART_ReceiveData(USART2); GPIO_SetBits(GPIOE,GPIO_Pin_5); delay_ms(1); USART_SendData(USART2,RX_dat); while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET){} //发送数据寄存器空标志位 delay_ms(2); GPIO_ResetBits(GPIOE,GPIO_Pin_5); } }
相关文章推荐
- stm32RS485串口通信——中断接收发送数据
- Stm32RS232串口通信——中断接收发送数据
- Android 串口通信之间的发送数据与接收数据(详解)
- STM8S003F使用IO口模拟串口(三)使用中断方式发送和接收数据
- Stm32RS232串口通信——中断接收发送数据
- dsPIC33F串口通讯中断接收发送数据-UART
- upD79F8513A(NEC)或upD78F0511A(NEC)之串口0中断发送和接收数据
- unity3D和串口通信,接收下位机数据和发送指令
- 51单片机中用中断方式实现串口数据发送与接收
- 使用UDP非连线式发送接收数据(聊天室模式)
- c#中UDP数据发送和接收
- 利用ASP发送和接收XML数据的处理方法与代码
- 地磅称量系统之(60) 接收地磅发送过来的数据显示在LCD上
- 异步 SOCKET 编程 - 发送和接收数据
- 异步SOCKET编程-发送和接收数据[转] 异步SOCKET编程-发送和接收数据[转]
- udp发送接收数据
- 利用ASP发送和接收XML数据的处理方法
- 让JRTPLIB 发送/接收数据时使用同一个端口
- 异步SOCKET编程-发送和接收数据[转]
- 利用ASP发送和接收XML数据的处理方法