Hjælp til at kommentere Keyboard 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 "keyboard.h"
#include "gear.h"
#include "state.h"
/** Constant definitions *********************************************/
/** Global variables *************************************************/
unsigned char keyboardFlag = 0;
unsigned char keyboardReadFlag = 0;
unsigned char keyboardReadCounter = 0;
/** Subroutines ******************************************************/
//-----------------------------------------------------------------
// Matrix keybard initialization with interrupt INT0
//-----------------------------------------------------------------
void keyboardInit()
{
/* Matrix keyboard use following ports
PB3 as coloumn #1 (input)
PD4 as coloumn #2 (input)
PD5 as coloumn #3 (input)
PD6 as row #1 (output)
PD7 as row #2 (output)
*/
// Setup PD6-7 to output
DDRD |= (3<<6);
// The rising edge of INT0 generates an interrupt request.
MCUCR |= (1<<ISC01)|(1<<ISC00);
// External Interrupt Request 0 Enable
GICR |= (1<<INT0);
// Set matrix keyboard ready for read
PORTD &= ~(3<<6);
// reset key
key = KEY_NOTSET;
}
//-----------------------------------------------------------------
// Read key pressed on keyboard
//-----------------------------------------------------------------
void readKey()
{
unsigned int i = 0;
unsigned char temp = 0;
if (key == KEY_NOTSET)
{
// for (i = 0; i < 200; i++);
temp |= (CHKBIT(PINB, 3) ? (1 << 0) : 0);
temp |= (CHKBIT(PIND, 4) ? (1 << 1) : 0);
temp |= (CHKBIT(PIND, 5) ? (1 << 2) : 0);
SETBIT(PORTD, 6);
for (i = 0; i < 20; i++);
if (CHKBIT(PINB, 3) && CHKBIT(PIND, 4) && CHKBIT(PIND, 5))
{
temp |= (1<<3);
}
else
{
SETBIT(PORTD, 7);
for (i = 0; i < 20; i++);
if (CHKBIT(PINB, 3) && CHKBIT(PIND, 4) && CHKBIT(PIND, 5))
{
temp |= (1<<4);
}
}
CLRBIT(PORTD, 6);
CLRBIT(PORTD, 7);
switch (temp)
{
case KEY_UP:
key = KEY_UP;
break;
case KEY_DOWN:
key = KEY_DOWN;
break;
case KEY_LEFT:
key = KEY_LEFT;
break;
case KEY_RIGHT:
key = KEY_RIGHT;
break;
case GEAR_UP:
key = GEAR_UP;
break;
case GEAR_DOWN:
key = GEAR_DOWN;
break;
default:
key = KEY_NOTSET;
break;
}
// if (key != KEY_NOTSET)
// keyboardFlag = 1;
}
}
//-----------------------------------------------------------------
// Update keyboard
//-----------------------------------------------------------------
void updateKeyboard()
{
switch (key)
{
case KEY_UP:
case KEY_DOWN:
case KEY_LEFT:
case KEY_RIGHT:
updateState();
break;
case GEAR_UP:
if (state == STATE_QUICK)
gearUp();
break;
case GEAR_DOWN:
if (state == STATE_QUICK)
gearDown();
break;
default:
break;
}
key = KEY_NOTSET;
}
/****************** END OF PROGRAM **********************************/