25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

7segments.c 4.3 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. /*
  2. * 7segments.c:
  3. * Simple test program to see if we can drive a 7-segment LED
  4. * display using the GPIO and little else on the Raspberry Pi
  5. *
  6. * Copyright (c) 2013 Gordon Henderson
  7. ***********************************************************************
  8. */
  9. #undef PHOTO_HACK
  10. #include <wiringPi.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13. #include <ctype.h>
  14. #include <string.h>
  15. /*
  16. * Segment mapping
  17. *
  18. * --a--
  19. * | |
  20. * f b
  21. * | |
  22. * --g--
  23. * | |
  24. * e c
  25. * | |
  26. * --d-- p
  27. */
  28. // GPIO Pin Mapping
  29. static int digits [6] = { 7, 11, 10, 13, 12, 14 } ;
  30. static int segments [7] = { 6, 5, 4, 3, 2, 1, 0 } ;
  31. static const int segmentDigits [] =
  32. {
  33. // a b c d e f g Segments
  34. // 6 5 4 3 2 1 0, // wiringPi pin No.
  35. 1, 1, 1, 1, 1, 1, 0, // 0
  36. 0, 1, 1, 0, 0, 0, 0, // 1
  37. 1, 1, 0, 1, 1, 0, 1, // 2
  38. 1, 1, 1, 1, 0, 0, 1, // 3
  39. 0, 1, 1, 0, 0, 1, 1, // 4
  40. 1, 0, 1, 1, 0, 1, 1, // 5
  41. 1, 0, 1, 1, 1, 1, 1, // 6
  42. 1, 1, 1, 0, 0, 0, 0, // 7
  43. 1, 1, 1, 1, 1, 1, 1, // 8
  44. 1, 1, 1, 1, 0, 1, 1, // 9
  45. 1, 1, 1, 0, 1, 1, 1, // A
  46. 0, 0, 1, 1, 1, 1, 1, // b
  47. 1, 0, 0, 1, 1, 1, 0, // C
  48. 0, 1, 1, 1, 1, 0, 1, // d
  49. 1, 0, 0, 1, 1, 1, 1, // E
  50. 1, 0, 0, 0, 1, 1, 1, // F
  51. 0, 0, 0, 0, 0, 0, 0, // blank
  52. } ;
  53. // display:
  54. // A global variable which is written to by the main program and
  55. // read from by the thread that updates the display. Only the first
  56. // 6 characters are used.
  57. char display [8] ;
  58. /*
  59. * displayDigits:
  60. * This is our thread that's run concurrently with the main program.
  61. * Essentially sit in a loop, parsing and displaying the data held in
  62. * the "display" global.
  63. *********************************************************************************
  64. */
  65. PI_THREAD (displayDigits)
  66. {
  67. int digit, segment ;
  68. int index, d, segVal ;
  69. piHiPri (50) ;
  70. for (;;)
  71. {
  72. for (digit = 0 ; digit < 6 ; ++digit)
  73. {
  74. for (segment = 0 ; segment < 7 ; ++segment)
  75. {
  76. d = toupper (display [digit]) ;
  77. /**/ if ((d >= '0') && (d <= '9')) // Digit
  78. index = d - '0' ;
  79. else if ((d >= 'A') && (d <= 'F')) // Hex
  80. index = d - 'A' + 10 ;
  81. else
  82. index = 16 ; // Blank
  83. segVal = segmentDigits [index * 7 + segment] ;
  84. digitalWrite (segments [segment], segVal) ;
  85. }
  86. digitalWrite (digits [digit], 1) ;
  87. delay (2) ;
  88. digitalWrite (digits [digit], 0) ;
  89. }
  90. }
  91. }
  92. /*
  93. * setup:
  94. * Initialise the hardware and start the thread
  95. *********************************************************************************
  96. */
  97. void setup (void)
  98. {
  99. int i, c ;
  100. wiringPiSetup () ;
  101. // 7 segments
  102. for (i = 0 ; i < 7 ; ++i)
  103. { digitalWrite (segments [i], 0) ; pinMode (segments [i], OUTPUT) ; }
  104. // 6 digits
  105. for (i = 0 ; i < 6 ; ++i)
  106. { digitalWrite (digits [i], 0) ; pinMode (digits [i], OUTPUT) ; }
  107. strcpy (display, " ") ;
  108. piThreadCreate (displayDigits) ;
  109. delay (10) ; // Just to make sure it's started
  110. // Quick countdown LED test sort of thing
  111. c = 999999 ;
  112. for (i = 0 ; i < 10 ; ++i)
  113. {
  114. sprintf (display, "%06d", c) ;
  115. delay (400) ;
  116. c -= 111111 ;
  117. }
  118. strcpy (display, " ") ;
  119. delay (400) ;
  120. #ifdef PHOTO_HACK
  121. sprintf (display, "%s", "123456") ;
  122. for (;;)
  123. delay (1000) ;
  124. #endif
  125. }
  126. /*
  127. * teenager:
  128. * No explanation needed. (Nor one given!)
  129. *********************************************************************************
  130. */
  131. void teenager (void)
  132. {
  133. char *message = " feedbeef babe cafe b00b " ;
  134. int i ;
  135. for (i = 0 ; i < strlen (message) - 4 ; ++i)
  136. {
  137. strncpy (display, &message [i], 6) ;
  138. delay (200) ;
  139. }
  140. delay (1000) ;
  141. for (i = 0 ; i < 3 ; ++i)
  142. {
  143. strcpy (display, " ") ;
  144. delay (150) ;
  145. strcpy (display, " b00b ") ;
  146. delay (250) ;
  147. }
  148. delay (1000) ;
  149. strcpy (display, " ") ;
  150. delay (1000) ;
  151. }
  152. /*
  153. *********************************************************************************
  154. * main:
  155. * Let the fun begin
  156. *********************************************************************************
  157. */
  158. int main (void)
  159. {
  160. struct tm *t ;
  161. time_t tim ;
  162. setup () ;
  163. teenager () ;
  164. tim = time (NULL) ;
  165. for (;;)
  166. {
  167. while (time (NULL) == tim)
  168. delay (5) ;
  169. tim = time (NULL) ;
  170. t = localtime (&tim) ;
  171. sprintf (display, "%02d%02d%02d", t->tm_hour, t->tm_min, t->tm_sec) ;
  172. delay (500) ;
  173. }
  174. return 0 ;
  175. }