新手入门
- 积分
- 14
- 金钱
- 14
- 注册时间
- 2020-10-30
- 在线时间
- 6 小时
|
1金钱
本帖最后由 anven 于 2021-5-12 16:19 编辑
- #include "main.h"
- #include "stm32f0xx.h"
- int main(void)
- {
- uint16_t i=0;
-
- //定义串口结构体
- USART_InitTypeDef USART_InitStructure;
- //定义IO口结构体
- GPIO_InitTypeDef GPIO_InitStructure;
-
- NVIC_InitTypeDef NVIC_InitStructure;
-
- //初始化系统时钟
- RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
- //使能端口时钟
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
-
- //配置PA2,PA3的串口复用功能Alternate functions
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; //端口复用模式
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //推挽方式输出
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//GPIO_PuPd_NOPULL; //悬空
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //使能串口时钟
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- //设置串口硬件参数
- USART_DeInit(USART2);
- USART_InitStructure.USART_BaudRate = 4800;
- 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(USART2, &USART_InitStructure);
-
- /* Enable the USART2 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); /* 使能接收中断 */
-
- USART_Cmd(USART2, ENABLE); /* 使能串口 */
- USART_ClearFlag(USART2, USART_FLAG_TC); /* 清发送完成标志,Transmission Complete flag */
-
-
- Delay_ms(10);
-
- Usart_SendByte(USART2, 0x99);
-
- while (1)
- {
- i=i+1;
复制代码
|
|