Difference between revisions of "U23 2008/Gruppe2"

From C4 Wiki
Jump to: navigation, search
(uint8_t mode)
(Gruppe 2)
Line 69: Line 69:
 
  }
 
  }
  
 +
<nowiki>
 +
== '''UltraX8's Source (no guarantee^^)''' ==
 +
/*****************************************************
 +
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    : 2,000000 MHz
 +
Memory model        : Small
 +
External RAM size  : 0
 +
Data Stack size    : 256
 +
*****************************************************/
 +
/* Include the header files here */
 +
#include <avr/io.h>
 +
#include <util/delay.h>
 +
 +
/* Declare global variables here */
 +
const uint8_t mode          = 0;                            // running direction of LEDs or diagnostic mode (0-3)
 +
const uint8_t* sequence[]  = {1, 3, 4, 1, 3, 1, 2, 3};    // led sequence
 +
const uint16_t delay        = 100;                          // delay in ms
 +
 +
static void led_control(uint8_t mode, uint8_t* sequence, uint8_t lastled) {
 +
    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;}
 +
            switch (lastled) {
 +
                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;
 +
                default:
 +
            }
 +
            break;
 +
        case 1:    // upshift
 +
            if(lastled == 1){lastled = 4;}else{--lastled;}
 +
            switch (lastled){
 +
                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;
 +
                default:
 +
            }
 +
            break;
 +
        case 2:    // sequence 
 +
            switch (sequence[array_position]){
 +
                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;
 +
                default:
 +
            }
 +
            if(array_position <= 7){array_position = 0;}else{++array_position;}
 +
            break;   
 +
        default:    // all LEDs 1 (diagnostic mode)
 +
            ledset(1, 1, 1, 1);
 +
        }
 +
}
 +
/* Needs the LED configuration */
 +
static void ledset(uint8_t led1, uint8_t led2, uint8_t led3, uint8_t led4) {
 +
    if(led1){PORTD |= (1 << PC4);}          // set the first LED
 +
        else{PORTD &= (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);}
 +
}
 +
 +
static void mydelay(uint16_t delay) {
 +
    uint8_t passes;
 +
    uint16_t i;
 +
    passes = delay;
 +
    for(i = 0; i <= passes; i++)
 +
    {
 +
        _delay_loop_2(5000);                // delay
 +
    }
 +
}
 +
 +
/* Entry Point */
 +
void main(void) {
 +
    DDRC = _BV(PC4);                            // set PC4 as output
 +
    DDRD = _BV(PD3) | _BV(PD6) | _BV(PD7);      // set PD3, PD6 and PD7 as output
 +
       
 +
    while(1)                                  // never-ending loop
 +
    {
 +
        led_control(mode, sequence, lastled);  // call led_control
 +
        mydelay(delay);         
 +
    }
 +
}
 +
</nowiki>
  
 
== '''Bezeichnungen der LEDs (siehe Datenblatt)''' ==
 
== '''Bezeichnungen der LEDs (siehe Datenblatt)''' ==

Revision as of 17:56, 25 August 2008

Gruppe 2

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)
-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)

Sourcecode vom ersten Projektabend:

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

uint8_t globalconfig; 

static void led_control(uint8_t mode, uint8_t* sequence, uint8_t size, uint16_t delay) {
        switch (mode) {
                case 0: //rechtsshift
                        
                        break;
                case 1: //linksshift
                        
                        break;
                default:
                        for(uint8_t = 0; i<size; i++) {
                                ledset(sequence[i]);
                                mydelay(delay);
                        }
        }
} 
//Uebergabe ist die gewuenschte LED Konfiguration, die gesetzt werden soll
//Die Konfiguration wird als Binaerzahl uebergeben 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);
        }
}

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;
        //Blinkschleife
        while(1) {
                ledset(bla);
                mydelay(500);
                bla++;
        }
}

== '''UltraX8's Source (no guarantee^^)''' == /***************************************************** 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 : 2,000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ /* Include the header files here */ #include <avr/io.h> #include <util/delay.h> /* Declare global variables here */ const uint8_t mode = 0; // running direction of LEDs or diagnostic mode (0-3) const uint8_t* sequence[] = {1, 3, 4, 1, 3, 1, 2, 3}; // led sequence const uint16_t delay = 100; // delay in ms static void led_control(uint8_t mode, uint8_t* sequence, uint8_t lastled) { 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;} switch (lastled) { 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; default: } break; case 1: // upshift if(lastled == 1){lastled = 4;}else{--lastled;} switch (lastled){ 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; default: } break; case 2: // sequence switch (sequence[array_position]){ 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; default: } if(array_position <= 7){array_position = 0;}else{++array_position;} break; default: // all LEDs 1 (diagnostic mode) ledset(1, 1, 1, 1); } } /* Needs the LED configuration */ static void ledset(uint8_t led1, uint8_t led2, uint8_t led3, uint8_t led4) { if(led1){PORTD |= (1 << PC4);} // set the first LED else{PORTD &= (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);} } static void mydelay(uint16_t delay) { uint8_t passes; uint16_t i; passes = delay; for(i = 0; i <= passes; i++) { _delay_loop_2(5000); // delay } } /* Entry Point */ void main(void) { DDRC = _BV(PC4); // set PC4 as output DDRD = _BV(PD3) | _BV(PD6) | _BV(PD7); // set PD3, PD6 and PD7 as output while(1) // never-ending loop { led_control(mode, sequence, lastled); // call led_control mydelay(delay); } }

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.