Avatar billede axel21 Juniormester
04. januar 2008 - 23:07 Der er 1 løsning

Hjælp til at kommentere Main Script

Hej. Jeg har brug for hjælp til at få nogle gode kommentarer skrevet på koden så den bliver lettere at forstå. Der skal kun skrives lige præcis hvad hver linie gør. Det skal siges at jeg ikke er en haj til C så derfor beder jeg om hjælp til at kunne forstå dette, og samtidigt lade nogen tjene nogle "lette point". På forhånd tak...

#include <io.h>
#include <avr/interrupt.h>
#include "defines.h"
#include "lcd_buffer.h"
#include "timer.h"
#include "lcd.h"
#include "spi.h"
#include "training_program.h"
#include "gear.h"
#include "keyboard.h"
#include "usart.h"
#include "bikedata.h"

/**********************************************************************
*  Main
*  Martin Anthoniussen
*  Version 01
*  12-09-2007.
**********************************************************************/

/**********************************************************************
  Constant definitions. */

    #define LED 0
    #define SS 4
    #define FRAMING_ERROR FE
    #define PARITY_ERROR PE
    #define DATA_OVERRUN DOR

/**********************************************************************
  Global variables.*/

    unsigned int bufferCounter = 0;
    unsigned char bufferFlag = 0;

    unsigned int trainingProgramCounter = 0;
    unsigned char trainingProgramFlag = 0;

    extern volatile char recieveCompleteFlag;

    extern unsigned char keyboardReadFlag;
    extern unsigned char keyboardReadCounter;
    extern unsigned char keyboardFlag;

    extern unsigned long timerCounter;
    extern unsigned int delayCounter;

    extern unsigned int deltaRotationCounter;
    extern unsigned char rotationFlag;

    extern unsigned int deltaHeartbeatCounter;
    extern unsigned char heartbeatFlag;

    extern volatile char transmitCompleteFlag;
    extern unsigned int receiveBufferIndex;

    extern unsigned char gearTransmitIndex;
    extern unsigned char gearIndex;

    extern unsigned char receiveBuffer[];

