新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32F4入手調(diào)試USART,ADC-DMA

STM32F4入手調(diào)試USART,ADC-DMA

作者: 時間:2016-12-02 來源:網(wǎng)絡(luò) 收藏
在F4-Discovery上調(diào)試串口,板上無串口接口芯片,需外接電平轉(zhuǎn)換芯片和串口接口,通過插針引線連接兩塊電路板,板上3.3V供電180mA不足以支持MAX3232工作,故用usb提供的+5V來為其供電。STM32F405xx/STM32F407xx手冊里里寫道USART1的Pin map: Tx-PA9;RX-PA10。參考IAP的示例,昨天搞了一晚上無論如何都沒有輸出,很是奇怪。一直以為初始化不對。今天早晨發(fā)現(xiàn)手冊Page56中Table7. Alternate function mapping中USART1_TX/USART1_RX映射到PB6/PB7。然后做了如下的初始化,串口的引腳跳到PB6、PB7,果然有輸出。那么既然管腳map上首推的映射是PA9/PA10那么為什么沒有輸出呢,而且用示波器測試一直為高電平?原來Discovery的試驗(yàn)板將PA9連接到usb的vbus供電上了。

如下STM32F4xxxx的USART初始化。

本文引用地址:http://2s4d.com/article/201612/325060.htm

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();

}

使用帶參數(shù)的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_Write(buf, strlen(buf));

free(buf);

return len;

}

Stm32f4xx_it.c中isr添加:

#include

#include

#include

extern struct kfifo usart1_tx_fifo;

extern struct kfifo usart1_rx_fifo;

void USART1_IRQHandler(void)

{

uint8_t chr;

if(USART_GetITStatus(USART1, USART_IT_RXNE))

{

chr = USART_ReceiveData(USART1);

kfifo_in(&usart1_rx_fifo, &chr, 1);

}

if(USART_GetITStatus(USART1, USART_IT_TXE))

{

USART_ClearITPendingBit(USART1, USART_IT_TXE);

if(kfifo_out(&usart1_tx_fifo, &chr, 1))

USART_SendData(USART1, chr);

else

USART_ITConfig(USART1, USART_IT_TXE, DISABLE);

}

}

主函數(shù)調(diào)用,在ADC3_DMA示例上進(jìn)行USART1的添加和使用,需要注意的是用fifo時發(fā)送和接收占用了1K多的內(nèi)存還有USART1_Printf也使用了0.5K的內(nèi)存,所以需要將heap的容量增大,改到0x800就夠。

int main(void)

{ USART1_AF_Config();

ADC3_CH12_DMA_Config();

/* Start ADC3 Software Conversion */

ADC_SoftwareStartConv(ADC3);

while (1)

{

/* convert the ADC value (from 0 to 0xFFF) to a voltage value (from 0V to 3.3V)*/

ADC3ConvertedVoltage = ADC3ConvertedValue *3300/0xFFF;

BSP_USART1_Printf("ADCdata: %drn",ADC3ConvertedVoltage);

Delay(0x3FFFFF);

BSP_USART1_Printf("%srn",dispstr);

}

}

STM32F4的DMA channel map也由原來F1的2維DMAx_Channely變成3維DMA_Channelx_DMAy_Streamz,增加了許多。

ADC的DMA全在DMA2上,通道0、1、2上。

/* DMA2 Stream0 channel0 configuration **************************************/

DMA_InitStructure.DMA_Channel = DMA_Channel_2;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC3_DR_ADDRESS;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC3ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);


關(guān)鍵詞: STM32F4USARTADC-DM

評論


相關(guān)推薦

技術(shù)專區(qū)

關(guān)閉