uwezi

labb_04_code_02.c

Nov 19th, 2019
149
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. ATmega8, 48, 88, 168, 328
  3.  
  4. /Reset PC6|1   28|PC5
  5.  row_1 PD0|2   27|PC4
  6.  row_2 PD1|3   26|PC3
  7.  row_3 PD2|4   25|PC2
  8.  row_4 PD3|5   24|PC1
  9.  row_5 PD4|6   23|PC0
  10.        Vcc|7   22|Gnd
  11.        Gnd|8   21|Aref
  12.        PB6|9   20|AVcc
  13.        PB7|10  19|PB5 SCK   Anode_1
  14.  row_6 PD5|11  18|PB4 MISO  Anode_2
  15.  row_7 PD6|12  17|PB3 MOSI  Anode_3
  16.  row_8 PD7|13  16|PB2       Anode_4
  17.        PB0|14  15|PB1       Anode_4
  18. */
  19.  
  20. #define F_CPU 1000000UL
  21. #include <avr/io.h>
  22. #include <util/delay.h>
  23. #include <avr/interrupt.h>
  24.  
  25. uint8_t font[10][6]=
  26. {
  27.     {0x3E, 0x41, 0x49, 0x41, 0x3E, 0x00}, // 0x30 0
  28.     {0x00, 0x42, 0x7F, 0x40, 0x00, 0x00}, // 0x31 1
  29.     {0x42, 0x61, 0x51, 0x49, 0x46, 0x00}, // 0x32 2
  30.     {0x21, 0x41, 0x45, 0x4B, 0x31, 0x00}, // 0x33 3
  31.     {0x18, 0x14, 0x12, 0x7F, 0x10, 0x00}, // 0x34 4
  32.     {0x27, 0x45, 0x45, 0x45, 0x39, 0x00}, // 0x35 5
  33.     {0x3C, 0x4A, 0x49, 0x49, 0x30, 0x00}, // 0x36 6
  34.     {0x01, 0x71, 0x09, 0x05, 0x03, 0x00}, // 0x37 7
  35.     {0x36, 0x49, 0x49, 0x49, 0x36, 0x00}, // 0x38 8
  36.     {0x06, 0x49, 0x49, 0x29, 0x1E, 0x00}, // 0x39 9
  37. };
  38.  
  39. volatile uint8_t framebuffer[5];
  40.  
  41. ISR(TIMER0_OVF_vect)
  42. {
  43.   static uint8_t digit;
  44.   PORTB &= 0b11000001;             // clear all columns
  45.   PORTB |= (0b00000010 << digit);  // set current column
  46.   PORTD = framebuffer[digit];      // update rows
  47.   digit++;
  48.   digit = digit % 5;
  49. }
  50.  
  51. void init(void)
  52. {
  53.   DDRD = ?;   // rows
  54.   DDRB = ?;   // columns
  55.  
  56.   // we start with
  57.   //   normal operation
  58.   //   clk/1024 prescaler (about 1kHz)  
  59.   //   interrupt on Timer 0 overflow
  60.  
  61.   TCCR0A = (0 << COM0A1) | (0 << COM0A0) | (0 << COM0B1) | (0 << COM0B0)
  62.          | (? << WGM01)  | (? << WGM00);  
  63.   TCCR0B = (0 << WGM02)
  64.          | (? << CS02)   | (? << CS01)   | (? << CS00);
  65.   TIMSK0 = (0 << OCIE0B) | (0 << OCIE0A) | (? << TOIE0);
  66.  
  67.   // allow interrupts globally
  68.   sei();    
  69. }
  70.  
  71. int main(void)
  72. {
  73.   uint16_t count=0;
  74.   uint8_t  i;
  75.   init();
  76.   while(1)
  77.   {
  78.     for (i=0; i<5; i++)
  79.     {
  80.       framebuffer[i] = ~font[count][i];
  81.     }
  82.     count++;
  83.     if (count >= 10)
  84.     {
  85.       count = 0;
  86.     }
  87.     _delay_ms(1000);
  88.   }
  89. }
RAW Paste Data