1 问题
在利用USART 结构体进行初始化操作时,
根据函数库提供的例子进行初始化时候
compiling main.c...
../User/main.c(73): error: #134: expected a field name
../User/main.c(78): error: #136: struct "<unnamed>" has no field "USART_Clock"
../User/main.c(79): error: #136: struct "<unnamed>" has no field "USART_CPOL"
../User/main.c(80): error: #136: struct "<unnamed>" has no field "USART_CPHA"
../User/main.c(81): error: #136: struct "<unnamed>" has no field "USART_LastBit"
../User/main.c - 5 Error(s), 0 Warning(s).
通过查看相关头文件 时候 发现
typedef struct
{
uint32_t USART_BaudRate;
uint16_t USART_WordLength;
uint16_t USART_StopBits;
uint16_t USART_Parity;
uint16_t USART_Mode;
uint16_t USART_HardwareFlowControl;
} USART_InitTypeDef;
typedef struct
{
uint16_t USART_Clock;
uint16_t USART_CPOL;
uint16_t USART_CPHA;
uint16_t USART_LastBit;
} USART_ClockInitTypeDef;
中文提供的初始化 例子如下
{
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_Parity_Odd;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_RTS_CTS;
USART_InitStructure.USART_Mode = USART_Mode_Tx|USART_Mode_Rx;
USART_InitStructure.USART_Clock = USART_Clock_Disable;
USART_InitStructure.USART_CPOL = USART_CPOL_High;
USART_InitStructure.USART_CPHA = USART_CPHA_1Edge;
USART_InitStructure.USART_LastBit = USART_LastBit_Enable;
USART_Init(USART1,&USART_InitStructure);
}
根据ST英文库函数提供的例子
Example:
/* The following example illustrates how to configure the USART1 */
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_Odd;
USART_InitStructure.USART_HardwareFlowControl =
USART_HardwareFlowControl_RTS_CTS;
USART_InitStructure.USART_Mode = USART_Mode_Tx I USART_Mode_Rx;
USART_Init(USART1, &USART_InitStructure);
通过比较可以发现 第一个提供的例子的确是存在问题!
就是分明是两个函数体的 参数 在该例子中统一赋值处理! 早上编译器出现问题
得出结论! 学习以英文为参考!!
当我在将串口进行设置完毕后, 用软件进行仿真,始终不能得到我想要发送字符0xaa~~
仔细检查设置情况 没问题,通过外设端口设置发现在USART1 的通讯设置如下
Clock Disabled, 9603 baud,
1 Start Bit,
8 Data Bits,
1.0 Stop Bits,
No Parity
发现USART1的时钟为 Disable状态~!
O ! my god!!!
又是忘记了将外设始终 进行使能了~~!~!
//注意 此处要打开用到USART1的时钟 否则引脚不工作
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
编译后 用软件仿真后 成功~~~!!
在使用USART1进行初始化时遇到了编译错误,错误涉及结构体字段。对比英文库函数例子发现,示例代码存在混淆。修复后,通讯仍然无法正常工作,原因是忘了使能USART1的时钟。启用时钟后,通讯成功。
2万+

被折叠的 条评论
为什么被折叠?



