使用串行端口从C程序打开或关闭LED的步骤?

时间:2021-08-27 15:09:18

I knew there is a similar post: Steps to make a LED blink from a C/C++ program?

我知道有一个类似的帖子:从C / C ++程序中使LED闪烁的步骤?

But now I am working on a arm-based development board, and it seems to have two serial ports that I could use it to make a LED on or off.

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

Basically I think the flow is , make one pin in serial "1" or on and the LED will be turned on and "0" to make it off.

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

Is there some reference code in C-language I could refers?

我可以参考一些C语言的参考代码吗?

5 个解决方案

#1


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.

一般来说,电路板应该附带一些电路板支持包(BSP),它允许您控制内置I / O.如果您真的想使用硬件流控制信号,请查找串行库。

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.

我建议在电路板上寻找一些GPIO(通用I / O或数字I / O),这通常可以将其配置为输入或输出。您应该能够通过数字I / O线和接地引脚之间的限流电阻连接LED。如果向后连接它,请确保LED的方向正确,否则会阻挡电流而不是照明。在连接之前,请务必使用数字电压表进行检查。

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.

即使您没有用于数字I / O的BSP,配置通常也非常简单。在寄存器中设置一个位以使能它,将另一个寄存器中的位置1以选择输入或输出它们通常将被安排在8位“端口”中。某些系统允许您配置各个I / O引脚,其他系统只允许您配置整个端口用于输入或输出。然后,您只需将1或0写入要在写/输出寄存器中控制的位。

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.

ARM芯片目前通常拥有相当数量的内置外设,因此大多数电路板只会将I / O输出到电路板上的物理连接器,您可能需要阅读芯片供应商的文档以找到寄存器存储器映射。更好的电路板供应商将提供文档,库(BSP)和示例。 Luminary Micro甚至提供内置以太网MAC和PHY的芯片,只需添加连接器和Magnetics,您就拥有1芯片Web服务器。

#2


This will, I'm afraid, be heavily dependent on the specifications of the particular arm-based development board you are using.

我担心,这将严重依赖于您正在使用的特定基于臂的开发板的规格。

You need to find documentation specific to that board.

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

#3


I used to do this kind of programming before.

我之前曾经做过这种编程。

You need to study the serial port connection http://www.lammertbies.nl/comm/cable/RS-232.html http://www.beyondlogic.org/serial/serial.htm

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

It has +5v, -5v on the output, I can't remember clearly now. Not every pin is needed.

输出上有+ 5v,-5v,我现在记不住了。不是每个引脚都需要。

I never use ARM before, but I use a 8-bit PIC controller to program it. I guess you can find a lot of example online.

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

#4


The preferred alternative for controlling a GPIO is via a BSP. Because this BSP (board support package) does all the work for you in setting all peripherals to good defaults and and allowing you to call a function. Possibly your BSP of choice will have a function to write a byte to an 8-bit GPIO port; your LED will only have one bit. In this case your C code could look like: (at least: it will work like this on Luminary Micro kits). (Example code; requires a bit of extra work to make it compile especially on your kit).

控制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);
 }

Of course this is all spelled out; it can be done in a single lines like this:

当然这都是拼写出来的;它可以在一行中完成,如下所示:

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

this will do the same, but only makes sense when you can dream bit masks, and you only want to toggle some LEDs to debug the real program...

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

The alternative: bare metal. In this case you need to set up everything for yourself. For an embedded system, you need to be aware of pin multiplexing and power management (assuming memory controller and cpu clocks are already set up!)

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

  • initialization: set pin multiplexing in such a way that the function you want to control is actually mapped on the package.
  • 初始化:设置引脚多路复用,使您想要控制的功能实际映射到包上。

  • initialization of pheripheral (in this case either a UART, or a GPIO function on the same pin)
  • 外围设备的初始化(在这种情况下,UART或同一引脚上的GPIO功能)

#5


You can't do it using Rx or Tx pins of Serial port. For that you just need to control the RTS or CTS pins of serial port. Just google for "access COM port in VC++ code" and then control the RTS and CTS status pins to turn ON and OFF any external device.

