/******************************************************************************
 Title:    bot control transmitter
 Author:   dan williams
 Date:     Apr 17 2005
 Software: AVR-GCC 3.3 
 Hardware: Any AVR with built-in ADC, tested with ATmega8 at 1Mhz
  
  
  bot controller:
     joystick x (0-5V) on pin 40  PA0
     joystick y (0-5V) on pin 39  PA1
     
     reset should have 1K resistor to +5V
     GND pins 11, and 31
     +5V pins 10, 32, 30
     
    FUSES: 
       H: D9    
       L: E1  
  
*******************************************************************************/
#include<avr/io.h>
#include "usart.h"

void adc_init( );
int  Analog  ( uint8_t n);
void dostuff ( ) ;



int main (void)  {
  DDRB = 0x00;      // port B input
  DDRD = 0x00;      // port D input
  
  adc_init();  
  
  USART_Init( 207 ); //4800 @ 1Mhz
  USART_printstring("READY.\n\r");
                  
  dostuff();
}
 
   
//------------------------| FUNCTIONS |------------------------


void adc_init() {
  // Activate ADC with Prescaler 
  ADCSRA =  1 << ADEN  |
            0 << ADSC  |
            0 << ADATE |
            0 << ADIF  |
            0 << ADIE  |
            1 << ADPS2 |  /* 16Mhz */
            1 << ADPS1 |
            1 << ADPS0 ;
}


int Analog ( uint8_t n ) {

    // Select pin ADC0 using MUX
    ADMUX = n & 7;
    
    //Start conversion
    ADCSRA |= _BV(ADSC);
    
    // wait until converstion completed
    while (ADCSRA & _BV(ADSC) ) {}
    
    // get converted value
    return ADC;  
}


void dostuff ( ) {
      int X, Y;
      
      while (1) {
        // Gather values
        X = Analog(0) / 4;
        Y = Analog(1) / 4;
        
        USART_printstring("PX");
        USART_printhex(X);
        USART_printstring("PY");
        USART_printhex(Y);
        
        
      }

}


