一个小小的串口居然让我捣鼓了两天还没出来,时钟正确,usart寄存器值也是对的。然后试了试源码居然一样乱码。不知道是怎么了。板子是官方的论坛送的stm32f429zit6。下面贴上代码和源码。 #include "stm32f4xx_usart.h" #include "stm32f4xx_gpio.h" #include "stm32f4xx_rcc.h" #include "stm32f4xx_flash.h" #include "stm32f4xx_pwr.h" #include "stm32f4xx_exti.h" #include "misc.h" #include #include #include void USART_GPIO_Config(void); void USART_Config(void); void USART_SendString(char *str); void RCC_Config(void); void uart3_init(void); int main(void) { SystemInit(); uart3_init(); //USART_GPIO_Config(); USART_Config(); USART_SendData(USART3, '9'); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {} while(1) { USART_SendData(USART3, 'a'); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET) {} //USART_SendString("hello world\r"); //Array[num++] = USART_ReceiveData(USART1); //printf("hello world/r"); } } void uart3_init(void) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable GPIO clock */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); /* Enable USART clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); /* Connect USART pins to AF7 */ GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3); /* Configure USART Tx and Rx as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; 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_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOC, &GPIO_InitStructure); } void USART_SendString(char *str) { while(*str!=0) { while(RESET == USART_GetFlagStatus(USART1,USART_FLAG_TXE)); USART_SendData(USART1, *str++); } } void USART_Config(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); 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_Tx | USART_Mode_Rx; USART_Init(USART3, &USART_InitStructure); /* NVIC configuration */ /* Configure the Priority Group to 2 bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); /* Enable the USARTx Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig(USART1, USART_IT_TXE|USART_IT_RXNE, ENABLE); //USART_ITConfig(USART1,USART_IT_CTS, DISABLE); USART_Cmd(USART3, ENABLE); } void uart_init(u32 pclk2,u32 bound) { float temp; u16 mantissa; u16 fraction; temp=(float)(pclk2*1000000)/(bound*16); mantissa=temp; fraction=(temp-mantissa)*16; mantissa |
comport.zip
下载147.38 KB, 下载次数: 4, 下载积分: ST金币 -1