Synergia AVR 0.1

line/main.cpp

Go to the documentation of this file.
00001 /* Project info here */
00002 
00003 #include <avr/io.h>
00004 #include <avr/interrupt.h>
00005 #include <util/delay.h>
00006 #include "../lib/usart/usart.h"
00007 
00008 #define min(a,b) ((a) < (b) ? (a) : (b))
00009 
00010 USART32(usb);
00011 
00012 unsigned char command = 0;
00013 int pwm_value = 0;
00014 char dir = 0;
00015 volatile int adc_value =0, enc_value=0;
00016 
00017 SIGNAL (SIG_INTERRUPT0){
00018         if(PINB & (1 << PB0)) enc_value--;
00019         else enc_value++;
00020 }
00021 
00022 SIGNAL (SIG_ADC){
00023         adc_value = (ADCL | (ADCH << 8));
00024         ADCSRA |= _BV(ADSC);
00025 }
00026 
00027 void init(){
00028         MCUCR |= (1 << ISC01) | (1 << ISC00); // encoder, raising edge fires interrupt
00029         GICR  |= (1 << INT0); // INT0 enabled
00030         
00031         // TCNT1 = 0xC2F7;
00032         // TIMSK = _BV(TOIE1);
00033         // TIFR |= _BV(TOV1);
00034         
00035         // pwm interrupt offblast
00036         TCCR1A = _BV(WGM10) | _BV(WGM11) | _BV(COM1A1);
00037         TCCR1B = _BV(CS11);
00038         
00039         // ADC init
00040         ADCSRA = _BV(ADEN) | _BV(ADIE) | _BV(ADSC) | _BV(ADPS0) | _BV(ADPS1) | _BV(ADPS2); //INIT ADC
00041         ADMUX = _BV(REFS0);
00042         
00043         DDRB &= ~(1 << PB0);
00044         DDRD = (1 << PD6) | (1 << PD5);
00045         
00046         OCR1A = 0;
00047 }
00048 
00049 
00050 int main(void){
00051         init();
00052     sei();
00053         
00054         
00055         for(;;){
00056                 
00057                 if(usb.gotLine()){
00058                         usb >> command;
00059                         usb >> pwm_value;
00060                         
00061                         while(!usb.empty()){
00062                                 unsigned char c;
00063                                 usb >> c;
00064                                 if(c == '\n') break;
00065                         }
00066                         
00067                         char d =- 1;
00068                         
00069                         switch(command){
00070                                 case 'S': // stop
00071                                         pwm_value = 0;
00072                                         break;
00073                                 case 'F': // forward
00074                                         d = 1;
00075                                         break;
00076                                 case 'B': // backward
00077                                         d = 0;
00078                                         break;
00079                         }
00080                         
00081                         if(d != dir){
00082                                 dir = d;
00083                                 if(dir >= 0){
00084                                         // stop, change dir
00085                                         OCR1A = 0;
00086                                         _delay_ms(100);
00087                                         PORTD |= (dir << PD6);
00088                                         PORTD = (1 << PD5) | (dir << PD6);
00089                                 }
00090                         }
00091                         
00092                         OCR1A = min(pwm_value, 500);
00093                 }
00094                 
00095                 usb << "A=" << adc_value << ":E=" << enc_value << "\n";
00096         }
00097         
00098     return 0;
00099 }
 All Classes Files Functions Variables Defines