Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #include <util/atomic.h>
- #include <string.h>
- #define STEP_OFF2 7
- #define STEP_OFF1 5
- #define F_CPU 16000000UL
- #define select_PB4 (PORTB ^= (1 << PB6))
- #define START_TIMER2 (TCCR2 |= (1 << CS22) | (1 << CS20) | (1 << CS21))
- #define STOP_TIMER2 (TCCR2 &= ~(1 << CS22) & ~(1 << CS20) & ~(1 << CS21))
- #define write_SPI(data) (SPDR = data)
- #define a 1
- #define b 2
- #define c 4
- #define d 8
- #define e 16
- #define f 32
- #define g 64
- #define dp 128
- #define LCD_0 (a | b | c | d | e | f)
- #define LCD_1 (b | c)
- #define LCD_2 (a | b | g | e | d)
- #define LCD_3 (a | b | g | c | d)
- #define LCD_4 (f | g | b | c)
- #define LCD_5 (a | f | g | c | d)
- #define LCD_6 (a | f | g | c | d | e)
- #define LCD_7 (a | b | c)
- #define LCD_8 (a | b | c | d | e |f |g)
- #define LCD_9 (a | b | c | d | f | g)
- #define LCD_t (f | g | e | d)
- #define LCD_f (a | e | f | g)
- #define LCD_CLR 0x00
- 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};
- static uint8_t spi_counter = 0;
- static uint8_t clock = 0;
- typedef struct
- {
- uint8_t value;
- uint8_t buffer[4];
- } LCD_ctx;
- LCD_ctx LCD = {0,{LCD_0,LCD_1,LCD_2,LCD_3}};
- #ifdef COMM_ANODE
- #define shift_LCD (write_SPI(1 << LCD.value))
- #define write_LCD (write_SPI(0xFF ^ LCD.buffer[LCD.value]))
- #else
- #define shift_LCD (write_SPI(0xFF & ~(1 << LCD.value)))
- #define write_LCD (write_SPI(LCD.buffer[LCD.value]))
- #endif
- ISR (SPI_STC_vect)
- {
- switch (spi_counter)
- {
- case 0:
- {
- write_LCD;
- ++LCD.value;
- ++spi_counter;
- if (LCD.value == sizeof(LCD_ctx))
- {
- LCD.value = 0;
- }
- break;
- }
- case 1:
- {
- spi_counter = 0;
- }
- }
- }
- ISR (TIMER2_COMP_vect)
- {
- ++clock;
- if (clock == 230)
- {
- LCD.buffer[3] ^= (1 << 7);
- clock = 0;
- }
- if (spi_counter == 0)
- {
- PORTB &= ~(1 << PB0);
- PORTB |= (1 << PB0);
- shift_LCD;
- };
- }
- uint8_t read_ADC(uint8_t mux)
- {
- ADMUX= (ADMUX & 0xE0) | mux;
- asm("nop;");
- asm("nop;");
- asm("nop;");
- ADCSRA |= (1 << ADSC);
- loop_until_bit_is_set(ADCSRA,ADIF);
- return ADCH;
- };
- typedef struct
- {
- uint8_t H;
- uint8_t L;
- } t_divmod;
- inline t_divmod divmod (uint8_t _a, uint8_t _b) // a делим на b
- {
- uint8_t high = 0;
- while( _a >= _b)
- {
- ++high;
- _a -= _b;
- };
- t_divmod divm = {high, _a};
- return divm;
- };
- void LCD_update(uint8_t start_byte,uint8_t data)
- {
- t_divmod div = divmod(data, 100);
- uint8_t _a = div.H;
- div = divmod(div.L, 10);
- // uint8_t H1 = data / 100;
- // uint8_t HL = data % 100;
- // uint8_t H2 = HL / 10;
- // uint8_t L = HL % 10;
- ATOMIC_BLOCK(ATOMIC_FORCEON)
- {
- uint8_t ldp = (LCD.buffer[3] >> 7) & 1;
- LCD.buffer[0] = start_byte;
- LCD.buffer[1] = led_table[_a];
- LCD.buffer[2] = led_table[div.H];
- LCD.buffer[3] = ldp ? led_table[div.L] | dp : led_table[div.L] & ~dp ;
- LCD.value = 0;
- };
- }
- int main(void)
- {
- SPCR=0b11010000 ;//настройка spi
- DDRB |= (1 << PB1) | (1 << PB0) | (1 << PB5) | (1 << PB3) | (1 << PB2);
- PORTB = 0x00;
- TCCR2 = (1 << WGM21); //таймер 2 обновляет данные на индикаторе
- TIMSK = (1 << OCIE2);
- ADMUX |= (1 << REFS0) | (1 << REFS1) | (1 << ADLAR);
- ADCSRA |= (1 << ADEN) | (1 << ADPS0) | (1 << ADPS1) | (1 << ADPS2) ;// (1 << ADIE) | (1 << ADFR) для прерывания
- OCR2 = 13;
- START_TIMER2;
- sei();
- while(1)
- {
- uint8_t t = read_ADC(0);
- LCD_update(LCD_f,t);
- _delay_ms(80);
- }
- return 1;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement