#include "stm32f10x_lib.h"
#include
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
ErrorStatus HSEStartUpStatus;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void USART_Config(void);
//void USART1_Puts(char * str);//输出字符串
void USART1_Putchar(u8 ch);//输出一个字符
//unsigned char USART1_ReceiveChar(void);//接收一个字符
//char rbuff[100];
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
USART_Config();
USART1_Putchar(0x11);
while(1)
{
}
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
// ErrorStatus HSEStartUpStatus;
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1); //串口
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* PLLCLK = 8MHz * 9 = 72 MHz */
//RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLConfig(RCC_PLLSource_HSI_Div2, RCC_PLLMul_12); //4M*12 =48M
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
}
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Configure USART1_Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1_Rx as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//USART工作在异步模式下
USART_InitStructure.USART_BaudRate = 9600;//波特率
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;//发送接收均使能
/* Configure the USARTx */
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //若接收寄存器满,产生中断
//printf("ab") ;
/* Enable the USARTx */
USART_Cmd(USART1, ENABLE); //控制寄存器13位置1
/*解决第一字节无法发送的问题*/
// USART_ClearFlag(USART1,USART_FLAG_TC); //清标志
}
/*******************************************************************************
* Function Name : NVIC_Configuration
* Description : Configures Vector Table base location.
* 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
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置优先级组为第1组(抢占式优先级有1位,副优先级有3位)
//配置USART中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQChannel;//中断通道为RTC全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级为1
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//副优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断通道使能
NVIC_Init(&NVIC_InitStructure);//初始化NVIC结构体
}
void USART1_Putchar(u8 ch)
{
USART_SendData(USART1,(u8) ch);
/* Loop until the end of transmission */
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//等待发送完成
}
void USART1_IRQHandler(void)
{
u8 dat;
// if (USART_GetITStatus(USART1,USART_IT_RXNE)!= RESET)//查询是否为中断标志
if(USART_GetFlagStatus(USART1, USART_FLAG_RXNE)!=RESET)
{
dat=USART_ReceiveData(USART1);
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
if(dat==0x63)
{dat=0;
USART1_Putchar(0x77);
USART1_Putchar(0x97);
}
while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET){}
}
}