Difference between revisions of "U23 2008/Gruppe2"

From C4 Wiki
Jump to: navigation, search
(Lösung zur Lauflicht-Aufgabe)
(Lösung zur Lauflicht-Aufgabe)
Line 222: Line 222:
 
----
 
----
  
==Lösung zur Lauflicht-Aufgabe==
+
==Lösung zur Lauflicht-Aufgabe [UltraX8]==
<source>
+
<source lang="c">
 
/*****************************************************
 
/*****************************************************
 
Project : ATmega168 - LED Sequence
 
Project : ATmega168 - LED Sequence

Revision as of 18:57, 1 September 2008

Gruppenchat:

u23group2@conference.koeln.ccc.de

Also einfach einen Jabber Account auf koeln.ccc.de oder jabber.ccc.de einrichten und schon gehts los. Wer zum Beispiel einen Gmail-Account hat, hat selbst schon einen Jabber Account und kann damit loslegen.

Mitglieder:


-kellertür (Mathias) (dunkles Haar, 12" Asus Notebook)
-UltraX8 (Arnaud) (Tom Tailor Polo-Shirt, blonde Haare)
-T06T (Thorsten mit H) (braune haare, braune augen, schwarze kleidung/humor)
-Thomas
-Lind (Bene) (ca. 190 groß, kurze Haare, schwarzes HardRock Cafe Shirt)


Erster Abend

Sourcecode vom ersten Projektabend:

#include <avr/io.h>
#include <util/delay.h>

uint8_t globalconfig;

//Uebergabe ist die gewünschte LED Konfiguration, die gesetzt werden soll
//Die Konfiguration wird als Binärzahl übergeben wobei nur die letzten vier stellen ausgewertet werden
static void ledset(uint8_t config) {
	globalconfig=config;
	PORTC=(config&0x01)<<PC4;
	PORTD=((config&0x02)<<(PD3-1))|((config&0x04)<<(PD6-2))|((config&0x08)<<(PD7-3));
}

//Delay in ms
static void mydelay(uint16_t delay){
	uint16_t i; 
	for (i = 0; i < delay; i++) {
		/* wait 4 * 65536 cycles */
		_delay_loop_2(5000);
	}
} 


static void led_control(uint8_t mode, uint8_t* sequence, uint8_t size, uint16_t delay) {
        while(1) {
		switch (mode) {
			case 0: //rechtsshift
				
				break;
			case 1: //linksshift
				
				break;
			default: {
				for(uint8_t i = 0; i<size; i++) {
					ledset(sequence[i]);
					mydelay(delay);
				}
				break;
			}
		}
	}
}


void main(void) {
	//Port Direction config
	DDRC = _BV(PC4); /* == 1<<7 == 128 */
	DDRD = _BV(PD3)|_BV(PD6)|_BV(PD7); /* == 1<<7 == 128 */

	
	uint8_t bla=0;
	
	//Definition der Blinkfolge
	uint8_t array[]={15,8,3,4};
	
	led_control(12,array ,4,500);
	
}


Bezeichnungen der LEDs (siehe Datenblatt)

LED1 => PC4
LED2 => PD3
LED3 => PD6
LED4 => PD7

Funktionen der Globalen Variablen

uint8_t mode

0 = links...rechts
1 = rechts...links
2 = Sequenz aus Array
3 = default => alle LEDs an bzw. 1

uint8_t* sequence

Sequenz nach Schema uint8_t*[] = {0, 3, 4, 1, 3, 1, 2, 0};
Nach 4 LED-Moves fängt die Sequenz an der Stelle, wo sie zuletzt war wieder mit LED1 an.

uint16_t delay

Delay der Sequenz in MS.


Zweiter Abend

Lösung zur Lauflicht-Aufgabe

<source lang="c">

  1. include <avr/io.h>
  2. include <util/delay.h>

/* Diese Funktion dient zum exakten Setzen eines bestimmten Bits und soll die Lesbarkeit des restlichen Codes erhöhen. Übergeben wird die Adresse des Speichers für den Register, die Stelle (0-7), die gesetzt werden soll und ein Wert dafür (0 oder 1)!!

  • /

static void bitset(uint8_t *reg, uint8_t digit, uint8_t value) { /* wenn value zu einem Wert ungleich null ausgewertet wird, dann wird das Bit an Stelle digit gesetzt sonst wird es auf Null gesetzt */ if(value) *reg=(*reg|_BV(digit)); else *reg=(*reg&(~_BV(digit))); }


