完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#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){} } } |
|
相关推荐
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
238 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
734 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1128 浏览 1 评论
LL库F030进行3个串口收发,2个串口为232,一个为485,长时间后,会出现串口1停止运行,另外两个正常,只有重启复原
1600 浏览 1 评论
538 浏览 0 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-9 10:11 , Processed in 0.503086 second(s), Total 72, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号