Nios® V/II Embedded Design Suite (EDS)
Support for Embedded Development Tools, Processors (SoCs and Nios® V/II processor), Embedded Development Suites (EDSs), Boot and Configuration, Operating Systems, C and C++
12745 Discussions

what\'s wrong with this programme?

Altera_Forum
Honored Contributor II
887 Views

I follow the \"button test \" source code given by ken,however I pushed several times to change the value of \"numclicks\",the I find one place maybe wrong,I changed some codeS in it as follow: 

 

while (read (fd, buf, 1) != 0) { 

if (buf[0] == \'1\') { 

numclicks += 1; 

printf (\"The button has been pushed %d times.\\n\", numclicks); 

It\'s still takes several pushes to make \"numclicks\" increased by one.I don\'t know what\'s the matter. 

 

code given by ken: 

 

# include <stdio.h># include <unistd.h># include <fcntl.h># include <stdlib.h>  

 

/* open /dev/button device for blocked reading 

* whenever button is pressed, 

* display number of times button was pressed since startup 

*/ 

 

int 

main (int argc, char* argv[]) 

int fd; 

char path[] = \"/dev/button\"; 

 

int numclicks = 0; 

char buf[1]; 

 

fd = open (path, O_RDONLY); 

 

if (fd < 0) { 

/* error occurred */ 

perror (\"open\"); 

exit (EXIT_FAILURE); 

} else { 

printf (\"opened %s\\n\", path); 

 

/* file is now open for reading... do your stuff... */ 

while (read (fd, buf, 1) != 0) { 

if (buf[0] == \&#39;1\&#39;) { 

numclicks += 1; 

printf (\"The button has been pushed %d times.\\n\", numclicks); 

 

if (close (fd) < 0) { 

perror (\"close\"); 

exit (EXIT_FAILURE); 

} else { 

printf (\"closed %s\\n\", path); 

 

exit (EXIT_SUCCESS); 

}
0 Kudos
0 Replies
Reply