2011-07-31, 13:51 #1
Subject: Snake - April 2009
Snake Aufgabe vom 04.09.
Korrektur und Kommentare erwünscht...
#include <avr/io.h>
#include <stdint.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
void init();
void showSnake(uint8_t pos, unsigned char len);
void active_wait(volatile unsigned int len);
static volatile uint8_t button = 0;
static uint8_t position = 0;
static unsigned char laenge = 1;
static volatile unsigned int len = 5000;
void main(void){
init();
sei();
while(1){
while(laenge != 6){
cli();
if(button == 0){
sei();
showSnake(position,laenge);
active_wait(len);
}
cli();
if(button == 1 && laenge > 1){
sei();
button = 0;
showSnake(position,laenge);
active_wait(len);
laenge -= 2;
}
position++;
position %= 6;
if(position == 0){
laenge++;
}
}
cli();
while(button == 0){
sleep_enable();
sei();
sleep_cpu();
sleep_disable();
cli();
}
button = 0;
position = 0;
laenge = 1;
sei();
}
}
ISR(INT0_vect){
button = 1;
}
void init(){
for(uint8_t a = 0; a<6,a++){
DDRB |=(1<<a);
PORTB &= ~(1<<a);
}
DDRD &= ~(1<<2);
PORTD |= (1<<2);
GICR |= (1<<INT0);
MCUCR |= (1<<ISC01);
MCUCR &= ~(1<<ISC00);
}
void showSnake(int8_t pos, unsigned char len){
PORTB = 0xff;
for(uint8_t a = 0; a<= len; a++){
PORTB &= ~(1<<((a+pos)%6));
}
}
void acitve_wait(volatile unsigned int len){
for(uint16_t a = 0; a<= len; a++){}
}
|

This post was edited on 2011-07-31, 14:30 by Leo.
|