|
本帖最后由 soloforce 于 2013-5-20 12:13 编辑
在hipboi的这个帖子http://forum.cubietech.com/forum.php?mod=viewthread&tid=456,我们可以用Python GPIO里面的GPIO C库文件直接编写应用程序了。下面是读和写的两个例子。
读GPIO:本例读取PD8的值,并打印提示信息- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- #include <signal.h>
- #include "gpio_lib.h"
- #define PD0 SUNXI_GPD(0)
- #define PD1 SUNXI_GPD(1)
- #define PD2 SUNXI_GPD(2)
- #define PD3 SUNXI_GPD(3)
- #define PD4 SUNXI_GPD(4)
- #define PD5 SUNXI_GPD(5)
- #define PD6 SUNXI_GPD(6)
- #define PD7 SUNXI_GPD(7)
- #define PD8 SUNXI_GPD(8)
- #define PD9 SUNXI_GPD(9)
- #define PD10 SUNXI_GPD(10)
- #define PD11 SUNXI_GPD(11)
- #define PD12 SUNXI_GPD(12)
- #define PD13 SUNXI_GPD(13)
- #define PD14 SUNXI_GPD(14)
- #define PD15 SUNXI_GPD(15)
- #define PD16 SUNXI_GPD(16)
- #define PD17 SUNXI_GPD(17)
- #define PD18 SUNXI_GPD(18)
- #define PD19 SUNXI_GPD(19)
- #define PD20 SUNXI_GPD(20)
- #define PD21 SUNXI_GPD(21)
- #define PD22 SUNXI_GPD(22)
- #define PD23 SUNXI_GPD(23)
- #define PD24 SUNXI_GPD(24)
- #define PD25 SUNXI_GPD(25)
- #define PD26 SUNXI_GPD(26)
- #define PD27 SUNXI_GPD(27)
- #define MISO SUNXI_GPE(3)
- #define MOSI SUNXI_GPE(2)
- #define SCK SUNXI_GPE(1)
- #define CS SUNXI_GPE(0)
- struct sigaction new_act,new_act2, old_act;
- /**
- * @brief Signal INT handler
- * */
- void sigint_handler(int sig_num)
- {
- printf("Uninitialzing GPIO...\n");
-
- /* uninitialize the GPIO */
- sunxi_gpio_cleanup();
-
- /* change back to the original handler */
- sigaction(SIGINT, &old_act,NULL);
-
- /* emit the SIGINT again */
- kill(0,SIGINT);
- }
-
- /**
- * @brief Signal TERM handler
- * */
- void sigterm_handler(int sig_num)
- {
- printf("Uninitialzing GPIO...\n");
-
- /* uninitialize the GPIO */
- sunxi_gpio_cleanup();
-
- /* change back to the original handler */
- sigaction(SIGTERM, &old_act,NULL);
-
- /* emit the SIGTERM again */
- kill(0,SIGTERM);
- }
- int main()
- {
- memset(&new_act,0,sizeof(new_act));
- memset(&new_act2,0,sizeof(new_act));
- memset(&old_act,0,sizeof(old_act));
- new_act.sa_handler=&sigint_handler;
- new_act2.sa_handler=&sigterm_handler;
- sigaction(SIGINT, &new_act, &old_act);
- sigaction(SIGTERM, &new_act2, &old_act);
- if(SETUP_OK!=sunxi_gpio_init()){
- printf("Failed to initialize GPIO\n");
- return -1;
- }
- if(SETUP_OK!=sunxi_gpio_set_cfgpin(PD8,INPUT)){
- printf("Failed to config GPIO pin\n");
- return -1;
- }
- while(1){
- if(HIGH==sunxi_gpio_input(PD8)){
- printf("HIGH\n");
- }
- usleep(1000);
- }
- sunxi_gpio_cleanup();
-
- return 0;
-
- }
复制代码
本例写PD16脚,如果上面接了一个led,且负极接地,则会周期闪烁。- #include <stdlib.h>
- #include <stdio.h>
- #include "gpio_lib.h"
- #define PD0 SUNXI_GPD(0)
- #define PD1 SUNXI_GPD(1)
- #define PD2 SUNXI_GPD(2)
- #define PD3 SUNXI_GPD(3)
- #define PD4 SUNXI_GPD(4)
- #define PD5 SUNXI_GPD(5)
- #define PD6 SUNXI_GPD(6)
- #define PD7 SUNXI_GPD(7)
- #define PD8 SUNXI_GPD(8)
- #define PD9 SUNXI_GPD(9)
- #define PD10 SUNXI_GPD(10)
- #define PD11 SUNXI_GPD(11)
- #define PD12 SUNXI_GPD(12)
- #define PD13 SUNXI_GPD(13)
- #define PD14 SUNXI_GPD(14)
- #define PD15 SUNXI_GPD(15)
- #define PD16 SUNXI_GPD(16)
- #define PD17 SUNXI_GPD(17)
- #define PD18 SUNXI_GPD(18)
- #define PD19 SUNXI_GPD(19)
- #define PD20 SUNXI_GPD(20)
- #define PD21 SUNXI_GPD(21)
- #define PD22 SUNXI_GPD(22)
- #define PD23 SUNXI_GPD(23)
- #define PD24 SUNXI_GPD(24)
- #define PD25 SUNXI_GPD(25)
- #define PD26 SUNXI_GPD(26)
- #define PD27 SUNXI_GPD(27)
- #define MISO SUNXI_GPE(3)
- #define MOSI SUNXI_GPE(2)
- #define SCK SUNXI_GPE(1)
- #define CS SUNXI_GPE(0)
- int main()
- {
- if(SETUP_OK!=sunxi_gpio_init()){
- printf("Failed to initialize GPIO\n");
- return -1;
- }
- if(SETUP_OK!=sunxi_gpio_set_cfgpin(PD16,OUTPUT)){
- printf("Failed to config GPIO pin\n");
- return -1;
- }
- int i;
- for(i=0;i<5;i++){
- if(sunxi_gpio_output(PD16,HIGH)){
- printf("Failed to set GPIO pin value\n");
- return -1;
- }
- usleep(500000);
- if(sunxi_gpio_output(PD16,LOW)){
- printf("Failed to set GPIO pin value\n");
- return -1;
- }
- usleep(500000);
- }
- sunxi_gpio_cleanup();
- return 0;
-
- }
复制代码 |
|