Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.38 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <util/delay.h>
  3. #include <avr/interrupt.h>
  4.  
  5. #define LCD_DDR DDRB
  6. #define LCD_PORT PORTB
  7. #define LCD_RS 0
  8. #define LCD_EN 1
  9. #define LCD_DB4 4
  10. #define LCD_DB5 5
  11. #define LCD_DB6 6
  12. #define LCD_DB7 7
  13.  
  14. static char numberOfChars = 0;
  15.  
  16.  
  17. void send_char(char key) //funkcja wysyłająca znak do wyświetlacza
  18. {
  19. PORTB|=0b00000001; //najmłodszy bit ustawiony na 1 informuje wyświetlacz że chcemy wyświetlić znak
  20. send(key); //wywołanie funkcji wysyłania
  21. }
  22. void send(char key) //funkcja wysyłająca informacje do wyświetlacza
  23. {
  24. PORTB|=00000010; // podniesienie stanu drugiego bitu
  25. PORTB=(PORTB & 0x0F)| (key & 0xF0); //PORTB przyjmie wartość czterech lewych bitów znaku „key” , a pozostałe //bity tego rejestru (4 młodsze) pozostaną bez zmian
  26. asm("nop"); //odczekanie trzech taktów procesora
  27. asm("nop");
  28. asm("nop");
  29. PORTB&=0b11111101; //ustawienie drugiego bitu na 0 powoduje wysłanie części znaku do wyświetlacza
  30. _delay_ms(2); //opóźnienie 2ms
  31.  
  32. PORTB|=0b00000010; //ustawienie drugiego bitu w stan wysoki
  33. PORTB=(PORTB & 0x0F)| ((key & 0x0F)<<4); //PORTB przyjmie wartość czterech prawych bitów znaku „key” , a pozostałe //bity tego rejestru (4 młodsze) pozostaną bez zmian
  34. asm("nop"); //odczekanie trzech taktów procesora
  35. asm("nop");
  36. asm("nop");
  37. PORTB&=0b11111101; // wysłanie części znaku do wyświetlacza (reakcja na opadające zbocze)
  38. _delay_ms(2);
  39. }
  40.  
  41.  
  42. void printText(char *text, int textLength)
  43. {
  44. int k=0;
  45.  
  46. while(k < textLength)
  47. {
  48. if(numberOfChars >= 17)
  49. {
  50. nextLine();
  51. numberOfChars = 0;
  52. }
  53. printChar(text[k]);
  54. numberOfChars++;
  55. k++;
  56. }
  57. }
  58.  
  59. void nextLine()
  60. {
  61. LCD_PORT &= ~(_BV(LCD_RS));
  62. printChar( 0x80+0x28 );
  63. LCD_PORT |= _BV(LCD_RS);
  64. }
  65.  
  66. uint8_t myChar[] = {0x08, 0x08, 0x04, 0x02, 0x04, 0x08, 0x08, 0x00};
  67.  
  68. void clear_LCD() //funkcja „czyszcząca” wyświetlacz
  69. {
  70. send_command(1); //wysłanie do wyświetlacza komendy o kodzie 0b00000001
  71. _delay_ms(2); //czekamy 2ms
  72. }
  73. void clear_LCD() //funkcja „czyszcząca” wyświetlacz
  74. {
  75. send_command(1); //wysłanie do wyświetlacza komendy o kodzie 0b00000001
  76. _delay_ms(2); //czekamy 2ms
  77. }
  78.  
  79. void configure_LCD() //funkcja przygotowująca wyświetlacz do pracy
  80. {
  81. send_command(0b00101100); // 4 bity, 2 linie, 5x10
  82. send_command(0b00001110); //wyświetlanie, kursor , miganie
  83. }
  84.  
  85.  
  86. unsigned char KbScan()
  87. {
  88. unsigned char result = 0xFF;
  89. unsigned char i, j;
  90.  
  91. PORTA = 0xFF;
  92. for(i = 0; i < 4; i++)
  93. {
  94. DDRA = (1<<i+4);
  95. PORTA = ~(1<<i+4);
  96. for(j = 0; j < 4; j++)
  97. {
  98. _delay_ms(2);
  99. if(!(PINA & (1<<j)))
  100. {
  101. _delay_ms(2);
  102. result = PINA;
  103. }
  104. }
  105.  
  106. }
  107. switch(result)
  108. {
  109. case 0b11101110:
  110. return 1;
  111. case 0b11011110:
  112. return 2;
  113. case 0b10111110:
  114. return 3;
  115. case 0b01111110:
  116. return 4;
  117. case 0b11101101:
  118. return 5;
  119. case 0b11011101:
  120. return 6;
  121. case 0b10111101:
  122. return 7;
  123. case 0b01111101:
  124. return 8;
  125. case 0b11101011:
  126. return 9;
  127. case 0b11011011:
  128. return 10;
  129. case 0b10111011:
  130. return 11;
  131. case 0b01111011:
  132. return 12;
  133. case 0b11100111:
  134. return 13;
  135. case 0b11010111:
  136. return 14;
  137. case 0b10110111:
  138. return 15;
  139. case 0b01110111:
  140. return 16;
  141. case 0b11111111:
  142. return 0;
  143. default:
  144. return 1;
  145. }
  146. }
  147.  
  148.  
  149. volatile char temp = 0;
  150.  
  151.  
  152. ISR(INT0_vect)
  153. {
  154. LCD_PORT &= ~(_BV(LCD_RS));
  155. printChar(0b00000001); //czyszczenie wyswietlacza
  156. LCD_PORT |= _BV(LCD_RS);
  157.  
  158. print(temp);
  159. _delay_ms(10);
  160.  
  161. }
  162.  
  163. void print(char x)
  164. {
  165. switch(x)
  166. {
  167. case 1:
  168. send_char('1');break;
  169. case 2:
  170. send_char('2');break;
  171. case 3:
  172. send_char('3');break;
  173. case 4:
  174. send_charr('4');break;
  175. case 5:
  176. send_char('5');break;
  177. case 6:
  178. send_char('6');break;
  179. case 7:
  180. send_char('7');break;
  181. case 8:
  182. send_char('8');break;
  183. case 9:
  184. send_char('9');break;
  185. case 10:
  186. send_char('1');send_char('0');break;
  187. case 11:
  188. send_char('1');send_char('1');break;
  189. case 12:
  190. send_char('1');send_char('2');break;
  191. case 13:
  192. send_char('1');send_charr('3');break;
  193. case 14:
  194. send_char('1');send_char('4');break;
  195. case 15:
  196. send_char('1');send_char('5');break;
  197. case 16:
  198. send_charr('1');send_char('6');break;
  199. }
  200. }
  201.  
  202. int main()
  203. {
  204.  
  205. DDRA = 0xFF;
  206. PORTA = 0xFF;
  207. PORTB = 0xFF;
  208. DDRB = 0xFF;
  209.  
  210. sei();
  211. MCUCR |= (1<<ISC01);
  212. GICR |= (1<<INT0);
  213.  
  214. clear_LCD();
  215. configure_LCD();
  216.  
  217. while(1)
  218. {
  219. temp = KbScan();
  220.  
  221. }
  222.  
  223. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement