Proteus模拟STM32F103R6微控制器之串口通信USART的方法
生活随笔
收集整理的這篇文章主要介紹了
Proteus模拟STM32F103R6微控制器之串口通信USART的方法
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
Proteus模擬STM32F103R6微控制器之串口通信USART的方法,實驗環(huán)境如下:
模擬軟件:Proteus 8.11 SP0
開發(fā)環(huán)境:Keil MDK 5.33
參考資料:ST公司官方參考手冊
一、原理圖
STM32F103微處理器的USART1發(fā)送:PA9,接收:PA10。
虛擬終端的發(fā)送接到STM32F103的接收,虛擬終端的接收接到STM32F103的發(fā)送。原理圖如下:
二、源碼
// NVIC配置 static void NVIC_Configuration(void) {NVIC_InitTypeDef NVIC_InitStructure;// Configure the NVIC Preemption Priority BitsNVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);// Enable the USART1 InterruptNVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;NVIC_Init(&NVIC_InitStructure); }// USART1配置 void USART1_Config(void) {GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);// Configure USART1 Tx as alternate function push-pullGPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;GPIO_Init(GPIOA, &GPIO_InitStructure);// Configure USART1 Rx as input floatingGPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;GPIO_Init(GPIOA, &GPIO_InitStructure);// Configure USART1USART_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(USART1, &USART_InitStructure);// NVIC configurationNVIC_Configuration();// Enable USART1 Receive and Transmit interruptsUSART_ITConfig(USART1, USART_IT_RXNE, ENABLE);USART_ITConfig(USART1, USART_IT_TXE, ENABLE);// Enable the USART1USART_Cmd(USART1, ENABLE); }// 中斷處理函數(shù) void USART1_IRQHandler(void) {uint16_t temp;if(USART_GetITStatus(USART1, USART_IT_RXNE) != RESET){temp = USART_ReceiveData(USART1);USART_SendData(USART1, temp);} }三、實現(xiàn)效果
對應(yīng)上面的原理圖,接收是上面2個虛擬終端,發(fā)送是下面1個虛擬終端。效果圖如下:
?
總結(jié)
以上是生活随笔為你收集整理的Proteus模拟STM32F103R6微控制器之串口通信USART的方法的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Proteus仿真STM32F103R6
- 下一篇: 浅析Codewarrior、IAR和Ke