Guest User

paralcd-0.0.0alpha

a guest
Oct 30th, 2011
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.46 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <parapin.h>
  5. #include <sys/io.h>
  6. #define BASEPORT 0x378
  7. // correspondances LCD PARA pin à  pin
  8. #define LCD_E  LP_PIN01
  9. #define LCD_D0 LP_PIN02
  10. #define LCD_D1 LP_PIN03
  11. #define LCD_D2 LP_PIN04
  12. #define LCD_D3 LP_PIN05
  13. #define LCD_D4 LP_PIN06
  14. #define LCD_D5 LP_PIN07
  15. #define LCD_D6 LP_PIN08
  16. #define LCD_D7 LP_PIN09
  17. // Si on a connecté le pin LCD 5 au pin 16 du PP et non pas au 14
  18. #define LCD_RW LP_PIN16
  19. #define LCD_RS LP_PIN17
  20. // Peut etre que faire #define LP_CONTROL_PINS LP_SWITCHABLE_PINS suffit
  21. #define LP_CONTROL_PINS (LP_PIN01 | LP_PIN16 | LP_PIN17)
  22. //  
  23. #define LCD_CNTL LP_CONTROL_PINS
  24. // LCD_DATA peut aussi être vu comme un octet
  25. #define LCD_DATA LP_DATA_PINS
  26. // instructions du LCD
  27. #define LCD_iClearDisplay  0x01
  28. #define LCD_iReturnHome    0x02
  29. #define LCD_iEntryModeSet  0x04
  30. #define LCD_iDisplayOnOff  0x08
  31. #define LCD_iShift         0x10
  32. #define LCD_iFunctionSet   0x20
  33. #define LCD_iSetCGRAMAddr  0x40
  34. #define LCD_iSetDDRAMAddr  0x80
  35. static char LCD_DDRAM_ADDR; // adresse actuelle dans la mémoire DDRAM du LCD
  36. static char LCD_LINE; // ligne actuelle sur le LCD
  37. void set_lcd_data(unsigned char data) {
  38.   // char et non int afin d'être sur de n'écrire qu'un octet
  39.   // On utilise le fait que LCD_DATA est aussi visible comme un octet
  40.   // pour set-pin
  41.   set_pin(data);
  42. }
  43. void LCD_BusyCheck() {
  44.   clear_pin(LCD_DATA);
  45.   pin_input_mode(LCD_DATA); // Port de données en lecture  
  46.   clear_pin(LCD_RS); // LCD en mode instruction
  47.   set_pin(LCD_RW);   // LCD en mode écriture
  48.   do {
  49.     set_pin(LCD_E);
  50.     clear_pin(LCD_E); // créneau pour lancer l'opération
  51.   } while (pin_is_set(LCD_D7)); // teste le signal busy
  52.   clear_pin(LCD_RW); // LCD en mode lecture (pour éviter les conflits de bus)  
  53.   pin_output_mode(LCD_DATA); // Port de données en écriture  
  54. }
  55. void LCD_SendCommand(unsigned char command, unsigned char data) {
  56.   LCD_BusyCheck();
  57.   clear_pin(LCD_RW|LCD_RS); // LCD en mode lecture + instruction
  58.   set_lcd_data(command|data); // On place la donnée dans le bus
  59.   set_pin(LCD_E);
  60.   clear_pin(LCD_E); // créneau pour lancer l'opération
  61. }
  62. void LCD_Init() {
  63.   pin_output_mode(LP_DATA_PINS);
  64.   pin_output_mode(LP_CONTROL_PINS);
  65.   clear_pin(LCD_DATA);
  66.   set_lcd_data(0x30);
  67.   //set_pin(LCD_E);
  68.   //clear_pin(LCD_E);
  69.   usleep(200000);
  70.   { //Test
  71.     printf("Turn input LCD_DATA\n" );
  72.     pin_input_mode(LCD_DATA);
  73.     printf("\nData Pin Status: " );
  74.     printf("%s", pin_is_set(LCD_D7)?"1":"0" );
  75.     printf("%s", pin_is_set(LCD_D6)?"1":"0" );
  76.     printf("%s", pin_is_set(LCD_D5)?"1":"0" );
  77.     printf("%s", pin_is_set(LCD_D4)?"1":"0" );
  78.     printf("%s", pin_is_set(LCD_D3)?"1":"0" );
  79.     printf("%s", pin_is_set(LCD_D2)?"1":"0" );
  80.     printf("%s", pin_is_set(LCD_D1)?"1":"0" );
  81.     printf("%s", pin_is_set(LCD_D0)?"1":"0" );
  82.     printf("\n" );
  83.     printf("Expected: 00110000\n" );
  84.     pin_output_mode(LCD_DATA);
  85.   }
  86.   usleep(2000);  // Pour l'embarque ou on attend après la mise sous tension plus de 1.5 ms (2 ms)
  87.   clear_pin(LCD_CNTL);
  88.    
  89.   set_lcd_data(0x30);
  90.   set_pin(LCD_E);
  91.   clear_pin(LCD_E); // On envoie 0011xxxx
  92.   usleep(5000);     // attente de plus de 4.1 ms (5 ms)
  93.    
  94.   set_lcd_data(0x30);
  95.   set_pin(LCD_E);
  96.   clear_pin(LCD_E); // On envoie 0011xxxx
  97.   usleep(128000);     // attente de plus de 100 ms (128 ms)
  98.    
  99.   set_lcd_data(0x30);
  100.   set_pin(LCD_E);
  101.   clear_pin(LCD_E); // On envoie 0011xxxx
  102.   { //Test
  103.     pin_input_mode(LCD_DATA);
  104.     printf("\nData Pin Status: " );
  105.     printf("%s", pin_is_set(LCD_D7)?"1":"0" );
  106.     printf("%s", pin_is_set(LCD_D6)?"1":"0" );
  107.     printf("%s", pin_is_set(LCD_D5)?"1":"0" );
  108.     printf("%s", pin_is_set(LCD_D4)?"1":"0" );
  109.     printf("%s", pin_is_set(LCD_D3)?"1":"0" );
  110.     printf("%s", pin_is_set(LCD_D2)?"1":"0" );
  111.     printf("%s", pin_is_set(LCD_D1)?"1":"0" );
  112.     printf("%s", pin_is_set(LCD_D0)?"1":"0" );
  113.     printf("\n" );
  114.     printf("Expected: 00110000\n" );
  115.     pin_output_mode(LCD_DATA);
  116.   }
  117.   // a partir de maintenant on pourra tester le signal BUSY pour l'attente
  118.   LCD_SendCommand(LCD_iFunctionSet,
  119.           0x18);  // Interface 8 bits, 2 lignes, caracteres 5*8 pixels
  120.   LCD_SendCommand(LCD_iDisplayOnOff,
  121.           0x04); // Allumer l'écran
  122.   LCD_SendCommand(LCD_iEntryModeSet,
  123.           0x06); // sens g. à  d., décalage=0
  124.   LCD_SendCommand(LCD_iClearDisplay,
  125.           0x00); // effacer l'écran
  126.   LCD_DDRAM_ADDR = 0;
  127.   LCD_LINE = 1;
  128.    
  129. }
  130. void LCD_SendChar(unsigned char c) {
  131.   LCD_BusyCheck();
  132.   clear_pin(LCD_RW); // LCD en mode lecture
  133.   set_pin(LCD_RS);   // LCD en mode données
  134.   set_lcd_data(c);   // place la donnée sur le bus
  135.   set_pin(LCD_E);
  136.   clear_pin(LCD_E); // créneau pour lancer l'opération
  137.   LCD_DDRAM_ADDR++;
  138. }
  139. void LCD_SendString(unsigned char *string)
  140. {
  141.   while(*string) {             //till string ends
  142.     LCD_SendChar(*string++);  //send characters one by one
  143.   }
  144. }
  145. int main (int argc, char * argv[]) {
  146.   int parapin_init_success = 0;
  147.   if (argc == 1)  {
  148.     printf("Usage: paralcd prend au moins un argument. \n" );
  149.     return 1;
  150.   }
  151.   if (ioperm(BASEPORT, 3, 1))  {
  152.     perror("paralcd::ioperm" );
  153.     return 1;
  154.   }
  155.   parapin_init_success = pin_init_user(BASEPORT);
  156.   if (parapin_init_success != 0) {
  157.     printf("Echec de l'initialisation de parapin. \n" );
  158.   }
  159.  
  160.   LCD_Init();
  161.   LCD_SendString("Jovalise" );
  162.   clear_pin(LP_DATA_PINS|LP_CONTROL_PINS);  // vu dans les exemples de parapin
  163.   return 0;
  164. }
  165.  
  166.  
Advertisement
Add Comment
Please, Sign In to add comment