Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- // Макрос управления защелкой
- #define registerLatch(code) { PORTD &= ~(1 << 6); code; PORTD |= 1 << 6; }
- // Данные о каждой цифре
- const uint8_t digits[] = {
- 0b11000000,
- 0b11111001,
- 0b10100100,
- 0b10110000,
- 0b10011001,
- 0b10010010,
- 0b10000010,
- 0b11111000,
- 0b10000000,
- 0b10010000,
- 0b11111111
- };
- // Данные о группах светодиодов (16бит)
- const uint16_t groups[] = {
- 0b0000010000000000, // DIGIT 1 (0)
- 0b0000001000000000, // DIGIT 2 (1)
- 0b0000000100000000, // DIGIT 3 (2)
- /* ------------------------------ */
- 0b0000000010000000, // DIGIT 1 (3)
- 0b0000000001000000, // DIGIT 2 (4)
- 0b0000000000100000, // DIGIT 3 (5)
- 0b0000000000010000, // DIGIT 4 (6)
- 0b0000000000001000, // DIGIT 5 (7)
- 0b0000000000000100, // DIGIT 6 (8)
- /* ------------------------------ */
- 0b0010000000000000, // SCALE 1 (9)
- 0b0001000000000000, // SCALE 2 (10)
- 0b0000100000000000, // SCALE 3 (11)
- 0b0000000000000010, // SCALE 4 (12)
- 0b0000000000000001, // SCALE 5 (13)
- };
- uint8_t NC = 0b11111111;
- // Временный массив для теста индикации
- uint8_t disp[] = {
- digits[1], digits[2], digits[0], digits[3], (digits[2] & ~(1 << 7)), digits[7], digits[6],
- digits[8], NC, 0b00000000, 0b00000000, 0b11100000, NC, NC
- };
- unsigned char counter = 0;
- // Отправка данных по SPI
- void shiftOut2(uint8_t data) {
- for (uint8_t i = 0; i < 8; i++) {
- byte value = !!(data & (1 << (7 - i)));
- if (value) PORTD |= 1 << 5; else PORTD &= ~(1 << 5);
- PORTD |= 1 << 7;
- PORTD &= ~(1 << 7);
- }
- }
- // Запись в регистры
- void registerWrite(uint16_t groups, uint8_t segments) {
- registerLatch({
- shiftOut2(groups >> 8);
- shiftOut2((groups) & 0xFF);
- shiftOut2(segments);
- });
- }
- // Описание таймера
- ISR (TIMER0_OVF_vect) {
- TCNT0 = 0x00;
- counter = (counter + 1) % 13;
- registerWrite(groups[counter], disp[counter]);
- for (int i = 0; i < 1000; i++) { asm("NOP"); }
- }
- // Основная программа
- int main () {
- // SPI - выводы для регистров
- DDRD = 0b11100000;
- // Инициализация таймера
- TIMSK = (1 << TOIE0);
- TCCR0 = (0 << CS00) | (1 << CS01) | (0 << CS02);
- asm("SEI");
- // Вечный цикл
- while (1) {
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment