求助:帮忙看下我的串口程序
#include "stm32f30x.h"#define uint unsigned int
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_DeInit(GPIOA);
USART_DeInit(USART1);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
USART_InitStructure.USART_BaudRate = 115200;//UART
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);//EN UART
USART_SendData(USART1, 0X55);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5);
}
} 串口助手收到的都是000000~ 引脚配置不同功能,每一个引脚都要调用一次GPIO_Init(),你这样写相当于只配置了最后一个引脚PA5,还有要调用GPIO_PinAFConfig()配置复用功能 void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
缺少端口复用的吧,你试试 端口复用吧? 三楼说的正确,GPIO配置不对,A9 A10默认是串口不用映射,如果是B6 B7需要映射。 #include "stm32f30x.h"
#define uint unsigned int
int main()
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);//enable clk
RCC_APB2PeriphResetCmd(RCC_APB2Periph_USART1, ENABLE);
GPIO_DeInit(GPIOA);
USART_DeInit(USART1);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//RX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_Pin_10, GPIO_AF_7);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9;//TX
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA, GPIO_Pin_9, GPIO_AF_7);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;//LED
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate = 9600;//UART
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No ;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
USART_Cmd(USART1, ENABLE);//EN UART
USART_ClearFlag(USART1,USART_FLAG_TC);
USART_SendData(USART1, 0X0055);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==1)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5);
}
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
}
修改后还是不行,主芯片是STM32F334R8 lkl0305 发表于 2015-5-14 14:19
端口复用吧?
还是不行,你有代码?能给参考一下吗?谢谢了 Winddddd 发表于 2015-5-14 14:04
引脚配置不同功能,每一个引脚都要调用一次GPIO_Init(),你这样写相当于只配置了最后一个引脚PA5,还有要调 ...
还是不行, 小蚂蚁快溜跑 发表于 2015-5-14 14:08
void GPIO_PinAFConfig(GPIO_TypeDef* GPIOx, uint16_t GPIO_PinSource, uint8_t GPIO_AF)
缺少端口复用的 ...
不行呀 ,你有串口代码吗? 给我看一下呗 ,谢谢啦
页:
[1]
2