/*Uebergabe ist die gewünschte LED Konfiguration, die gesetzt werden soll

 Die Konfiguration wird als Binärzahl Übergeben wobei nur die letzten vier stellen ausgewertet werden
  • /

static void ledset(uint8_t config) { //Die folgenden Befehle zerpflücken die ersten vier Bits in config und setzten sie in den entsprechenden Registern bitset(&PORTC,PC4,(config&0x01)); bitset(&PORTD,PD3,(config&0x02)); bitset(&PORTD,PD6,(config&0x04)); bitset(&PORTD,PD7,(config&0x08)); }

//Delay in ms static void mydelay(uint16_t delay){ uint16_t i; for (i = 0; i < delay; i++) { /* wait 4 * 5000 cycles */ _delay_loop_2(5000); } }

/*Prüft ob den Status eines Buttons. Eingabe 1-4 für Button 1 bis Button 4. Ausgabe 1=gedrückt sonst gleich 0

 Übernimmt auch das entprellen durch vergleiche im 10ms Takt bis zwei aufeinanderfolgende gleich sind
  • /

static uint8_t buttonstate(uint8_t button) { /*Einlesen des Buttons. Dazu Maskierung von PortC mit übergebenem Button. Von dem wird eins Abgezogen weil wir intuitiver Weise bei 1 anfangen die Buttons zu nummerieren.*/ uint8_t state=(!(PINC&_BV(button-1))); while (1) { mydelay(20); if(state==(!(PINC&_BV(button-1)))) return state; //Zustand ist gleich dem letzten Zustand -> Ausbruch aus der Schleife else state=(!(PINC&_BV(button-1))); //aktuellen Zustand speichern } }

/*Diese Funktion fragt auch den Status des Buttons ab wartet aber mit der Rückgabe bis der Button los gelassen

  wurde damit es nicht zu mehrfacher Ausführrung kommt wenn der Button länger gedrückt wird.
  • /

static uint8_t buttonstateextended(uint8_t button) { uint8_t state=(!(PINC&_BV(button-1))); //Button einlesen if(!(state)) return state; //Ist der Button nicht gedrückt wird das sofort zurück gegeben. mydelay(5); if(PINC&_BV(button-1)) return state; //Falls des nur ein Fehlalarm war und der Button gar nicht dedrückt ist

while (1) { //Warten auf das Loslassen des Buttons mydelay(5); if(PINC&_BV(button-1)) return state; //Wir geben den letzten Zustand=Gedrückt noch zurück } }

void main(void) { //Port Direction config DDRC = _BV(PC4); //LED 1 ist Ausgang DDRC &=~(_BV(PC0)|_BV(PC1)|_BV(PC2)|_BV(PC3)); //PIN0-PIN 4 von Port C sind Eingänge; DDRD = _BV(PD3)|_BV(PD6)|_BV(PD7);//LEDs 2-4 sind Ausgänge PORTC |= _BV(PC0)|_BV(PC1)|_BV(PC2)|_BV(PC3); //Pullups für die Buttons 1-4 einschalten

//Variablen für die Steuerung des Lauflichts uint8_t state=0; //Status 0-3 für die gerade leuchtende LED uint8_t direction = 1; //Laufrichtung 1= 0...3 0= 3...0 uint16_t delay=200; uint8_t enable=1;

while(1) { if(enable) { if(direction){ if(state==3) state=0; //Übertrag ab 4, dann wieder von vorne else state++; } else{ if(state==0) state=3; //0 abfangen, dann wieder von hinten else state--; } ledset(_BV(state)); //Hier ist fast noch mal die delay Funktion implementiert aber mit Abfrage der Taster for (uint16_t i = 0; i < delay; i++) { /* wait 4 * 5000 cycles */ _delay_loop_2(5000); if(buttonstateextended(1)) { enable^=1; //Schaltet Blinklicht ein oder aus ledset(0); } if(buttonstateextended(2)) { direction^=1; //Schaltet laufrichtung um } if(buttonstateextended(3)) { delay/=2; } if(buttonstateextended(4)) { delay*=2; //schneller } } }

if(buttonstateextended(1)) { enable^=1; //Schaltet Blinklicht ein oder aus ledset(0); } } }

</source>


Lösung zur Lauflicht-Aufgabe [UltraX8]

<source lang="c"> /***************************************************** Project : ATmega168 - LED Sequence Version : 1.0.0.1 Date  : 25.08.2008 Author  : Group 2 Company : C4 Comments:

Chip type  : ATmega168 Program type  : Application Clock frequency  : 20 MHz Memory model  : Small External RAM size  : 0 Data Stack size  : 256

                                                                                                          • /

/***************************************************** Possible instructions: mode_ex => The following modes are possible:

               0: left to right
               1: right to left
               2: sequence from array
               3: diagnostic mode (all LEDs 1)

sequence_ex => Set a sequence as a non ending loop for ex.:

               1, 3, 4, 1, 3, 1, 2, 3,...

delay_ex => Set the delay between the rotation in ms.

                                                                                                          • /

/* Include the header files here */

  1. include <avr/io.h>
  2. include <util/delay.h>

/* Declare global variables here */ uint8_t enable_ex = 1; // state of the moving light uint8_t mode_ex = 0; // running direction of LEDs or diagnostic mode (0-3) uint8_t sequence_ex[] = {1, 3, 4, 1, 3, 1, 2, 3}; // led sequence uint16_t delay_ex = 150; // delay in ms

/* Needs the delay in ms */ static void mydelay(uint16_t delay) {

   uint16_t i;
   for(i = 0; i <= delay; i++)                             //
   {
       _delay_loop_2(5000);                                // Wait for 5 000 cycles
   }

}

/* Needs the LED configuration */ static void ledset(uint8_t led1, uint8_t led2, uint8_t led3, uint8_t led4) {

   if(led1){PORTC |= (1 << PC4);}                          // set the first LED
       else{PORTC &= ~(1 << PC4);}
       
   if(led2){PORTD |= (1 << PD3);}                          // set the second LED
       else{PORTD &= ~(1 << PD3);}
       
   if(led3){PORTD |= (1 << PD6);}                          // set the third LED
       else{PORTD &= ~(1 << PD6);}
       
   if(led4){PORTD |= (1 << PD7);}                          // set the fourth LED
       else{PORTD &= ~(1 << PD7);}

}

/* Checks the status of a button and antibeats it with a delay of 10ms */ static uint8_t buttonstate(uint8_t button) {

   uint8_t state = (!(PINC &_BV(button - 1)));
   
   while(1) {
       if(state == (!(PINC&_BV(button - 1)))) {return state;}
       else {state = (!(PINC&_BV(button - 1)));}
   }

}

/* Checks the status of a button and waits until the button state is 0 */ static uint8_t buttonstateextended(uint8_t button) {

   uint8_t state = buttonstate(button);                    // checks the button
   if(!(state)) {return 0;}                                // Ist der Button nicht gedrückt wird das sofort zurück gegeben.

   while(1) {                                             // Warten auf das Loslassen des Buttons
       mydelay(5);
       if(PINC&_BV(button - 1)) {return state;}            // Wir geben den letzten Zustand=Gedrückt noch zurück
   }

}

static void setoneled(uint8_t led) {

   switch (led) {
       case 1:
           ledset(1, 0, 0, 0);                             // power on LED1
           break;
       case 2:
           ledset(0, 1, 0, 0);                             // power on LED2
           break;
       case 3:
           ledset(0, 0, 1, 0);                             // power on LED3
           break;
       case 4:
           ledset(0, 0, 0, 1);                             // power on LED4
           break;
   }

}

static void led_control(uint8_t mode, uint8_t* sequence) {

   static uint8_t lastled;                                 // includes the last LED with state 1
   static uint8_t array_position;                          // includes the last array position
   switch (mode) {
       case 0:                                            // Downshift
           if(lastled == 4){lastled = 1;}else{lastled++;}
           setoneled(lastled);
           break;
       case 1:                                            // Upshift
           if(lastled == 1){lastled = 4;}else{lastled--;}
           setoneled(lastled);
           break;
       case 2:                                            // Sequence
           if(array_position == 7){array_position = 0;}else{array_position++;}
           setoneled(sequence[array_position]);
           break;    
       case 3:                                            // All LEDs 1 (diagnostic mode)
           ledset(1, 1, 1, 1);
           break;
       }

}

/* React on key down events */ static void key_button(void) {

   if(buttonstateextended(1)) {                           // Enables or disables the moving light                             
       enable_ex ^= 1;                                     // reverse enable_ex on
       ledset(0, 0, 0, 0);                                 // disable the LEDs
   }
   
   if(buttonstateextended(2)) {                           // Changes the mode
       if(mode_ex == 3) {mode_ex = 0;}                     // The mode can not be greater than 3
           else {mode_ex++;}                               // Jump up to the next mode
   }
   if(buttonstateextended(3)) {                           // Speeds up the delay about 10ms
   delay_ex += 10;                                         // delay + 10ms
   }
   
   if(buttonstateextended(4)) {                           // Drags the delay about 10ms
   delay_ex -= 10;                                         // delay -10ms
   }

}

/* Entry Point */ int main(void) {

   DDRC = _BV(PC4);                                        // set PC4 as output (LED1)
   DDRD = _BV(PD3) | _BV(PD6) | _BV(PD7);                  // set PD3, PD6 and PD7 as output (LED2-LED4)
   
   DDRC &= ~(_BV(PC0) | _BV(PC1) | _BV(PC2) | _BV(PC3));   // set PC0 - PC3 as input (Button1-Button4)
   PORTC |= _BV(PC0) | _BV(PC1) | _BV(PC2) | _BV(PC3);     // activate pull-up for PC0 - PC3 (Button1-Button4)
       
    while(1)                                               // never-ending loop
    {
       if(enable_ex) {
           led_control(mode_ex, sequence_ex);              // call led_control
           mydelay(delay_ex);                              // call mydelay
       }
       key_button();                                   // call key_button           
    }

} </source>