微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32F4入手调试USART,ADC-DMA

STM32F4入手调试USART,ADC-DMA

时间:12-02 来源:互联网 点击:
在F4-Discovery上调试串口,板上无串口接口芯片,需外接电平转换芯片和串口接口,通过插针引线连接两块电路板,板上3.3V供电180mA不足以支持MAX3232工作,故用usb提供的+5V来为其供电。STM32F405xx/STM32F407xx手册里里写道USART1的Pin map: Tx-PA9;RX-PA10。参考IAP的示例,昨天搞了一晚上无论如何都没有输出,很是奇怪。一直以为初始化不对。今天早晨发现手册Page56中Table7. Alternate function mapping中USART1_TX/USART1_RX映射到PB6/PB7。然后做了如下的初始化,串口的引脚跳到PB6、PB7,果然有输出。那么既然管脚map上首推的映射是PA9/PA10那么为什么没有输出呢,而且用示波器测试一直为高电平?原来Discovery的试验板将PA9连接到usb的vbus供电上了。

如下STM32F4xxxx的USART初始化。

void USART1_AF_Config(void)

{

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);

/* Connect PXx to USARTx_Tx*/

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1);

/* Connect PXx to USARTx_Rx*/

GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;//TX

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;//RX

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_Init(GPIOB, &GPIO_InitStructure);

/* USARTx configured as follow:

- BaudRate = 115200 baud

- Word Length = 8 Bits

- One Stop Bit

- No parity

- Hardware flow control disabled (RTS and CTS signals)

- Receive and transmit enabled */

USART_InitStructure.USART_BaudRate = 115200;

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_ClockInitStructure.USART_Clock = USART_Clock_Disable;

USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;

USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;

USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;

USART_Init(USART1, &USART_InitStructure);

USART_ClockInit(USART1, &USART_ClockInitStructure);

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;

NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

NVIC_Init(&NVIC_InitStructure);

USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);

/* Enable USART */

USART_Cmd(USART1, ENABLE);

BSP_USART1_Init();

}

使用带参数的USART Printf:

#define BSP_USART1_TX_FIFO_SIZE 1024

#define BSP_USART1_RX_FIFO_SIZE 128

void BSP_USART1_Init(void)

{

kfifo_alloc(&usart1_tx_fifo, BSP_USART1_TX_FIFO_SIZE);

kfifo_alloc(&usart1_rx_fifo, BSP_USART1_RX_FIFO_SIZE);

}

unsigned int BSP_USART1_Write(char* data, unsigned int len)

{

USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

len = kfifo_in(&usart1_tx_fifo, (unsigned char*)data, len);

USART_ITConfig(USART1, USART_IT_TXE, ENABLE);

return len;

}

unsigned int BSP_USART1_Read(char* data, unsigned int len)

{

len = kfifo_out(&usart1_rx_fifo, (unsigned char*)data, len);

return len;

}

unsigned int BSP_USART1_Printf(char* fmt, ...)

{

va_list args;

unsigned int len;

char *buf = malloc(512);

va_start(args, fmt);

vsprintf(buf, fmt, args);

va_end(args);

len = BSP_USART1

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top