新闻中心

EEPW首页>嵌入式系统>设计应用> stm32f407(cortex-M4)USART串口调试程序

stm32f407(cortex-M4)USART串口调试程序

作者: 时间:2016-11-25 来源:网络 收藏
上文通过调试TIM1遇到了一些问题,深入了解了stm32F407的复用功能。网上流传的很多资料都是cortex-M3的,现在都M4了,观念自然得跟上,一味照搬没有自己的思考是不行的。记得我最早的调试的程序就是串口USART,刚入手嘛,就网上找了个例程,例程对IO复用是这么写的:

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE); //打开复用时钟GPIO_StructInit(&GPIO_InitStructure);

本文引用地址://m.amcfsurvey.com/article/201611/321572.htm

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //PA9 作为 US1 的 TX 端,打开复用,负责发送数据

GPIO_Init(GPIOA , &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//PA10 作为 US1 的 RX 端,负责接收数据

GPIO_Init(GPIOA, &GPIO_InitStructure);

因为M4没有复用时钟功能,故复用功能打开如下:

GPIO_PinAFConfig(GPIOC, GPIO_PinSource6 |GPIO_PinSource7, GPIO_AF_USART6);//复用RX与TX

程序下载下去,发现只能发送而不能接收数据!!百思不得其解,因为网上USART都是这么写的!调试了一天无果而终。

直到昨天调试TIM1出错后深究其因,找到了固件库函数的最底层才发现问题的所在,也突然想到了当初usart的接收功能为什么用不了,也对M4的复用功能有了深入的了解。不敢独享,先分享出来。

1.m3有复用功能时钟,复用IO时必须打开复用时钟RCC_APB2PeriphClockCmd( RCC_APB2Periph_AFIO, ENABLE);而M4就没有这一项,取而代之的是GPIO_PinAFConfig();而且运用时不能通过与元算符“|”来配置多个IO,这一点查看GPIO_PinAFConfig()函数定义就可知道。

2.m3只要打开了AFIO复用时钟,就配置好了IO复用功能,相应IO可以设置为AF_PP、IN_FLOATING、OUT,但是在M4里,GPIO_PinAFConfig()开启后,相应IO必须设置为“AF”,只有这样才能真正复用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,当前下指输出(此时为灭)
Delay(0xFFFFFF);


}
}


void GPIO_Config(void)
{

GPIO_InitTypeDef GPIO_InitStructure;

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG , ENABLE);//使能GPIOG时钟(时钟结构参见“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);//调用IO初始化函数
}


void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStruct;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART6, ENABLE); //开启USART6时钟
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);//开启GPIOC时钟
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_USART6);//这相当于M3的开启复用时钟?只配置复用的引脚,
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;//设置为复用,必须为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;//这也必须为复用,与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);//嵌套优先级分组为 1
NVIC_InitStructure.NVIC_IRQChannel = USART6_IRQn;//嵌套通道为USART6_IRQn
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级为 0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//响应优先级为 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页,当TXE被置起时,一帧数据传输完成
}
}



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

中断服务函数如下:


void USART6_IRQHandler(void)
{
if (USART_GetITStatus(USART6, USART_IT_RXNE) != RESET) //判断为接收中断
{
USART_SendData(USART6, USART_ReceiveData(USART6));//发送收到的数据
GPIO_ResetBits(GPIOG, GPIO_Pin_6);//点亮LED,起到中断指示作用
}
}



评论


技术专区

关闭