Advertisement
desdemona

wżż

Apr 10th, 2013
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.77 KB | None | 0 0
  1. /*
  2.  * lab7.c
  3.  *
  4.  * Created: 2013-04-10 11:28:36
  5.  *  Author: student
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <lcd.h>
  10. #include <util/delay.h>
  11. #include <stdio.h>
  12. #include <avr/interrupt.h>
  13.  
  14. /* Dodatkowe procedury obsługi wyświetlacza LCD, wymagają dołączenia
  15. modułu <lcd.h>.
  16. Rejestry wyświetlacza dostępne są pod adresami:
  17. - rejestr sterujący 0x1F90 (COMM_LCD)
  18. char *COMM_LCD = 0x1F90;
  19. - rejestr danych 0x1F91 (DATA_LCD)
  20. char *DATA_LCD = 0x1F91; */
  21. #ifndef __PGMSPACE_H_
  22. # include <avr/pgmspace.h>
  23. #endif
  24. // Zerowanie wyświetlacza
  25. void lcd_clear(void);
  26. // Przesunięcie kursora
  27. void lcd_home(void);
  28. // Inicjalizacja wyświetlacza 5x7, 2 wiersze
  29. void init_lcd(void);
  30. // Przepisanie tekstu (koniec = 0xFF) z pamięci programu na wyświetlacz
  31. // Funkcja dopuszcza wykorzystanie znaku o kodzie 0x00
  32. // _adres - adres tekstu w pam. FLASH
  33. void disp_txt_P(const char* _adres);
  34. // Przepisanie tekstu (koniec = 0x00) z pamięci danych na wyświetlacz
  35. // _adres - adres tekstu w pam. RAM
  36. void disp_txt_D(char* _adres);
  37. //Implementacja
  38. void lcd_clear(void)
  39. {
  40.     pisz_com(0x01);
  41. }
  42. void lcd_home(void)
  43. {
  44.     pisz_com(0x02);
  45. }
  46. void init_lcd(void)
  47. {
  48.     _delay_ms(15);
  49.     COMM_LCD = 0x30;
  50.     _delay_ms(5);
  51.     COMM_LCD = 0x30;
  52.     _delay_ms(0.2);
  53.     COMM_LCD = 0x30;
  54.     _delay_ms(30);
  55.     COMM_LCD = 0x38; //Słowo danych 8-bitów, dwa wiersze, znak 7x5
  56.     //pikseli
  57.     test_bf();
  58.     pisz_com(0x0C); //Włączenie wyświetlacza, bez kursora, bez
  59.     //migotania
  60.     lcd_clear();
  61.     pisz_com(0x06); //Wpisywanie znaków od lewej, autoinkrementacja
  62.     lcd_home();
  63. }
  64. void disp_txt_P(const char* _adres)
  65. {
  66.     volatile uint8_t al;
  67.     for (int i = 0; i<16; i++)
  68.     {
  69.         al = pgm_read_byte(&_adres[i]);
  70.         if (al == 0xFF) break;
  71.         pisz_ws(al);
  72.     }
  73. }
  74.  
  75. void disp_txt_D(char* _adres)
  76. {
  77.     volatile uint8_t al;
  78.     for (int i = 0; i<16; i++)
  79.     {
  80.         al = _adres[i];
  81.         if (al == 0x00) break;
  82.         pisz_ws(al);
  83.     }
  84. }
  85.  
  86. char w1[] = "Przetwornik A/C";
  87. char w2[32];
  88. int d1, d2;
  89.  
  90. /*ISR(ADC_vect)
  91. {
  92.    
  93.            
  94.     d1 = 5 * ADC / 1024;
  95.     d2 = (10 * 5 * ADC / 1024) % 10;
  96.  
  97.    
  98. }*/
  99.  
  100. int main(void)
  101. {
  102.     sei();
  103.     MCUCR =_BV(SRE) | _BV(SRW10);
  104.     XMCRA =_BV(SRW01) |_BV(SRW00) |_BV(SRW11);
  105.    
  106.     init_lcd();
  107.    
  108.     /*ADCSRA = _BV(7);
  109.     ADMUX = _BV(6) | _BV(1);
  110.    
  111.     while(1)
  112.     {  
  113.         ADCSRA |= _BV(6);
  114.        
  115.         while (ADCSRA & _BV(4))
  116.             ;
  117.            
  118.         ADCSRA &= ~_BV(6);
  119.        
  120.         lcd_clear();
  121.         pisz_com(0x80);
  122.         disp_txt_D(w1);
  123.            
  124.         d1 = 5 * ADC / 1024;
  125.         d2 = (10 * 5 * ADC / 1024) % 10;
  126.  
  127.         sprintf(w2, "Pomiar: %d.%dV", d1, d2);
  128.  
  129.         pisz_com(0xC0);
  130.         disp_txt_D(w2);
  131.        
  132.         _delay_ms(500);
  133.        
  134.     }*/
  135.    
  136.     /*ADCSRA = _BV(7) | _BV(3);
  137.     ADMUX = _BV(6) | _BV(1);
  138.    
  139.     while(1)
  140.     {  
  141.         ADCSRA |= _BV(6);
  142.        
  143.         while (ADCSRA & _BV(4))
  144.             ;
  145.            
  146.         ADCSRA &= ~_BV(6);
  147.        
  148.         //lcd_clear();
  149.         pisz_com(0x80);
  150.         disp_txt_D(w1);
  151.         sprintf(w2, "Pomiar: %d.%dV", d1, d2);
  152.  
  153.         pisz_com(0xC0);
  154.         disp_txt_D(w2);
  155.        
  156.         _delay_ms(500);
  157.        
  158.     }*/
  159.    
  160.     ADCSRA = _BV(7);
  161.     ADMUX = _BV(6) | _BV(1);
  162.    
  163.     DDRB = 0xFF;
  164.     PORTB = PIND;
  165.    
  166.     while(1)
  167.     {  
  168.         ADCSRA |= _BV(6);
  169.        
  170.         while (ADCSRA & _BV(4))
  171.             ;
  172.            
  173.         ADCSRA &= ~_BV(6);
  174.        
  175.         //lcd_clear();
  176.         pisz_com(0x80);
  177.         disp_txt_D(w1);
  178.            
  179.         d1 = 5 * ADC / 1024;
  180.         d2 = (10 * 5 * ADC / 1024) % 10;
  181.  
  182.         sprintf(w2, "Pomiar: %d.%dV", d1, d2);
  183.  
  184.         if (d1 > 0)
  185.         {          
  186.             PORTB &= ~_BV(0);
  187.         }
  188.         else
  189.         {
  190.             PORTB |= _BV(0);
  191.         }
  192.         if (d1 > 1)
  193.         {          
  194.             PORTB &= ~_BV(1);
  195.         }
  196.         else
  197.         {
  198.             PORTB |= _BV(1);
  199.         }
  200.         if (d1 > 2)
  201.         {          
  202.             PORTB &= ~_BV(2);
  203.         }
  204.         else
  205.         {
  206.             PORTB |= _BV(2);
  207.         }
  208.         if (d1 > 3)
  209.         {          
  210.             PORTB &= ~_BV(3);
  211.         }
  212.         else
  213.         {
  214.             PORTB |= _BV(3);
  215.         }
  216.         if (d1 > 4)
  217.         {          
  218.             PORTB &= ~_BV(4);
  219.         }
  220.         else
  221.         {
  222.             PORTB |= _BV(4);
  223.         }
  224.  
  225.         pisz_com(0xC0);
  226.         disp_txt_D(w2);
  227.        
  228.         _delay_ms(500);
  229.        
  230.     }
  231. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement