STM32F103ZET串口问题,三天没有搞定!
发布: 2010-1-29 21:25 | 作者: win2000_li | 来源: StmFans思蜕盟 OPELC 自由电子联盟
说来都不好意思,这个USART都搞了三天了,始终搞不定。
只有请各位老师指点一下。。。。。。。。。。。。。
谢谢。。。。。。。
我用的是STM32F103ZET, 8M(HSE), IAR540, library3.0.
用的是串口1(USART1),发上来的数据都是乱的。
不知道为什么????
程序如下:
只有请各位老师指点一下。。。。。。。。。。。。。
谢谢。。。。。。。
我用的是STM32F103ZET, 8M(HSE), IAR540, library3.0.
用的是串口1(USART1),发上来的数据都是乱的。
不知道为什么????
程序如下:
Polling.rar
(2010-01-29 21:25:17, Size: 988 KB, Downloads: 1)

{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* USART1 configuration -----------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_USART1, ENABLE);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- Even parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
int main(void)
{
McuInit();
while (1)
{
if (TRUE == CpuTickFalg)
{
CpuTickFalg = ClrZero;
ScanKeyIn();
PrtLedFun();
USART_SendData(USART1, 0x12);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
}
}
}
截图20.jpg
还是不行啊!!!
问题到底在哪里呢?????????
发送上来的数据是:0x3b
/****************************************************************************
* 名 称:void UsartInit(void)
* 功 能:USART初始化程序
* 入口参数:无
* 出口参数:无
* 说 明:
* 作 者: 李彦平
* 创建时间: 2010/01/25
* 修改记录:
****************************************************************************/
void UsartInit(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* USART1 configuration -----------------------------------------------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_AFIO, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART configured as follow:
- BaudRate = 115200 baud
- Word Length = 8 Bits
- One Stop Bit
- Even parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
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;
/* Configure USART1 */
USART_Init(USART1, &USART_InitStructure);
/* Enable the USART1 */
USART_Cmd(USART1, ENABLE);
}
int main(void)
{
McuInit();
while (1)
{
//if (TRUE == CpuTickFalg)
//{
//CpuTickFalg = ClrZero;
//ScanKeyIn();
//PrtLedFun();
USART_SendData(USART1, 0x12);
while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);
//}
}
}
同时把串口部分修正(你在4楼的串口初始化码是正确的了),编译后写入豆皮(我修改配置改为在FLASH中运行)。
用串口调试器观察,可以正常收到豆皮发上来的“0x12"数据。
截图21.jpg
我今天到同事那里,发现程序是可以的。
问题出在USB转串口的东东上,我的本本没有串口,所以前些天买了一个
18元的USB转串口,没有想到问题出在这里。。。。
非常感谢您的帮助!!!!!!!!
质量肯定堪忧的
这样说,这个东东,并且没有坏啊。。。。
为什么会出现这种问题啊????????
另外你在你的板子上短接MAX3232的STM32侧的收发引脚(这两脚与STM32的联线要断开),测一测PC、USB to 232、MAX3232这一段有没有问题。
建议你也检查你板子上了电平转换电路。