純小白發出請救........
問題:串口發送不到,配置是不是有很多錯誤?請賜教!!
我看到自己寫的代碼,我自己都想笑

【test.c程式】
#include "bsp_text.h"
void RCC_Configuration(void)
{
//GPIO?¢USARTê±?óê1?ü
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_SetBits(GPIOC,GPIO_Pin_13);
}
void USART_Configuration(void)
{
USART_InitTypeDef USART2_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART2_InitStructure.USART_BaudRate = 115200;
USART2_InitStructure.USART_WordLength = USART_WordLength_8b;
USART2_InitStructure.USART_StopBits = USART_StopBits_1;
USART2_InitStructure.USART_Parity = USART_Parity_No;
USART2_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART2_InitStructure.USART_Mode = USART_Mode_Tx | USART_Mode_Tx;
USART_Init(USART2, &USART2_InitStructure);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel=USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
【main.c程式】
#include "stm32f10x.h"
#include "bsp_text.h"
#include "stdio.h"
void GpuSend(char * buf1);
void USART2_Init(void);
void NVIC_Configuration(void);
int fputc(int ch, FILE *f)
{
USART_SendData(USART2, (unsigned char) ch);
while (USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
return (ch);
}
void USART_SendChar(USART_TypeDef* pUSARTx, uint8_t c)
{
USART_SendData(pUSARTx, c);
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
void USART_SendString(USART_TypeDef* pUSARTx, char* str)
{
uint32_t n = 0;
while (*(str + n) != '\0')
{
USART_SendChar(pUSARTx, *(str + n));
n++;
}
while (USART_GetFlagStatus(pUSARTx, USART_FLAG_TXE) == RESET);
}
int main()
{
NVIC_Configuration();
printf("110-119-120");
while(1){}
}
【test.h程式】
#ifndef _GPIO_H
#define _GPIO_H
#include "stm32f10x.h"
#endif
————————————————
著作權宣告:本文為CSDN博主「『純※小白』」的原創文章,遵循 CC 4.0 BY-SA 著作權協議,轉載請附上原文出處鏈接及本宣告。
原文鏈接:https://blog.csdn.net/weixin_45454723/article/details/103541931
————————————————
著作權宣告:本文為CSDN博主「『純※小白』」的原創文章,遵循 CC 4.0 BY-SA 著作權協議,轉載請附上原文出處鏈接及本宣告。
原文鏈接:https://blog.csdn.net/weixin_45454723/article/details/103541931
uj5u.com熱心網友回復:
你在main()并沒有初始化串口,串口當然不會干活。uj5u.com熱心網友回復:
在mian.c{
USART2_InitStructure();
GPIO_InitStructure();
.................................
.................................
}
先初始化這兩個?還有其他要初始化的嗎?
uj5u.com熱心網友回復:
這種東西例程很多,找個先來學習學習。uj5u.com熱心網友回復:
例程有道理但是有些中斷、系統定時器,還是看不懂
uj5u.com熱心網友回復:
沒看明白自己寫難度更大,還是一步步來,先看明白。uj5u.com熱心網友回復:
好吧,謝謝您@xuyaqi029你是男的嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/69021.html
標籤:單片機/工控
上一篇:匯編偽指令,求此題詳解
