F103求助USART3无法工作,MDK仿真能够输出,但板子不能打印
代码如下:那位大神指点一下啊~#include "stm32f10x.h"
#include "stdio.h"
void delay(__IO u32 nCount);
int main(void)
{
/* USART1 config 115200 8-N-1 */
USART3_Config();
printf("\r\nSTM32f103 USART1 test");
//USART3_Config();
while(1)
{
printf("\r\n TM32f103 USART3 test \r\n");
// 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 = tmppriority;
/* Enable the Selected IRQ Channels --------------------------------------*/
NVIC->ISER =
(uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F);
}
else
{
/* Disable the Selected IRQ Channels -------------------------------------*/
NVIC->ICER =
(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 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**********************/
原来是在GPIO Config()里面没有开启总线时钟,里面加上这句就可以了。
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOB, ENABLE); 无能为力帮你顶个帖子吧 :'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(:'(大神呢 你把那些乱码处理下,没法看 没有把复用功能映射到IO口吧,有一个GPIO_PinAFConfig函数就是这个功能,可以试试 你这个代码哎:(:(:(:(:(:(:(:(:(:(:(:(:( 黑皮男 发表于 2016-12-29 09:01
没有把复用功能映射到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,仿真显示结果正常。为什么啊?求指点。 五哥1 发表于 2016-12-29 02:37
你把那些乱码处理下,没法看
重新整理了一下,在9楼,10楼有进一步的描述,麻烦帮我看一下呗。
页:
[1]
2