Commentaar toegevoegd

This commit is contained in:
2016-02-15 14:44:27 +01:00
parent 07961c8f8a
commit abc1e9d622
2 changed files with 46 additions and 41 deletions
Binary file not shown.
+46 -41
View File
@@ -2,30 +2,25 @@
* MIC-Week1.c * MIC-Week1.c
* *
* Created: 2-2-2016 11:48:44 * Created: 2-2-2016 11:48:44
* Author : kenny * Author : Kenneth en Martijn
*/ */
#define F_CPU 8000000UL #define F_CPU 8000000UL //CPU op 8MHz
#define NELEMS(x) (sizeof(x) / sizeof(x[0])) #define NELEMS(x) (sizeof(x) / sizeof(x[0])) //Geeft het aantal elementen uit een array terug.
#include <avr/interrupt.h> #include <avr/interrupt.h>
#include <avr/io.h> #include <avr/io.h>
#include <util/delay.h> #include <util/delay.h>
int reset = 0; int reset = 0; //Reset voor opgave 6 (wordt gebruikt in de wait methode)
int count = 0; //Counter voor opgave 5 om bij te houden bij welke pattern in de array hij is
int speed = 1; //De snelheid voor opgave 5 van de patronen
int blink = 1000; //De snelheid voor opgave 6 van het ledje
int b1 = 0; //Houdt bij voor opgave 6 of de knop al weer losgelaten is
int b2 = 0; //Houdt bij voor opgave 6 of de knop al weer losgelaten is
int direction = 0; //De richting van het looplicht in opgave 4
/******************************************************************/ void wait( int ms ) {
void wait( int ms )
/*
short: Busy wait number of millisecs
inputs: int ms (Number of millisecs to busy wait)
outputs:
notes: Busy wait, not very accurate. Make sure (external)
clock value is set. This is used by _delay_ms inside
util/delay.h
Version : DMK, Initial code
*******************************************************************/
{
for (int i=0; i<ms; i++) for (int i=0; i<ms; i++)
{ {
if(reset == 1) if(reset == 1)
@@ -38,11 +33,13 @@ Version : DMK, Initial code
} }
} }
//Struct voor een pattroon. Eerste onderdeel is welke ledjes aan moeten staan, tweede hoelang deze dan aan moeten staan
typedef struct { typedef struct {
unsigned char data; unsigned char data;
unsigned int delay ; unsigned int delay ;
} PATTERN_STRUCT; } PATTERN_STRUCT;
//Array van patronen die elkaar opvolgen.
PATTERN_STRUCT pattern[] = { PATTERN_STRUCT pattern[] = {
{0x00, 100}, {0x01, 100}, {0x02, 100}, {0x04, 100}, {0x10, 100}, {0x20, 100}, {0x40, 100}, {0x80, 100}, {0x00, 100}, {0x01, 100}, {0x02, 100}, {0x04, 100}, {0x10, 100}, {0x20, 100}, {0x40, 100}, {0x80, 100},
{0x00, 100}, {0x00, 100},
@@ -54,39 +51,38 @@ PATTERN_STRUCT pattern[] = {
{0x00, 0x00} {0x00, 0x00}
}; };
int count = 0; //System timer interrupt.
int speed = 1;
int blink = 1000;
int b1 = 0;
int b2 = 0;
int direction = 0;
ISR(TIMER0_OVF_vect) { ISR(TIMER0_OVF_vect) {
//C0 ingedrukt
if( (0b00000001 & PINC) != 0 && b1 == 0) if( (0b00000001 & PINC) != 0 && b1 == 0)
{ {
blink = blink*2; blink = blink*2; //Slomer knipperen
reset = 1; reset = 1; //Reset de timer
b1 = 1; b1 = 1;
} }
//C1 ingedrukt
if( (0b00000010 & PINC) != 0 && b2 == 0 ) if( (0b00000010 & PINC) != 0 && b2 == 0 )
{ {
blink = blink/2; blink = blink/2; //Sneller knipperen
reset = 1; reset = 1; //Reset de timer
b2 = 1; b2 = 1;
} }
//Knoppen losgelaten
if( PINC == 0) if( PINC == 0)
{ {
b1 = 0; b1 = 0;
b2 = 0; b2 = 0;
} }
//Lower limit
if(blink < 20) if(blink < 20)
{ {
blink = 20; blink = 20;
} }
//Upper limit
else if(blink > 4000) else if(blink > 4000)
{ {
blink = 4000; blink = 4000;
@@ -100,11 +96,12 @@ int main(void)
// Prescaler // Prescaler
TCCR0 |= (1 << CS01) | (1 << CS00); TCCR0 |= (1 << CS01) | (1 << CS00);
//Enable Overflow Interrupt Enable //Het overflow event aanzetten
TIMSK|=(1<<TOIE0); TIMSK|=(1<<TOIE0);
//Initialize Counter //Counter op waarde 0 zetten
TCNT0=0; TCNT0=0;
//Luister naar System interrupts
sei(); sei();
//Alleen voor opgave 4 //Alleen voor opgave 4
@@ -112,12 +109,11 @@ int main(void)
while (1) while (1)
{ {
//opgave1();
//opgave2(); //opgave2();
//opgave3(); //opgave3();
opgave4(); //opgave4();
//opgave5(); //opgave5();
//opgave6(); opgave6();
//charlieplexing(); //charlieplexing();
@@ -129,33 +125,36 @@ int main(void)
void extraopgave(void) void extraopgave(void)
{ {
//Een knop op PORTC is ingedrukt
if( (PINC) != 0 ) if( (PINC) != 0 )
{ {
//Verschillende toonhoogtes door de waarde van de PORT te pakken.
speaker( PINC ); speaker( PINC );
} }
} }
void speaker(int i) void speaker(int i)
{ {
//PORTD moet hiervoor output zijn en PORTC input
PORTD = 0b01010101; PORTD = 0b01010101;
wait( i ); wait( i );
PORTD = 0b00000000; PORTD = 0b00000000;
wait( i ); wait( i );
} }
void opgave1(void) void opgave2(void)
{ {
PORTD = 0b10000000; PORTD = 0b10000000; //Led D7 aan
wait( 500 ); wait( 500 );
PORTD = 0b01000000; PORTD = 0b01000000; //Led D6 aan
wait ( 500 ); wait ( 500 );
} }
void opgave3(void) void opgave3(void)
{ {
//Knop C0 ingedrukt
if( (0b00000001 & PINC) != 0 ) if( (0b00000001 & PINC) != 0 )
{ {
//knipperen
PORTD = 0b10000000; PORTD = 0b10000000;
wait( 100 ); wait( 100 );
PORTD = 0b00000000; PORTD = 0b00000000;
@@ -166,35 +165,41 @@ void opgave3(void)
void opgave4(void) void opgave4(void)
{ {
wait( 50 ); wait( 50 );
//Als het looplicht aan het einde is
if (PORTD>= 0x80) if (PORTD>= 0x80)
{ {
direction = 1; direction = 1;
} }
//Als het looplicht aan het begin is
else if (PORTD <= 0x1) else if (PORTD <= 0x1)
{ {
direction = 0; direction = 0;
} }
if(direction) if(direction)
PORTD = (PORTD>>1); PORTD = (PORTD>>1); //Naar een hogere waarde shiften
else else
PORTD = (PORTD<<1); PORTD = (PORTD<<1); //Naar een lagere waarde shiften
} }
void opgave5(void) void opgave5(void)
{ {
//Als een knop ingedrukt wordt, wordt de waarde daarvan de snelheid. Werkt niet met system interrupts, dus je moet de knop soms ingedrukt houden
if(PINC != 0 ) if(PINC != 0 )
{ {
speed = PINC; speed = PINC;
} }
// Write data to PORTD //Schrijf de data van het huidige patroon naar PORTD
PORTD = pattern[count].data; PORTD = pattern[count].data;
// wait //Wacht het aantal milliseconde dat in het huidige patroon staat
wait(pattern[count].delay * speed); wait(pattern[count].delay * speed);
count++; count++; //patroon verder
//als alle patronen geweest zijn, opnieuw beginnen
if(count >= NELEMS(pattern)) if(count >= NELEMS(pattern))
{ {
count = 0; count = 0;