//USART
void UART2_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//USART_WordLength_8b; //8 位数据
//USART_WordLength_9b; //9 位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//USART_StopBits_1 ;//在帧结尾传输 1 个停止位
//USART_StopBits_0.5;//在帧结尾传输 0.5 个停止位
//USART_StopBits_2 ;//在帧结尾传输 2 个停止位
//USART_StopBits_1.5;//在帧结尾传输 1.5 个停止位
USART_InitStructure.USART_Parity = USART_Parity_No;
//USART_Parity_No ;//奇偶失能
//USART_Parity_Even;//偶模式
//USART_Parity_Odd ;//奇模式
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//USART_HardwareFlowControl_None; //硬件流控制失能
//USART_HardwareFlowControl_RTS; //发送请求 RTS使能
//USART_HardwareFlowControl_CTS; //清除发送 CTS使能
//USART_HardwareFlowControl_RTS_CTS;//RTS和 CTS使能
//TIM1_OCIdleState是检测定时器是否空闲,USART_HardwareFlowControl是硬件数据流控制,这个一般是设为无
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//USART_Mode_Tx;//发送使能
//USART_Mode_Rx;//接收使能
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
//USART_Clock_Enable ;//时钟高电平活动
//USART_Clock_Disable;//时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
//USART_CPOL_High;//时钟高电平
//USART_CPOL_Low ;//时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
//USART_CPHA_1Edge;//时钟第一个边沿进行数据捕获
//USART_CPHA_2Edge;//时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
//USART_LastBit_Disable;//最后一位数据的时钟脉冲不从 SCLK输出
//USART_LastBit_Enable ;//最后一位数据的时钟脉冲从 SCLK输出
USART_Init(USART2, &USART_InitStructure);//初始化外设 USARTx 寄存器
USART_Cmd(USART2, ENABLE);
//ENABLE-DISABLE//使能或者失能 USART 外设
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);//ENABLE-DISABLE//使能或者失能指定的 USART 中断
//USART_IT_PE //奇偶错误中断
//USART_IT_TXE //发送中断
//USART_IT_TC //传输完成中断
//USART_IT_RXNE//接收中断
//USART_IT_IDLE//空闲总线中断
//USART_IT_LBD //LIN中断检测中断
//USART_IT_CTS //CTS中断
//USART_IT_ERR //错误中断
USART_ClockInit(USART2, &USART_ClockInitStructure);
}
void USART3_Putc(unsigned char c)//串口3发送一个字符
{
USART_SendData(USART3, c);
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET );
}
void USART3_Puts(unsigned char * str)//串口3发送一个字
{
while(*str)
{
USART_SendData(USART3, *str++);
/* 循环,直到传输结束*/
while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
}
}
//用于查询接收
void USART1_IRQHandler(void)
{
u8 c;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
c=USART1->DR;
}
}
/**********************调试成功例程**************************/
//==================串口1 NVIC初始化==============
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
//NVIC_ClearIRQChannelPendingBit(USART1_IRQChannel);
//设置NVIC优先级分组为Group2:0-3抢占式优先级,0-3的响应式优先级
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
/* enabling interrupt */
NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//==================串口1 RCC初始化==============
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
// RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
//==================串口1初始化GPIO_USART==============
void USART1_Configuration(void)
{
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_StructInit(&USART_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(USART1,&USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* Enable the USART1 */
USART_Cmd(USART1,ENABLE);
}
//==================串口1中断处理函数==============
void USART1_IRQHandler(void)
{
u16 usarnum;
if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET)
{
/* Disable USART3 RXNE Interrupt */
USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);
usarnum=USART_ReceiveData(USART1);
USART_SendData(USART2, usarnum);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
USART_ITConfig( USART1,USART_IT_RXNE, ENABLE);
}
}
//==================串口2中断处理函数==============
void USART2_IRQHandler(void)
{
}
//==================串口3中断处理函数==============
void USART3_IRQHandler(void)
{
}
//==================串口发送处理函数==============
void USART1_Putc(unsigned char c)
{
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
USART_SendData(USART1, c);
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET );
}
//==================串口发送底层函数==============
void USART_SendData(USART_TypeDef* USARTx, u16 Data)//
{
/* Check the parameters */
assert_param(IS_USART_ALL_PERIPH(USARTx));
assert_param(IS_USART_DATA(Data));
/* Transmit Data */
USARTx->DR = (Data & (u16)0x01FF);
}
//==================串口main==============
int main(void)
{
RCC_Configuration(); //初始化时钟
NVIC_Configuration();//初始化中断
USART_Configuration();//初始化串口
while (1) ;
上一篇:关于stm32 的 USB 转串口 virtual_Com_Port的例程的一些问题
下一篇:stm32库函数GPIO_Init()解析
推荐阅读最新更新时间:2024-03-16 15:26
设计资源 培训 开发板 精华推荐
- EEWorld 芯积分兑换年度回馈来袭~多种赚积分捷径曝光+礼品兑换剧透
- 【答题有奖】Altera Cyclone V 开发板全体验之初识 & 评估Cyclone V !
- 抢先体验NUCLEO家族新贵,ST STM32 NUCLEO-F091RC开发板28元包邮!
- Littelfuse 2022 最新电池方案在线展厅
- 直播:计算机视觉影像处理应用于智能驾驶的未来及挑战
- 【新年活动】2023,兔 do list!
- 有奖直播:DIY 家用监控边缘 AI Box——基于 8TOPS 算力,售价 199 美元的开发套件搭建深度学习案例
- 悦读 TI 工业应用精选方案,闯关赢好礼喽!
- 双重好礼等你来!2022年,让英飞凌更懂你!
- 金秋十月,我们跟金尔雅老师学MSP432