嵌入式设备的C语言编程与Node.js开发指南
1. GPIO引脚操作
GPIO(通用输入输出)接口通过虚拟文件系统管理,呈现为常规文件,标准UNIX文件系统权限可控制对其操作。
1.1 引脚导出到用户域
要使GPIO对应用程序可用,需先将其导出。导出后,应用程序独占该引脚,使用完需释放。
- 代码示例 :
// Define the target pin number
myGPIOPinNumber = 19;
// Open a messaging channel to the kernel
if((myGPIoExportFd = fopen("/sys/class/gpio/export", "w")) == NULL)
{
printf("Error: unable to export GPIO pin\n");
return false;
}
// Tell the kernel which pin to use
fprintf(myGPIoExportFd, "%d\n", myGPIOPinNumber);
// Close the kernel messaging channel
fclose(myGPIoExportFd);
- 命令行等效操作 :
echo 19 > /sys/class/gpio/export