新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > stm32f407(cortex-M4)USART串口調(diào)試程序

stm32f407(cortex-M4)USART串口調(diào)試程序

作者: 時(shí)間:2016-11-25 來(lái)源:網(wǎng)絡(luò) 收藏
上文通過(guò)調(diào)試TIM1遇到了一些問題,深入了解了stm32F407的復(fù)用功能。網(wǎng)上流傳的很多資料都是cortex-M3的,現(xiàn)在都M4了,觀念自然得跟上,一味照搬沒有自己的思考是不行的。記得我最早的調(diào)試的程序就是串口USART,剛?cè)胧致?,就網(wǎng)上找了個(gè)例程,例程對(duì)IO復(fù)用是這么寫的:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE); //打開復(fù)用時(shí)鐘GPIO_StructInit(&GPIO_InitStructure);

本文引用地址:http://butianyuan.cn/article/201611/321572.htm

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA9 作為 US1 的 TX 端,打開復(fù)用,負(fù)責(zé)發(fā)送數(shù)據(jù)

GPIO_Init(GPIOA , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA10 作為 US1 的 RX 端,負(fù)責(zé)接收數(shù)據(jù)

GPIO_Init(GPIOA, &GPIO_InitStructure);

因?yàn)镸4沒有復(fù)用時(shí)鐘功能,故復(fù)用功能打開如下:

GPIO_PinAFConfig(GPIOC, GPIO_PinSource6 |GPIO_PinSource7, GPIO_AF_USART6); //復(fù)用RX與TX

程序下載下去,發(fā)現(xiàn)只能發(fā)送而不能接收數(shù)據(jù)?。“偎疾坏闷浣?,因?yàn)榫W(wǎng)上USART都是這么寫的!調(diào)試了一天無(wú)果而終。

直到昨天調(diào)試TIM1出錯(cuò)后深究其因,找到了固件庫(kù)函數(shù)的最底層才發(fā)現(xiàn)問題的所在,也突然想到了當(dāng)初usart的接收功能為什么用不了,也對(duì)M4的復(fù)用功能有了深入的了解。不敢獨(dú)享,先分享出來(lái)。

1.m3有復(fù)用功能時(shí)鐘,復(fù)用IO時(shí)必須打開復(fù)用時(shí)鐘RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);而M4就沒有這一項(xiàng),取而代之的是GPIO_PinAFConfig();而且運(yùn)用時(shí)不能通過(guò)與元算符“|”來(lái)配置多個(gè)IO,這一點(diǎn)查看GPIO_PinAFConfig()函數(shù)定義就可知道。

2.m3只要打開了AFIO復(fù)用時(shí)鐘,就配置好了IO復(fù)用功能,相應(yīng)IO可以設(shè)置為AF_PP、IN_FLOATING、OUT,但是在M4里,GPIO_PinAFConfig()開啟后,相應(yīng)IO必須設(shè)置為“AF”,只有這樣才能真正復(fù)用IO。

USART6串口程序(查詢和中斷)如下:

#include


uint16_t usart6_get_data;

void GPIO_Config(void);
void USART_Config(void);
void USART6_Puts(char * str);
void NVIC_Config(void);
void Delay(uint32_t nCount);

main()
{

GPIO_Config();
USART_Config();
NVIC_Config();
while (1)
{
GPIO_SetBits(GPIOG,GPIO_Pin_6); //setbits使能IO,當(dāng)前下指輸出(此時(shí)為滅)
Delay(0xFFFFFF);


}
}


void GPIO_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG , ENABLE);//使能GPIOG時(shí)鐘(時(shí)鐘結(jié)構(gòu)參見“stm32圖解.pdf”)

GPIO_StructInit(&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //指定第六引腳
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //模式為輸出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //頻率為快速
GPIO_Init(GPIOG, &GPIO_InitStructure); //調(diào)用IO初始化函數(shù)
}


void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //開啟USART6時(shí)鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE); //開啟GPIOC時(shí)鐘
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//這相當(dāng)于M3的開啟復(fù)用時(shí)鐘?只配置復(fù)用的引腳,
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_USART6);//

GPIO_StructInit(&GPIO_InitStructure); //缺省值填入


GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //設(shè)置為復(fù)用,必須為AF,OUT不行
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);


GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF; //這也必須為復(fù)用,與M3不同!
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);



USART_StructInit(&USART_InitStructure);
USART_InitStructure.USART_BaudRate =115200;
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(USART6, &USART_InitStructure);
USART_ClockStructInit(&USART_ClockInitStruct); //之前沒有填入缺省值,是不行的
USART_ClockInit(USART6, &USART_ClockInitStruct);

USART_ITConfig(USART6, USART_IT_RXNE, ENABLE); //使能 USART6中斷
USART_Cmd(USART6, ENABLE); //使能 USART6
}

void NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//嵌套優(yōu)先級(jí)分組為 1
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//嵌套通道為USART6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //搶占優(yōu)先級(jí)為 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //響應(yīng)優(yōu)先級(jí)為 0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //通道中斷使能
NVIC_Init(&NVIC_InitStructure);
}


void USART6_Puts(char * str)
{
while (*str)
{
USART_SendData(USART6, *str++);


while (USART_GetFlagStatus(USART6, USART_FLAG_TXE) == RESET); //詳見英文參考的521頁(yè),當(dāng)TXE被置起時(shí),一幀數(shù)據(jù)傳輸完成
}
}



void Delay(uint32_t nCount)
{
while (nCount--);
}

中斷服務(wù)函數(shù)如下:


void USART6_IRQHandler(void)
{
if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //判斷為接收中斷
{
USART_SendData(USART6, USART_ReceiveData(USART6));//發(fā)送收到的數(shù)據(jù)
GPIO_ResetBits(GPIOG, GPIO_Pin_6);//點(diǎn)亮LED,起到中斷指示作用
}
}



評(píng)論


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

關(guān)閉