main.c

 

 #include <avr/io.h>
 #include <avr/interrupt.h>
 #include <avr/signal.h>
 #include <inttypes.h>
 #include <avr/iom16.h>
 #include <avr/delay.h>
 #include <string.h>
 
 void init_usart() {
         UBRRL = 12;                             // Set baudrate to 9600
         UCSRA = _BV(U2X);                       // Double the USART transmission speed
         UCSRB = _BV(RXEN)                       // Reciever enable
                 | _BV(TXEN);                    // Transmitter enable
         UCSRC = _BV(URSEL)                      // Register select
                 | _BV(UCSZ0)                    // 8-bit character size
                 | _BV(UCSZ1);
 }
 
 void init_timer(void) {
         TCCR1A = _BV(COM1A1)                    // set OC1A/B at TOP
                 | _BV(COM1B1)                   // clear OC1A/B when match
                 | _BV(WGM11)
                 | _BV(WGM12)
                 | _BV(WGM13);                   // mode 14 (fast PWM, clear TCNT1 
					   // on match ICR1)
 
         TCCR1B = _BV(WGM13)
                 | _BV(WGM12)
                 | _BV(CS11);                    // timer uses main system clock 
					   // with 1/8 prescale
 
         ICR1 = 2000;                            // used for TOP, makes for 50 hz PWM
         OCR1A = 50;                             // set 50-280
         DDRD = _BV(PD4)
                 | _BV(PD5)
                 | _BV(PD1);                     // have to set up pins as outputs
 }
 
 void init_adc(void) {
         ADMUX = _BV(REFS1)                      // Internal 2.56V Voltage 
					   // Reference with external
                 | _BV(REFS0)                    // capacitor at AREF pin
                 | _BV(ADLAR);                   // Left adjust result
         ADCSRA = _BV(ADEN)                      // Enables the ADC with a 
					   // prescaler of 16.
                 | _BV(ADPS2);
 }
 
 void USART_Transmit(unsigned char data) {
         while (! (UCSRA & (1<<UDRE)));          // wait for empty transmit buffer
         UDR = data;                             // Put data into buffer, 
					   // sends the data
 }
 
 void send_string(char* s) {
         int size = strlen(s);
         int i = 0;
         for(i=0;i<size;i++){
                 unsigned char ch = s[i];
                 USART_Transmit(ch);
         }
 }
 
 unsigned char read_adc(){
         unsigned int i = 0;
         unsigned int sum = 0;
         for (i=0; i<10; i++){
                 ADCSRA |= _BV(ADSC);            // Start conversion
                 while (ADCSRA & _BV(ADSC)) {}   // wait until converstion completed
                 sum = sum+ADCH;
         }
         unsigned char result = (sum/10);
         return result;
 }
 
 int main(void) {
         sei();
         init_usart();
         init_timer();
         init_adc();
 
         int i = 50;
         int dir = 1;
         while(1)
         {
                 OCR1A = i;
                 i = i + (1*dir);
                 if (i == 280)
                         dir = dir*-1;
                 else if (i == 50)
                         dir = dir*-1;
 
                 unsigned char angle = (OCR1A-50);
                 char* data1[30];
                 sprintf(data1, "%u", angle);
                 send_string("angle: ");
                 send_string(data1);
                 send_string(";");
 
                 unsigned char dist = read_adc();
                 char* data[30];
                 sprintf(data, "%u", dist);
                 send_string("dist: ");
                 send_string(data);
                 send_string(";");
         }
 }