Main Page | Data Structures | File List | Data Fields | Globals | Related Pages

LED_sensor.c

Go to the documentation of this file.
00001 #include <inavr.h>
00002 #include <ioavr.h>
00003 
00004 unsigned char minLightLevel;
00005 unsigned char maxLightLevel;
00006 unsigned char lightLevel;
00007 
00008 void Init_Ports (void)
00009 {
00010   DDRB = (1<<DDB4);
00011   PORTB = (1<<PB4);
00012 }
00013 
00014 
00015 void Init_ADC (void)
00016 {
00017   // AREF as voltage reference, sample on ADC channel 0
00018   ADMUX  = (0 << REFS1) | (0 << REFS0) | (1 << ADLAR) | 0x0;
00019   // Enable ADC, and start conversion, no prescaling
00020   ADCSRA = (1 << ADEN) | (1 << ADSC) | (1 << ADIF) |
00021            (0 << ADPS2) | (0 << ADPS1) | (0 << ADPS0);
00022 
00023   while (ADCSRA & (1 << ADSC))
00024   {
00025   }
00026 
00027   minLightLevel = ADCH;
00028 }
00029 
00030 void Init_PWM (void)
00031 {
00032 
00033   TCCR0A = (1<<WGM01)|(1<<WGM00)|(1<<COM0A1)|(1<<COM0A0)|
00034            (0<<CS02)|(1<<CS01)|(1<<CS00);
00035 }
00036 
00037 void Calibrate_Sensor (void)
00038 
00039 {
00040   unsigned char scale;
00041   unsigned char lastLightLevel;
00042 
00043   scale = 0;
00044   OCR0A = 0x00;
00045 
00046 
00047   while(!(OCR0A == 0xFF))
00048   {
00049     ADCSRA |= (1<<ADSC);
00050     while (ADCSRA & (1 << ADSC))
00051     {
00052     }
00053     lightLevel = ADCH;
00054 
00055     if (lightLevel > initialLightLevel)
00056     {
00057       scale ++;
00058       __no_operation();
00059     }
00060     OCR0A ++;
00061      __delay_cycles(5000);
00062   }
00063 }
00064 
00065 
00066 void main (void)
00067 
00068 {
00069 
00070   Init_Ports();
00071   Init_ADC();
00072   Init_PWM();
00073   Calibrate_Sensor();
00074 
00075   for(;;)
00076   {
00077   }
00078 
00079 }
00080 

Generated on Thu Nov 10 11:04:14 2005 for AVR221 - PID controller by  doxygen 1.4.4