您的位置:首页 > 其它

基于STM32F429-Discovery USART1 PA9 PA10 调试成功。

2016-05-22 16:44 886 查看
原则上,像USART1等这些基本的外设,根据官方的库,应该直接成功了,可是,我试了一下,就是不成功。虽然有STM32F10X的基础,但是感觉打印的就是乱码,并且发送时,竟然中断接收函数里,中断进不了!!

后来查看原理图,原来STLink V2有引脚与PA9 PA10连接。应该是USB转串口吧,因此,再把USB-TTL 接到PA9 PA10上,就会发生 两个TXD->TXD相互干扰的现象,因此不通!!我这里直接去掉连接的短接电阻,程序正常了。



注意开始时需要修改一下库里的晶振设置:8M外部晶振与PLL_M=8。官方库好像是基于25MHz晶振的,但是实际的焊接的为:8MHz晶振。

修改后:测试成功了。收发正常了。

/********************     (C)  2016     ***************************
* 文件名 	:usart1.c
* 描述   	:将printf函数重定向到USART3。这样就可以用printf函数将单片机的数据
*        	  打印到PC上的超级终端或串口调试助手。
* 实验平台	:STM32F429
* 硬件连接	:------------------------
*          | PA9  - USART1(Tx)      |
*          | PA10  - USART1(Rx)      |
*           ------------------------
* 库版本  	:V1.6.1
* 编写日期	:2016-05-22
* 修改日期	:
* 作者    	:
*****************************************************************************/
#include "usart1.h"
#include <stdarg.h>

/*
* 函数名:USART1_Config
* 描述  :USART1 GPIO 配置
* 输入  :uint32_t uBaud
* 输出  : 无
* 调用  :外部调用
*/
void USART1_Config(uint32_t uBaud)
{
USART1_Configuration(uBaud);
USART1_NVIC_Configuration();
}

/*
* 函数名:USART1_Configuration
* 描述  :USART1 GPIO 配置,工作模式配置。uBaud 8-N-1
* 输入  :uint32_t uBaud
* 输出  : 无
* 调用  :外部调用
*/
void USART1_Configuration(uint32_t uBaud)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;

/* config USART1 clock */

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);

GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_USART1);

/* USART1 GPIO config */
/* Configure USART1 Tx (PA.9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
//GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
//GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);

/* USART1 mode config */
USART_InitStructure.USART_BaudRate = uBaud;
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);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_Cmd(USART1, ENABLE);
}

void USART1_NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);

/* Enable the USARTy Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}

/*
* 函数名:fputc
* 描述  :重定向c库函数printf到USART1
* 输入  :无
* 输出  :无
* 调用  :由printf调用
*/
int fputc(int ch, FILE *f)
{
/* 将Printf内容发往串口 */
USART_SendData(USART1, (unsigned char) ch);
while (!(USART1->SR & USART_FLAG_TXE));
//while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
return (ch);
}


/**
* @brief  This function handles USART1 Handler.
* @param  None
* @retval None
*/
void USART1_IRQHandler(void)
{
uint8_t c;
if(USART_GetITStatus(USART1,USART_IT_RXNE))
{
USART_ClearITPendingBit(USART1, USART_IT_RXNE);
c=(uint8_t)USART_ReceiveData(USART1);
printf("%c",c);
}
}


Keil V5.10工程下载:

点击打开链接
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: