在学习STM32F103芯片的USART1时,当只开启发送模式时,终端能正常显示,但当再加入接收模式时,MDK能编译成功,但烧录后终端不显示,开发板上指示芯片工作的LED灯也不闪烁。希望高人给指点一下!
程序:
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h" //调用的库文件
#include <stdio.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
USART_InitTypeDef USART_InitStructure;
ErrorStatus HSEStartUpStatus;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void); //申明时钟初始化函数
void GPIO_Configuration(void); //申明IO初始化函数
void NVIC_Configuration(void); //申明中断管理器初始化函数
void USART1_Puts(char * str);
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void) //主函数
{
#ifdef DEBUG
debug();
#endif
//int Tx_Data;
int i,RX_status = 0;
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
/* USART1 configuration ------------------------------------------------------*/
/* USART1 configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
- USART Clock disabled
- USART CPOL: Clock is active low
- USART CPHA: Data is captured on the second edge
- USART LastBit: The clock pulse of the last data bit is not output to
the SCLK pin
*/
USART_InitStructure.USART_BaudRate = 115200; //设置USART传输波特率
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_InitStructure.USART_Clock = USART_Clock_Disable;//USART时钟低电平活动
USART_InitStructure.USART_CPOL = USART_CPOL_Low; //指定了下SLCK引脚上时钟输出的极性
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;//指定了下SLCK引脚上时钟输出的相位
/******控制是否在同步模式下,在SCLK引脚上输出最后发送的哪个数据字对应的时钟脉冲
最后一位数据的时钟脉冲不从SCLK输出***/
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
/* Configure the USART1 */
USART_Init(USART1, &USART_InitStructure); //初始化外设USARTx寄存器
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //使能接收中断
USART_ITConfig(USART1, USART_IT_TXE, ENABLE); //使能发送中断
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);//使能或者失能USART外设
// Tx_Data=0x30;
while(1)
{ USART1_Puts("Hello 电科
");
for(i=0;i<1500000;i++);
GPIO_WriteBit(GPIOA, GPIO_Pin_1, (BitAction)0x01);
RX_status = USART_GetFlagStatus(USART1, USART_FLAG_RXNE);
if(RX_status == SET)
{
USART_SendData(USART1 , USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
}
for(i=0;i<200000;i++);
GPIO_WriteBit(GPIOA, GPIO_Pin_1, (BitAction)0x00);
for(i=0;i<200000;i++);
}
}
/********发送字符串函数*********/
void USART1_Puts(char * str)
{
while(*str)
{
USART_SendData(USART1, *str++);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit(); //将外设RCC寄存器重设为缺省值
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON); //设置外部高速晶振
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp(); //等待HSE起振
if(HSEStartUpStatus == SUCCESS) //起振成功
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1); //设置AHB时钟
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); //设置高速AHB时钟
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速AHB时钟
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);//设置PLL时钟源及倍频系数
/* Enable PLL */
RCC_PLLCmd(ENABLE);//使能或者失能PLL
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) //检查指定的RCC标志位设置与否
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOA、GPIOB and USART1 clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);//使能或者失能APB2外设时钟
}
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure USART1 Tx (PA9) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //IO端口的第9位
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //翻转速度为50M
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //端口模式为复用推拉输出方式
GPIO_Init(GPIOA, &GPIO_InitStructure); //用以上几个参数初始化PA口
/* Configure USART1 Rx (PA10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //IO端口的第10位
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //端口模式为浮点输入方式
GPIO_Init(GPIOA, &GPIO_InitStructure); //用以上几个参数初始化PA口
/*Configure PB11 as output for LD3*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1; //IO端口的第1位
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //翻转速度为50M
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //端口模式为推拉输出方式
GPIO_Init(GPIOA, &GPIO_InitStructure); //用以上几个参数初始化PA口
/*Configure PB10 as output for LD4*/
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4; //IO端口的第4位
GPIO_Init(GPIOA, &GPIO_InitStructure); //用以上几个参数初始化PA口
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures the nested vectored interrupt controller.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;//使能或者失能指定IRQ通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;//设置该成员的先占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//设置该成员的从优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//指定了在成员中定义的IRQ通道使能还是失能
NVIC_Init(&NVIC_InitStructure);
}
#ifdef DEBUG
/*******************************************************************************
* Function Name : assert_failed
* Description : Reports the name of the source file and the source line number
* where the assert error has occurred.
* Input : - file: pointer to the source file name
* - line: assert error line source number
* Output : None
* Return : None
*******************************************************************************/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
此帖出自
小平头技术问答
一周热门 更多>