打印

选择串口的问题

[复制链接]
2226|9
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
a_bb|  楼主 | 2007-5-6 22:44 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
在keil的例子中使用如下程序设置串口:

  /*Configure UART0_CTS P2.0*/                (1)
  GPIO_DeInit(GPIO2);
  /*After DeInit function P2.0 = UART0_CTS (defaut configuration)*/

  GPIO_DeInit(GPIO5);
  /*Gonfigure UART0_Rx pin GPIO5.1*/
  GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;        
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;    (2)
  GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
  GPIO_InitStructure.GPIO_Alternate = GPIO_InputAlt1  ;
  GPIO_Init (GPIO5, &GPIO_InitStructure);

   GPIO_DeInit(GPIO3);
  /*Gonfigure UART0_Tx pin GPIO3.4*/
  GPIO_InitStructure.GPIO_Direction = GPIO_PinInput;        (3)
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3  ;
  GPIO_Init (GPIO3, &GPIO_InitStructure);

  /*Gonfigure UART0_RTS pin GPIO3.3*/
  GPIO_InitStructure.GPIO_Direction = GPIO_PinOutput;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Type = GPIO_Type_PushPull ;
  GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3  ;
  GPIO_Init (GPIO3, &GPIO_InitStructure);

有几个问题如下:
(1)P2.0的缺省功能是UART0_CTS ,就不需要GPIO_IPConnected = GPIO_IPConnected_Enable了吗?
(2)Rx是输入口,却为什么GPIO_Direction = GPIO_PinInput?作为输入口还需将口类型设为GPIO_Type_PushPull 吗?
(3)将P3.4设为Tx为什么没有GPIO_IPConnected = GPIO_IPConnected_Enable?
沙发
ST_ARM| | 2007-5-7 10:05 | 只看该作者

请问你用的是哪个型号的芯片?

如题。

使用特权

评论回复
板凳
a_bb|  楼主 | 2007-5-7 12:06 | 只看该作者

912FW44

原程序在KeilARMRV30ExamplesSTSTR91xLibUART

使用特权

评论回复
地板
stf| | 2007-5-7 17:50 | 只看该作者

选择串口的问题

a_bb 你好,

1)在str9的datasheet有一个"Device pin description"的表格, UART0_CTS 市gpio2.0的"default input function" 所以不需要用GPIO_IPConnected_Enable。 但是Rx是gpio5.1的"alternate pin function", alternate pin function 的话需要用GPIO_IPConnected_Enable。 你也可以看在str9 reference manual里面的gpio框图.

2)Rx是输入口,却为什么GPIO_Direction = GPIO_PinInput?
在"str91x_gpio.c"文件我们可以看GPIO_Init的函数,这个函数里面不用GPIO_Direction = GPIO_PinInput的话,代码会配置output的方向:

  if(GPIO_InitStruct->GPIO_Direction == GPIO_PinOutput)
  {
  GPIOx->DDR |= GPIO_InitStruct->GPIO_Pin;
  }
  else
  {
   GPIOx->DDR &= ~GPIO_InitStruct->GPIO_Pin;
  }

还有一个作用,GPIO_InitStructure 是一个全程变量,如果以前用的数值是GPIO_PinOuput,可能有问题。GPIO_Direction = GPIO_PinInput 比较好。

3)作为输入口还需将口类型设为GPIO_Type_PushPull 吗?
不用配置.

4)将P3.4设为Tx为什么没有GPIO_IPConnected = GPIO_IPConnected_Enable?
也不用

如果你觉得配置有一点儿奇怪, 你可以用CAPS查一下:

// ****************************************************************
// GPIO input register setting
// ****************************************************************
#define GPIOIN0 0x0
#define GPIOIN1 0x0
#define GPIOIN2 0x0
#define GPIOIN3 0x0
#define GPIOIN4 0x0
#define GPIOIN5 0x2
#define GPIOIN6 0x0
#define GPIOIN7 0x0

// ****************************************************************
// GPIO output register setting
// ****************************************************************
#define GPIOOUT0 0x0
#define GPIOOUT1 0x0
#define GPIOOUT2 0x0
#define GPIOOUT3 0x3F0
#define GPIOOUT4 0x0
#define GPIOOUT5 0x0
#define GPIOOUT6 0x0
#define GPIOOUT7 0x0

