1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/* Test du port parallele et afficheur LCD * Broche LCD Enable sur Broche Init du port // * Broche LCD RegisterSelect sur Broche Strobe sur port // * Compiler avec gcc lcd.c -o lcd -lm * Pour sortir proprement Ctrl+D puis Enter */ #include <sys/ioctl.h> #include <string.h> #include <fcntl.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <sys/errno.h> #include <linux/ppdev.h> #include <linux/parport.h> #include <math.h> void clean_stdin(void) { int c; do { c = getchar(); } while (c != '\n' && c != EOF); } void SendCommand(int fd, int value, int command){ struct ppdev_frob_struct valout; valout.mask=command; if(value==0){ valout.val=0; }else{ valout.val=command; } if (ioctl (fd, PPFCONTROL, &valout) < 0) { exit(EXIT_FAILURE); } } void SendData(int fd, int data){ if (ioctl (fd, PPWDATA, &data) < 0) { fprintf(stderr,"Send Error : %s (%d)\n", strerror(errno),errno); exit(EXIT_FAILURE); } } void ToggleEnable(fd){ SendCommand(fd,1,PARPORT_CONTROL_INIT); usleep(40); SendCommand(fd,0,PARPORT_CONTROL_INIT); } void SendLCDData(int fd, int data){ SendCommand(fd,0,PARPORT_CONTROL_STROBE); SendData(fd,data); ToggleEnable(fd); } void SendLCDCommand(int fd, int command){ SendCommand(fd,1,PARPORT_CONTROL_STROBE); SendData(fd,command); ToggleEnable(fd); } int main(){ int fd; char c; int lc=0; char * cPort = malloc(0); printf("Veuillez saisir le périphérique associé au port parallèle: "); while ((c = getchar()) != '\n') { cPort=realloc(cPort,lc+1); cPort[lc]=c; lc++; } cPort=realloc(cPort,lc+1); cPort[lc] = '\0'; if (lc==0){ cPort=realloc(cPort,25); sprintf(cPort,"/dev/.static/dev/parport0"); } //Ouverture du port if ((fd = open(cPort, O_RDWR)) < 0) { fprintf(stderr,"Open %s Error : %s (%d)\n",cPort, strerror(errno),errno); exit(EXIT_FAILURE); } //On prend la main sur le port if (ioctl(fd, PPCLAIM) < 0) { fprintf(stderr,"PPCLAIM ioctl Error : %s (%d)\n", strerror(errno),errno); exit(EXIT_FAILURE); } SendLCDCommand(fd,0x38); //Initialisation du LCD SendLCDCommand(fd,0x0F); //Initialisation du LCD SendLCDCommand(fd,0x01); SendLCDCommand(fd,0x02); printf("En attende de caractères:\n"); //Attente d'un texte while ((c = getchar())!=EOF) { SendLCDData(fd,(int) c); } //On rend la main sur le port if (ioctl(fd, PPRELEASE) < 0) { fprintf(stderr,"PPRELEASE ioctl Error : %s (%d)\n", strerror(errno),errno); exit(EXIT_FAILURE); } //On referme le port if(close(fd) < 0) { fprintf(stderr,"Close Error : %s (%d)\n", strerror(errno),errno); exit(EXIT_FAILURE); } return EXIT_SUCCESS; }
|