Advertisement
Guest User

Untitled

a guest
Mar 16th, 2015
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.51 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <util/delay.h>
  4. #include <util/atomic.h>
  5. #include <string.h>
  6. #define STEP_OFF2 7
  7. #define STEP_OFF1 5
  8. #define F_CPU 16000000UL
  9.  
  10. #define select_PB4 (PORTB ^= (1 << PB6))
  11.  
  12. #define START_TIMER2 (TCCR2 |= (1 << CS22) | (1 << CS20) | (1 << CS21))
  13. #define STOP_TIMER2 (TCCR2 &= ~(1 << CS22) & ~(1 << CS20) & ~(1 << CS21))
  14. #define write_SPI(data) (SPDR = data)
  15.  
  16. #define a 1
  17. #define b 2
  18. #define c 4
  19. #define d 8
  20. #define e 16
  21. #define f 32
  22. #define g 64
  23. #define dp 128
  24.  
  25.  
  26. #define LCD_0 (a | b | c | d | e | f)
  27. #define LCD_1 (b | c)
  28. #define LCD_2 (a | b | g | e | d)
  29. #define LCD_3 (a | b | g | c | d)
  30. #define LCD_4 (f | g | b | c)
  31. #define LCD_5 (a | f | g | c | d)
  32. #define LCD_6 (a | f | g | c | d | e)
  33. #define LCD_7 (a | b | c)
  34. #define LCD_8 (a | b | c | d | e |f |g)
  35. #define LCD_9 (a | b | c | d | f | g)
  36. #define LCD_t (f | g | e | d)
  37. #define LCD_f (a | e | f | g)
  38. #define LCD_CLR 0x00
  39.  
  40.  
  41.  
  42. static const uint8_t led_table[] = {LCD_0, LCD_1, LCD_2, LCD_3, LCD_4, LCD_5, LCD_6, LCD_7, LCD_8, LCD_9};
  43. static uint8_t spi_counter = 0;
  44. static uint8_t clock = 0;
  45.  
  46. typedef struct
  47. {
  48.     uint8_t value;
  49.     uint8_t buffer[4];
  50. } LCD_ctx;
  51.  
  52. LCD_ctx LCD = {0,{LCD_0,LCD_1,LCD_2,LCD_3}};
  53.  
  54.  
  55. #ifdef COMM_ANODE
  56. #define shift_LCD (write_SPI(1 << LCD.value))
  57. #define write_LCD (write_SPI(0xFF ^ LCD.buffer[LCD.value]))
  58. #else
  59. #define shift_LCD (write_SPI(0xFF & ~(1 << LCD.value)))
  60. #define write_LCD (write_SPI(LCD.buffer[LCD.value]))
  61. #endif
  62.  
  63. ISR (SPI_STC_vect)
  64. {
  65.     switch (spi_counter)
  66.     {
  67.         case 0:
  68.         {
  69.             write_LCD;
  70.             ++LCD.value;
  71.             ++spi_counter;
  72.             if (LCD.value == sizeof(LCD_ctx))
  73.             {
  74.                 LCD.value = 0;
  75.             }
  76.             break;
  77.         }
  78.         case 1:
  79.         {
  80.             spi_counter = 0;
  81.         }
  82.     }
  83.    
  84. }  
  85.  
  86. ISR (TIMER2_COMP_vect)
  87. {  
  88.     ++clock;
  89.     if (clock == 230)
  90.     {
  91.         LCD.buffer[3] ^= (1 << 7);
  92.         clock = 0;
  93.     }
  94.     if (spi_counter == 0)
  95.     {
  96.         PORTB &= ~(1 << PB0);
  97.         PORTB |= (1 << PB0);
  98.         shift_LCD;
  99.     };
  100. }
  101.  
  102. uint8_t read_ADC(uint8_t mux)
  103. {
  104.     ADMUX= (ADMUX & 0xE0) | mux;
  105.     asm("nop;");
  106.     asm("nop;");
  107.     asm("nop;");
  108.     ADCSRA |= (1 << ADSC);
  109.     loop_until_bit_is_set(ADCSRA,ADIF);
  110.     return ADCH;
  111. };
  112.  
  113. typedef struct
  114. {
  115.     uint8_t H;
  116.     uint8_t L;
  117. } t_divmod;
  118.  
  119.  
  120. inline t_divmod divmod (uint8_t _a, uint8_t _b) // a делим на b
  121. {
  122.     uint8_t high = 0;
  123.     while( _a >= _b)
  124.     {
  125.         ++high;
  126.         _a -= _b;
  127.     };
  128.    
  129.     t_divmod divm = {high, _a};
  130.     return divm;
  131. };
  132.  
  133. void LCD_update(uint8_t start_byte,uint8_t data)
  134. {
  135.     t_divmod div = divmod(data, 100);
  136.     uint8_t _a = div.H;
  137.     div = divmod(div.L, 10);
  138. //  uint8_t H1 = data / 100;
  139. //  uint8_t HL = data % 100;
  140. //  uint8_t H2 = HL / 10;
  141. //  uint8_t L = HL % 10;
  142.     ATOMIC_BLOCK(ATOMIC_FORCEON)
  143.     {
  144.         uint8_t ldp = (LCD.buffer[3] >> 7) & 1;
  145.         LCD.buffer[0] = start_byte;
  146.         LCD.buffer[1] = led_table[_a];
  147.         LCD.buffer[2] = led_table[div.H];
  148.         LCD.buffer[3] = ldp ? led_table[div.L] | dp : led_table[div.L] & ~dp ;
  149.         LCD.value = 0;
  150.     };
  151. }
  152.  
  153. int main(void)
  154. {
  155.     SPCR=0b11010000 ;//настройка spi
  156.    
  157.     DDRB |= (1 << PB1) | (1 << PB0) | (1 << PB5) | (1 << PB3) | (1 << PB2);
  158.     PORTB = 0x00;
  159.    
  160.     TCCR2 = (1 << WGM21); //таймер 2 обновляет данные на индикаторе
  161.     TIMSK = (1 << OCIE2);
  162.  
  163.     ADMUX |= (1 << REFS0) | (1 << REFS1) | (1 << ADLAR);
  164.     ADCSRA |= (1 << ADEN) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2)  ;// (1 << ADIE) | (1 << ADFR) для прерывания
  165.      
  166.     OCR2 = 13;
  167.     START_TIMER2;
  168.     sei();
  169.     while(1)
  170.     {
  171.         uint8_t t = read_ADC(0);
  172.         LCD_update(LCD_f,t);
  173.         _delay_ms(80);
  174.     }
  175.     return 1;
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement