代码如下,用的串口5
上电后自己先发一个0x55出去,以后收到数据回一个0xAA
void GPIO_Configura
tion(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE);
//TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //管脚位置定义
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING; //浮空输入 IN_FLOATING
GPIO_Init(GPIOD,&GPIO_InitStructure); //C组GPIO初始化
}
void NVIC_Configuration(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);//1 也试过了
NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn; //通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //中断响应优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
}
void UART5_Init(void)
{
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(UART5, &USART_InitStructure); //初始化
USART_ClearFlag(UART5, USART_FLAG_RXNE); // 清标志
USART_ITConfig(UART5, USART_IT_RXNE, ENABLE); // 若接收数据寄存器满,则产生中断
// USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
USART_Cmd(UART5, ENABLE); //启动串口
}
void UART5_IRQHandler(void)
{
if(USART_GetITStatus(UART5, USART_IT_RXNE) != RESET)
{
while (!(UART5->SR & USART_FLAG_RXNE));
UART5->DR = (0XAA & (uint16_t)0x01FF);
USART_ITConfig(UART5, USART_IT_TXE, DISABLE);
}
if(USART_GetITStatus(UART5, USART_IT_TXE) != RESET)
{
while (!(UART5->SR & USART_FLAG_TXE));
UART5->DR = (0X55 & (uint16_t)0x01FF);
USART_ITConfig(UART5, USART_IT_TXE, DISABLE);
}
}
main函数就比较简单了
GPIO_Configuration();
NVIC_Configuration();
UART5_Init();
USART_ITConfig(UART5, USART_IT_TXE, ENABLE);
while(1);
我这有UART4和UART5的
//========== 我是分隔符 =========
#include "stm32f10x.h"
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configure UART4
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void UART4_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC ,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART4 ,ENABLE);
/*
* UART4_TX -> PC10 , UART4_RX -> PC11
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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(UART4, &USART_InitStructure);
USART_ClearFlag(UART4,USART_FLAG_TC);
//USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);
USART_Cmd(UART4, ENABLE);
}
/*----------------------------------------------------------------------------
* sendchar: Write a character to Serial Port
*---------------------------------------------------------------------------*/
int sendchar (int ch) {
if (ch == ' ') {
while (!(UART4->SR & 0x0080));
UART4->DR = 0x0D;
}
while (!(UART4->SR & 0x0080));
UART4->DR = (ch & 0xFF);
return (ch);
}
/*----------------------------------------------------------------------------
* getkey: Read a character from Serial Port
*---------------------------------------------------------------------------*/
int getkey (void) {
while (!(UART4->SR & 0x0020));
return (UART4->DR);
}
//========== 我是分隔符 =========
/*******************************************************************************
* Function Name : USART_Configuration
* Description : Configure UART5
* Input : None
* Output : None
* Return : None
* Attention : None
*******************************************************************************/
void UART5_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD ,ENABLE);
RCC_APB1PeriphClockCmd( RCC_APB1Periph_UART5 ,ENABLE);
/*
* UART5_TX -> PC12 , UART5_RX -> PD2
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOD, &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_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(UART5, &USART_InitStructure);
USART_ClearFlag(UART5,USART_FLAG_TC);
//USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);
USART_Cmd(UART5, ENABLE);
}
一周热门 更多>