575 lines
6.2 KiB
C
575 lines
6.2 KiB
C
/*
|
|
* MIC-Week3.c
|
|
*
|
|
* Created: 23-2-2016 10:18:35
|
|
* Author : kenny
|
|
*/
|
|
#define F_CPU 8000000UL //CPU op 8MHz
|
|
#define NELEMS(x) (sizeof(x) / sizeof(x[0])) //Geeft het aantal elementen uit een array terug.
|
|
|
|
#include <avr/io.h>
|
|
#include <util/delay.h>
|
|
#include <avr/interrupt.h>
|
|
#include <stdlib.h>
|
|
|
|
#include "lcd.h"
|
|
|
|
typedef struct {
|
|
unsigned char data[10];
|
|
} PATTERN_STRUCT;
|
|
|
|
//PIN 0 == SEG A
|
|
//PIN 1 == SEG B
|
|
//PIN 2 == SEG C
|
|
//PIN 3 == SEG D
|
|
//PIN 4 == SEG E
|
|
//PIN 5 == SEG F
|
|
//PIN 6 == SEG G
|
|
//PIN 7 == SEG DP
|
|
|
|
//Array van patronen die elkaar opvolgen.
|
|
PATTERN_STRUCT pattern[] = {
|
|
{"Kenneth"}, {"Janco"}, {"Arthur"}, {"Martijn"}, {"Mauro"}, {"Rene"}, {"Remco"}, {"Jannick"}, {"Tim"}
|
|
};
|
|
|
|
int opgave = 0;
|
|
int flip = 0;
|
|
int count = 0;
|
|
int current = 0;
|
|
|
|
int main(void)
|
|
{
|
|
//opgave1();
|
|
|
|
//opgave2knop();
|
|
|
|
//opgave3();
|
|
|
|
opgaveextra();
|
|
|
|
return 1;
|
|
}
|
|
|
|
void opgave1()
|
|
{
|
|
lcd_init();
|
|
|
|
srand(14152);
|
|
|
|
int r = 0;
|
|
|
|
while(1){
|
|
|
|
r = rand() % 9;
|
|
|
|
display_text(1, ":-) ~ (-:");
|
|
display_text(0, pattern[r].data);
|
|
|
|
_delay_ms(1000);
|
|
|
|
set_cursor(15);
|
|
display_char(0b01000001);
|
|
|
|
_delay_ms(1000);
|
|
|
|
shift_display(1, 3);
|
|
|
|
_delay_ms(1000);
|
|
|
|
//clear
|
|
lcd_command(0x01);
|
|
_delay_ms(100);
|
|
}
|
|
}
|
|
|
|
typedef struct {
|
|
unsigned int frequency;
|
|
unsigned int delay;
|
|
} NOTES;
|
|
|
|
NOTES song[] = {
|
|
{10, 250},{20, 250},{30, 250},{40, 250},{50, 250},{60, 250},{70, 250},{80, 250},{90, 250},{100, 250},
|
|
{110, 250},{120, 250},{130, 250},{140, 250},{150, 250},{160, 250},{170, 250},{180, 250},{190, 250},{200, 250},
|
|
{210, 250},{220, 250},{230, 250},{240, 250}
|
|
};
|
|
|
|
NOTES mario[] = {
|
|
{660, 100},
|
|
{0, 150},
|
|
{660, 100},
|
|
{0, 300},
|
|
{660, 100},
|
|
{0, 300},
|
|
{510, 100},
|
|
{0, 100},
|
|
{660, 100},
|
|
{0, 300},
|
|
{770, 100},
|
|
{0, 550},
|
|
{380, 100},
|
|
{0, 575},
|
|
|
|
{510, 100},
|
|
{0, 450},
|
|
{380, 100},
|
|
{0, 400},
|
|
{320, 100},
|
|
{0, 500},
|
|
{440, 100},
|
|
{0, 300},
|
|
{480, 80},
|
|
{0, 330},
|
|
{450, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 200},
|
|
{660, 80},
|
|
{0, 200},
|
|
{760, 50},
|
|
{0, 150},
|
|
{860, 100},
|
|
{0, 300},
|
|
{700, 80},
|
|
{0, 150},
|
|
{760, 50},
|
|
{0, 350},
|
|
{660, 80},
|
|
{0, 300},
|
|
{520, 80},
|
|
{0, 150},
|
|
{580, 80},
|
|
{0, 150},
|
|
{480, 80},
|
|
{0, 500},
|
|
|
|
{510, 100},
|
|
{0, 450},
|
|
{380, 100},
|
|
{0, 400},
|
|
{320, 100},
|
|
{0, 500},
|
|
{440, 100},
|
|
{0, 300},
|
|
{480, 80},
|
|
{0, 330},
|
|
{450, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 200},
|
|
{660, 80},
|
|
{0, 200},
|
|
{760, 50},
|
|
{0, 150},
|
|
{860, 100},
|
|
{0, 300},
|
|
{700, 80},
|
|
{0, 150},
|
|
{760, 50},
|
|
{0, 350},
|
|
{660, 80},
|
|
{0, 300},
|
|
{520, 80},
|
|
{0, 150},
|
|
{580, 80},
|
|
{0, 150},
|
|
{480, 80},
|
|
{0, 500},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 150},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 150},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
{430, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 100},
|
|
{570, 100},
|
|
{0, 220},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 200},
|
|
{0, 300},
|
|
|
|
{1020, 80},
|
|
{0, 300},
|
|
{1020, 80},
|
|
{0, 150},
|
|
{1020, 80},
|
|
{0, 300},
|
|
|
|
{380, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 150},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 150},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
{430, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 100},
|
|
{570, 100},
|
|
{0, 420},
|
|
|
|
{585, 100},
|
|
{0, 450},
|
|
|
|
{550, 100},
|
|
{0, 420},
|
|
|
|
{500, 100},
|
|
{0, 360},
|
|
|
|
{380, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 150},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 150},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
{430, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 100},
|
|
{570, 100},
|
|
{0, 220},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 200},
|
|
{0, 300},
|
|
|
|
{1020, 80},
|
|
{0, 300},
|
|
{1020, 80},
|
|
{0, 150},
|
|
{1020, 80},
|
|
{0, 300},
|
|
|
|
{380, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{760, 100},
|
|
{0, 100},
|
|
{720, 100},
|
|
{0, 150},
|
|
{680, 100},
|
|
{0, 150},
|
|
{620, 150},
|
|
{0, 300},
|
|
|
|
{650, 150},
|
|
{0, 300},
|
|
{380, 100},
|
|
{0, 150},
|
|
{430, 100},
|
|
{0, 150},
|
|
|
|
{500, 100},
|
|
{0, 300},
|
|
{430, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 100},
|
|
{570, 100},
|
|
{0, 420},
|
|
|
|
{585, 100},
|
|
{0, 450},
|
|
|
|
{550, 100},
|
|
{0, 420},
|
|
|
|
{500, 100},
|
|
{0, 360},
|
|
|
|
{380, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 300},
|
|
{500, 100},
|
|
{0, 150},
|
|
{500, 100},
|
|
{0, 300},
|
|
|
|
{500, 60},
|
|
{0, 150},
|
|
{500, 80},
|
|
{0, 300},
|
|
{500, 60},
|
|
{0, 350},
|
|
{500, 80},
|
|
{0, 150},
|
|
{580, 80},
|
|
{0, 350},
|
|
{660, 80},
|
|
{0, 150},
|
|
{500, 80},
|
|
{0, 300},
|
|
{430, 80},
|
|
{0, 150},
|
|
{380, 80},
|
|
{0, 600},
|
|
|
|
{500, 60},
|
|
{0, 150},
|
|
{500, 80},
|
|
{0, 300},
|
|
{500, 60},
|
|
{0, 350},
|
|
{500, 80},
|
|
{0, 150},
|
|
{580, 80},
|
|
{0, 150},
|
|
{660, 80},
|
|
{0, 550},
|
|
|
|
{870, 80},
|
|
{0, 325},
|
|
{760, 80},
|
|
{0, 600},
|
|
|
|
{500, 60},
|
|
{0, 150},
|
|
{500, 80},
|
|
{0, 300},
|
|
{500, 60},
|
|
{0, 350},
|
|
{500, 80},
|
|
{0, 150},
|
|
{580, 80},
|
|
{0, 350},
|
|
{660, 80},
|
|
{0, 150},
|
|
{500, 80},
|
|
{0, 300},
|
|
{430, 80},
|
|
{0, 150},
|
|
{380, 80},
|
|
{0, 600},
|
|
|
|
{660, 100},
|
|
{0, 150},
|
|
{660, 100},
|
|
{0, 300},
|
|
{660, 100},
|
|
{0, 300},
|
|
{510, 100},
|
|
{0, 100},
|
|
{660, 100},
|
|
{0, 300},
|
|
{770, 100},
|
|
{0, 550},
|
|
{380, 100},
|
|
{0, 575}
|
|
};
|
|
|
|
ISR(TIMER2_OVF_vect)
|
|
{
|
|
if(opgave == 2)
|
|
{
|
|
TCNT2 = 0;
|
|
PORTD = 0x0F;
|
|
|
|
clear_display();
|
|
|
|
_delay_ms(100);
|
|
|
|
display_text(1, "Overflow");
|
|
|
|
_delay_ms(500);
|
|
|
|
PORTD = 0x00;
|
|
}
|
|
else if(opgave == 3)
|
|
{
|
|
PORTD ^= 0x01;
|
|
|
|
if(flip)
|
|
{
|
|
TCNT2 = 255 - 117;
|
|
flip = 0;
|
|
}
|
|
else
|
|
{
|
|
TCNT2 = 255 - 195;
|
|
flip = 1;
|
|
}
|
|
}
|
|
else if(opgave == 4)
|
|
{
|
|
if(mario[current].frequency != 0)
|
|
PORTD ^= 0x01;
|
|
|
|
TCNT2 = (mario[current].frequency / 7);
|
|
}
|
|
}
|
|
|
|
void opgave2knop()
|
|
{
|
|
opgave = 2;
|
|
|
|
lcd_init();
|
|
|
|
DDRD = 0x0F;
|
|
|
|
TCNT2 = 245;
|
|
TCCR2 |= 0x06;
|
|
TIMSK |= (1<<6);
|
|
sei();
|
|
|
|
char str[15];
|
|
|
|
while(1)
|
|
{
|
|
sprintf(str, "%d", TCNT2);
|
|
|
|
display_text(0, str);
|
|
display_text(1, "---------");
|
|
|
|
_delay_ms(1000);
|
|
}
|
|
}
|
|
|
|
void opgave3()
|
|
{
|
|
opgave = 3;
|
|
flip = 0;
|
|
|
|
DDRD = 0xFF;
|
|
|
|
TCNT2 = 255 - 117;
|
|
TCCR2 |= 0x05;
|
|
TIMSK |= (1<<6);
|
|
sei();
|
|
|
|
while(1)
|
|
{
|
|
_delay_ms(10);
|
|
}
|
|
}
|
|
|
|
void opgaveextra()
|
|
{
|
|
opgave = 4;
|
|
count = 0;
|
|
current = 0;
|
|
|
|
DDRD = 0xFF;
|
|
DDRB = 0xFF;
|
|
|
|
PORTB = 0x01110000;
|
|
|
|
_delay_ms(500);
|
|
|
|
PORTB = 0;
|
|
|
|
TCNT2 = 0;
|
|
TCCR2 |= 0x03;
|
|
TIMSK |= (1<<6);
|
|
sei();
|
|
|
|
while(1)
|
|
{
|
|
PORTB = 0x10000000;
|
|
|
|
if(count > ((mario[current].delay / 10)*8))
|
|
{
|
|
current++;
|
|
|
|
count = 0;
|
|
|
|
if(current >= NELEMS(song))
|
|
current = 0;
|
|
}
|
|
|
|
_delay_ms(1);
|
|
|
|
PORTB = 0x00000000;
|
|
|
|
count++;
|
|
}
|
|
} |