Mình đã sửa lại HSE_VALUE trong stm32f4xx.h là 8000000.
Còn đây là code UART gửi 1 chuỗi lên máy tính. Có tín hiệu gửi lên nhưng ko phải là HelloWorld.
Còn đây là code UART gửi 1 chuỗi lên máy tính. Có tín hiệu gửi lên nhưng ko phải là HelloWorld.
Code:
#include <stm32f4xx.h> #include <misc.h> #include <stm32f4xx_usart.h> void init_USART1(uint32_t baudrate){ GPIO_InitTypeDef GPIO_InitStruct; USART_InitTypeDef USART_InitStruct; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // Pins 6 (TX) and 7 (RX) are used GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStruct.GPIO_OType = GPIO_OType_PP; GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStruct); GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); // GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1); USART_InitStruct.USART_BaudRate = baudrate; USART_InitStruct.USART_WordLength = USART_WordLength_8b; USART_InitStruct.USART_StopBits = USART_StopBits_1; USART_InitStruct.USART_Parity = USART_Parity_No; USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx; USART_Init(USART1, &USART_InitStruct); USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_Cmd(USART1, ENABLE); } void sendstring(char* string) { while(*string !='\0') { USART_SendData(USART1,*string); string++; while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET) {}; } } int main(void) { while(1){ init_USART1(9600); //USART1 @ 9600 baud sendstring("HelloWorld"); } }
Comment