Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * ATMega8_Uhr_mit_Schieberegistern.c
- *
- * Created: 12.04.2013 11:50:12
- * Author: Alexander Brysch
- */
- #define F_CPU 16000000UL
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #include <util/delay.h>
- #define SHIFT_REGISTER DDRB
- #define SHIFT_PORT PORTB
- #define DATA (1<<PB3) // MOSI (SI)
- #define LATCH (1<<PB2) // SS (RCK)
- #define CLOCK (1<<PB5) // SCK (SCK)
- #define INITLED (1<<PB1)
- unsigned int SekundenEiner = 0;
- unsigned int SekundenZehner = 0;
- unsigned int MinutenEiner = 0;
- unsigned int MinutenZehner = 0;
- unsigned int StundenEiner = 0;
- unsigned int StundenZehner = 0;
- unsigned int LEDON = 0;
- unsigned int DOT = 0;
- unsigned int x=0;
- void init_IO(void)
- {
- SHIFT_REGISTER |= (DATA | LATCH | CLOCK | INITLED); // SET Control Pins as Output
- DDRD |= (1<<PD2);
- SHIFT_PORT &= ~(DATA | LATCH | CLOCK | INITLED); // SET Control Pins low
- }
- void init_SPI(void)
- {
- SPCR = (1<<SPE) | (1<<MSTR); // Start SPI as Master
- }
- void spi_send(unsigned char data0, unsigned char data1, unsigned char data2, unsigned char data3, unsigned char data4)
- {
- SPDR = data0;
- while(!(SPSR & (1<<SPIF))); //Wait for SPI process to finish
- SPDR = data1;
- while(!(SPSR & (1<<SPIF))); //Wait for SPI process to finish
- SPDR = data2;
- while(!(SPSR & (1<<SPIF))); //Wait for SPI process to finish
- SPDR = data3;
- while(!(SPSR & (1<<SPIF))); //Wait for SPI process to finish
- SPDR = data4;
- while(!(SPSR & (1<<SPIF))); //Wait for SPI process to finish
- SHIFT_PORT |= LATCH;
- SHIFT_PORT &= ~LATCH;
- }
- void reset_Shift()
- {
- spi_send(0b00000000, 0b00000000, 0b00000000, 0b00000000, 0b00000000);
- SHIFT_PORT |= LATCH;
- SHIFT_PORT &= ~LATCH;
- }
- uint8_t timer_start()
- {
- TIMSK = (1<<OCIE1A);
- sei(); // Enable Interupts
- return 0;
- }
- void timer_stop()
- {
- TIMSK = (0<<OCIE1A); // Disable Timer 0
- cli(); // Disable Interupts
- }
- ISR(TIMER1_COMPA_vect)
- {
- if(DOT == 0)
- {
- DOT = 1;
- SekundenEiner++;
- LEDON=1;
- PORTB |= INITLED; // Turn LED ON
- }else{
- DOT = 0;
- }
- }
- // INTERRUPT
- ISR(INT0_vect)
- {
- timer_stop();
- SekundenEiner = 0;
- SekundenZehner = 0;
- MinutenEiner = 0;
- MinutenZehner = 0;
- StundenEiner = 0;
- StundenZehner = 0;
- timer_start();
- GIFR |= (1<<INTF0);
- }
- int main(void)
- {
- init_IO();
- init_SPI();
- reset_Shift();
- _delay_ms(500);
- TCCR1B |= (1 << WGM12); //CTC Modus
- TCCR1B |= (1 << CS12) | (0 << CS11) | (0 << CS10); //Prescaler Clock / 256
- //OCR1A = 61250; // 1000 MS //Compare Wert setzen
- OCR1A = 30625; // 500 MS
- // Interrupt Start
- GIMSK |= (1<<INT0);
- //PCMSK |= (1<<INT0);
- MCUCR |= (1<< ISC01) | (1<<ISC00);
- timer_start();
- // Interrupt Stop
- char Segment_Zahlen[10] =
- { // DPgfedcba
- // 0b 00000001,
- 0b11000000, // 0 a, b, c, d, e, f
- 0b11111001, // 1 b,c
- 0b10100100, // 2 a, b, d e, g
- 0b10110000, // 3 a, b,c, d g,
- 0b10011001, // 4 , b,cf g,
- 0b10010010, // 5 a,c, d f, g,
- 0b10000010, // 6 acdefg
- 0b11111000, // 7 abc
- 0b10000000, // 8 abcdefg
- 0b10010000 // 9 abcdfg
- };
- char Segment_DOTZahlen[10] =
- { // DPgfedcba
- // 0b 00000001,
- 0b01000000, // 0 a, b, c, d, e, f
- 0b01111001, // 1 b,c
- 0b00100100, // 2 a, b, d e, g
- 0b00110000, // 3 a, b,c, d g,
- 0b00011001, // 4 , b,cf g,
- 0b00010010, // 5 a,c, d f, g,
- 0b00000010, // 6 acdefg
- 0b01111000, // 7 abc
- 0b00000000, // 8 abcdefg
- 0b00010000 // 9 abcdfg
- };
- char Segment_Masse[4] = {
- 0b00010000,
- 0b00100000,
- 0b01000000,
- 0b10000000
- };
- char Matrix_Zahlen[10][5] =
- {
- { 0b01110000, 0b01010000, 0b01010000, 0b01010000, 0b01110000 }, // 0
- { 0b00100000, 0b01100000, 0b00100000, 0b00100000, 0b01110000 }, // 1
- { 0b01110000, 0b00010000, 0b01110000, 0b01000000, 0b01110000 }, // 2
- { 0b01110000, 0b00010000, 0b01110000, 0b00010000, 0b01110000 }, // 3
- { 0b01010000, 0b01010000, 0b01110000, 0b00010000, 0b00010000 }, // 4
- { 0b01110000, 0b01000000, 0b01110000, 0b00010000, 0b01110000 }, // 5
- { 0b01110000, 0b01000000, 0b01110000, 0b01010000, 0b01110000 }, // 6
- { 0b01110000, 0b00010000, 0b00010000, 0b00010000, 0b00010000 }, // 7
- { 0b01110000, 0b01010000, 0b01110000, 0b01010000, 0b01110000 }, // 8
- { 0b01110000, 0b01010000, 0b01110000, 0b00010000, 0b01110000 } // 9
- };
- char Matrix_Masse[5] = {
- 0b11110000, // Zeile 1
- 0b11101000, // Zeile 2
- 0b11011000, // Zeile 3
- 0b10111000, // Zeile 4
- 0b01111000 // Zeile 5
- };
- while(1)
- {
- if(LEDON==1) { PORTB &= ~INITLED; }
- if (SekundenEiner>9) { SekundenEiner = 0; SekundenZehner++; }
- if (SekundenZehner > 5) { SekundenZehner = 0; MinutenEiner++; }
- if (MinutenEiner>9) { MinutenEiner=0; MinutenZehner++; }
- if (MinutenZehner>5) { StundenEiner++; MinutenZehner=0; }
- if (StundenEiner>9) { StundenZehner++; StundenEiner=0; }
- if (StundenZehner>9) { StundenZehner=0; }
- SHIFT_PORT &= ~LATCH;
- for (x=0; x<5; x++)
- {
- spi_send(Matrix_Masse[x], Matrix_Zahlen[SekundenEiner][x], Matrix_Zahlen[SekundenZehner][x], 0b00000000, 0b00000000);
- }
- spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[0], Segment_Zahlen[MinutenEiner]);
- spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[1], Segment_Zahlen[MinutenZehner]);
- if(DOT == 1)
- {
- spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[2], Segment_DOTZahlen[StundenEiner]);
- }else{
- spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[2], Segment_Zahlen[StundenEiner]);
- }
- spi_send(0b00000000, 0b00000000, 0b00000000, Segment_Masse[3], Segment_Zahlen[StundenZehner]);
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment