1

我知道有一个类似的帖子: Steps to make a LED blink from a C/C++ program?

但现在我正在开发一个基于 arm 的开发板,它似乎有两个串行端口,我可以用它来打开或关闭 LED。

基本上我认为流程是,使一个引脚串行“1”或打开,LED 将打开,“0”使其关闭。

是否有一些我可以参考的 C 语言参考代码?

4

5 回答 5

4

Generally speaking, the board should come with some Board Support Package (BSP) which lets you control the built in I/O. Look for a serial library if you really want to use the Hardware flow control signals.

I'd recommend looking for some GPIO (General Purpose I/O, or digial I/O) on the board, which typically lets you configure it as an input or an output. You should be able to connect the LED via a current limiting resister between a digital I/O line and a ground pin. Make sure you have the LED oriented correctly if you connect it backwards it will block the current instead lighting. And as always make sure you check it out with a digital voltage meter before connecting it.

Even if you don't have a BSP for digital I/O the configuration is usually pretty simple. Set a bit in a register to enable it, set bit in another register to select input or output they will normally be arranged in 8-bit "ports." Some systems allow you configure individual I/O pins, other will only allow you to configure the whole port for input or output. Then you just write a 1 or 0 to the bit you want to control in an write/output register.

ARM chips typically have a considerable amount of built in peripherals today, so most boards will just be bringing the I/O out to physical connectors on the board and you may need to read the chip vender's documentation to find the register memory map. Better board venders will supply documentation, a library (BSP) and examples. Luminary Micro even supplies chips with built in ethernet MACs and PHYs, just add a connector and Magnetics and you have a 1 chip Webserver.

于 2009-07-19T04:36:07.490 回答
2

恐怕这在很大程度上取决于您正在使用的特定基于 arm 的开发板的规格。

您需要找到特定于该板的文档。

于 2009-07-19T03:21:00.403 回答
0

您不能使用串行端口的 Rx 或 Tx 引脚来做到这一点。为此,您只需要控制串口的 RTS 或 CTS 引脚即可。只需谷歌搜索“在 VC++ 代码中访问 COM 端口”,然后控制 RTS 和 CTS 状态引脚来打开和关闭任何外部设备。

于 2012-11-05T13:26:04.470 回答
0

控制 GPIO 的首选替代方法是通过 BSP。因为这个 BSP(板级支持包)为您完成所有工作,将所有外设设置为良好的默认值并允许您调用函数。您选择的 BSP 可能具有将字节写入 8 位 GPIO 端口的功能;您的 LED 将只有一位。在这种情况下,您的 C 代码可能如下所示:(至少:它将在 Luminary Micro 套件上像这样工作)。(示例代码;需要一些额外的工作才能使其编译,尤其是在您的工具包上)。

/* each LED is addressed by an address (byte) and a bit-within-this-byte */
struct {
   address,  // address of IO register for LED port
   bit       // bit of LED
} LEDConfigPair;

struct LEDConfigPair LEDConfig[NUMBER_OF_LEDS] = {
    {GPIO_PORTB_BASE,0},    // LED_0 is at port B0 
    {GPIO_PORTB_BASE,1}     // LED_1 is at port B1
} ;



 /* function LED_init configures the GPIOs where LEDs are connected as output */
 led_init(void)
 {  
     U32 i;
     for(i=0;i<NUMBER_OF_LEDS;i++)
     {
        GPIODirModeSet( LEDConfig[i][0], LEDConfig[i][1], GPIO_DIR_MODE_OUT );
     }
 }


/* my LED function 
     set_led_state makes use of the BSP of Luminary Micro to access a GPIO function

   Implementation: this BSP requires setting 8 port wide IO, so the function will calculate a mask (

*/
set_led_state(U8 led,bool state)
{
    U8 andmask;
    U8 setmask;

    andmask = ~(1 << LEDConfig[led].bit);// a bitmask with all 1's except bit of LED

    if (true == state)
    {
       setmask = (1 << LEDConfig[led].bit); // set bit for LED
    } else
    {
       setmask = 0;
    }
    GPIOPinWrite(LEDConfig[led].address, andmask, setmask);
 }

当然,这一切都说明了;它可以像这样在一行中完成:

#DEFINE SETLEDSTATE(led,state) GPIOPinWrite(LEDConfig[led].address, ~(1<<LEDConfig[led].bit),(state<<LEDConfig[led].bit))

这会做同样的事情,但只有当你可以梦想位掩码时才有意义,并且你只想切换一些 LED 来调试真正的程序......

替代方案:裸机。在这种情况下,您需要自己设置所有内容。对于嵌入式系统,您需要了解引脚复用和电源管理(假设内存控制器和 cpu 时钟已经设置好!)

  • 初始化:设置引脚复用,使您要控制的功能实际映射到封装上。
  • 外围设备的初始化(在这种情况下是 UART 或同一引脚上的 GPIO 功能)
于 2009-08-13T03:27:44.840 回答
0

我以前做过这种编程。

需要学习串口连接 http://www.lammertbies.nl/comm/cable/RS-232.html http://www.beyondlogic.org/serial/serial.htm

输出有+5v,-5v,我现在记不太清楚了。并非每个引脚都需要。

我以前从未使用过 ARM,但我使用 8 位 PIC 控制器对其进行编程。我想你可以在网上找到很多例子。

于 2009-08-13T02:37:48.437 回答