/* --------------------------------------------------------------------------- ** This software is in the public domain, furnished "as is", without technical ** support, and with no warranty, express or implied, as to its usefulness for ** any purpose. ** ** ioisr.c ** ** Beschrijving: ISR on PORTD demonstrattion ** Target: AVR mcu ** Build: avr-gcc -std=c99 -Wall -O3 -mmcu=atmega128 -D F_CPU=8000000UL -c ioisr.c ** avr-gcc -g -mmcu=atmega128 -o ioisr.elf ioisr.o ** avr-objcopy -O ihex ioisr.elf ioisr.hex ** or type 'make' ** Author: dkroeske@gmail.com ** -------------------------------------------------------------------------*/ #define F_CPU 8000000UL //CPU op 8MHz #define NELEMS(x) (sizeof(x) / sizeof(x[0])) //Geeft het aantal elementen uit een array terug. #include #include #include int opdr; int count = 0; //Counter voor opgave 4 om bij te houden bij welke pattern in de array hij is int speed = 1; //De snelheid voor opgave 4 van de patronen /******************************************************************/ 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>1); else PORTC = (PORTC<<1); //Als het looplicht aan het einde is if (PORTC >= 0x80) { PORTC= 0x01; } //Als het looplicht aan het begin is else if (PORTC < 0x1) { PORTC= 0x80; } } void opgave3() { DDRC = 0x00; DDRD = 0xFF; int count = 0; while(1) { wait(150); //Button C0 & C1 if( (0x03 & PINC) == 0x03) { count = 0; } //Button C0 else if( (0x01 & PINC) != 0) { count++; } //Button C1 else if( (0x02 & PINC) != 0) { count--; } if(count > 15) { display(14, 1); count = 16; } else if(count < 0) { display(14, 1); count = -1; } else display(count, 0); } } void display(int d, int dot) { if(dot) PORTD = data[d] | 0b10000000; else PORTD = data[d]; } void opgave4(void) { DDRD = 0xFF; DDRC = 0x00; while(1) { //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 ) { speed = PINC; } //Schrijf de data van het huidige patroon naar PORTD PORTD = pattern[count].data; //Wacht het aantal milliseconde dat in het huidige patroon staat wait(pattern[count].delay * speed); count++; //patroon verder //als alle patronen geweest zijn, opnieuw beginnen if(count >= NELEMS(pattern)) { count = 0; } } }