
参考香橙派zero3配个iic屏显示ip.csdnorangepiwiki.site 用户手册香橙派zero3资料下载香橙派zero3 wifi配置orangepiorangepizero3:~$ gpio readall -------------------------------- H616 --------------------------------|GPIO|wPi|Name|Mode|V|Physical|V|Mode|Name|wPi|GPIO|------------------------------------------------------------------------|||3.3V|||1||2|||5V||||229|0|SDA.3|ALT5|0|3||4|||5V||||228|1|SCL.3|ALT5|0|5||6|||GND||||73|2|PC9|OFF|0|7||8|0|ALT5|TXD.5|3|226||||GND|||9||10|0|ALT5|RXD.5|4|227||70|5|PC6|ALT5|0|11||12|0|OFF|PC11|6|75||69|7|PC5|ALT5|0|13||14|||GND||||72|8|PC8|OFF|0|15||16|0|OFF|PC15|9|79||||3.3V|||17||18|0|OFF|PC14|10|78||231|11|MOSI.1|OFF|0|19||20|||GND||||232|12|MISO.1|OFF|0|21||22|0|OFF|PC7|13|71||230|14|SCLK.1|OFF|0|23||24|0|OFF|CE.1|15|233||||GND|||25||26|0|OFF|PC10|16|74||65|17|PC1|OFF|0|27||28|0|ALT5|PWM3|21|224||272|18|PI16|ALT2|0|29||30|0|ALT5|PWM4|22|225||262|19|PI6|OFF|0|31||32|||||||234|20|PH10|ALT3|0|33||34||||||------------------------------------------------------------------------|GPIO|wPi|Name|Mode|V|Physical|V|Mode|Name|wPi|GPIO|-------------------------------- H616 -------------------------------- rootorangepizero3:~# ls /sys/class/gpioexportgpiochip0 gpiochip352 unexport# 切换rootorangepiorangepizero3:~$sudo-i# 操作PC9# 导出引脚rootorangepizero3:~# echo 73 /sys/class/gpio/export# 设置为输出模式rootorangepizero3:~# echo out | sudo tee /sys/class/gpio/gpio73/directionout# 输出高电平rootorangepizero3:~# echo 1 | sudo tee /sys/class/gpio/gpio73/value1# 输出低电平rootorangepizero3:~# echo 0 | sudo tee /sys/class/gpio/gpio73/value0rootorangepizero3:~# ls /sys/class/gpioexportgpio73 gpiochip0 gpiochip352 unexport代码源码目录orangepiorangepizero3:~/gpio_demo_prj$lsbuild gpio.cpp gpio.h main.cpp MakefileMakefileCXXg CXXFLAGS-stdc17-Wall-O2-pthreadTARGETbuild/gpio_demo SRCS :$(wildcard *.cpp)OBJS :$(patsubst %.cpp,build/%.o,$(SRCS))all:$(TARGET)build:mkdir-pbuild$(TARGET):build$(OBJS)$(CXX)$(CXXFLAGS)-o$$(OBJS)build/%.o: %.cpp|build$(CXX)$(CXXFLAGS)-c$-o$clean:rm-rfbuild .PHONY: all clean buildmain.cpp/** GPIO69 (PC5)输出 1kHz 方波 GPIO70 (PC6)普通输出先初始化不使用 GPIO73 (PC9)输入检测电平变化并打印 **/#includechrono#includeiostream#includethread#includegpio.husing namespace std::chrono_literals;staticvoidOutputThread(GPIOgpio){while(true){gpio.SetHigh();std::this_thread::sleep_for(std::chrono::microseconds(500));gpio.SetLow();std::this_thread::sleep_for(std::chrono::microseconds(500));}}staticvoidInputThread(GPIOgpio){intlastgpio.Read();std::coutGPIO73 laststd::endl;while(true){intvaluegpio.Read();if(value!last){lastvalue;std::coutGPIO73 valuestd::endl;}// 防止CPU占满std::this_thread::sleep_for(std::chrono::milliseconds(1));}}intmain(){GPIOgpio69(GPIO_PORT_69);GPIOgpio70(GPIO_PORT_70);GPIOgpio73(GPIO_PORT_73);if(!gpio69.Open(GPIO::OUTPUT))return-1;if(!gpio70.Open(GPIO::OUTPUT))return-1;if(!gpio73.Open(GPIO::INPUT))return-1;// GPIO70 默认拉低gpio70.SetLow();std::threadoutputThread(OutputThread,std::ref(gpio69));std::threadinputThread(InputThread,std::ref(gpio73));outputThread.join();inputThread.join();return0;}gpio.h#ifndefGPIO_H#defineGPIO_H#includestringenumGPIO_PORT{GPIO_PORT_6969,// PC5GPIO_PORT_7070,// PC6GPIO_PORT_7373// PC9};class GPIO{public:enumDirection{INPUT,OUTPUT};public:GPIO(GPIO_PORT port);~GPIO();boolOpen(Direction dir);voidClose();boolSetHigh();boolSetLow();boolWrite(bool value);intRead();private:boolExport();boolSetDirection(Direction dir);boolWriteFile(conststd::stringfile,conststd::stringvalue);private:intm_gpio;intm_fd;};#endifgpio.cpp#includegpio.h#includefcntl.h#includeunistd.h#includechrono#includecstring#includeiostream#includethreadGPIO::GPIO(GPIO_PORT port):m_gpio(static_castint(port)),m_fd(-1){}GPIO::~GPIO(){Close();}bool GPIO::WriteFile(conststd::stringfile,conststd::stringvalue){intfdopen(file.c_str(),O_WRONLY);if(fd0)returnfalse;write(fd,value.c_str(),value.size());close(fd);returntrue;}bool GPIO::Export(){WriteFile(/sys/class/gpio/export,std::to_string(m_gpio));std::this_thread::sleep_for(std::chrono::milliseconds(50));returntrue;}bool GPIO::SetDirection(Direction dir){std::string path/sys/class/gpio/gpiostd::to_string(m_gpio)/direction;returnWriteFile(path,dirOUTPUT?out:in);}bool GPIO::Open(Direction dir){Export();if(!SetDirection(dir)){std::cerrGPIO m_gpio set direction failed.std::endl;returnfalse;}std::string path/sys/class/gpio/gpiostd::to_string(m_gpio)/value;m_fdopen(path.c_str(),O_RDWR);if(m_fd0){std::cerrGPIO m_gpio open failed.std::endl;returnfalse;}returntrue;}voidGPIO::Close(){if(m_fd0){close(m_fd);m_fd-1;}}bool GPIO::Write(bool value){if(m_fd0)returnfalse;lseek(m_fd,0,SEEK_SET);if(value)returnwrite(m_fd,1,1)1;elsereturnwrite(m_fd,0,1)1;}bool GPIO::SetHigh(){returnWrite(true);}bool GPIO::SetLow(){returnWrite(false);}intGPIO::Read(){if(m_fd0)return-1;charvalue;lseek(m_fd,0,SEEK_SET);if(read(m_fd,value,1)!1)return-1;returnvalue1;}