Guest User

AVR Segment + DotMatrix with 74HC595

a guest
Apr 12th, 2013
166
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.75 KB | None | 0 0
  1. /*
  2.  * ATMega8_Uhr_mit_Schieberegistern.c
  3.  *
  4.  * Created: 12.04.2013 11:50:12
  5.  *  Author: Alexander Brysch
  6.  */
  7.  
  8. #define F_CPU 16000000UL
  9. #include <avr/io.h>
  10. #include <avr/interrupt.h>
  11. #include <util/delay.h>
  12.  
  13. #define SHIFT_REGISTER DDRB
  14. #define SHIFT_PORT PORTB
  15. #define DATA  (1<<PB3)     // MOSI (SI)
  16. #define LATCH (1<<PB2)     // SS   (RCK)
  17. #define CLOCK (1<<PB5)     // SCK  (SCK)
  18. #define INITLED (1<<PB1)
  19.  
  20. unsigned int SekundenEiner = 0;
  21. unsigned int SekundenZehner = 0;
  22. unsigned int MinutenEiner = 0;
  23. unsigned int MinutenZehner = 0;
  24. unsigned int StundenEiner = 0;
  25. unsigned int StundenZehner = 0;
  26.  
  27. unsigned int LEDON = 0;
  28. unsigned int DOT = 0;
  29. unsigned int x=0;
  30.  
  31.  
  32. void init_IO(void)
  33. {
  34.   SHIFT_REGISTER |= (DATA | LATCH | CLOCK | INITLED);   // SET Control Pins as Output
  35.   DDRD |= (1<<PD2);
  36.   SHIFT_PORT &= ~(DATA | LATCH | CLOCK | INITLED);      // SET Control Pins low
  37. }
  38.  
  39. void init_SPI(void)
  40. {
  41.   SPCR = (1<<SPE) | (1<<MSTR);                          // Start SPI as Master  
  42. }
  43.  
  44. void spi_send(unsigned char data0, unsigned char data1, unsigned char data2, unsigned char data3, unsigned char data4)
  45. {
  46.   SPDR = data0;
  47.   while(!(SPSR & (1<<SPIF)));   //Wait for SPI process to finish
  48.   SPDR = data1;
  49.   while(!(SPSR & (1<<SPIF)));   //Wait for SPI process to finish
  50.   SPDR = data2;
  51.   while(!(SPSR & (1<<SPIF)));   //Wait for SPI process to finish
  52.   SPDR = data3;
  53.   while(!(SPSR & (1<<SPIF)));   //Wait for SPI process to finish
  54.   SPDR = data4;
  55.   while(!(SPSR & (1<<SPIF)));   //Wait for SPI process to finish
  56.   SHIFT_PORT |= LATCH;
  57.   SHIFT_PORT &= ~LATCH;
  58. }
  59.  
  60. void reset_Shift()
  61. {
  62.   spi_send(0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000);
  63.   SHIFT_PORT |= LATCH;
  64.   SHIFT_PORT &= ~LATCH;
  65. }
  66.  
  67. uint8_t timer_start()
  68. {
  69.   TIMSK = (1<<OCIE1A);
  70.   sei();  // Enable Interupts
  71.   return 0;
  72. }
  73.  
  74. void timer_stop()
  75. {
  76.   TIMSK = (0<<OCIE1A); // Disable Timer 0
  77.   cli(); // Disable Interupts
  78. }
  79.  
  80. ISR(TIMER1_COMPA_vect)
  81. {
  82.   if(DOT == 0)
  83.   {
  84.     DOT = 1;
  85.     SekundenEiner++;
  86.     LEDON=1;
  87.     PORTB |= INITLED; // Turn LED ON
  88.   }else{
  89.     DOT = 0;
  90.   }
  91. }
  92.  
  93. // INTERRUPT
  94. ISR(INT0_vect)
  95. {
  96.   timer_stop();
  97.   SekundenEiner = 0;
  98.   SekundenZehner = 0;
  99.   MinutenEiner = 0;
  100.   MinutenZehner = 0;
  101.   StundenEiner = 0;
  102.   StundenZehner = 0;
  103.   timer_start();
  104.   GIFR |= (1<<INTF0);
  105.  
  106. }
  107.  
  108. int main(void)
  109. {
  110.  
  111.   init_IO();
  112.   init_SPI();
  113.   reset_Shift();
  114.   _delay_ms(500);
  115.  
  116.   TCCR1B |= (1 << WGM12);                //CTC Modus
  117.   TCCR1B |= (1 << CS12) | (0 << CS11) | (0 << CS10);   //Prescaler Clock / 256
  118.   //OCR1A = 61250; // 1000 MS                     //Compare Wert setzen
  119.   OCR1A = 30625; // 500 MS
  120.  
  121.  
  122.   // Interrupt Start
  123.   GIMSK |= (1<<INT0);
  124.   //PCMSK |= (1<<INT0);
  125.   MCUCR |= (1<< ISC01) | (1<<ISC00);
  126.   timer_start();
  127.   // Interrupt Stop
  128.  
  129.   char Segment_Zahlen[10] =
  130.   { //   DPgfedcba
  131.     // 0b 00000001,
  132.     0b11000000, // 0 a, b, c, d, e, f
  133.     0b11111001, // 1 b,c
  134.     0b10100100, // 2 a, b, d e,  g
  135.     0b10110000, // 3 a, b,c, d g,
  136.     0b10011001, // 4 , b,cf g,
  137.     0b10010010, // 5 a,c, d f, g,
  138.     0b10000010, // 6 acdefg
  139.     0b11111000, // 7 abc
  140.     0b10000000, // 8 abcdefg
  141.     0b10010000 // 9 abcdfg
  142.   };
  143.   char Segment_DOTZahlen[10] =
  144.   { //   DPgfedcba
  145.     // 0b 00000001,
  146.     0b01000000, // 0 a, b, c, d, e, f
  147.     0b01111001, // 1 b,c
  148.     0b00100100, // 2 a, b, d e,  g
  149.     0b00110000, // 3 a, b,c, d g,
  150.     0b00011001, // 4 , b,cf g,
  151.     0b00010010, // 5 a,c, d f, g,
  152.     0b00000010, // 6 acdefg
  153.     0b01111000, // 7 abc
  154.     0b00000000, // 8 abcdefg
  155.     0b00010000 // 9 abcdfg
  156.   };
  157.  
  158.   char Segment_Masse[4] = {
  159.     0b00010000,
  160.     0b00100000,
  161.     0b01000000,
  162.     0b10000000
  163.   };
  164.   char Matrix_Zahlen[10][5] =
  165.   {
  166.     { 0b01110000, 0b01010000, 0b01010000, 0b01010000, 0b01110000 }, // 0
  167.     { 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b01110000 }, // 1
  168.     { 0b01110000, 0b00010000, 0b01110000, 0b01000000, 0b01110000 }, // 2
  169.     { 0b01110000, 0b00010000, 0b01110000, 0b00010000, 0b01110000 }, // 3
  170.     { 0b01010000, 0b01010000, 0b01110000, 0b00010000, 0b00010000 }, // 4
  171.     { 0b01110000, 0b01000000, 0b01110000, 0b00010000, 0b01110000 }, // 5
  172.     { 0b01110000, 0b01000000, 0b01110000, 0b01010000, 0b01110000 }, // 6
  173.     { 0b01110000, 0b00010000, 0b00010000, 0b00010000, 0b00010000 }, // 7
  174.     { 0b01110000, 0b01010000, 0b01110000, 0b01010000, 0b01110000 }, // 8
  175.     { 0b01110000, 0b01010000, 0b01110000, 0b00010000, 0b01110000 } // 9
  176.   };
  177.  
  178.   char Matrix_Masse[5] = {
  179.     0b11110000, // Zeile 1
  180.     0b11101000, // Zeile 2
  181.     0b11011000, // Zeile 3
  182.     0b10111000, // Zeile 4
  183.     0b01111000  // Zeile 5
  184.   };
  185.   while(1)
  186.   {
  187.     if(LEDON==1) { PORTB &= ~INITLED; }
  188.     if (SekundenEiner>9) { SekundenEiner = 0; SekundenZehner++; }
  189.     if (SekundenZehner > 5) { SekundenZehner = 0; MinutenEiner++; }
  190.     if (MinutenEiner>9) { MinutenEiner=0; MinutenZehner++; }
  191.     if (MinutenZehner>5) { StundenEiner++; MinutenZehner=0; }
  192.     if (StundenEiner>9) { StundenZehner++; StundenEiner=0; }
  193.     if (StundenZehner>9) { StundenZehner=0; }
  194.     SHIFT_PORT &= ~LATCH;
  195.     for (x=0; x<5; x++)
  196.     {
  197.       spi_send(Matrix_Masse[x], Matrix_Zahlen[SekundenEiner][x], Matrix_Zahlen[SekundenZehner][x], 0b00000000, 0b00000000);
  198.     }    
  199.     spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[0], Segment_Zahlen[MinutenEiner]);
  200.     spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[1], Segment_Zahlen[MinutenZehner]);
  201.     if(DOT == 1)
  202.     {
  203.       spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[2], Segment_DOTZahlen[StundenEiner]);
  204.     }else{
  205.       spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[2], Segment_Zahlen[StundenEiner]);
  206.     }
  207.     spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[3], Segment_Zahlen[StundenZehner]);
  208.   }
  209.   return 0;
  210. }
Advertisement
Add Comment
Please, Sign In to add comment