STM32F030C8的USART2的配置问题 发送不出去
第一次用STM32F0系列的MCU,在用STM32F030C8T6的USART2,即PA2和PA3作为串口使用时,始终发送不出去,USART1可以正常使用,USART2的配置与USART1一样,但是USART2不能正常工作,不知道哪里的问题,希望可以得到大神的帮助,谢谢!源码如下:void USART1_INIT(void){
GPIO_InitTypeDefGPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, 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_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);
}
void USART2_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
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_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, 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_Rx | USART_Mode_Tx;
USART_Init(USART2, &USART_InitStructure);
USART_Cmd(USART2, ENABLE);
}
void UART2_Send(uint8_t *Buffer, uint32_t Length)
{
while(Length != 0)
{
while(!((USART2->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
USART2->TDR = *Buffer;
Buffer++;
Length--;
}
}
void UART1_Send(uint8_t *Buffer, uint32_t Length)
{
while(Length != 0)
{
while(!((USART1->ISR)&(1<<7)));//µÈ´ý·¢ËÍÍê
USART1->TDR = *Buffer;
Buffer++;
Length--;
}
}
uint8_t Tx2_Buffer[] = "good";
uint8_t Tx1_Buffer[] = "nice";
#define countof(a) (sizeof(a) / sizeof(*(a)))
int main(void)
{
NVIC_Configuration();
SysTick_Start(5);
USART1_INIT();
USART2_INIT();
while (1)
{
if (SysTick->CTRL & 0x10000)// 5ms一次任务
{
UART1_Send(Tx1_Buffer, countof(Tx1_Buffer)-1);
UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1);
}
}
}
在主函数中每隔5ms发送Tx1_Buffer[]和Tx2_Buffer[],但是在进入UART2_Send(Tx2_Buffer, countof(Tx2_Buffer)-1);之后就在while(!((USART2->ISR)&(1<<7)));这里死掉了,即出不来这个while,谢谢!
外设时钟开启没有?
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); 建议在cubemx试试 本帖最后由 wenyangzeng 于 2017-11-1 10:33 编辑
串口2在PA2,PA3
static void MX_USART2_UART_Init(void)
{
huart2.Instance = USART2;
huart2.Init.BaudRate = 9600;
huart2.Init.WordLength = UART_WORDLENGTH_8B;
huart2.Init.StopBits = UART_STOPBITS_1;
huart2.Init.Parity = UART_PARITY_NONE;
huart2.Init.Mode = UART_MODE_TX_RX;
huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart2.Init.OverSampling = UART_OVERSAMPLING_16;
huart2.Init.OneBitSampling = UART_ONE_BIT_SAMPLE_DISABLE;
huart2.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
if (HAL_UART_Init(&huart2) != HAL_OK)
{
_Error_Handler(__FILE__, __LINE__);
}
}
while(!((USART2->ISR)&(1<<6)));改成这样看看 4、NUCLEO-L476RG试验三_LED闪闪亮续(定时、串口)
之前曾写过一个Cubemx配置串口的例子,供您参考。
看看你的工程中
接收时,需要设置的函数
HAL_UART_RxCpltCallback(.....)
HAL_UART_Receive_IT
发送时需要设置的内容:
PUTCHAR_PROTOTYPE
用Printf(....)
看看呢?
串口很简单的。
需要清除相应的发送中断标志位,才能发送下一BIT
页:
[1]