STM32F103ZET串口问题,三天没有搞定!

发布: 2010-1-29 21:25 | 作者: win2000_li | 来源: StmFans思蜕盟 OPELC 自由电子联盟

说来都不好意思,这个USART都搞了三天了,始终搞不定。

只有请各位老师指点一下。。。。。。。。。。。。。

谢谢。。。。。。。

我用的是STM32F103ZET, 8M(HSE), IAR540, library3.0.

用的是串口1(USART1),发上来的数据都是乱的。

不知道为什么????

程序如下:

Polling.rar
(2010-01-29 21:25:17, Size: 988 KB, Downloads: 1)

win2000_li (2010-1-29 21:27:01)
void UsartInit(void)
{
        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);
                }
        }
}
binglin (2010-1-29 21:32:29)
把下图中红圈处的这行去掉,你又不是用重映射USART1为什么又要去做重映谢呢。


截图20.jpg

win2000_li (2010-1-29 21:51:56)
[i=s] 本帖最后由 win2000_li 于 2010-1-29 21:57 编辑

还是不行啊!!!

问题到底在哪里呢?????????

发送上来的数据是: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);
}
binglin (2010-1-29 22:30:28)
我用豆皮来测试你的程序,由于我的硬件上与你的不同,把main()的函数改为:

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

win2000_li (2010-1-30 11:00:00)
非常感谢binglin老师的指点。

我今天到同事那里,发现程序是可以的。

问题出在USB转串口的东东上,我的本本没有串口,所以前些天买了一个

18元的USB转串口,没有想到问题出在这里。。。。

非常感谢您的帮助!!!!!!!!
trinove (2010-1-30 11:30:20)
18块钱的 USB转串口,太便宜了

质量肯定堪忧的
win2000_li (2010-1-30 11:48:31)
但是我单独用串口发数据,再只接短接2.3脚,发什么数据,就可以接到什么数据啊。

这样说,这个东东,并且没有坏啊。。。。

为什么会出现这种问题啊????????
binglin (2010-1-30 14:21:50)
如果你有示波器,可以看看你这个平价的USB to 232的转换线输出的电平,很有可能是它的输出及输入并不符合标准。

另外你在你的板子上短接MAX3232的STM32侧的收发引脚(这两脚与STM32的联线要断开),测一测PC、USB to 232、MAX3232这一段有没有问题。
binglin (2010-1-30 15:52:35)
还有一个就是你在楼主位所说的发上来的数据是乱码的问题,我经测试没有这个现象。

建议你也检查你板子上了电平转换电路。