In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces how to use RPi.GPIO on raspberry pie 3B. It is very detailed and has certain reference value. Friends who are interested must finish reading it.
I have always heard that raspberry pie is very powerful, so I bought a pi3 to try it out these days. When you get the system installed by hand, start testing the GPIO port and light up a LED. It doesn't seem to work to use python to operate GPIO directly, but fortunately there are many python packages available online. RPi.GPIO is relatively easy to use, and the official home page also provides a more detailed introduction with examples.
RPi.GPIO Home Page: https://sourceforge.net/p/raspberry-gpio-python/wiki/Home/
RPi.GPIO is the python call package that provides ways to manipulate raspberries with GPIO pins. Using the python program, you can easily call these methods. At present, RPi.GPIO provides GPIO input, output and software simulation PWM methods, but unfortunately it does not provide SPI, I2C, UART and hardware PWM methods.
It's not difficult to use. Next, I'll introduce some of the feelings of using it.
First of all, since you are controlling the GPIO port, you have to look at the speed of its operation, although python is notoriously slow.
Import RPi.GPIO as GPIOimport timeGPIO.setmode (GPIO.BOARD) GPIO.setup (35, GPIO.OUT) start_time = time.time () for i in range (0, 1000000): GPIO.output (35, 1) passend_time = time.time () print (end_time-start_time) GPIO.cleanup ()
I ran the above code five times, with an average of 2.4396s each time. Then remove the GPIO.output (35,1) and run it five times with an average time of 0.5222s each time. With these data, it can be calculated that the time of executing 1000000 GPIO.output (35,1) is 1.9174s, then the time of each execution of GPIO.output (35,1) is 1.9us. Oh, my God, it's too slow. Later, for the output 0 and changing it to input mode, the test time is basically the same, one word "slow". Now many 8-bit single-chip computers are faster than this. It is estimated that it is not possible to use this speed to simulate SPI and I2C to transmit larger data. I originally wanted to simulate a SPI-driven TFT display screen, but I had to give up this speed.
RPi.GPIO has a function similar to hardware interrupt, which is very interesting.
Import RPi.GPIO as GPIOimport timeGPIO.setmode (GPIO.BOARD) GPIO.setwarnings (False) GPIO.setup (35, GPIO.IN, GPIO.PUD_DOWN) def my_callback (channel): print ('- my_callback start--') for i in range (0, 10000000): pass print ('- my_callback end--') GPIO.add_event_detect (35, GPIO.RISING, my_callback, bouncetime=200) while True: time.sleep (1) GPIO.cleanup ()
GPIO.add_event_detect adds event detection, GPIO.RISING rising edge trigger, my_callback callback function, and it also has an anti-shake delay bouncetime (unit ms), which saves us from going to the software anti-shake. Why should I add for i in range (0, 10000000)? What I want to know is that when the interrupt is triggered but hasn't come out yet, give it a rising edge again, will it enter the callback function again? The answer is no.
Interrupts in hardware are nested and need to be prioritized for each interrupt. Class 51 needs to set one (default if not set), and stm32 needs to set two: preemption priority and sub-priority. This is not stated in the RPi.GPIO home page documentation, and I want to test it with a piece of code.
Import RPi.GPIO as GPIOimport timeGPIO.setmode (GPIO.BOARD) GPIO.setwarnings (False) GPIO.setup ([35 my_callback_one start--' 37], GPIO.IN, GPIO.PUD_UP) def my_callback_one (channel): print ('--my_callback_one start--') for i in range (0 100000): pass print ('--my_callback_one end--') def my_callback_two (channel): print ('--my_callback_two start--') for i in range (0, 100000): pass print ('--my_callback_two end--') GPIO.add_event_detect (35, GPIO.FALLING, my_callback_one, bouncetime=200) GPIO.add_event_detect (37, GPIO.FALLING, my_callback_two Bouncetime=200) while True: time.sleep (1) GPIO.cleanup ()
I first connect the 35 and 37 pins together and give them to the descending edge at the same time, and find that although they are all triggered, I will execute one first and then the next one without nesting. Then I tried to give the 35-pin a falling edge first, and when the 35-pin interrupt was triggered and started to execute the callback function, but before exiting the callback function, I immediately gave the 37-pin a falling edge. At this time, the 37-pin is not immediately triggered to call its callback function, but waits for the 35-pin callback function to be triggered. It's the same with changing the sequence of 35 pins and 37 pins. In theory, the GPIO port interrupts of the BCM2837 processor for raspberry pie 3 should be nested and priority, which is estimated to have been set by default by RPi.GPIO. The pins that may be tested are just set to priority by RPi.GPIO, one high and one low, and cannot be preempted.
The above is all the contents of the article "how to use RPi.GPIO on Raspberry Pie 3B". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.