新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32學(xué)習(xí)筆記(7):USART串口的使用

STM32學(xué)習(xí)筆記(7):USART串口的使用

作者: 時(shí)間:2016-11-25 來源:網(wǎng)絡(luò) 收藏
//RCC時(shí)鐘配置
void RCC_cfg()
{
//定義錯(cuò)誤狀態(tài)變量
ErrorStatus HSEStartUpStatus;
//將RCC寄存器重新設(shè)置為默認(rèn)值
RCC_DeInit();
//打開外部高速時(shí)鐘晶振
RCC_HSEConfig(RCC_HSE_ON);
//等待外部高速時(shí)鐘晶振工作
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
//設(shè)置AHB時(shí)鐘(HCLK)為系統(tǒng)時(shí)鐘
RCC_HCLKConfig(RCC_SYSCLK_Div1);
//設(shè)置高速AHB時(shí)鐘(APB2)為HCLK時(shí)鐘
RCC_PCLK2Config(RCC_HCLK_Div1);
//設(shè)置低速AHB時(shí)鐘(APB1)為HCLK的2分頻
RCC_PCLK1Config(RCC_HCLK_Div2);
//設(shè)置FLASH代碼延時(shí)
FLASH_SetLatency(FLASH_Latency_2);
//使能預(yù)取指緩存
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
//設(shè)置PLL時(shí)鐘,為HSE的9倍頻 8MHz * 9 = 72MHz
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
//使能PLL
RCC_PLLCmd(ENABLE);
//等待PLL準(zhǔn)備就緒
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
//設(shè)置PLL為系統(tǒng)時(shí)鐘源
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
//判斷PLL是否是系統(tǒng)時(shí)鐘
while(RCC_GetSYSCLKSource() != 0x08);
}
//打開GPIO時(shí)鐘,復(fù)用功能,串口1的時(shí)鐘
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
}
//IO口配置
void GPIO_cfg()
{
GPIO_InitTypeDef GPIO_InitStructure;
//PA9作為US1的TX端,打開復(fù)用,負(fù)責(zé)發(fā)送數(shù)據(jù)
GPIO_StructInit(&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA , &GPIO_InitStructure);
//PA10作為US1的RX端,負(fù)責(zé)接收數(shù)據(jù)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//LED顯示串口正在發(fā)送/接收數(shù)據(jù)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//串口初始化
void USART_cfg()
{
USART_InitTypeDef USART_InitStructure;
//將結(jié)構(gòu)體設(shè)置為缺省狀態(tài)
USART_StructInit(&USART_InitStructure);
//波特率設(shè)置為115200
USART_InitStructure.USART_BaudRate = 115200;
//一幀數(shù)據(jù)的寬度設(shè)置為8bits
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//在幀結(jié)尾傳輸1個(gè)停止位
USART_InitStructure.USART_StopBits = USART_StopBits_1;
//奇偶失能模式,無奇偶校驗(yàn)
USART_InitStructure.USART_Parity = USART_Parity_No;
//發(fā)送/接收使能
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
//硬件流控制失能
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//設(shè)置串口1
USART_Init(USART1, &USART_InitStructure);
//打開串口1的中斷響應(yīng)函數(shù)
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
//打開串口1
USART_Cmd(USART1, ENABLE);
}
//配置中斷
void NVIC_cfg()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //選擇中斷分組2
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel; //選擇串口1中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占式中斷優(yōu)先級(jí)設(shè)置為0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)式中斷優(yōu)先級(jí)設(shè)置為0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中斷
NVIC_Init(&NVIC_InitStructure);
}
然后在stm32f10x_it.c文件中找到相應(yīng)的中斷處理函數(shù),并填入一下內(nèi)容。注意在stm32f10x_it.c中,要聲明一下外部變量RX_status
extern FlagStatus RX_status;
void USART1_IRQHandler(void)
{
GPIO_SetBits(GPIOB, GPIO_Pin_5);
//確認(rèn)是否接收到數(shù)據(jù)
RX_status = USART_GetFlagStatus(USART1, USART_FLAG_RXNE);
//接收到數(shù)據(jù)
if(RX_status == SET)
{
//將數(shù)據(jù)回送至超級(jí)終端
USART_SendData(USART1, USART_ReceiveData(USART1));
//等待數(shù)據(jù)發(fā)送完畢
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
}
}

上一頁 1 2 下一頁

評(píng)論


技術(shù)專區(qū)

關(guān)閉