Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to simulate Serial Port with IO Port in STM32

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

This article shows you how STM32 uses IO port to simulate serial port, the content is concise and easy to understand, it can definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.

PB12 simulation TXD,PB13 simulation RXD transmission timing is relatively simple, timing diagram can be quickly realized. The receiving is more complicated. Here, the overflow interrupt of timer TM4 and the external interrupt of PB13 are used to calculate the level time to obtain serial port data.

# define BuadRate_9600 100u8 len = 0; / / receive count U8 USART_buf [11] / / receive buffer # define OI_TXD PBout (12) # define OI_RXD PBin (13) enum {COM_START_BIT, COM_D0_BIT, COM_D1_BIT, COM_D2_BIT, COM_D3_BIT, COM_D4_BIT, COM_D5_BIT, COM_D6_BIT, COM_D7_BIT, COM_STOP_BIT } U8 recvStat = COM_STOP_BIT;u8 recvData = 0 GPIO_Pin_12 void rf315_IoConfig () {GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStruct; RCC_APB2PeriphClockCmd (RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOB, ENABLE); / / enable PB,PC port clock / / SoftWare Serial TXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; / / push-pull output GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; / / IO port speed is 50MHz GPIO_Init (GPIOC, & GPIO_InitStructure); GPIO_SetBits (GPIOB,GPIO_Pin_12) / / SoftWare Serial RXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init (GPIOB, & GPIO_InitStructure); GPIO_EXTILineConfig (GPIO_PortSourceGPIOB, GPIO_PinSource13) EXTI_InitStruct.EXTI_Line = EXTI_Line13; EXTI_InitStruct.EXTI_Mode=EXTI_Mode_Interrupt; EXTI_InitStruct.EXTI_Trigger=EXTI_Trigger_Falling; / / falling edge triggers interrupt EXTI_InitStruct.EXTI_LineCmd=ENABLE; EXTI_Init (& EXTI_InitStruct); NVIC_InitStructure.NVIC_IRQChannel= EXTI15_10_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2 NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE; NVIC_Init (& NVIC_InitStructure);} void TIM4_Int_Init (U16 arr,u16 psc) {TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd (RCC_APB1Periph_TIM4, ENABLE); / / clock enable / / timer TIM4 initialize TIM_TimeBaseStructure.TIM_Period = arr / / sets the value of the automatic reload register period of the next update event load activity TIM_TimeBaseStructure.TIM_Prescaler = psc; / / sets the pre-division value used as the divisor of the TIMx clock frequency TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; / / sets the clock division: TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up / / TIM up count mode TIM_TimeBaseInit (TIM4, & TIM_TimeBaseStructure); / / initialize the time base unit of TIMx according to the specified parameters TIM_ClearITPendingBit (TIM4,TIM_ FLAG_Update); TIM_ITConfig (TIM4,TIM_IT_Update,ENABLE); / / enable the specified TIM3 interrupt, allowing update interrupt / / interrupt priority NVIC setting NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn / / TIM4 interrupt NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; / priority level 1 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; / / from priority level 1 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; / / IRQ channel is enabled NVIC_Init (& NVIC_InitStructure); / / initialize NVIC register} void rf315_uart4Init () {rf315_IoConfig () TIM4_Int_Init (107,71); / / 1m count frequency} void rf315_SendChar (U8 Data) {U8 I = 0; OI_TXD = 0; delay_us (BuadRate_9600); for (I = 0; I

< 8; i++) { if(Data&0x01) OI_TXD = 1; else OI_TXD = 0; delay_us(BuadRate_9600); Data = Data>

> 1;} OI_TXD = 1; delay_us (BuadRate_9600);} void rf315_SendData (U8 * buf, U8 len) {u8 t; for (t = 0; t < len; tasking +) {rf315_SendChar (buf [t]) }} void EXTI15_10_IRQHandler (void) {if (EXTI_GetFlagStatus (EXTI_Line13)! = RESET) {if (OI_RXD = = 0) {if (recvStat = = COM_STOP_BIT) {recvStat = COM_START_BIT TIM_Cmd (TIM4, ENABLE);}} EXTI_ClearITPendingBit (EXTI_Line13);}} U8 rf315_data_need_handler=0 Void TIM4_IRQHandler (void) {if (TIM_GetFlagStatus (TIM4, TIM_FLAG_Update)! = RESET) {TIM_ClearITPendingBit (TIM4, TIM_FLAG_Update); recvStat++; if (recvStat = = COM_STOP_BIT) {TIM_Cmd (TIM4, DISABLE) USART_ Buf [len + +] = recvData; / * data protocol tail * / if (USART_ Buf [len-1] = = RF315_PROTOCOL_TAIL) rf315_data_need_handler=1; return } if (OI_RXD) {recvData | = (1)

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report