Advertisement
patryk

BACKUP

Apr 25th, 2015
532
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.42 KB | None | 0 0
  1. #include <avr/io.h>
  2. #include <avr/delay.h>
  3. #include <avr/interrupt.h>
  4.  
  5. volatile unsigned int one_second_ready;
  6. volatile unsigned int overflow_ticks;
  7.  
  8. /*--------------------------INTERUPTS----------------------------*/
  9.  
  10. // TIMER 0 OVERFLOW INTERRUPT
  11. ISR (TIMER0_OVF_vect) {
  12.     overflow_ticks++;
  13. }
  14.  
  15.  
  16. // TIMER 1 COMPARE VALUE INTERRUPT
  17. ISR (TIMER1_COMPA_vect) {
  18.  
  19.     // T0 DISABLE
  20.     TCCR0 &= ~(1 << CS02) & ~(1 << CS01) & ~(1 << CS00);
  21.     TIMSK &= ~(1 << TOIE0);
  22.  
  23.     // T1 DISABLE
  24.     TCCR1B &= ~(1 << WGM12) & ~(1 << CS12) & ~(1 << CS10);
  25.     TIMSK &= ~(1 << OCIE1A);
  26.  
  27.     one_second_ready = 1;   // Send Signal To sensorMeasurement() Function
  28. }
  29.  
  30. /*---------------------------------------------------------------*/
  31.  
  32.  
  33. int sensorMeasurement() {
  34.     one_second_ready = 0;
  35.     overflow_ticks = 0;
  36.  
  37.     PORTC |= (1 << PC5) | (1 << PC4);   // Prescaler 100%
  38.     PORTC &= ~(1 << PC1);   // OUT Turned On
  39.  
  40. //  T0 INITIALIZE
  41.     TCCR0 |= (1 << CS02) | (1 << CS01) | (1 << CS00);   // External Clock T0 Rising Edge
  42.     TIMSK |= (1 << TOIE0);  // Enable T0 Interrupt
  43.  
  44. //   T1 INITIALIZE
  45.     TCCR1B |= (1 << WGM12) | (1 << CS12) | (1 << CS10);     // CTC MODE
  46.     OCR1A = F_CPU / 1024;       // 1 Second Delay
  47.     TIMSK |= (1 << OCIE1A);     // Enable T1 Interrupt
  48.  
  49.     sei();  // Enable Interrupts
  50.  
  51.     while (one_second_ready == 0);  // Wait until T1 is 1 second
  52.  
  53.     cli();  // Disable Interrupts
  54.  
  55.     PORTC |= (1 << PC1);
  56.  
  57.     return overflow_ticks * 256;    // Signal Frequency
  58. }
  59.  
  60. int checkRed() {
  61.     PORTC &= ~(1 << PC2);   // S2 - L
  62.     PORTC &= ~(1 << PC3);   // S3 - L
  63.  
  64.     return sensorMeasurement();
  65. }
  66.  
  67. int checkBlue() {
  68.     PORTC &= ~(1 << PC2);   // S2 - L
  69.     PORTC |= (1 << PC3);    // S3 - H
  70.  
  71.     return sensorMeasurement();
  72. }
  73.  
  74. int checkGreen() {
  75.     PORTC |= (1 << PC2);    // S2 - H
  76.     PORTC |= (1 << PC3);    // S3 - H
  77.  
  78.     return sensorMeasurement();
  79. }
  80.  
  81. void moveServo(int pulseWidth) {
  82.     TCCR1A = 0;
  83.     ICR1 = 19999;
  84.  
  85.     TCCR1A = (1 << WGM11);
  86.     TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
  87.  
  88.     DDRB |= (1 << PB1);
  89.     TCCR1A |= 2 << 6;
  90.  
  91.     OCR1A = pulseWidth;
  92. }
  93.  
  94. int main() {
  95.  
  96.     DDRB |= (1 << PB0);
  97.     DDRC |= (1 << PC5) | (1 << PC4) | (1 << PC3) | (1 << PC2) | (1 << PC1);
  98.  
  99.     int measure = checkRed();
  100.  
  101.     for (int i = 0 ; i < measure / 10000 ; i++) {
  102.         PORTB ^= (1 << PB0);
  103.         _delay_ms(300);
  104.         PORTB ^= (1 << PB0);
  105.         _delay_ms(300);
  106.     }
  107.  
  108.     while(1) {`
  109.         /*int measure = checkRed();
  110.         if(measure > 10000) {
  111.             PORTB |= (1 << PB0);
  112.         } else {
  113.             PORTB &= ~(1 << PB0);
  114.         }*/
  115.     }
  116.  
  117.     return 0;
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement