2554 views|7 replies

3180

Posts

0

Resources
The OP
 

Problems with STM32F4 serial port 4 standard library program [Copy link]

There is a board, STM32F407VET6, serial port 4 is connected to PC10 and PC11.

I wrote a program using the standard library, but it can't communicate.

The initialization code is as follows:

//IO 4

//bound:¨

void uart_init(u32 bound){

//GPIOè

GPIO_InitTypeDef GPIO_InitStructure;

USART_InitTypeDef USART_InitStructure;

NVIC_InitTypeDef NVIC_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC,ENABLE); //GPIOC±

RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//UART4±

//1

GPIO_PinAFConfig(GPIOC,GPIO_PinSource10,GPIO_AF_UART4); //GPIOC10UART4

GPIO_PinAFConfig(GPIOC,GPIO_PinSource11,GPIO_AF_UART4); //GPIOC11UART4

//USART1

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_10; //GPIOC11GPIOC10

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;//

GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //50MHz

GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; //ì

GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; //-

GPIO_Init(GPIOC,&GPIO_InitStructure); //PC11PC10

//USART1 è

USART_InitStructure.USART_BaudRate = bound;//¨è

USART_InitStructure.USART_WordLength = USART_WordLength_8b;//פ8

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(UART4, &USART_InitStructure); //1

USART_Cmd(UART4, ENABLE); //1

USART_ClearFlag(UART4, USART_FLAG_TC);

//#if EN_USART1_RX

// USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//à

// //Usart1 NVIC

// NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn;//1¨

// NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3;//3

// NVIC_InitStructure.NVIC_IRQChannelSubPriority =3; //×3

// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ¨

// NVIC_Init(&NVIC_InitStructure); //ù¨VIC÷

//#endif

}

At first I tried interruption, but it didn't work, then I tried querying, but it still didn't work.

Interrupt procedure:

void UART4_IRQHandler(void) //1·ò

{

u8 Res;

#if SYSTEM_SUPPORT_OS //SYSTEM_SUPPORT_OSòè§OS.

OSIntEnter();

#endif

if(USART_GetITStatus(UART4, USART_IT_RXNE) != RESET) //(±0x0d 0x0aá)

{

Res =USART_ReceiveData(UART4);//(UART4->DR); //

if((USART_RX_STA&0x8000)==0)//ê

{

if(USART_RX_STA&0x4000)//0x0d

{

if(Res!=0x0a)USART_RX_STA=0;//íó,

else USART_RX_STA|=0x8000; //ê

}

else //0X0D

{

if(Res==0x0d)USART_RX_STA|=0x4000;

else

{

USART_RX_BUF[USART_RX_STA&0X3FFF]=Nothing ;

USART_RX_STA++;

if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;//íó,

}

}

}

}

#if SYSTEM_SUPPORT_OS //SYSTEM_SUPPORT_OSòè§OS.

OSIntExit();

#endif

}

Query code:

while (USART_GetFlagStatus(UART4, USART_FLAG_RXNE) == RESET);

data=USART_ReceiveData(UART4);

Could you please tell me how to modify the code? Thanks!

This post is from stm32/stm8

Latest reply

RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);// You must have made a mistake here.   Details Published on 2019-12-18 17:46
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

3180

Posts

0

Resources
2
 

Someone said to use the GPIO_PinRemapConfig() function, but I don't know how to use it.
I searched on Baidu and found GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
but I still don't know how to use it. It says that PA0, PA1 are mapped to PC10, PC11.

This post is from stm32/stm8

Comments

The function you selected is correct, but the parameters are wrong. Your function (GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);) means "change the mapping of the specified pin GPIO_Remap_SWJ_Disable SWJ completely disabled (JTAG+SW-DP)" because  Details Published on 2019-12-18 14:01
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 

875

Posts

1

Resources
3
 
chenbingjy posted on 2019-12-7 16:29 Someone said to use the GPIO_PinRemapConfig() function, but I don't quite know how to use it. Baidu searched and found GPIO_PinRemapConfig(GPIO_ ...

The function you selected is correct, but the parameters are wrong. Your function (GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); ) means "change the mapping of the specified pin GPIO_Remap_SWJ_Disable SWJ completely disable (JTAG+SW-DP)" because "After the STM32F10x series MCU is reset, PA13/14/15 & PB3/4 are configured as JTAG function by default. Sometimes in order to make full use of the resources of the MCU I/O port, we will
set these ports as normal I/O ports." The function you should use is this " GPIO_PinRemapConfig(GPIO_PartialRemap_USART4, ENABLE); "This function maps PC10 and PC11 to serial port 4 (Note: it should be added after GPIO_Init(GPIOC,&GPIO_InitStructure); )

This post is from stm32/stm8
 
 

3180

Posts

0

Resources
4
 

Thank you! You are right

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

4005

Posts

0

Resources
5
 

/* Peripheral clock enable */
LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_UART4);

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_GPIOC);
/**UART4 GPIO Configuration
PC10 ------> UART4_TX
PC11 ------> UART4_RX
*/
GPIO_InitStruct.Pin = LL_GPIO_PIN_10|LL_GPIO_PIN_11;
GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_VERY_HIGH;
GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
GPIO_InitStruct.Pull = LL_GPIO_PULL_UP;
GPIO_InitStruct.Alternate = LL_GPIO_AF_8;
LL_GPIO_Init(GPIOC, &GPIO_InitStruct);

USART_InitStruct.BaudRate = 115200;
USART_InitStruct.DataWidth = LL_USART_DATAWIDTH_8B;
USART_InitStruct.StopBits = LL_USART_STOPBITS_1;
USART_InitStruct.Parity = LL_USART_PARITY_NONE;
USART_InitStruct.TransferDirection = LL_USART_DIRECTION_TX_RX;
USART_InitStruct.HardwareFlowControl = LL_USART_HWCONTROL_NONE;
USART_InitStruct.OverSampling = LL_USART_OVERSAMPLING_16;
LL_USART_Init(UART4, &USART_InitStruct);
LL_USART_ConfigAsyncMode(UART4);
LL_USART_Enable(UART4);

I cut a section for you.

This post is from stm32/stm8
 
 
 

3180

Posts

0

Resources
6
 

Thanks!

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

4005

Posts

0

Resources
7
 

RCC_APB2PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);//

You must have made a mistake here.

This post is from stm32/stm8
 
 
 

3180

Posts

0

Resources
8
 

Yes, just change this.

This post is from stm32/stm8
 
Personal signature为江山踏坏了乌骓马,为社稷拉断了宝雕弓。
 
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews

Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list