{"id":157,"date":"2012-10-09T16:33:39","date_gmt":"2012-10-09T16:33:39","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=157"},"modified":"2020-10-05T19:33:16","modified_gmt":"2020-10-05T19:33:16","slug":"bcm2835-by-mike-mccauley","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/io-pins\/bcm2835-by-mike-mccauley","title":{"rendered":"bcm2835 by Mike McCauley"},"content":{"rendered":"<p><em><span style=\"color: #ff0000;\"><strong>This library also supports the RPi2 and upwards, with the bcm2836 and upwards chipsets<\/strong><\/span><\/em><\/p>\n<h4>Installing The Library<\/h4>\n<p><a href=\"http:\/\/www.open.com.au\/mikem\/bcm2835\/\" target=\"_blank\" rel=\"noopener noreferrer\">The library homepage<\/a><\/p>\n<p>In the commands below change the .XX to match the current library version number, e.g. &#8220;.37&#8221;.<\/p>\n<p>If you are using the GUI then open the command prompt using Menu &gt; Other &gt; LXTerminal<\/p>\n<p>Using your RPi\u00a0download the .tar.gz file from the library page to your &#8220;\/home\/pi\/&#8221; root directory. \u00a0You can do this using wget on the command line:<\/p>\n<p style=\"margin-left: 40px;\">cd\u00a0\/home\/pi\/<\/p>\n<p style=\"margin-left: 40px;\">wget http:\/\/www.airspayce.com\/mikem\/bcm2835\/bcm2835-1.XX.tar.gz<\/p>\n<p>Install the library files using the following commands<\/p>\n<p>Unzip the downloaded file (Change the file version number to match your downloaded version)<\/p>\n<pre><code>tar zxvf bcm2835-1.XX.tar.gz<\/code><\/pre>\n<p>The files will be unzipped into a folder called &#8220;\/home\/pi\/bcm2835-#.#&#8221; where # is the version number. (The following instructions are based on the instructions in the bcm2835.h file so if something doesn&#8217;t work check there to see if the instructions have changed)<\/p>\n<p>Change to the directory the files we&#8217;re unzipped into (Change the directory version number to match your downloaded version)<\/p>\n<pre><code>cd bcm2835-1.XX<\/code><\/pre>\n<p>Run the configure exe<\/p>\n<pre><code>.\/configure<\/code><\/pre>\n<p>Execute the makefile<\/p>\n<pre><code>make<\/code><\/pre>\n<p>Then<\/p>\n<pre><code>sudo make check<\/code><\/pre>\n<p>Then<\/p>\n<pre><code>sudo make install<\/code><\/pre>\n<p>(The sudo is needed to elevate permissions to the root user)<\/p>\n<p>The library is now ready to use.<\/p>\n<h4>Using the Library In A NetBeans\u00a0Project<\/h4>\n<p>Including the library header file<\/p>\n<pre><code>#include &lt;bcm2835.h&gt;<\/code><\/pre>\n<p>When you compile you also need to include -lbcm2835\u00a0so the libraries object file is added to the final compilation.<\/p>\n<p>Right click the project &gt; Properties &gt; Build &gt; Linker &gt; In the &#8216;Libraries&#8217; section press the &#8216;&#8230;&#8217; button &gt; Add Option&#8230; &gt; Other Option &gt; Enter:\u00a0-lbcm2835<\/p>\n<h4>Using the Library In A Geany Project<\/h4>\n<p>Including the library header file<\/p>\n<pre><code>#include &lt;bcm2835.h&gt;<\/code><\/pre>\n<p>When you compile you also need to include -lbcm2835 so the libraries object file is added to the final compilation.<br \/>\nFor example at the command line:<\/p>\n<pre><code>gcc clk.c -o clk -lbcm2835<\/code><\/pre>\n<p>In a simple makefile for a project with a single file called main.c:<\/p>\n<pre><code>\nall: output_file_name\n\noutput_file_name: main.o\n\tgcc main.o -lbcm2835 -o output_file_name\n\nmain.o: main.c\n\tgcc -c main.c\n\nclean:\n\trm -rf *o output_file_name\n<\/code><\/pre>\n<h4>Trying out an example project using the command line C compiler<\/h4>\n<p>You can compile one of the examples using this:<\/p>\n<pre><code>\ncd examples\/blink\ngcc -o blink -l rt blink.c -l bcm2835 \n<\/code><\/pre>\n<p>and then run the blink exe with this command:<\/p>\n<pre><code>\nsudo .\/blink\n<\/code><\/pre>\n<p>If you connect the positive pin of a 5V LED to pin 11 of the P1 header it should be blinking. Use CTRL+Break or CTRL+SHIFT+C to stop the exe.<\/p>\n<h4>Problems With The Library<\/h4>\n<p>DelayMicroseconds() is a handy function but if you use it a lot you&#8217;ll find operation takes much longer than it should. \u00a0This is because the function allows the linux scheduler to know the process is sleeping and decide to move on to another process. \u00a0See <a href=\"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/timing\/clock_gettime-for-acurate-timing\">here <\/a>for a better solution if you need to avoid this.<\/p>\n<h4>Controlling Pins<\/h4>\n<p>See the header file for all of the functions.<\/p>\n<h5>RPi1 Model B+ and\u00a0RPi2 Model\u00a0B<\/h5>\n<pre><code>\n\/\/Setup\nbcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_FSEL_INPT);\t\t\/\/RPI1B+ &amp; RPi2B &lt;&lt;Set as input\nbcm2835_gpio_fsel(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_FSEL_OUTP);\t\t\/\/RPI1B+ &amp; RPi2B &lt;&lt;Set as output\nbcm2835_gpio_set_pud(RPI_BPLUS_GPIO_J8_40, BCM2835_GPIO_PUD_UP);\t\t\/\/RPI1B+ &amp; RPi2B &lt;&lt;Set pull up\n\n\/\/Outputs\n#define LED_GREEN(state)\t\t\t\tbcm2835_gpio_write(RPI_BPLUS_GPIO_J8_40, !state)\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/RPI1B+ &amp; RPi2B\n\n\/\/Inputs\n\/\/bcm2835_gpio_lev returns uint8_t\n#define SW_A_INPUT\t\t\t\t\t\tbcm2835_gpio_lev(RPI_BPLUS_GPIO_J8_40)\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\u00a0 \u00a0\u00a0\u00a0 \u00a0\/\/RPI1B+ &amp; RPi2B\n<\/code><\/pre>\n<h5>RPi1 V2 (old)<\/h5>\n<pre><code>\n\/\/Outputs\n#define LED_GREEN(state)\t\t\t\tbcm2835_gpio_write(RPI_V2_GPIO_P1_24, !state)\n#define LED_YELLOW(state)\t\t\t\tbcm2835_gpio_write(RPI_V2_GPIO_P1_26, !state)\n\n\/\/Inputs\n\/\/bcm2835_gpio_lev returns uint8_t\n#define SW_A_INPUT\t\t\t\t\t\tbcm2835_gpio_lev(RPI_V2_GPIO_P1_03)\n<\/code><\/pre>\n<h4>Upgrading to a\u00a0newer version\u00a0of the library<\/h4>\n<p>Just reinstall, no need to uninstall the old one.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>This library also supports the RPi2 and upwards, with the bcm2836 and upwards chipsets Installing The Library The library homepage In the commands below change the .XX to match the current library version number, e.g. &#8220;.37&#8221;. If you are using the GUI then open the command prompt using Menu &gt; Other &gt; LXTerminal Using your [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19],"tags":[],"class_list":["post-157","post","type-post","status-publish","format-standard","hentry","category-io-pins"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/157","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=157"}],"version-history":[{"count":38,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/157\/revisions"}],"predecessor-version":[{"id":3436,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/157\/revisions\/3436"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=157"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=157"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=157"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}