void USART_Configuration()
{
USART_InitTypeDef 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_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Clock=USART_Clock_Disable;
USART_InitStructure.USART_CPOL=USART_CPOL_Low;
USART_InitStructure.USART_CPHA=USART_CPHA_2Edge;
USART_InitStructure.USART_LastBit=USART_LastBit_Disable;
USART_Init(USART1,&USART_InitStructure);
USART_Cmd(USART1,ENABLE);
}
[
本帖最后由 TopMars 于 2011-8-12 15:04 编辑 ]
此帖出自
小平头技术问答
一个是:void USART_ClockInit(USART_TypeDef* USARTx, USART_ClockInitTypeDef* USART_ClockInitStruct),其第二个参数类型为指向USART_ClockInitTypeDef类型的结构体的指针,这个结构的成员如下:
typedef struct
{
u16 USART_Clock;
u16 USART_CPOL;
u16 USART_CPHA;
u16 USART_LastBit;
} USART_ClockInitTypeDef;
里面有你要找的USART_Clock等四个成员
第二个函数是:void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct)
第二个参数是一个指向USART_InitTypeDef类型结构体的指针,该类型的成员为:
typedef struct
{
u32 USART_BaudRate;
u16 USART_WordLength;
u16 USART_StopBits;
u16 USART_Parity;
u16 USART_Mode;
u16 USART_HardwareFlowControl;
} USART_InitTypeDef;
所以,楼主是搞混了USART_ClockInitTypeDef和USART_InitTypeDef这两个结构体同义字,好好查看一下吧。
正确的配置方法是:
/* 定义USART初始化结构体 USART_InitStructure */
USART_InitTypeDef USART_InitStructure;
/* 定义USART初始化结构体 USART_ClockInitStructure */
USART_ClockInitTypeDef USART_ClockInitStructure;
/*
* 波特率为115200bps;
* 8位数据长度;
* 1个停止位,无校验;
* 禁用硬件流控制;
* 禁止USART时钟;
* 时钟极性低;
* 在第2个边沿捕获数据
* 最后一位数据的时钟脉冲不从 SCLK 输出;
*/
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_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(USART1 , &USART_InitStructure);
仅供参考
一周热门 更多>