// ****************************************************************
// GPIO type register setting
// ****************************************************************
#define GPIOTYPE0 0x0
#define GPIOTYPE1 0x0
#define GPIOTYPE2 0x0
#define GPIOTYPE3 0x0
#define GPIOTYPE4 0x0
#define GPIOTYPE5 0x0
#define GPIOTYPE6 0x0
#define GPIOTYPE7 0x0
#define GPIOTYPE8 0x0
#define GPIOTYPE9 0x0

// ****************************************************************
// GPIO direction register setting
// ****************************************************************
#define GPIO_DIR0 0x0
#define GPIO_DIR1 0x0
#define GPIO_DIR2 0x0
#define GPIO_DIR3 0x0
#define GPIO_DIR4 0x0
#define GPIO_DIR5 0x0
#define GPIO_DIR6 0x0
#define GPIO_DIR7 0x0
#define GPIO_DIR8 0x0
#define GPIO_DIR9 0x0


Regards,
Stephane

使用特权

评论回复
5
a_bb|  楼主 | 2007-5-8 08:44 | 只看该作者

谢谢stf

解释很详细,但仍有两个疑问:
(1)p3.4的缺省输入为EXINT4,现在选用Uart0_tx,为什么就不用GPIO_IPConnected = GPIO_IPConnected_Enable?

(2)我在原叙述中有错,应该是:
选择p3.4作为Uart0_tx,但为什么要GPIO_InitStructure.GPIO_Direction = GPIO_PinInput,是不是应该为GPIO_PinOutput?

以上两个问题都在原程序(3)的位置。

再次谢谢

使用特权

评论回复
6
stf| | 2007-5-8 11:01 | 只看该作者

gpio配置

a_bb 你好,

1)你在配置输入的时候需要用GPIO_IPConnected = GPIO_IPConnected_Enable, 你用的是输出不需要。 在"str91x_gpio.c"文件我们可以看GPIO_Init的函数:

if(GPIO_InitStruct->GPIO_IPConnected == GPIO_IPConnected_Enable)
      {
         /*IP Connected enable*/
         SCU->GPIOIN[GPIO_Number] |= 0x1 << Counter;
       }

2)如果我们看reference manual的I/O Control Block Diagram 的话,GPIO_Input和GPIO_Output是配置IO口的方向用的 ( GPIOx->DDR ).你用alternate function 2 还是 alternate function 3, 不用配置。

也可以用这个代码(我没试过,因该没什么问题):

  /*Configure UART0_CTS P2.0*/                
  GPIO_DeInit(GPIO2);
  /*After DeInit function P2.0 = UART0_CTS (defaut configuration)*/

  GPIO_DeInit(GPIO5);
  /*Gonfigure UART0_Rx pin GPIO5.1*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
  GPIO_InitStructure.GPIO_IPConnected = GPIO_IPConnected_Enable;
  GPIO_Init (GPIO5, &GPIO_InitStructure);

 GPIO_DeInit(GPIO3);
 /*Gonfigure UART0_Tx pin GPIO3.4*/
 GPIO_StructInit(&GPIO_InitStructure);
 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
 GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3  ;
 GPIO_Init (GPIO3, &GPIO_InitStructure);

  /*Gonfigure UART0_RTS pin GPIO3.3*/
  GPIO_StructInit(&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
  GPIO_InitStructure.GPIO_Alternate = GPIO_OutputAlt3  ;
  GPIO_Init (GPIO3, &GPIO_InitStructure);

对性能不是最好的(调试很多次一样的函数)但是可能清楚一点儿。


Regards,
Stephane

使用特权

评论回复
7
gyt| | 2007-5-8 11:44 | 只看该作者

热心人啊

使用特权

评论回复
8
a_bb|  楼主 | 2007-5-8 12:21 | 只看该作者

非常感谢

经过这些解释我想总结如下,不知正确与否
1。输出口是不需要GPIO_IPConnected = GPIO_IPConnected_Enable的
2。选择自带方向性的功能口如Uart是不需要对相应的IO设置方向的(其实很多其他芯片就是这样的)

我没有"str91x_gpio.c"文件,API函数都封装在一个库了,stf可否给一个?

使用特权

评论回复
9
stf| | 2007-5-8 14:29 | 只看该作者

91x_gpio.c

你好,

我写错了应该是91x_gpio.c文件,你可以找一下,你找不到的话在 http://mcu.st.com/mcu/ 可以下载str91x函数库的代码。

请问,你用哪一个IDE?

Regards,
Stephane

使用特权

评论回复
10
a_bb|  楼主 | 2007-5-8 15:16 | 只看该作者

Keil RVCT3.05

如题

使用特权

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

本版积分规则

35

主题

98

帖子

0

粉丝