/** Interrupt Service Routines ****************************************/
    //-----------------------------------------------------------------
    // Interrupt routine Timer/Counter1 Overflow
    //-----------------------------------------------------------------
    ISR(SIG_OUTPUT_COMPARE1A)
    {
        // Increment
        timerCounter++;
        delayCounter++;
        deltaHeartbeatCounter++;
        deltaRotationCounter++;

        bufferCounter++;
        trainingProgramCounter++;

       
        if (keyboardReadFlag)
        {
            keyboardReadCounter++;

            if (keyboardReadCounter == 20)
            {
            //    if (!keyboardFlag)
                    readKey();
            }
            else if (keyboardReadCounter >= 40)
            { 
                keyboardFlag = 1;
                keyboardReadCounter = 0;
                keyboardReadFlag = 0;
                GICR |= (1<<INT0);
            }
        }
       

        // set update buffer flag
        if (bufferCounter > 250)
        {
            bufferFlag = 1;
            bufferCounter = 0;
        }

        // set update buffer flag
        if (trainingProgramCounter > 1000)
        {
            trainingProgramFlag = 1;
            trainingProgramCounter = 0;
        }
               
        // if no rotation pulse in some time, set speed
        if (deltaRotationCounter >= 5000)
        {
            setSpeed(deltaRotationCounter);
            deltaRotationCounter = 0;
            rotationFlag = 0;
        }
       
        // if no heart pulse in some time, set pulse
        if (deltaHeartbeatCounter >= 5000)
        {
            setPulse(deltaHeartbeatCounter);
            deltaHeartbeatCounter = 0;
            heartbeatFlag = 0;
        }           
    }

    //-----------------------------------------------------------------
    // Interrupt routine External Interrupt Request 0
    //-----------------------------------------------------------------
    ISR(SIG_INTERRUPT0)
    {
        //if (!keyboardFlag)
        //{
            keyboardReadFlag = 1;
            keyboardReadCounter = 0;
   
            // External Interrupt Request 0 Disable
            GICR &= ~(1<<INT0);   
   
        //    if (!keyboardFlag)
        //        readKey();
   
            // External Interrupt Request 0 Enable
        //    GICR |= (1<<INT0);
        //}
    }

    //-----------------------------------------------------------------
    // Interrupt routine External Interrupt Request 1
    //-----------------------------------------------------------------
    ISR(SIG_INTERRUPT1)
    {
        if (rotationFlag)
            setSpeed(deltaRotationCounter);
        rotationFlag = 1;
        deltaRotationCounter = 0;
    }

    //-----------------------------------------------------------------
    // Interrupt routine External Interrupt Request 2
    //-----------------------------------------------------------------
    ISR(SIG_INTERRUPT2)
    {
        if (heartbeatFlag)
            setPulse(deltaHeartbeatCounter);
        heartbeatFlag = 1;
        deltaHeartbeatCounter = 0;
    }

    //-----------------------------------------------------------------
    // Interrupt routine USART, Tx Complete
    //-----------------------------------------------------------------
    ISR(SIG_USART_TRANS)
    {
        transmitCompleteFlag = 1;
    }

    //-----------------------------------------------------------------
    // Interrupt routine USART, Rx Complete
    //-----------------------------------------------------------------
    ISR(SIG_USART_RECV)
    {
        char c = UDR;
          if ((UCSRA & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN)) == 0)
        {
            if (receiveBufferIndex != -1 && receiveBufferIndex < 50)
            {
                receiveBuffer[receiveBufferIndex++] = c;
            }
   
            if (c == '$')
            {
                receiveBufferIndex = 0;
                recieveCompleteFlag = 0;
            }
            else if (c == '#')
            {
                receiveBufferIndex = -1;
                recieveCompleteFlag = 1;
            }
        }   
    }

    //-----------------------------------------------------------------
    // Interrupt routine Timer/Counter0 Overflow
    //-----------------------------------------------------------------
    ISR(SIG_OUTPUT_COMPARE0)
    {
        if (gearTransmitIndex < gearIndex)
            gearUpTransmit();
        else if (gearTransmitIndex > gearIndex)
            gearDownTransmit();

        gearTransmit();
    }

    //-----------------------------------------------------------------
    // Interrupt routine Serial Transfer Complete
    //-----------------------------------------------------------------
    ISR(SIG_SPI)
    {
        // Set SS to high to move data to the output of the shiftregister
        PORTB |= (1<<SS);
    }

/*********************** Program entry. *******************************/

int main() {

    /****** Initialization ***************/

    // Timer/Counter1 setup
    timerInit();

    // SPI setup
    spiInit();

    // INT0 setup
    keyboardInit();

    // INT1 setup
    speedInit();

    // INT2 setup
    pulseInit();

    // USART setup
    usartInit(2082); // 300 baud

    // Timer/Counter0 setup
    gearInit();

    // Start all interrupt
    SREG  |= 0X80;

    // LCD setup
    lcdInit();

    //**********End of initialization ************

      // Main loop.
      while (1)
      {
        if (bufferFlag)
        {
            //lcdSetCursor(1,1);
            //lcdWrite(receiveBuffer);
            updateBuffer();
            bufferFlag = 0;
        }

        if (trainingProgramFlag)
        {
            stepTrainingProgram();
            trainingProgramFlag = 0;
        }

        if (keyboardFlag)
        {
            updateKeyboard();
            keyboardFlag = 0;
        }

        if (recieveCompleteFlag)
        {
            //lcdWrite(receiveBuffer);
            parseRecivedData();
            recieveCompleteFlag = 0;
        }
       
      }
      return 0;
}

/****************** END OF PROGRAM **********************************/
Avatar billede axel21 Juniormester
25. januar 2008 - 13:57 #1
Closed
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Kurser inden for grundlæggende programmering

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester