您好,登錄后才能下訂單哦!
本篇文章為大家展示了STM32怎么用IO口模擬串口,內容簡明扼要并且容易理解,絕對能使你眼前一亮,通過這篇文章的詳細介紹希望你能有所收獲。
PB12模擬TXD,PB13模擬RXD 發送時序比較單一,按時序圖可很快實現。 接收比較復雜這里用定時器TM4的溢出中斷和PB13的外部中斷配合計算電平時間來獲取串口數據。
#define BuadRate_9600 100 u8 len = 0; //接收計數 u8 USART_buf[11]; //接收緩沖區 #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; void rf315_IoConfig() { GPIO_InitTypeDef GPIO_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; EXTI_InitTypeDef EXTI_InitStruct; RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO|RCC_APB2Periph_GPIOB, ENABLE); //使能PB,PC端口時鐘 //SoftWare Serial TXD GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽輸出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度為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; //下降沿觸發中斷 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); //時鐘使能 //定時器TIM4初始化 TIM_TimeBaseStructure.TIM_Period = arr; //設置在下一個更新事件裝入活動的自動重裝載寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc; //設置用來作為TIMx時鐘頻率除數的預分頻值 TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; //設置時鐘分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上計數模式 TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根據指定的參數初始化TIMx的時間基數單位 TIM_ClearITPendingBit(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //使能指定的TIM3中斷,允許更新中斷 //中斷優先級NVIC設置 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM4中斷 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; //先占優先級1級 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //從優先級1級 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器 } void rf315_uart4Init() { rf315_IoConfig(); TIM4_Int_Init(107, 71); //1M計數頻率 } 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; t++) { 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; /*數據協議尾*/ if(USART_buf[len-1]==RF315_PROTOCOL_TAIL) rf315_data_need_handler=1; return; } if(OI_RXD) { recvData |= (1 << (recvStat - 1)); }else{ recvData &= ~(1 << (recvStat - 1)); } } }
上述內容就是STM32怎么用IO口模擬串口,你們學到知識或技能了嗎?如果還想學到更多技能或者豐富自己的知識儲備,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。