初学串口,串口发送有数据,但数据错误,按照例程编的,就是不知道哪里有问题,贴出来请教各位老师。
- #include "stm32f10x.h"
- void Delay(unsigned int x);
- void UART_Init(void);
- void UART2_PutChar(unsigned char ch);
- int main(void)
- {
- SystemInit();
- UART_Init();
- while(1)
- {
- Delay(6550);
- UART2_PutChar(0x26);
- }
- void UART_Init(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOC,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
- 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_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- USART_InitTypeDef USART_InitStructure;//定义串口结构体
- 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(USART2,&USART_InitStructure);
- USART_Cmd(USART2,ENABLE);
- }
- void UART2_PutChar(unsigned char ch)
- {
- USART_SendData(USART2,ch);
- while(USART_GetFlagStatus(USART2,USART_FLAG_TXE) == RESET);
- }
- void Delay(unsigned int x)
- {
- unsigned int t;
- t=x;
- while(t--);
- }
|