Подключение дисплея к Raspberry Pi через сдвиговый регистр 74HC595 http://blindage.org/?p=4835
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

226 lines
4.5 KiB

  1. #include <time.h>
  2. #include <wiringPi.h>
  3. #include <wiringShift.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. int latchPin=2;
  8. int clockPin=3;
  9. int dataPin=0;
  10. #define BIT_RS 4
  11. #define BIT_E 5
  12. #define BIT_ENABLE(x,n) ((x) |= (1<<(n)))
  13. #define BIT_DISABLE(x,n) ((x) &=~ (1<<(n)))
  14. unsigned char pos_addr[5];
  15. uint8_t pos_last=1;
  16. uint8_t reverse_bits(uint8_t to_reverse) {
  17. uint8_t reversed=0;
  18. for( uint8_t i=0; i<8; ++i)
  19. {
  20. reversed = reversed << 1;
  21. reversed |= (to_reverse & 0x1);
  22. to_reverse = to_reverse >> 1;
  23. }
  24. return reversed;
  25. }
  26. void reg_send(uint8_t value)
  27. {
  28. uint8_t i;
  29. for(i = 8; i; --i)
  30. {
  31. if(value & 0x80)
  32. digitalWrite(dataPin,HIGH);
  33. else
  34. digitalWrite(dataPin,LOW);
  35. delayMicroseconds(15);
  36. digitalWrite(clockPin,HIGH);
  37. delayMicroseconds(15);
  38. digitalWrite(clockPin,LOW);
  39. delayMicroseconds(15);
  40. value <<= 1;
  41. }
  42. digitalWrite(dataPin,LOW);
  43. delayMicroseconds(15);
  44. digitalWrite(latchPin,HIGH);
  45. delayMicroseconds(15);
  46. digitalWrite(latchPin,LOW);
  47. }
  48. void low_send(unsigned char data, uint8_t type) {
  49. data = reverse_bits(data);
  50. data = data >> 4;
  51. if (type>0) BIT_ENABLE(data,BIT_RS); else BIT_DISABLE(data, BIT_RS);
  52. BIT_ENABLE(data,BIT_E);
  53. reg_send(data);
  54. BIT_DISABLE(data,BIT_E);
  55. reg_send(data);
  56. //reg_send((1<<BIT_E)|(type<<BIT_RS)|data);
  57. //reg_send((0<<BIT_E)|(type<<BIT_RS)|data);
  58. }
  59. void lcd_send(unsigned char data, uint8_t _RS) {
  60. uint8_t H_BITS, L_BITS;
  61. H_BITS=0|(data >> 4);
  62. L_BITS=(data & 0x0F);
  63. low_send(H_BITS,_RS);
  64. low_send(L_BITS,_RS);
  65. digitalWrite (latchPin, HIGH) ;
  66. delayMicroseconds(15);
  67. digitalWrite (latchPin, LOW) ;
  68. }
  69. void lcd_init() {
  70. lcd_send(0x20,0);
  71. lcd_send(0,0);
  72. lcd_send(0x14,0);
  73. lcd_send(0x06,0);
  74. lcd_send(0x0f,0);
  75. lcd_send(0x28,0);
  76. pos_addr[1]=0x00;
  77. pos_addr[2]=0x40;
  78. pos_addr[3]=0x14;
  79. pos_addr[4]=0x54;
  80. pos_last=1;
  81. }
  82. void lcd_char(unsigned char data) {
  83. lcd_send(data,1);
  84. }
  85. void lcd_text (const char *s)
  86. {
  87. register char c;
  88. while (c = *s++)
  89. {
  90. if (c == '\n')
  91. {
  92. //next line
  93. pos_last=pos_last+1;
  94. lcd_send((1 << 7)|pos_addr[pos_last],0);
  95. if (pos_last>=5) pos_last=1;
  96. }
  97. else
  98. //just send
  99. lcd_char(c);
  100. }
  101. }
  102. void lcd_clear() {
  103. lcd_send(0x01,0);
  104. delay(5);
  105. pos_last=1;
  106. }
  107. void setup(){
  108. pinMode(clockPin,OUTPUT);
  109. pinMode(latchPin,OUTPUT);
  110. pinMode(dataPin,OUTPUT);
  111. digitalWrite(clockPin,HIGH);
  112. digitalWrite(latchPin,HIGH);
  113. delayMicroseconds(100);
  114. reg_send(0xff);
  115. reg_send(0xff);
  116. digitalWrite(latchPin,LOW);
  117. digitalWrite(clockPin,LOW);
  118. digitalWrite(dataPin,LOW);
  119. }
  120. void load_ext_map(char *fname_own)
  121. {
  122. FILE *file;
  123. char *fname;
  124. fname = fname_own;
  125. file = fopen(fname,"r");
  126. if(file == 0)
  127. {
  128. printf("cant open '%s'",fname);
  129. } else {
  130. //printf("Map '%s' opened\n",fname);
  131. //get file size and load to LCD memory
  132. fseek(file, 0L, SEEK_END);
  133. int fsz = ftell(file);
  134. fseek(file, 0L, SEEK_SET);
  135. unsigned char chbyte;
  136. int charnum = 1, charcnt = 0;
  137. for(int p=0;p<fsz;p++) {
  138. chbyte = getc(file);
  139. //printf("Sending addr and char\n");
  140. lcd_send(0x40+p,0);
  141. //delay(1);
  142. lcd_send(chbyte,1); //write byte to mem
  143. //delay(1);
  144. //uncomment for debug
  145. charcnt++;
  146. if (charcnt > 8) { charcnt = 1; charnum++; }
  147. //printf("CHAR %d ADDR %02X BYTE %02X\n",charnum, 0x40+p, chbyte);
  148. }
  149. lcd_send(0x02,0);
  150. fclose(file);
  151. delay(50);
  152. }
  153. }
  154. int main( int argc, char *argv[] ){
  155. if (wiringPiSetup()==-1) { exit(1); }
  156. setup();
  157. lcd_init();
  158. if ( argc < 3 ) /* argc should be 3 for correct execution */
  159. {
  160. printf("You forgot something\n");
  161. lcd_send(0x0C,0); //turn on display
  162. delay(1);
  163. while(1) {
  164. lcd_clear();
  165. lcd_text("Vladimir Smagin\n");
  166. lcd_text("vk.com/int21h\n");
  167. delay(3000);
  168. lcd_clear();
  169. lcd_text("Visit my site:\n");
  170. lcd_text("blindage.org\n");
  171. delay(3000);
  172. }
  173. } else {
  174. if (argc>2) {
  175. //printf("Loading %s \n", argv[1]);
  176. lcd_send(0x08,0); //turn off display
  177. delay(1);
  178. load_ext_map(argv[1]);
  179. lcd_clear();
  180. lcd_text(argv[2]);
  181. lcd_send(0x0C,0); //turn on display
  182. delay(1);
  183. } else {
  184. printf("./lcd595out ./charmap \"text\"");
  185. }
  186. }
  187. return 0;
  188. }