Subversion Repositories FlightCtrl

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

/***********************************************************************************************************************************
* File:                 main.c
*
* Purpose:              This program has been developed for the microcontroller test board AVR-CTRL
*                               It is reading a poti connected to analog input = ADC and passes that value to BL CTRL via I2C
*                               to set the turn rate of the connected electrical motor
*
* Functions:    int     main(void)
*                               ISR(ADC_vect)
*                               void init_ADC(void)
*
* hardware:             AVR-Ctrl experimental board without LCD display as I2C bus is used
*
* Created:              Jan 2013
*
* Version:              1.00    experimental version
*
* Copyright:    (c)2013 dk9nw -at- darc.de
*                               All rights reserved. This software is available only for non-commercial amateur radio or educational applications.  
*                               Other uses are prohibited. This software may be modified only if
*                               the resulting code be made available publicly and the original author(s) given credit.
*
***********************************************************************************************************************************/

#include "main.h"
#include "timer.h"
#include "twimaster.h"
#include "analog.h"


// **************************************************************************************************************************
// main function starting here
//
// INPUT:       None
// OUTPUT:      None
// RETURN:      1
// --------------------------------------------------------------------------------------------------------------------------
int main (void)
{
        // DDRA = Port A Data Direction Register –> DDA7 DDA6 DDA5 DDA4 DDA3 DDA2 DDA1 DDA0
        DDRA &= ~((1<<DDA7)|(1<<DDA6)|(1<<DDA5)|(1<<DDA4)|(1<<DDA3));   // those 5 keys are configured as input

        DDRB=0xFF;                                                                                                                      // all ports set as output as LEDs are connected here
        PORTB = 0x00;                                                                                                           // switch off all LEDs
       
        init_ADC();                                                                                                                     // initialize analog digial converter
        init_Timer0();                                                                                                          // initialize timer
        i2c_init();                                                                                                                     // initialize I2C Bus (TWI)
       

        // **************************************************************************************************************************
        void loop2ms(void)
        {
                // ADCSRA = Analog Digital Conversion Status Register A -> ADEN ADSC ADATE ADIF ADIE ADPS2 ADPS1 ADPS0
                // ADSC  = start conversion
                ADCSRA |= (1<<ADSC);
               
                while(!AdReady);                                                                // wait that ISR in analog.c is passed
                AdReady = 0;
               
                AdWertNick = AdWertNick / 2;                                    // AdWertNick 0...511
                Motor[0] = (AdWertNick);                                                // desired turn rate is passed over to the motor front side
                Motor[1] = (AdWertNick);                                                // desired turn rate is passed over to the motor rear side
               
                if (AdWertNick<32) PORTB = 0x01;                                                                        // switch on    LED 1
                if (AdWertNick>31 && AdWertNick<64)     PORTB = 0x02;                           // ...                  LED 2
                if (AdWertNick>63 && AdWertNick<96)     PORTB = 0x04;                           // ...                  LED 3
                if (AdWertNick>95 && AdWertNick<128)    PORTB = 0x08;                           // ...                  LED 4
                if (AdWertNick>127 && AdWertNick<160)   PORTB = 0x10;                           // ...                  LED 5
                if (AdWertNick>159 && AdWertNick<192)   PORTB = 0x20;                           // ...                  LED 6
                if (AdWertNick>191 && AdWertNick<224)   PORTB = 0x40;                           // ...                  LED 7
                if (AdWertNick>223 )                                    PORTB = 0x80;                           // ...                  LED 8
        }
        // **************************************************************************************************************************


       
        // **************************************************************************************************************************
        while (1)
        {
                if ( ((PINA & 0xF8)>> 3) == 0x01) break;                                                // red button to finish program if necessary
               
               
                if(Flag2ms())                                                                                                   // toggle that loop every 2 ms
                {
                        loop2ms();     
                }
        }
        // **************************************************************************************************************************

}
// *** EOF : main() *************************************************************************************************************