Advertisement
lunaticju

위치제어

Dec 20th, 2015
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.66 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #define F_CPU 16000000UL
  4. #include <util/delay.h>
  5. #include <stdio.h>
  6.  
  7. const char Segment_Data[ 10 ]   = { 0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x27, 0x7F, 0x67 };
  8. unsigned char Display_Num[ 4 ]  = { 0, 0, 0, 0 };   // Seven segment 4자리 숫자 출력 버퍼
  9. unsigned char Digit_Num         = 0;
  10. unsigned char Begin             = 1;
  11. unsigned char Direction         = 0;
  12. unsigned long Gap               = 0;
  13. long Count                      = 5000;
  14.  
  15.  
  16. void Init_Devices( void );
  17. void Init_Port( void );
  18. void Init_Timer0( void );
  19. void Init_Timer2( void );
  20. void Init_Ext( void );
  21. void Display( int Num );
  22. void Control( int Command, int Start );
  23.  
  24. int main( void )
  25. {
  26.     Init_Devices();
  27.     while( 1 )
  28.     {
  29.         Control( 1000, 0x10 );
  30.     }
  31.     return 0;
  32. }
  33.  
  34.  
  35. ISR( TIMER0_OVF_vect )
  36. {
  37.     Digit_Num++;
  38.     Digit_Num   = Digit_Num % 4;
  39.     PORTA       = Segment_Data[ Display_Num[ Digit_Num ] ];
  40.     PORTC       = ~( 1 << Digit_Num );
  41.     PORTB       |= ( 1 << PORTB0 );
  42.  
  43.     TCNT0       = 0xE7;
  44. }
  45.  
  46. ISR( TIMER2_OVF_vect )
  47. {
  48.     if( !Direction )
  49.     PORTB   |= ( 1 << PORTB7 );
  50.     else
  51.     PORTB   |= ( 1 << PORTB6 );
  52. }
  53.  
  54. ISR( TIMER2_COMP_vect )
  55. {
  56.     PORTB   &= ~( ( 1 << PORTB7 ) | ( 1 << PORTB6 ) );
  57. }
  58.  
  59. ISR( INT0_vect )
  60. {
  61.     Count++;
  62. }
  63.  
  64. ISR( INT1_vect )
  65. {
  66.     Count--;
  67. }
  68.  
  69. //call this routine to initialize all peripherals
  70. void Init_Devices( void )
  71. {
  72.     //stop errant interrupts until set up
  73.     cli(); //disable all interrupts
  74.     XMCRA   = 0x00; //external memory
  75.     XMCRB   = 0x00; //external memory
  76.     Init_Port();
  77.     Init_Timer0();
  78.     Init_Timer2();
  79.     Init_Ext();
  80.    
  81.     MCUCR   = 0x00;
  82.     // EICRA    = 0x00; //extended ext ints
  83.     // EICRB    = 0x00; //extended ext ints
  84.     // EIMSK    = 0x00;
  85.     // TIMSK    = 0x00; //timer interrupt sources
  86.     ETIMSK  = 0x00; //extended timer interrupt sources
  87.     sei(); //re-enable interrupt
  88.     //all peripherals are now initialized
  89. }
  90.  
  91. void Init_Port( void )
  92. {
  93.     PORTA   = 0x00;
  94.     DDRA    |= ( 1 << DDA7 ) | ( 1 << DDA6 ) | ( 1 << DDA5 ) | ( 1 << DDA4 ) | ( 1 << DDA3 ) | ( 1 << DDA2 ) | ( 1 << DDA1 ) | ( 1 << DDA0 );
  95.     PORTB   = 0x00;
  96.     DDRB    |= ( 1 << DDB7 ) | ( 1 << DDB6 );
  97.     PORTC   = 0x00; //m103 output only
  98.     DDRC    |= ( 1 << DDC3 ) | ( 1 << DDC2 ) | ( 1 << DDC1 ) | ( 1 << DDC0 );
  99.     PORTD   = 0x00;
  100.     DDRD    &= ~( ( 1 << DDD3 ) | ( 1 << DDD2 ) | ( 1 << DDD1 ) | ( 1 << DDD0 ) );
  101.     PORTE   = 0x00;
  102.     DDRE    = 0x00;
  103.     PORTF   = 0x00;
  104.     DDRF    = 0x00;
  105.     PORTG   = 0x00;
  106.     DDRG    |= ( 1 << DDG1 ) | ( 1 << DDG0 );
  107.     DDRG    &= ~( ( 1 << DDG3 ) | ( 1 << DDG2 ) );
  108. }
  109.  
  110. //TIMER0 initialize - prescale:64
  111. // WGM: Normal
  112. // desiired value: 100uSec
  113. // actual value: 100.000usec (0.0%)
  114. void Init_Timer0( void )
  115. {
  116.     TCCR0   = 0x00;
  117.     // ASSR = 0x00;
  118.     TCNT0   = 0xE7;
  119.     // OCR0 = 0xE7;
  120.     TCCR0   |= ( 1 << CS02 );
  121.     TIMSK   |= ( 1 << TOIE0 );
  122. }
  123.  
  124. void Init_Timer2( void )
  125. {
  126.     TCCR2   = 0x00; //stop
  127.     // ASSR = 0x00; //set async mode
  128.     // TCNT2    = 0x06; //set count
  129.     OCR2    = 0x00;
  130.     TCCR2   |= ( 1 << WGM20 );
  131.     TCCR2   |= ( 1 << CS22 ); //start timer
  132.     TIMSK   |= ( 1 << OCIE2 ) | ( 1 << TOIE2 );
  133. }
  134.  
  135. void Init_Ext( void )
  136. {
  137.     EICRA   |= ( 1 << ISC10 ) | ( 1 << ISC00 );
  138.     EICRB   = 0;
  139.     EIMSK   |= ( 1 << INT1 ) | ( 1 << INT0 );
  140. }
  141.  
  142. void Display( int Num )
  143. {
  144.     Display_Num[ 0 ]    = ( Num % 10000 ) / 1000;
  145.     Display_Num[ 1 ]    = ( Num % 1000 ) / 100;
  146.     Display_Num[ 2 ]    = ( Num % 100 ) / 10;
  147.     Display_Num[ 3 ]    = ( Num % 10 );
  148. }
  149.  
  150. void Control( int Command, int Start )
  151. {
  152.     if( Begin )
  153.     {
  154.         OCR2    = Start;
  155.         Begin   = 0;
  156.     }
  157.     if( Command > Count )
  158.     {
  159.         Gap     = Command - Count;
  160.         Direction = 0;
  161.     }
  162.  
  163.     if( Command < Count )
  164.     {
  165.         Gap     = Count - Command;
  166.         Direction = 1;
  167.     }
  168.     if( Gap > 3000 )
  169.         OCR2 = 127;
  170.     else if( Gap < 2000 )
  171.         OCR2 = 100;
  172.     else if( Gap < 1000 )
  173.         OCR2 = 50;
  174.     else if( Gap < 100 )
  175.         OCR2 = 40;
  176.     else if( Gap < 10 )
  177.         OCR2 = 1;
  178.     Display( Count );
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement