Beaglebone black board Lesson 7: gpio Programming Control

Source: Internet
Author: User

Lesson 7 on BBB Board: gpio Programming Control

In the first class, we made a simple gpio port output high and low level output through the IO function to enable and disable the control of an LED indicator. This section uses the complete C ++ program, implement all the functions of the shell script in Lesson 4 to enable alternate shining of two LED lights.

Directly go to the functional program

1. Implement the echo 44> export port enabling Function

In a simple test in the previous lesson, you can use the following program to manually open the gpio44 port in BBB Terminal Mode:

# Include<Stdio. h>

# DefineGpio_dir "/Sys/Class/Gpio/"/* Gpio main directory */

Int main()

{

File * stream = NULL;

Stream = fopen (gpio_dir "gpio44/Direction", "R + ");

If(Stream = NULL)/* Open the corresponding port if the file fails to be opened */

{

Stream = fopen (gpio_dir "Export", "W");/* You can only select W mode when enabling Export */

Fwrite ("44 ",Sizeof(Int), 2, stream );

Fclose (Stream );

Stream = fopen (gpio_dir "gpio44/Direction", "R +");/* re-open the file */

}

}

You can compile and execute this code first, and then check whether gpio44 is enabled in the gpio directory.

Here, we can see why the "Export" file can only use W mode. You can use the files in the LS-all list to find that the running mode on the left is-W ----- and there is only one w option, I didn't pay attention to it. I used the R or R + mode, and I was always unable to open the gpio44 port. I found this problem only after a long time.

We hope that you will not take so many detours.

 

2. Implement the echo 44> unexport disable port Function

Just like opening a port, a file name is different, because a good program always needs to close the corresponding port at the end of the process. Go directly to the code. Do not talk about anything else. Try it yourself.

 

...

Stream = fopen (gpio_dir "unexport", "W");/* You can only select W mode for opening unexport */

Fwrite ("44 ",Sizeof(Int), 2, stream );

Fclose (Stream );

...

 

3. Implement the complete program of p8.12 and p8.11 every second

 

# Include<Stdio. h>

# Include<Unistd. h>

# DefineGpio_dir "/Sys/Class/Gpio/"

 

Main ()

{

File * stream = NULL;

File * stream1 = NULL;

IntI = 0;

 

Stream = fopen (gpio_dir "gpio44/Direction", "R + ");

If(Stream = NULL)/* Open Port p8.12 */

{

Stream = fopen (gpio_dir "Export", "W ");

Fwrite ("44 ",Sizeof(Int), 2, stream );

Fclose (Stream );

Stream = fopen (gpio_dir "gpio44/Direction", "R + ");

}

Fwrite ("out ",Sizeof(Char), 3, stream);/* p8.12 port is output */

Fclose (Stream );

 

Stream1 = fopen (gpio_dir "gpio45/Direction", "R + ");

If(Stream1 = NULL)/* Open Port p8.11 */

{

Stream1 = fopen (gpio_dir "Export", "W ");

Fwrite ("45 ",Sizeof(Int), 2, stream1 );

Fclose (stream1 );

Stream1 = fopen (gpio_dir "gpio45/Direction", "R + ");

}

Fwrite ("out ",Sizeof(Char), 3, stream1);/* The p8.11 port is output */

Fclose (stream1 );

 

For(I = 0; I <10; I ++)/* 10 flashes */

{

Stream = fopen (gpio_dir "gpio44/value", "R +");/* p8.12 port output 1 */

Fwrite ("1 ",Sizeof(Char), 1, stream );

Fclose (Stream );

Stream1 = fopen (gpio_dir "gpio45/value", "R +");/* p8.11 port output 0 */

Fwrite ("0 ",Sizeof(Char), 1, stream1 );

Fclose (stream1 );

Sleep (1 );

Stream = fopen (gpio_dir "gpio44/value", "R +");/* p8.12 port output 0 */

Fwrite ("0 ",Sizeof(Char), 1, stream );

Fclose (Stream );

Stream1 = fopen (gpio_dir "gpio45/value", "R +");/* p8.11 port output 1 */

Fwrite ("1 ",Sizeof(Char), 1, stream1 );

Fclose (stream1 );

Sleep (1 );

}

Stream = fopen (gpio_dir "gpio44/value", "R +");/* p8.12 port output 0 */

Fwrite ("0 ",Sizeof(Char), 1, stream );

Fclose (Stream );

Stream1 = fopen (gpio_dir "gpio45/value", "R +");/* p8.11 port output 0 */

Fwrite ("0 ",Sizeof(Char), 1, stream1 );

Fclose (stream1 );

Stream = fopen (gpio_dir"Unexport"," W ");/* close the port */

Fwrite ("44 ",Sizeof(Int), 2, stream );

Fclose (Stream );

Stream1 = fopen (gpio_dir"Unexport"," W ");

Fwrite ("45 ",Sizeof(Int), 2, stream1 );

Fclose (stream1 );

}

 

After writing this program, let's look at the shell script in Lesson 4. I feel like the shell script is done with a few commands, C ++ and so on.

The benefits of advanced languages cannot be the same.

Careful people may find thatSizeof(Int) AndSizeof(Char), Why is it not used in a unified manner? In fact, I am also using it at will, because I mentioned earlier that the BBB board of a 32-bit system uses these two as constants, and I have not found any difference for the moment, all are calculated as 4 bytes. I am not familiar with the usage of these functions. I can just build the BBB board.

 

4. Embed shell commands or shell scripts in the C program to simplify implementation.

With programming comparison, it is true that shell commands are very practical and concise in some cases. I also checked the relevant functions and can use the system () function to embed shell commands into the C language for execution, here is just an example to learn and understand. For example, the Code for closing the corresponding gpio port in the last few lines of the above C program:

Stream = fopen (gpio_dir"Unexport"," W ");

Fwrite ("44 ",Sizeof(Int), 2, stream );

Fclose (Stream );

Stream1 = fopen (gpio_dir"Unexport"," W ");

Fwrite ("45 ",Sizeof(Int), 2, stream1 );

Fclose (stream1 );

 

You can use the following two lines of code:

 

System ("Echo 44>/Sys/Class/Gpio/Unexport");

System ("Echo 45>/Sys/Class/Gpio/Unexport");

 

To use the system () function, you must add the header file:

# Include <stdlib. h>

 

Test it by yourself to ensure success!

5. Transfer file data to character array

After opening the gpio port, how can we read and use the values of value and ction? I tried several read/write functions to finally transfer them to the variables for use. If you use the fgetc () function, you can only read one character. For example, if the value file has only one 0 or 1, you can confirm that the use of fscanf () is complete, however, the passed variable must define a string array; otherwise, the read direction can only read the first character.

Fscanf (File pointer, read format, variable), where file pointer is the file we open, read format I here take % s refers to the input data isString ending with a space characterThe variable is the name of the defined string array.

 

The test program led4.cpp is as follows: (The gpio44 port is enabled by default)

# Include<Stdio. h>

# DefineGpio_dir "/Sys/Class/Gpio/"

Int main()

{

File * stream = NULL;

File * stream1 = NULL;

IntI = 0;

CharA [10], B [10];

 

Stream = fopen (gpio_dir "gpio44/value", "R + ");

Fscanf (stream, "% s", a);/* read data to */

Printf ("value: % s \ n", a);/* output to screen */

 

Stream1 = fopen (gpio_dir "gpio44/Direction", "R + ");

Fscanf (stream1, "% s", B);/* read data to B */

Printf ("direction: % s \ n", B);/* output to screen */

 

Fclose (Stream );

Fclose (stream1 );

Return0;

}

 

The compilation and running results are as follows:

/Home/binc # G ++-O led4 led4.cpp

/Home/binc #./led4

Value: 0

Direction: In

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.