一、GPIO设置
- /****************************************************************************
- * Function Name : GPIO_Configuration
- * Description :
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- void GPIO_Configuration(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- }
同时需要开启复用功能的时钟,和USART1的时钟。
二、USART的初始化
- /*******************************************************************************
- * Function Name : USART_Configure
- * Description : Configures the USART
- * Input : None
- * Output : None
- * Return : None
- *******************************************************************************/
- void USART_Configuration(void)
- {
- 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(USART1, &USART_InitStructure);
- USART_Cmd(USART1, ENABLE);
- }
三、main函数
- /****************************************************************************
- * Function Name : main
- * Description : Main program.
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- int main(void)
- {
- RCC_Configuration();
-
- NVIC_Configuration();
-
- GPIO_Configuration();
-
- USART_Configuration();
-
- while(1)
- {
- GPIO_WriteBit(GPIOB, GPIO_Pin_8, (BitAction)(1 - GPIO_ReadOutputDataBit(GPIOB, GPIO_Pin_8)));
- fPutString("Hello World!", 13);
- Delay(0xffffff);
- }
- }
- /****************************************************************************
- * Function Name : fPutString
- * Description : Send a string.
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- void fPutString(u8 *buf, u8 len)
- {
- u8 i;
- for(i=0;i<len;i++)
- {
- fPutChar(*buf++);
- }
- }
-
- /****************************************************************************
- * Function Name : fPutChar
- * Description : Send a byte
- * Input : None
- * Output : None
- * Return : None
- ****************************************************************************/
- u8 fPutChar(u8 ch)
- {
- USART_SendData(USART1, (u8) ch);
- while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET)
- {
-
- }
- return ch;
- }
四、补充NVIC
- /**************************实现函数********************************************
- *函数原型: void U1NVIC_Configuration(void)
- *功 能: 串口1中断配置
- 输入参数:无
- 输出参数:没有
- *******************************************************************************/
- void UART1NVIC_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- /* Enable the USART1 Interrupt */
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
五、支持printf输出,不用选择use MicroLIB
- //////////////////////////////////////////////////////////////////
- //加入以下代码,支持printf函数,而不需要选择use MicroLIB
- #if 1
- #pragma import(__use_no_semihosting)
- //标准库需要的支持函数
- struct __FILE
- {
- int handle;
- /* Whatever you require here. If the only file you are using is */
- /* standard output using printf() for debugging, no file handling */
- /* is required. */
- };
- /* FILE is typedef’ d in stdio.h. */
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- _sys_exit(int x)
- {
- x = x;
- }
- //重定义fputc函数
- int fputc(int ch, FILE *f)
- {
- while((USART1->SR&0X40)==0);//循环发送,直到发送完毕
- USART1->DR = (u8) ch;
- return ch;
- }
- #endif
给主人留下些什么吧!~~
评论热议