完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
代码如下:那位大神指点一下啊~
#include "STM32f10x.h" #include "stdio.h" void delay(__IO u32 nCount); int main(void) { /* USART1 config 115200 8-N-1 */ USART3_Config(); printf("rn STM32f103 USART1 test"); //USART3_Config(); while(1) { printf("rn TM32f103 USART3 test rn"); // for (;;) { //} } } void delay(__IO u32 nCount) { for (; nCount != 0; nCount--); } void USART3_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB, ENABLE); /* USART3 GPIO config */ /* Configure USART3 Tx (PB.10) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART3 Rx (PB.11) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); /* USART3 mode config */ 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(USART3, &USART_InitStructure); /* ʹÄÜ´®¿Ú3½ÓÊÕÖÐ¶Ï */ USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3, ENABLE); } //ÅäÖÃUSART3½ÓÊÕÖÐ¶Ï void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Configure the NVIC Preemption Priority Bits */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_3); /* Enable the USARTy Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct) { uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F; /* Check the parameters */ assert_param(IS_FUNCTIONAL_STATE(NVIC_InitStruct->NVIC_IRQChannelCmd)); assert_param(IS_NVIC_PREEMPTION_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority)); assert_param(IS_NVIC_SUB_PRIORITY(NVIC_InitStruct->NVIC_IRQChannelSubPriority)); if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE) { /* Compute the Corresponding IRQ Priority --------------------------------*/ tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08; tmppre = (0x4 - tmppriority); tmpsub = tmpsub >> tmppriority; tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; tmppriority = tmppriority << 0x04; NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority; /* Enable the Selected IRQ Channels --------------------------------------*/ NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); } else { /* Disable the Selected IRQ Channels -------------------------------------*/ NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); } } #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000) void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) { /* Check the parameters */ assert_param(IS_NVIC_PRIORITY_GROUP(NVIC_PriorityGroup)); /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */ SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup; } /// Öض¨Ïòc¿âº¯Êýprintfµ½USART3 int fputc(int ch, FILE *f) { /* ·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ýµ½USART3 */ USART_SendData(USART3, (uint8_t) ch); /* µÈ´ý·¢ËÍÍê±Ï */ while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); return (ch); } /// Öض¨Ïòc¿âº¯Êýscanfµ½USART3 int fgetc(FILE *f) { /* µÈ´ý´®¿Ú1ÊäÈëÊý¾Ý */ while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART3); } /*********************************************END OF FILE**********************/ |
|
相关推荐
20个回答
|
|
无能为力帮你顶个帖子吧
|
|
|
|
:'(:'(:'(:'(:'(大神呢
|
|
|
|
你把那些乱码处理下,没法看
|
|
|
|
没有把复用功能映射到IO口吧,有一个GPIO_PinAFConfig函数就是这个功能,可以试试
|
|
|
|
你这个代码 哎 :(:(:(
|
|
|
|
我用的是LQFP48脚封装的,不需要开吧!等会我开了试试。 |
|
|
|
重新整理了一下。
#include "stm32f10x.h" #include "stdio.h" #include "misc.h" #include "led.h" void delay(__IO u32 nCount); void GPIO_Config(void); void USART3_Config(void); int main(void) { RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3 | RCC_APB2Periph_GPIOB,ENABLE); GPIO_Config(); USART3_Config(); while(1) { printf("adsfsfsfsfs"); } // return (1); } void delay(__IO u32 nCount) { for (; nCount != 0; nCount--); } void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; /* Configure USART3 Tx (PB.10) as alternate function push-pull */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); /* Configure USART3 Rx (PB.11) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); } void USART3_Config(void) { USART_InitTypeDef USART_InitStructure; //* USART3 mode config */ 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; USART_Init(USART3, &USART_InitStructure); USART_ITConfig(USART3, USART_IT_RXNE, ENABLE); USART_Cmd(USART3, ENABLE); } int fputc(int ch, FILE *f) { USART_SendData(USART3, (uint8_t) ch); while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET); return (ch); } int fgetc(FILE *f) { while (USART_GetFlagStatus(USART3, USART_FLAG_RXNE) == RESET); return (int)USART_ReceiveData(USART3); } |
|
|
|
如上所述,仿真的时候能够通过,也有打印消息输出,但连上芯片,芯片都没有打印输出。还发现了一个问题,就是仿真的时候如果波特率设置为115200在MDK仿真里面显示的是115384,如果设置为9600,仿真显示结果正常。为什么啊?求指点。
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
程序没有错,是硬件连接上的问题。能用了。
|
|
|
|
原来是在GPIO Config()里面没有开启总线时钟,里面加上这句就可以了。
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); |
|
|
|
|
|
重新整理了一下
|
|
|
|
|
|
检测串口打印那块的配置
|
|
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
991 浏览 0 评论
AD7686芯片不传输数据给STM32,但是手按住就会有数据。
970 浏览 2 评论
2080 浏览 0 评论
如何解决MPU-9250与STM32通讯时,出现HAL_ERROR = 0x01U
1177 浏览 1 评论
hal库中i2c卡死在HAL_I2C_Master_Transmit
1599 浏览 1 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-23 09:36 , Processed in 0.985712 second(s), Total 110, Slave 93 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号