原则上,像USART1等这些基本的外设,根据官方的库,应该直接成功了,可是,我试了一下,就是不成功。虽然有STM32F10X的基础,但是感觉打印的就是乱码,并且发送时,竟然中断接收函数里,中断进不了!!
后来查看原理图,原来STLink V2有引脚与PA9 PA10连接。应该是USB转串口吧,因此,再把USB-TTL 接到PA9 PA10上,就会发生 两个TXD->TXD相互干扰的现象,因此不通!!我这里直接去掉连接的短接电阻,程序正常了。
注意开始时需要修改一下库里的晶振设置:8M外部晶振与PLL_M=8。官方库好像是基于25MHz晶振的,但是实际的焊接的为:8MHz晶振。
修改后:测试成功了。收发正常了。
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
#include "usart1.h"
-
#include
-
-
-
-
-
-
-
-
-
void USART1_Config(uint32_t uBaud)
-
{
-
USART1_Configuration(uBaud);
-
USART1_NVIC_Configuration();
-
}
-
-
-
-
-
-
-
-
-
-
void USART1_Configuration(uint32_t uBaud)
-
{
-
GPIO_InitTypeDef GPIO_InitStructure;
-
USART_InitTypeDef USART_InitStructure;
-
-
-
-
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);
-
-
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-
-
-
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
-
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
-
-
-
-
GPIO_Init(GPIOA, &GPIO_InitStructure);
-
-
-
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;
-
-
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4);
-
-
-
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);
-
}
-
-
-
-
-
-
-
-
-
-
-
int fputc(int ch, FILE *f)
-
{
-
-
USART_SendData(USART1, (unsigned char) ch);
-
while (!(USART1->SR & USART_FLAG_TXE));
-
-
return (ch);
-
}
-
-
-
-
-
-
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工程下载:
阅读(2967) | 评论(0) | 转发(0) |