Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/interrupt.h>
- #define F_CPU 16000000UL
- #include <util/delay.h>
- #include <stdio.h>
- const char Segment_Data[ 10 ] = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x67 };
- unsigned char Display_Num[ 4 ] = { 0, 0, 0, 0 }; // Seven segment 4자리 숫자 출력 버퍼
- unsigned char Digit_Num = 0;
- unsigned char Begin = 1;
- unsigned char Direction = 0;
- unsigned long Gap = 0;
- long Count = 5000;
- void Init_Devices( void );
- void Init_Port( void );
- void Init_Timer0( void );
- void Init_Timer2( void );
- void Init_Ext( void );
- void Display( int Num );
- void Control( int Command, int Start );
- int main( void )
- {
- Init_Devices();
- while( 1 )
- {
- Control( 1000, 0x10 );
- }
- return 0;
- }
- ISR( TIMER0_OVF_vect )
- {
- Digit_Num++;
- Digit_Num = Digit_Num % 4;
- PORTA = Segment_Data[ Display_Num[ Digit_Num ] ];
- PORTC = ~( 1 << Digit_Num );
- PORTB |= ( 1 << PORTB0 );
- TCNT0 = 0xE7;
- }
- ISR( TIMER2_OVF_vect )
- {
- if( !Direction )
- PORTB |= ( 1 << PORTB7 );
- else
- PORTB |= ( 1 << PORTB6 );
- }
- ISR( TIMER2_COMP_vect )
- {
- PORTB &= ~( ( 1 << PORTB7 ) | ( 1 << PORTB6 ) );
- }
- ISR( INT0_vect )
- {
- Count++;
- }
- ISR( INT1_vect )
- {
- Count--;
- }
- //call this routine to initialize all peripherals
- void Init_Devices( void )
- {
- //stop errant interrupts until set up
- cli(); //disable all interrupts
- XMCRA = 0x00; //external memory
- XMCRB = 0x00; //external memory
- Init_Port();
- Init_Timer0();
- Init_Timer2();
- Init_Ext();
- MCUCR = 0x00;
- // EICRA = 0x00; //extended ext ints
- // EICRB = 0x00; //extended ext ints
- // EIMSK = 0x00;
- // TIMSK = 0x00; //timer interrupt sources
- ETIMSK = 0x00; //extended timer interrupt sources
- sei(); //re-enable interrupt
- //all peripherals are now initialized
- }
- void Init_Port( void )
- {
- PORTA = 0x00;
- DDRA |= ( 1 << DDA7 ) | ( 1 << DDA6 ) | ( 1 << DDA5 ) | ( 1 << DDA4 ) | ( 1 << DDA3 ) | ( 1 << DDA2 ) | ( 1 << DDA1 ) | ( 1 << DDA0 );
- PORTB = 0x00;
- DDRB |= ( 1 << DDB7 ) | ( 1 << DDB6 );
- PORTC = 0x00; //m103 output only
- DDRC |= ( 1 << DDC3 ) | ( 1 << DDC2 ) | ( 1 << DDC1 ) | ( 1 << DDC0 );
- PORTD = 0x00;
- DDRD &= ~( ( 1 << DDD3 ) | ( 1 << DDD2 ) | ( 1 << DDD1 ) | ( 1 << DDD0 ) );
- PORTE = 0x00;
- DDRE = 0x00;
- PORTF = 0x00;
- DDRF = 0x00;
- PORTG = 0x00;
- DDRG |= ( 1 << DDG1 ) | ( 1 << DDG0 );
- DDRG &= ~( ( 1 << DDG3 ) | ( 1 << DDG2 ) );
- }
- //TIMER0 initialize - prescale:64
- // WGM: Normal
- // desiired value: 100uSec
- // actual value: 100.000usec (0.0%)
- void Init_Timer0( void )
- {
- TCCR0 = 0x00;
- // ASSR = 0x00;
- TCNT0 = 0xE7;
- // OCR0 = 0xE7;
- TCCR0 |= ( 1 << CS02 );
- TIMSK |= ( 1 << TOIE0 );
- }
- void Init_Timer2( void )
- {
- TCCR2 = 0x00; //stop
- // ASSR = 0x00; //set async mode
- // TCNT2 = 0x06; //set count
- OCR2 = 0x00;
- TCCR2 |= ( 1 << WGM20 );
- TCCR2 |= ( 1 << CS22 ); //start timer
- TIMSK |= ( 1 << OCIE2 ) | ( 1 << TOIE2 );
- }
- void Init_Ext( void )
- {
- EICRA |= ( 1 << ISC10 ) | ( 1 << ISC00 );
- EICRB = 0;
- EIMSK |= ( 1 << INT1 ) | ( 1 << INT0 );
- }
- void Display( int Num )
- {
- Display_Num[ 0 ] = ( Num % 10000 ) / 1000;
- Display_Num[ 1 ] = ( Num % 1000 ) / 100;
- Display_Num[ 2 ] = ( Num % 100 ) / 10;
- Display_Num[ 3 ] = ( Num % 10 );
- }
- void Control( int Command, int Start )
- {
- if( Begin )
- {
- OCR2 = Start;
- Begin = 0;
- }
- if( Command > Count )
- {
- Gap = Command - Count;
- Direction = 0;
- }
- if( Command < Count )
- {
- Gap = Count - Command;
- Direction = 1;
- }
- if( Gap > 3000 )
- OCR2 = 127;
- else if( Gap < 2000 )
- OCR2 = 100;
- else if( Gap < 1000 )
- OCR2 = 50;
- else if( Gap < 100 )
- OCR2 = 40;
- else if( Gap < 10 )
- OCR2 = 1;
- Display( Count );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement