打印

ST 串口问题 新手

[复制链接]
1631|1
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
9pin|  楼主 | 2010-11-22 17:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
串口初始化,网上搬的,

GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
// USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO,ENABLE);
//配置串口TX作为推挽复用端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
     
//配置串口RX作为悬空输入端口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);

//先来处理串口对应的结构体数据,便于初始化串口寄存器
//配置串口前,请打开代码文件"stm32f10x.h",找到宏HSE_Value的定义,
//其默认值是8000000,表示8M外部晶振,请将此值改为正确的外部晶振值。
USART_StructInit(&USART_InitStructure);
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_ClockInitStructure.USART_Clock = USART_Clock_Disable;     
//    USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;      
//    USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;      
//    USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
//    USART_ClockInit(USART1,&USART_ClockInitStructure);

//将写好数值的结构体传递给库初始化串口函数,便于给相应寄存器赋值
USART_Init(USART1, &USART_InitStructure);
     
//使能串口
USART_Cmd(USART1, ENABLE);
//使能中断
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_ITConfig(USART1,USART_IT_TXE,ENABLE);

发送函数
void Usart_send_byte(INT8U ch)
{
USART_SendData(USART1,ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
}


主程序
SystemInit();
//初始化串口
Usart_init();
Usart_send_byte('a');
Usart_send_byte('b');

单步可以看到 ab
全速就只能看到b

可能是什么原因?
沙发
damy2005| | 2010-11-28 22:35 | 只看该作者
你该为
Usart_send_byte('a');
delay(150mS);
Usart_send_byte('b');
试试

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

10

主题

47

帖子

0

粉丝