#include "stm32f4xx.h" #include "stm32f4xx_rcc.c" #include "stm32f4xx_gpio.c" #include "stm32f4xx_usart.c" #include "stm32f4xx_dma.c" #include "LCD2x16.c" #include "dd.h" char OutString[4] = {'0', '0', 0x0a, 0x0d}; //USART3 initialization, GPIOD 8:9, TX&RX, IRQ on RX void USART3_init(int BaudRate) { USART_InitTypeDef USART_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_USART3); GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_USART3); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE); USART_DeInit(USART3); USART_InitStructure.USART_BaudRate = BaudRate; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_Init(USART3, &USART_InitStructure); USART_Cmd(USART3, ENABLE); } //DMA1 initialization, TX requests next character to send void DMA1fromUSART3_init(void) { DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA1, ENABLE); DMA_DeInit(DMA1_Stream3); /* Reset DMA Stream registers (for debug purpose) */ /* Configure DMA Stream */ DMA_InitStructure.DMA_Channel = DMA_Channel_4; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)(&USART3->DR); DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)(OutString); DMA_InitStructure.DMA_DIR = DMA_DIR_MemoryToPeripheral; DMA_InitStructure.DMA_BufferSize = (uint32_t)4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Byte; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Byte; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_Full; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA1_Stream3, &DMA_InitStructure); } void main(void) { unsigned Seconds = 0; USART3_init(921600); DMA1fromUSART3_init(); LCD_init(); while (1) { if (++Seconds > 59) Seconds = 0; unsigned x = Seconds; OutString[0] = x / 10 + '0'; x %= 10; OutString[1] = x + '0'; OutString[2] = '\n'; OutString[3] = '\r'; //USART_ClearFlag(USART3, USART_FLAG_TC); USART_DMACmd(USART3, USART_DMAReq_Tx, ENABLE); DMA_ClearFlag(DMA1_Stream3, DMA_FLAG_TCIF3 | DMA_FLAG_HTIF3); DMA_Cmd(DMA1_Stream3, ENABLE); // note: USART3->SR.TXE requests DMA LCD_uInt16(Seconds, 0x05, 1); // show Seconds on LCD for (int i = 0; i<30000000; i++) {}; // waste about 1 s }; }