本程序已经集成在CbOS 1.0 beta3内,目的为用C语言控制板载的两个LED
gpio_lib.c gpio_lib.h在hipboi的python控制GPIO的帖子里找。
用法:
led【LED号(1或2)】 【1(高电平)或0(低电平)】
代码:- #include <stdlib.h>
- #include <stdio.h>
- #include "gpio_lib.c"
- #include <string.h>
- int dy(int led,int tf)
- {
- int port;
- if(led == 1){
- port=20;
- }else if(led == 2){
- port=21;
- }else{
- printf("Unknown port.\n");
- printf("led [1 or 2(led)] [1(high) or 0(low)]\n");
- return 1;
- }
- //printf("SUNXI GPIO tool\n");
- if(SETUP_OK!=sunxi_gpio_init()){
- printf("Failed to initialize GPIO\n");
- return 1;
- }
-
- if(SETUP_OK!=sunxi_gpio_set_cfgpin(SUNXI_GPH(port),OUTPUT)){
- printf("Failed to config GPIO pin\n");
- return 1;
- }
-
- int i;
- if(tf == 0){
- if(sunxi_gpio_output(SUNXI_GPH(port),LOW)){
- printf("Failed to set GPIO pin value\n");
- return 1;
- }
- }else{
- if(sunxi_gpio_output(SUNXI_GPH(port),HIGH)){
- printf("Failed to set GPIO pin value\n");
- return 1;
- }
- }
- sunxi_gpio_cleanup();
-
- return 0;
-
- }
- int main(int argc,char* argv[]){
- if(argc != 3){
- printf("led [led1 or 2] [1(high) or 0(low)]\n");
- return 1;
- }else{
- int a = 0;
- int i = 0;
- for(i=0;i<strlen(argv[1]);i++){
- a*=10;
- a+=argv[1][i]-'0';
- }
- //dy(a,argv[2][0]-'0');
- //printf("%d \\ %d \n",argv[1][0]-'0',argv[2][0]-'0');
- //printf("%c\n",argv[1][0]);
- return dy(a,argv[2][0]-'0');
- }
- }
复制代码 |