Advertisement
lunaticju

속도제어

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