Main Page | File List | Globals

# main_uart.c

Go to the documentation of this file.

    00001
    00023 #include <iom16.h>
    00024 #include <inavr.h>
    00025
    00026 // Global variables
    00027 double        accumulator       = 0;
    00028 double        result            = 0;
    00029 double        Vin               = 0;
    00030 short         temp              = 0;
    00031 short         samples           = 0;
    00032
    00033 // Function prototypes
    00034 void send(unsigned char result);
    00035 void write_ascii(double accumulator);
    00036 void init_adc(void);
    00037 void init_uart(void);
    00038 void init_counter(void);
    00039 void vin(void);
    00040 void write_10bit(void);
    00041 void write_16bit(void);
    00042 void oversampled(void);
    00043 void write_space(void);
    00044
    00046 #pragma vector=ADC_vect
    00047 __interrupt void ADCinterrupt(void)
    00048 {
    00049   accumulator += ADC;
    00050   samples++;
    00051 }
    00052
    00054 void init_adc(void)
    00055 {
    00056   ADCSRA = (1<<ADEN)|(1<<ADIE)|(1<<ADSC)|(1<<ADATE)|(1<<ADPS2)|(1<<ADPS1);
    00057   ADMUX = (0<<REFS1)|(0<<REFS0);
    00058   __enable_interrupt();
    00059 }
    00060
    00064 void init_uart(void)
    00065 {
    00066   UBRRL = 25;
    00067   UCSRB = (1<<TXEN);
    00068 }
    00069
    00071 void send(unsigned char result)
    00072 {
    00073   while (!(UCSRA & (1<<UDRE)));
    00074   UDR = result;
    00075 }
    00076
    00078 void init_counter(void)
    00079 {
    00080   DDRD  = (1<<PD7);
    00081   TCCR2 = (1<<WGM20)|(1<<WGM21)|(1<<COM21)|(1<<CS22);
    00082   OCR2  = 128;
    00083 }
    00084
    00086 void write_ascii(double accumulator)
    00087 {
    00088   unsigned char result;
    00089   result = '0';
    00090   while ( accumulator >= 10000 )
    00091   {
    00092     result++;
    00093     accumulator -= 10000;
    00094   }
    00095   send(result);                              // Send first digit
    00096
    00097   result = '0';
    00098   while ( accumulator >= 1000 )
    00099   {
    00100     result++;
    00101     accumulator -= 1000;
    00102   }
    00103   send(result);                              // Send second digit
    00104
    00105   result = '0';
    00106   while ( accumulator >= 100 )
    00107   {
    00108     result++;
    00109     accumulator -= 100;
    00110   }
    00111   send(result);                              // Send third digit
    00112
    00113   result = '0';
    00114   while ( accumulator >= 10 )
    00115   {
    00116     result++;
    00117     accumulator -= 10;
    00118   }
    00119   send(result);                              // Send fourth digit
    00120
    00121   send('0' + accumulator);                   // Send fifth digit
    00122   send('\r');                                // Send return
    00123   send('\n');                                // Send newline
    00124 }
    00125
    00127 void vin(void)
    00128 {
    00129   Vin = (accumulator/65536)*4.910;           // VREF = 4.910
    00130 }
    00131
    00133 void write_10bit(void)
    00134 {
    00135   __disable_interrupt();
    00136   write_ascii(ADC);
    00137   __enable_interrupt();
    00138 }
    00139
    00141 void write_16bit(void)
    00142 {
    00143   __disable_interrupt();
    00144   write_ascii(accumulator);
    00145   __enable_interrupt();
    00146 }
    00147
    00149 void write_space(void)
    00150 {
    00151   int i;
    00152   for(i=0; i<4; i++)
    00153     {
    00154       send(32);                                 // Ascii value for 'space'
    00155     }
    00156 }
    00157
    00160 void oversampled(void)
    00161 {
    00162   __disable_interrupt();
    00163   accumulator += 5150;                         // Offset error compensation, (69*4096)/64*/
    00164   accumulator *= 0.9993;                       // Gain error compensation*/
    00165   temp=(int)accumulator%64;
    00166   accumulator/=64;
    00167   if(temp>=32)
    00168     {
    00169       accumulator += 1;                       //  Rounding up
    00170     }
    00171   write_space();
    00172   write_16bit();
    00173   vin();
    00174   samples     = 0;
    00175   accumulator = 0;
    00176   __enable_interrupt();
    00177 }
    00178
    00179 void main( void )
    00180 {
    00181   init_uart();
    00182   init_adc();
    00183   init_counter();
    00184
    00185   while(1)
    00186   {
    00187     write_10bit();
    00188     if(samples>4095)
    00189       {
    00190         oversampled();
    00191       }
    00192   }
    00193 }
    00194