简体   繁体   English

使用串口通过C程序打开或关闭LED的步骤?

[英]Steps to make a LED on or off from a C program using Serial Port?

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. 但是现在我正在研究基于手臂的开发板,它似乎有两个串行端口,可以用它来打开或关闭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语言中的一些参考代码吗?

Generally speaking, the board should come with some Board Support Package (BSP) which lets you control the built in I/O. 一般来说,主板应该带有一些主板支持软件包(BSP),使您可以控制内置的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. 我建议在板上寻找一些GPIO(通用I / O或数字I / O),通常可以将其配置为输入或输出。 You should be able to connect the LED via a current limiting resister between a digital I/O line and a ground pin. 您应该能够通过数字I / O线和接地引脚之间的限流电阻器连接LED。 Make sure you have the LED oriented correctly if you connect it backwards it will block the current instead lighting. 如果向后连接LED,请确保LED的方向正确,这将阻止当前的照明。 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. 即使您没有用于数字I / O的BSP,配置也通常非常简单。 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." 在一个寄存器中设置一个位使能它,在另一个寄存器中设置一个位以选择输入或输出,它们通常将安排在8位“端口”中。 Some systems allow you configure individual I/O pins, other will only allow you to configure the whole port for input or output. 一些系统允许您配置单个I / O引脚,其他系统仅允许您配置整个端口的输入或输出。 Then you just write a 1 or 0 to the bit you want to control in an write/output register. 然后,您只需将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. 目前,ARM芯片通常具有大量的内置外设,因此大多数电路板将I / O引到电路板上的物理连接器上,您可能需要阅读芯片供应商的文档才能找到寄存器存储器映射。 Better board venders will supply documentation, a library (BSP) and examples. 更好的董事会供应商将提供文档,库(BSP)和示例。 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. Luminary Micro甚至提供带有内置以太网MAC和PHY的芯片,只需添加一个连接器和Magnetics,您就会拥有一个芯片Web服务器。

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. 您需要查找特定于该板的文档。

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. 它的输出为+ 5v,-5v,我现在不记得了。 Not every pin is needed. 并非每个引脚都是必需的。

I never use ARM before, but I use a 8-bit PIC controller to program it. 我以前从未使用过ARM,但是我使用8位PIC控制器对其进行编程。 I guess you can find a lot of example online. 我想您可以在网上找到很多示例。

The preferred alternative for controlling a GPIO is via a BSP. 控制GPIO的首选替代方法是通过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. 因为此BSP(板级支持程序包)可以将所有外围设备设置为良好的默认值并允许您调用函数,所以可以为您完成所有工作。 Possibly your BSP of choice will have a function to write a byte to an 8-bit GPIO port; 可能您选择的BSP将具有向8位GPIO端口写入一个字节的功能。 your LED will only have one bit. 您的LED将只有一位。 In this case your C code could look like: (at least: it will work like this on Luminary Micro kits). 在这种情况下,您的C代码可能看起来像:(至少:它在Luminary Micro套件上将像这样工作)。 (Example code; requires a bit of extra work to make it compile especially on your kit). (示例代码;需要一些额外的工作才能使其特别在您的工具包上进行编译)。

/* 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功能)

You can't do it using Rx or Tx pins of Serial port. 您不能使用串行端口的Rx或Tx引脚来执行此操作。 For that you just need to control the RTS or CTS pins of serial port. 为此,您只需要控制串行端口的RTS或CTS引脚即可。 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. 只需在Google上搜索“使用VC ++代码访问COM端口”,然后控制RTS和CTS状态引脚即可打开和关闭任何外部设备。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM