Redfern_89

barno_ebanoe.c

Dec 11th, 2017
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.50 KB | None | 0 0
  1. #include <avr/io.h>
  2.  
  3. // Макрос управления защелкой
  4. #define registerLatch(code) { PORTD &= ~(1 << 6); code; PORTD |= 1 << 6; }
  5.  
  6. // Данные о каждой цифре
  7. const uint8_t digits[] = {
  8.   0b11000000,
  9.   0b11111001,
  10.   0b10100100,
  11.   0b10110000,
  12.   0b10011001,
  13.   0b10010010,
  14.   0b10000010,
  15.   0b11111000,
  16.   0b10000000,
  17.   0b10010000,
  18.   0b11111111
  19. };
  20.  
  21. // Данные о группах светодиодов (16бит)
  22. const uint16_t groups[] = {
  23.   0b0000010000000000, // DIGIT 1      (0)
  24.   0b0000001000000000, // DIGIT 2      (1)
  25.   0b0000000100000000, // DIGIT 3      (2)
  26. /* ------------------------------ */  
  27.   0b0000000010000000, // DIGIT 1      (3)
  28.   0b0000000001000000, // DIGIT 2      (4)
  29.   0b0000000000100000, // DIGIT 3      (5)
  30.   0b0000000000010000, // DIGIT 4      (6)
  31.   0b0000000000001000, // DIGIT 5      (7)
  32.   0b0000000000000100, // DIGIT 6      (8)
  33. /* ------------------------------ */
  34.   0b0010000000000000, // SCALE 1      (9)
  35.   0b0001000000000000, // SCALE 2      (10)
  36.   0b0000100000000000, // SCALE 3      (11)
  37.   0b0000000000000010, // SCALE 4      (12)
  38.   0b0000000000000001, // SCALE 5      (13)
  39. };
  40.  
  41. uint8_t NC = 0b11111111;
  42.  
  43.  
  44. // Временный массив для теста индикации
  45. uint8_t disp[] = {
  46.   digits[1], digits[2], digits[0], digits[3], (digits[2] & ~(1 << 7)), digits[7], digits[6],
  47.   digits[8], NC, 0b00000000, 0b00000000, 0b11100000, NC, NC
  48. };
  49.  
  50. unsigned char counter = 0;
  51.  
  52. // Отправка данных по SPI
  53. void shiftOut2(uint8_t data) {
  54.   for (uint8_t i = 0; i < 8; i++) {
  55.     byte value = !!(data & (1 << (7 - i)));
  56.     if (value) PORTD |= 1 << 5; else PORTD &= ~(1 << 5);
  57.     PORTD |= 1 << 7;
  58.     PORTD &= ~(1 << 7);
  59.   }  
  60. }
  61.  
  62. // Запись в регистры
  63. void registerWrite(uint16_t groups, uint8_t segments) {
  64.   registerLatch({
  65.     shiftOut2(groups >> 8);
  66.     shiftOut2((groups) & 0xFF);
  67.     shiftOut2(segments);
  68.   });
  69. }
  70.  
  71. // Описание таймера
  72. ISR (TIMER0_OVF_vect) {
  73.   TCNT0 = 0x00;
  74.   counter = (counter + 1) % 13;
  75.   registerWrite(groups[counter], disp[counter]);
  76.   for (int i = 0; i < 1000; i++) { asm("NOP"); }
  77. }
  78.  
  79. // Основная программа
  80. int main () {
  81.   // SPI - выводы для регистров
  82.   DDRD = 0b11100000;
  83.  
  84.   // Инициализация таймера
  85.   TIMSK = (1 << TOIE0);
  86.   TCCR0 = (0 << CS00) | (1 << CS01) | (0 << CS02);
  87.   asm("SEI");
  88.  
  89.   // Вечный цикл
  90.   while (1) {
  91.    
  92.   }
  93.  
  94.   return 0;
  95. }
Advertisement
Add Comment
Please, Sign In to add comment