Advertisement
uwezi

labb_04_code_01.c

Nov 19th, 2019
411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. /*
  2. ATmega8, 48, 88, 168, 328
  3.  
  4. /Reset PC6|1   28|PC5
  5.  seg_A PD0|2   27|PC4
  6.  seg_B PD1|3   26|PC3
  7.  seg_C PD2|4   25|PC2
  8.  seg_D PD3|5   24|PC1
  9.  seg_E 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.  seg_F PD5|11  18|PB4 MISO  Anode_2
  15.  seg_G PD6|12  17|PB3 MOSI  Anode_3
  16.  dot   PD7|13  16|PB2       Anode_4
  17.        PB0|14  15|PB1
  18. */
  19.  
  20. #define F_CPU 1000000UL
  21. #include <avr/io.h>
  22. #include <util/delay.h>
  23. #include <avr/interrupt.h>
  24.  
  25. /*     _   _
  26.       |  A  |
  27.       F     B
  28.       |- G -|
  29.       E     C
  30.       |_ D _| .H  
  31. */
  32.  
  33. uint8_t font[] =
  34.   {// HGFEDCBA
  35.     0b11000000,     // 0
  36.     0b11111001,     // 1
  37.     0b10100100,     // 2
  38.     0b10110000,     // 3
  39.     0b10011001,     // 4
  40.     0b10010010,     // 5
  41.     0b10000010,     // 6
  42.     0b11111000,     // 7
  43.     0b10000000,     // 8
  44.     0b10010000,     // 9
  45.   };
  46.  
  47. volatile uint8_t framebuffer[4];
  48.  
  49. ISR(TIMER0_OVF_vect)
  50. {
  51.   static uint8_t digit;
  52.   PORTB &= 0b11000011;             // clear all digits
  53.   PORTB |= (0b00000100 << digit);  // set current digit
  54.   PORTD = framebuffer[digit];      // update segments
  55.   digit++;
  56.   digit = digit % 4;               // keep digit in [0:3]
  57. }
  58.  
  59. void init(void)
  60. {
  61.   DDRD = 0b?;   // segments
  62.   DDRB = 0b?;   // digits
  63.  
  64.   // we start with
  65.   //   normal operation
  66.   //   clk/1024 prescaler (about 1kHz)  
  67.   //   interrupt on Timer 0 overflow
  68.  
  69.   TCCR0A = (0 << COM0A1) | (0 << COM0A0) | (0 << COM0B1) | (0 << COM0B0)
  70.          | (? << WGM01)  | (? << WGM00);  
  71.   TCCR0B = (0 << WGM02)
  72.          | (? << CS02)   | (? << CS01)   | (? << CS00);
  73.   TIMSK0 = (0 << OCIE0B) | (0 << OCIE0A) | (? << TOIE0);
  74.  
  75.   // allow interrupts globally
  76.   sei();    
  77. }
  78.  
  79. int main(void)
  80. {
  81.   uint16_t count=0;
  82.   uint16_t dummy;
  83.   uint8_t  i;
  84.   init();
  85.   while(1)
  86.   {
  87.     dummy = count;
  88.     for (i=0; i<4; i++)                  
  89.     {
  90.       framebuffer[i] = font[dummy % 10];
  91.       dummy = dummy / 10;
  92.     }
  93.     count++;
  94.     if (count >= 10000)
  95.     {
  96.       count = 0;
  97.     }
  98.     _delay_ms(70);
  99.   }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement