/*
 * photo_resistor.c
 *
 * Created: 1/29/2015 8:11:07 AM
 *  Author: soc
 */ 


 //-----------------------------------------------------------------------------
 //  ATmega168
 //  Internal 8 MH RC Osc.
 //  (1/8M = 125 ns)
 //-----------------------------------------------------------------------------
 //#define F_CPU 8000000UL /* 1 MHz Internal Oscillator */
 
 
 #include <avr/io.h>
 //#include <util/delay.h>
 #include <avr/interrupt.h>
 
 #define FOSC    8000000
 #define BAUD    9600
 #define UBRR    FOSC/16/BAUD-1
 uint8_t bit_value=0;
 uint8_t light_value;
 //-----------------------------------------------------------------------------
 //  Initialize USART0
 //-----------------------------------------------------------------------------
 void init_USART0(unsigned int baud)
 {
     UBRR0 = baud;                       // Set Baudrate
     UCSR0C = (3<<UCSZ00);               // Character Size 8 bit
     UCSR0B |= _BV(RXEN0) | _BV(TXEN0);  // Receiver and Transmitter Enable
 }
 
 
 void adc_init()
 {
 	/* internal pull-ups interfere with the ADC. disable the
 	 * pull-up on the pin if it's being used for ADC. either
 	 * writing 0 to the port register or setting it to output
 	 * should be enough to disable pull-ups. */
 	PORTC = 0x00;
 	DDRC = 0x00;
 	/* unless otherwise configured, arduinos use the internal Vcc
 	 * reference. MUX 0x0f samples the ground (0.0V). */
 	ADMUX = _BV(REFS0) | 0x0f;
 	/*
 	 * Enable the ADC system, use 128 as the clock divider on a 16MHz
 	 * arduino (ADC needs a 50 - 200kHz clock) and start a sample. the
 	 * AVR needs to do some set-up the first time the ADC is used; this
 	 * first, discarded, sample primes the system for later use.
 	 */
 	ADCSRA |= _BV(ADEN) | _BV(ADPS2) | _BV(ADPS1) |  _BV(ADSC);
 	/* wait for the ADC to return a sample */
 	loop_until_bit_is_clear(ADCSRA, ADSC);
 }
 
 uint16_t adc_read(unsigned char pin)
 {
 	unsigned char l, h, r;
 
 	r = (ADMUX & 0xf0) | (pin & 0x0f);
 	ADMUX = r; /* select the input channel */
 	ADCSRA |= _BV(ADSC);
 	loop_until_bit_is_clear(ADCSRA, ADSC);
 
 	/* must read the low ADC byte before the high ADC byte */
 
 	l = ADCL;
        h = ADCH;
 
 	return ((uint16_t)(( (h<<8)|l) )) ;
 }
 
 //-----------------------------------------------------------------------------
 //  Transmit 1 byte Data
 //-----------------------------------------------------------------------------
 
 /* function for long delay */
 void delay_ms(uint16_t x)
 {
   uint8_t y, z;
   for ( ; x > 0 ; x--){
     for ( y = 0 ; y < 90 ; y++){
       for ( z = 0 ; z < 6 ; z++){
         asm volatile ("nop");
       }
     }
   }
 }
 
 
 void transmit_1byte_USART0(uint8_t c)
 {
 
     loop_until_bit_is_set(UCSR0A, UDRE0)  ;
 	UDR0 =c;
 }
 
 
 
 //-----------------------------------------------------------------------------
 //  Main
 //-----------------------------------------------------------------------------
 int main()
 {
   uint8_t c;
   uint16_t raw;
   DDRB=0b0000000110;
   init_USART0(UBRR);  // initialize USART0  
   adc_init(0);
 
    // set a reference to light value, lets say
    // if we have a high value then we turn the LED on
   light_value=1.5*(adc_read(0));
                          
   sei(); //enable all interrupts
   DDRB |=(1<<1);// set pin1 of PortB to output connect the LED to pin1
 
 
      while(1)
      {
         raw=adc_read(0); // read adc value at channel 0
 
         transmit_1byte_USART0((uint8_t)raw);// send bit value to FTDI
         if(raw <=light_value)
         {
            PORTB |=(1<<1);
         }
         else
         {
            PORTB &=~(1<<1);
         }
            delay_ms(100);
      }
 
      return 0;
 }
