Not logged in. · Lost password · Register

All posts by Leo (1)

topic: Snake - April 2009  in the forum: 2. Semester Grundlagen der systemnahen Programmierung in C
Leo
Member since Mar 2011
1 post
Subject: Snake - April 2009
Snake Aufgabe vom 04.09.
Korrektur und Kommentare erwünscht...

  1.  
  2. #include <avr/io.h>
  3. #include <stdint.h>
  4. #include <avr/interrupt.h>
  5. #include <avr/sleep.h>
  6.  
  7. void init();
  8. void showSnake(uint8_t pos, unsigned char len);
  9. void active_wait(volatile unsigned int len);
  10.  
  11. static volatile uint8_t button = 0;
  12. static uint8_t position = 0;
  13. static unsigned char laenge = 1;
  14. static volatile unsigned int len = 5000;
  15.  
  16. void main(void){
  17.     init();
  18.     sei();
  19.     while(1){
  20.         while(laenge != 6){
  21.             cli();
  22.             if(button == 0){
  23.                 sei();
  24.                 showSnake(position,laenge);
  25.                 active_wait(len);
  26.                
  27.             }
  28.             cli();
  29.             if(button == 1 && laenge > 1){
  30.                 sei();
  31.                 button = 0;
  32.                
  33.                 showSnake(position,laenge);
  34.                 active_wait(len);
  35.                 laenge -= 2;
  36.                
  37.             }
  38.             position++;
  39.             position %= 6;
  40.             if(position == 0){
  41.                 laenge++;
  42.             }         
  43.         }
  44.         cli();
  45.         while(button == 0){
  46.             sleep_enable();
  47.             sei();
  48.             sleep_cpu();
  49.             sleep_disable();
  50.             cli();
  51.         }
  52.         button = 0;
  53.         position = 0;
  54.         laenge = 1;
  55.         sei();
  56.     }   
  57. }
  58.  
  59. ISR(INT0_vect){
  60.     button = 1;
  61. }
  62.  
  63. void init(){
  64.     for(uint8_t a = 0; a<6,a++){
  65.         DDRB |=(1<<a);
  66.         PORTB &= ~(1<<a);
  67.     }
  68.    
  69.     DDRD &= ~(1<<2);
  70.     PORTD |= (1<<2);
  71.     GICR |= (1<<INT0);
  72.     MCUCR |= (1<<ISC01);
  73.     MCUCR &= ~(1<<ISC00);
  74. }
  75.  
  76. void showSnake(int8_t pos, unsigned char len){
  77.     PORTB = 0xff;
  78.     for(uint8_t a = 0; a<= len; a++){
  79.         PORTB &= ~(1<<((a+pos)%6));
  80.     }
  81. }
  82.  
  83. void acitve_wait(volatile unsigned int len){
  84.     for(uint16_t a = 0; a<= len; a++){}
  85. }
This post was edited on 2011-07-31, 14:30 by Leo.
Close Smaller – Larger + Reply to this post:
Special characters:
Go to forum
Powered by the Unclassified NewsBoard software, 20110527-dev, © 2003-8 by Yves Goergen