您无法使用串行端口的Rx或Tx引脚进行此操作。为此,您只需要控制串行端口的RTS或CTS引脚。只需google“访问VC ++代码中的COM端口”,然后控制RTS和CTS状态引脚以打开和关闭任何外部设备。

#1


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.

一般来说,电路板应该附带一些电路板支持包(BSP),它允许您控制内置I / O.如果您真的想使用硬件流控制信号,请查找串行库。

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.

我建议在电路板上寻找一些GPIO(通用I / O或数字I / O),这通常可以将其配置为输入或输出。您应该能够通过数字I / O线和接地引脚之间的限流电阻连接LED。如果向后连接它,请确保LED的方向正确,否则会阻挡电流而不是照明。在连接之前,请务必使用数字电压表进行检查。

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.

即使您没有用于数字I / O的BSP,配置通常也非常简单。在寄存器中设置一个位以使能它,将另一个寄存器中的位置1以选择输入或输出它们通常将被安排在8位“端口”中。某些系统允许您配置各个I / O引脚,其他系统只允许您配置整个端口用于输入或输出。然后,您只需将1或0写入要在写/输出寄存器中控制的位。

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.

ARM芯片目前通常拥有相当数量的内置外设,因此大多数电路板只会将I / O输出到电路板上的物理连接器,您可能需要阅读芯片供应商的文档以找到寄存器存储器映射。更好的电路板供应商将提供文档,库(BSP)和示例。 Luminary Micro甚至提供内置以太网MAC和PHY的芯片,只需添加连接器和Magnetics,您就拥有1芯片Web服务器。

#2


This will, I'm afraid, be heavily dependent on the specifications of the particular arm-based development board you are using.

我担心,这将严重依赖于您正在使用的特定基于臂的开发板的规格。

You need to find documentation specific to that board.

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

#3


I used to do this kind of programming before.

我之前曾经做过这种编程。

You need to study the serial port connection http://www.lammertbies.nl/comm/cable/RS-232.html http://www.beyondlogic.org/serial/serial.htm

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

It has +5v, -5v on the output, I can't remember clearly now. Not every pin is needed.

输出上有+ 5v,-5v,我现在记不住了。不是每个引脚都需要。

I never use ARM before, but I use a 8-bit PIC controller to program it. I guess you can find a lot of example online.

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

#4


The preferred alternative for controlling a GPIO is via a BSP. Because this BSP (board support package) does all the work for you in setting all peripherals to good defaults and and allowing you to call a function. Possibly your BSP of choice will have a function to write a byte to an 8-bit GPIO port; your LED will only have one bit. In this case your C code could look like: (at least: it will work like this on Luminary Micro kits). (Example code; requires a bit of extra work to make it compile especially on your kit).

控制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);
 }

Of course this is all spelled out; it can be done in a single lines like this:

当然这都是拼写出来的;它可以在一行中完成,如下所示:

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

this will do the same, but only makes sense when you can dream bit masks, and you only want to toggle some LEDs to debug the real program...

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

The alternative: bare metal. In this case you need to set up everything for yourself. For an embedded system, you need to be aware of pin multiplexing and power management (assuming memory controller and cpu clocks are already set up!)

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

  • initialization: set pin multiplexing in such a way that the function you want to control is actually mapped on the package.
  • 初始化:设置引脚多路复用,使您想要控制的功能实际映射到包上。

  • initialization of pheripheral (in this case either a UART, or a GPIO function on the same pin)
  • 外围设备的初始化(在这种情况下,UART或同一引脚上的GPIO功能)

#5


You can't do it using Rx or Tx pins of Serial port. For that you just need to control the RTS or CTS pins of serial port. Just google for "access COM port in VC++ code" and then control the RTS and CTS status pins to turn ON and OFF any external device.

您无法使用串行端口的Rx或Tx引脚进行此操作。为此,您只需要控制串行端口的RTS或CTS引脚。只需google“访问VC ++代码中的COM端口”,然后控制RTS和CTS状态引脚以打开和关闭任何外部设备。