Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <avr/io.h>
- #include <avr/delay.h>
- #include <avr/interrupt.h>
- volatile unsigned int one_second_ready;
- volatile unsigned int overflow_ticks;
- /*--------------------------INTERUPTS----------------------------*/
- // TIMER 0 OVERFLOW INTERRUPT
- ISR (TIMER0_OVF_vect) {
- overflow_ticks++;
- }
- // TIMER 1 COMPARE VALUE INTERRUPT
- ISR (TIMER1_COMPA_vect) {
- // T0 DISABLE
- TCCR0 &= ~(1 << CS02) & ~(1 << CS01) & ~(1 << CS00);
- TIMSK &= ~(1 << TOIE0);
- // T1 DISABLE
- TCCR1B &= ~(1 << WGM12) & ~(1 << CS12) & ~(1 << CS10);
- TIMSK &= ~(1 << OCIE1A);
- one_second_ready = 1; // Send Signal To sensorMeasurement() Function
- }
- /*---------------------------------------------------------------*/
- int sensorMeasurement() {
- one_second_ready = 0;
- overflow_ticks = 0;
- PORTC |= (1 << PC5) | (1 << PC4); // Prescaler 100%
- PORTC &= ~(1 << PC1); // OUT Turned On
- // T0 INITIALIZE
- TCCR0 |= (1 << CS02) | (1 << CS01) | (1 << CS00); // External Clock T0 Rising Edge
- TIMSK |= (1 << TOIE0); // Enable T0 Interrupt
- // T1 INITIALIZE
- TCCR1B |= (1 << WGM12) | (1 << CS12) | (1 << CS10); // CTC MODE
- OCR1A = F_CPU / 1024; // 1 Second Delay
- TIMSK |= (1 << OCIE1A); // Enable T1 Interrupt
- sei(); // Enable Interrupts
- while (one_second_ready == 0); // Wait until T1 is 1 second
- cli(); // Disable Interrupts
- PORTC |= (1 << PC1);
- return overflow_ticks * 256; // Signal Frequency
- }
- int checkRed() {
- PORTC &= ~(1 << PC2); // S2 - L
- PORTC &= ~(1 << PC3); // S3 - L
- return sensorMeasurement();
- }
- int checkBlue() {
- PORTC &= ~(1 << PC2); // S2 - L
- PORTC |= (1 << PC3); // S3 - H
- return sensorMeasurement();
- }
- int checkGreen() {
- PORTC |= (1 << PC2); // S2 - H
- PORTC |= (1 << PC3); // S3 - H
- return sensorMeasurement();
- }
- void moveServo(int pulseWidth) {
- TCCR1A = 0;
- ICR1 = 19999;
- TCCR1A = (1 << WGM11);
- TCCR1B = (1 << WGM13) | (1 << WGM12) | (1 << CS11);
- DDRB |= (1 << PB1);
- TCCR1A |= 2 << 6;
- OCR1A = pulseWidth;
- }
- int main() {
- DDRB |= (1 << PB0);
- DDRC |= (1 << PC5) | (1 << PC4) | (1 << PC3) | (1 << PC2) | (1 << PC1);
- int measure = checkRed();
- for (int i = 0 ; i < measure / 10000 ; i++) {
- PORTB ^= (1 << PB0);
- _delay_ms(300);
- PORTB ^= (1 << PB0);
- _delay_ms(300);
- }
- while(1) {`
- /*int measure = checkRed();
- if(measure > 10000) {
- PORTB |= (1 << PB0);
- } else {
- PORTB &= ~(1 << PB0);
- }*/
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement