亲测有效,同PC间通信。
【*】 程序简介
-工程名称:工程模版
-实验平台: STM32F427 同步控制器
-MDK版本:
-ST固件库版本:
【*】 时钟
A.晶振:
-外部高速晶振:25MHz
-RTC晶振:32.768KHz
B.各总线运行时钟:
-系统时钟 = SYCCLK = AHB1 = 180MHz
-APB2 = 90MHz
-APB1 = 45MHz
C.浮点运算单元:
使能
单片机源程序如下:
- #include "stm32f4xx.h"
- #include "stm32f4xx_usart.h"
- uint8_t rx_flag=0;
- uint8_t rx_buff[100]={0x00,0x00};
- uint8_t i=0,j=0;
- uint8_t n=5;
- void USART_INIT(void);
- void nvic_configuration(void);
- int main(void)
- {
- USART_INIT();
- GPIO_SetBits(GPIOA, GPIO_Pin_6);
- USART_SendData(USART2, 66);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
- USART_SendData(USART2, 67);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TC)==RESET);
- GPIO_ResetBits(GPIOA, GPIO_Pin_6);
- while(1)
- {
- if(rx_flag==1)
- {
- rx_buff[i++]=USART_ReceiveData(USART2);
- rx_flag=0;
- }
- if(i==n)
- {
- GPIO_SetBits(GPIOA, GPIO_Pin_6);
- i=0;
- for(j=0;j<n;j++)
- {
- USART_SendData(USART2, rx_buff[j]);
- while(USART_GetFlagStatus(USART2, USART_FLAG_TC)==RESET);
- }
- GPIO_ResetBits(GPIOA, GPIO_Pin_6);
- }
-
- }
- }
- void USART_INIT(void)
- {
- // GPIO initiallize
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOA ,ENABLE); //使能外设时钟AHB1
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; // A6 control singal
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //实际上就是推挽输出
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOA, GPIO_Pin_6);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2); //GPIOA2复用为USART2
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //GPIOA3复用为USART2
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 |GPIO_Pin_3 ; //Tx and Rx
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //alternate function
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; //low rate
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- ////USART initiallize
- USART_InitTypeDef usart_2_initstructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);//使能USART2时钟 挂载在APB1外设时钟上
-
- usart_2_initstructure.USART_BaudRate=9600;
- usart_2_initstructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
- usart_2_initstructure.USART_Mode=USART_Mode_Rx |USART_Mode_Tx;
- usart_2_initstructure.USART_Parity=USART_Parity_No;
- usart_2_initstructure.USART_StopBits=USART_StopBits_1;
- usart_2_initstructure.USART_WordLength=USART_WordLength_8b;
- USART_Init(USART2, &usart_2_initstructure);
- USART_Cmd(USART2, ENABLE); //使能串口2
-
- //some flags and IT
- USART_ClearFlag(USART2, USART_FLAG_TC); //clear transmit flag
-
- nvic_configuration();
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启相关中断
- }
- //内嵌中断向量控制器config
- void nvic_configuration()
- {
- //NVIC config and initiallize
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
- NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE ;
- NVIC_Init(&NVIC_InitStructure);
- }
- void USART2_IRQHandler()
- {
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- // rx_buff[i++]=USART_ReceiveData(USART2);
- rx_flag=1;
- USART_ClearFlag(USART2, USART_FLAG_RXNE);
- }
- }
复制代码
所有资料51hei提供下载:
F427_USART2_485.rar
(612.81 KB, 下载次数: 38)
|