<meter id="pryje"><nav id="pryje"><delect id="pryje"></delect></nav></meter>
          <label id="pryje"></label>

          新聞中心

          EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > Stm32RS485串口通信——中斷接收發(fā)送數(shù)據(jù)

          Stm32RS485串口通信——中斷接收發(fā)送數(shù)據(jù)

          作者: 時(shí)間:2016-11-19 來源:網(wǎng)絡(luò) 收藏
          main.c源碼:

          本文引用地址:http://www.ex-cimer.com/article/201611/318281.htm
          /**說明:*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();//72mRCC_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_485GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;      //推挽輸出GPIO_Init(GPIOE, &GPIO_InitStructure);   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;             //管腳2GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;       //復(fù)用推挽輸出GPIO_Init(GPIOA, &GPIO_InitStructure);                //TX初始化GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;             //管腳3GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空輸入        GPIO_Init(GPIOA, &GPIO_InitStructure);                //RX初始化} void USART_Configuration(void)//串口初始化函數(shù){//串口參數(shù)初始化  USART_InitTypeDef USART_InitStructure;               //串口設(shè)置恢復(fù)默認(rèn)參數(shù)//初始化參數(shù)設(shè)置USART_InitStructure.USART_BaudRate = 9600;                  //波特率9600USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字長(zhǎng)8位USART_InitStructure.USART_StopBits = USART_StopBits_1;      //1位停止字節(jié)USART_InitStructure.USART_Parity = USART_Parity_No;         //無奇偶校驗(yàn)USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;//無流控制USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//打開Rx接收和Tx發(fā)送功能USART_Init(USART2,&USART_InitStructure);                   //初始化USART_ITConfig(USART2,USART_IT_TXE,ENABLE);                //允許串口1發(fā)送中斷。USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);               //允許串口1接收中斷。USART_Cmd(USART2,ENABLE);                                  //啟動(dòng)串口USART_ClearFlag(USART2,USART_FLAG_TC);                     //發(fā)送完成標(biāo)志位} void NVIC_Configuration(void)//配置中斷優(yōu)先級(jí)  {  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的中斷函數(shù)接收發(fā)送數(shù)據(jù):
          #include "..includestm32f10x.h" #include "..includestm32f10x_it.h"#include "..includestm32f10x_gpio.h"#include "..includestm32f10x_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){} //發(fā)送數(shù)據(jù)寄存器空標(biāo)志位delay_ms(2);GPIO_ResetBits(GPIOE,GPIO_Pin_5);}   }


          評(píng)論


          技術(shù)專區(qū)

          關(guān)閉
          看屁屁www成人影院,亚洲人妻成人图片,亚洲精品成人午夜在线,日韩在线 欧美成人 (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();