void USART2_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
DMA_InitTypeDef DMA_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
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);
//////////////////中断的相关配置////////////////////////////
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); //允许中断方式
USART_Cmd(USART2, ENABLE);
DMA_DeInit(DMA1_Channel6);
DMA_InitStructure.DMA_PeripheralBaseAddr = 0x40004404;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)USART2_RECEIVE_DATA;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 512;
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_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel6, &DMA_InitStructure);
USART_DMACmd(USART2, USART_DMAReq_Rx, ENABLE);
DMA_Cmd(DMA1_Channel6, ENABLE);
USART_ITConfig(USART2, USART_IT_IDLE , ENABLE);
}
void USART2_IRQHandler(void)
{
// uint8_t data;
int i = 0;
// static u8 usart2_i;
int DATA_LEN;
OSIntEnter();
// if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
// {
// USART_ClearITPendingBit(USART2,USART_IT_RXNE);
// data = USART_ReceiveData ( USART2 ) ;
// // USART_SendData(USART1, data);
// // printf("%c",data);
// // usart2_buffer[usart2_i] = data;
// }
if(USART_GetITStatus(USART2, USART_IT_IDLE) != RESET)
{
DMA_Cmd(DMA1_Channel6, DISABLE);
i = USART2->DR;
//防止不必要的错误出现
if( DMA_GetCurrDataCounter(DMA1_Channel6) > 512)
{
return;
}
DATA_LEN = 512 - DMA_GetCurrDataCounter(DMA1_Channel6);
for(i=0;i < DATA_LEN;i ++ )
{
USART2_SEND_DATA[i] = USART2_RECEIVE_DATA[i];
}
usart2_received_data(USART2_SEND_DATA);
//printf("%s",USART2_SEND_DATA);
DMA1_Channel6->CNDTR = 512;
DMA_Cmd(DMA1_Channel6, ENABLE);
USART_ClearITPendingBit(USART2, USART_IT_IDLE);
}
memset(USART2_SEND_DATA,'\0' ,sizeof(USART2_SEND_DATA));
memset(USART2_RECEIVE_DATA,'\0' ,sizeof(USART2_RECEIVE_DATA));
OSIntExit();
}
Previous article:RTC operation of stm32
Next article:The stm32 function is placed in the section
Recommended ReadingLatest update time:2024-11-16 14:41
- Popular Resources
- Popular amplifiers
Professor at Beihang University, dedicated to promoting microcontrollers and embedded systems for over 20 years.
- Innolux's intelligent steer-by-wire solution makes cars smarter and safer
- 8051 MCU - Parity Check
- How to efficiently balance the sensitivity of tactile sensing interfaces
- What should I do if the servo motor shakes? What causes the servo motor to shake quickly?
- 【Brushless Motor】Analysis of three-phase BLDC motor and sharing of two popular development boards
- Midea Industrial Technology's subsidiaries Clou Electronics and Hekang New Energy jointly appeared at the Munich Battery Energy Storage Exhibition and Solar Energy Exhibition
- Guoxin Sichen | Application of ferroelectric memory PB85RS2MC in power battery management, with a capacity of 2M
- Analysis of common faults of frequency converter
- In a head-on competition with Qualcomm, what kind of cockpit products has Intel come up with?
- Dalian Rongke's all-vanadium liquid flow battery energy storage equipment industrialization project has entered the sprint stage before production
- Allegro MicroSystems Introduces Advanced Magnetic and Inductive Position Sensing Solutions at Electronica 2024
- Car key in the left hand, liveness detection radar in the right hand, UWB is imperative for cars!
- After a decade of rapid development, domestic CIS has entered the market
- Aegis Dagger Battery + Thor EM-i Super Hybrid, Geely New Energy has thrown out two "king bombs"
- A brief discussion on functional safety - fault, error, and failure
- In the smart car 2.0 cycle, these core industry chains are facing major opportunities!
- The United States and Japan are developing new batteries. CATL faces challenges? How should China's new energy battery industry respond?
- Murata launches high-precision 6-axis inertial sensor for automobiles
- Ford patents pre-charge alarm to help save costs and respond to emergencies
- New real-time microcontroller system from Texas Instruments enables smarter processing in automotive and industrial applications
- [Help Post] High-frequency oscillation of high-frequency switching power supply current
- SHT31 Review - Unboxing and First Impressions
- IoT security himqtt firewall data structure ringbuffer ring buffer
- EEWORLD University Hall----Live Replay: TI's Latest Application of Millimeter-Wave Radar in the Automotive Field
- Changes in car rear lighting design
- EEWORLD University - Tektronix will teach you the oscilloscope usage skills you don't know
- [Download Gift] Demonstrate Multi-channel Signal Spectrum Analysis Using the Spectrum View Function of the New Tektronix MSO6 Oscilloscope
- Is there any MEMS or sensor expert?
- MSP430 Registers
- GPIO Features of TMS320C6748