- LiFePO4 Speicher Test         
Ergebnis 1 bis 10 von 24

Thema: Ping Pong umprogrammieren?

Hybrid-Darstellung

Vorheriger Beitrag Vorheriger Beitrag   Nächster Beitrag Nächster Beitrag
  1. #1
    Moderator Robotik Visionär Avatar von radbruch
    Registriert seit
    27.12.2006
    Ort
    Stuttgart
    Alter
    61
    Beiträge
    5.799
    Blog-Einträge
    8
    Hallo

    Um das Spiel zu testen kann man auch mit den Fingern auf P1 bis P4 oder C4 (Selbsttest) rumfummeln. Das Programm erkennt den Hautwiderstand dann als Poti ;)

    Meine Einstellungen beim KAMAvr, zusätzlich muss man dem Programm noch einmalig (klappt bei mir allerdings grad noch nicht) mitteilen, wo die WINAvr-Installation ist:

    Bild hier  

    Um die Makefiles braucht man sich dann nicht mehr kümmern, denn die werden automatisch erzeugt. Um ein neues Projekt anzulegen (oder um das aktuelle Projekt zu Clonen) speichere ich zuerst das Projekt mit [File->Save Project as..] und anschliessend die Datei mit [File->Save File as...] jeweils mit dem neuen Namen ab. Nach dem Speichern der Datei lasse ich dann noch die Projektdatei in den neuen Namen ändern (PopUp mit [Ja] beantworten) und fertig. Zusätzliche C-Dateien (wie z.B. asuro.c) werden über einen Rechtsklick auf "Files" (über dem test.c) und "Add File..." eingebunden. Die erzeugte Hex-Datei trägt immer den Projektnamen.

    Mit dem aktuellen Treiber wurde mein mySmartUSB als serieller Programmer eingebunden. Deshalb sollte meine Einstellung (über [Settings->AVRDUDE]) auch mit dem einfachen seriellen Programmer funktionieren:

    Bild hier  

    COM muss man an die eigenen Verhältnisse anpassen. (btw. hat auch mein HighTech-Board noch eine echte serielle Schnittstelle :)

    Gruß

    mic

    [Edit]
    Bild hier  
    http://www.youtube.com/watch?v=ofswO2OCq-U

    Leider ist das Video schlecht, aber mit etwas gutem Willen kann man die vier Helligkeitsstufen erkennen. Interessanterweise zeigen die blauen Phantomleds bei Sekunde drei und vier genau den gewollten Effekt;)

    Der ungeputzte Quellcode:
    Code:
    #define F_CPU 8000000UL
    
    #include <inttypes.h>
    #include <avr/interrupt.h>
    #include <util/delay.h>
    #include <avr/pgmspace.h>
    
    #define colors 4 // Anzahl der Farbebenen
    
    uint8_t bildspeicher[colors][15];
    volatile uint8_t col = 0;
    uint8_t x, y;
    
    void set(uint8_t x, uint8_t y, uint8_t c);
    
    int main(void)
    {
    	cli();
    
    	DDRC = 0x0f;
    	DDRB = 0xff;
    	DDRD = 0xff;
    
    	/*---------------------------------------------------------------------------
    	 * 8-Bit Timer TCCR0 für das Multiplexing der LEDs initialisieren
    	 * Es wird ca. alle 2 Mikrosekunden ein Overflow0 Interrupt ausgelöst
    	 * Berechnung: T = Vorteiler * Wertebereich Zähler / Taktfreuenz
    	 * = 64 * 256 / ( 8000000 Hz ) = 2,048 ms
    	 *---------------------------------------------------------------------------*/
    	TCCR0 |= (1<<CS01) | (0<<CS00);		// 8-bit Timer mit 1/8 Vorteiler !!!!!!!!!!
    	TIFR |= (1<<TOV0); 					// Clear overflow flag (TOV0)
    	TIMSK |= (1<<TOIE0); 				// timer0 will create overflow interrupt
    
    	sei();							    // Interrupts erlauben
    
       for(x=0; x<15; x++)
    		for(y=0; y<colors; y++)bildspeicher[y][x] = 0b10101010; // voll hell
       _delay_ms(300);
    	   for(x=0; x<12; x++)
    	   	for(y=0; y<10; y++)
    				set(x, y, (x/3)%colors+1); // Helligkeitsstufen anzeigen
       _delay_ms(2000);
       for(x=0; x<15; x++)
    		for(y=0; y<colors; y++)bildspeicher[y][x] = 0; // alle LEDs in allen Ebenen aus
    
    	while(1)
    	{
    	   for(x=0; x<12; x++)
    	   	for(y=0; y<10; y++)
    				set(x, y, ((x+1)^(y+3)^(col+5))%colors+1); // Zufallsfarbe ;)
          _delay_ms(100);
    	}
    	return (0);
    }
    
    SIGNAL (SIG_OVERFLOW0)
    {
    	static uint8_t ebene=0;
    	uint16_t ledval;
    	uint8_t portcout;
    	uint8_t portdout;
    
    	cli();							/* Interrupts verbieten */ // aha ;)
    
    	PORTD = 0;
    	PORTB = 0;
    	PORTC = 0;
    
    	if ( col == 0 ) PORTB &= ~(1 << 4);/* Bei der ersten Spalte eine 0 ausgeben (PB4 = 0) */
    		else PORTB |= (1 << 4);         /* Danach Einsen hinterherschicken (PB4 = 1) */
    
    	PORTB |= (1 << 3);             /* PB3 = 1 (cl) */
    	PORTB &= ~(1 << 3);            /* PB3 = 0 (!cl) */
    
    	PORTB |= (1 << 2);             /* PB2 = 1 (str) */
    	PORTB &= ~(1 << 2);            /* PB2 = 0 (!str) */
    
    	/*---------------------------------------------------
    	 * Daten der Spalte holen und auf die Ports verteilen
    	 *---------------------------------------------------*/
    	ledval = bildspeicher[ebene][col];
    	portdout = ledval & 0xff;      /* low byte */
    	portcout = portdout & 0x0f;    /* low nibble */
    	portdout = portdout & 0xf0;    /* high nibble */
    
    	PORTD = portdout & 0xff;
    	PORTC = portcout & 0xff;
    	if(col<4) ledval=bildspeicher[ebene][12];
    		else if(col<8) ledval=bildspeicher[ebene][13];
    			else ledval=bildspeicher[ebene][14];
    	PORTB = (ledval >> (col%4)*2) & 0x03;  /* high byte */
    
    	col++;
    	if(col>11)
    	{
    	   col=0;
    	   ebene++;
    		if(ebene == colors) ebene=0;
    	}
    
    	sei();						   /* Interrupts wieder erlauben */
    }
    void set(uint8_t x, uint8_t y, uint8_t c)
    {
    	uint8_t ebene, temp;
    
    	y = 9-y; // Koordinatennullpunkt unten links
    	if(y < 8)
    	   for(ebene=0; ebene<colors; ebene++)
    	   	if(c>ebene) bildspeicher[ebene][x] |= (1 << y);
    	   	   else bildspeicher[ebene][x] &= ~(1 << y);
    	else
    	{
    		if(x<4) temp = 12;
    	   	else if(x<8) temp = 13;
    	      	else temp = 14;
    		if(y & 1)
    			for(ebene=0; ebene<colors; ebene++)
    	   		if(c>ebene) bildspeicher[ebene][temp] |= (1<<((x%4)*2+1));  // y 10
    	   		   else bildspeicher[ebene][temp] &= ~(1<<((x%4)*2+1));
    		else
    			for(ebene=0; ebene<colors; ebene++)
    	   		if(c>ebene) bildspeicher[ebene][temp] |= (1<<((x%4)*2));  // y 9
    	   		   else bildspeicher[ebene][temp] &= ~(1<<((x%4)*2));
    	}
    }
    Geputzte Version:
    Code:
    // https://www.roboternetz.de/phpBB2/vi...=529702#529702 mic 15.12.2010
    
    #include <avr/interrupt.h>
    #include <util/delay.h>
    #include <inttypes.h>
    
    //#define F_CPU 8000000UL // macht KAMAvr automatisch
    #define colors 4 // Anzahl der Farbebenen
    
    // Einen Bildpunkt an x, y setzen. Werte für c: 0 ist aus, 1 ist dunkel, 4 ist hell
    void set(uint8_t x, uint8_t y, uint8_t c);
    
    volatile uint8_t col = 0;
    uint8_t x, y, bildspeicher[colors][15];
    
    int main(void)
    {
    	cli();
    
    	DDRB = 0xff;
    	DDRC = 0x0f;
    	DDRD = 0xff;
    
    	TCCR0 |= (1<<CS01) | (0<<CS00);	// 8-bit Timer mit 1/8 Vorteiler !!!!!!!!!!
    	TIFR |= (1<<TOV0); 					// Clear overflow flag (TOV0)
    	TIMSK |= (1<<TOIE0); 				// timer0 will create overflow interrupt
    	sei();							    	// Interrupts erlauben
    
       for(x=0; x<15; x++)
    		for(y=0; y<colors; y++)bildspeicher[y][x] = 0b10101010; // LEDs voll hell
       _delay_ms(300);
    
    	for(x=0; x<12; x++)
    	   for(y=0; y<10; y++)
    			set(x, y, (x/3)%colors+1); // Helligkeitsstufen anzeigen
       _delay_ms(2000);
    
       for(x=0; x<15; x++)
    		for(y=0; y<colors; y++)bildspeicher[y][x] = 0; // alle LEDs in allen Ebenen aus
    
    	while(1)
    	{
    	   for(x=0; x<12; x++)
    	   	for(y=0; y<10; y++)
    				set(x, y, (x^y^TCNT0)%colors+1); // Zufallsfarbe ;)
          _delay_ms(100);
    	}
    	return (0);
    }
    
    SIGNAL (SIG_OVERFLOW0)
    {
    	static uint8_t ebene=0;
    	uint16_t ledval;
    
    	PORTB &= ~0x03; // Nur die Pins der Displaymatrix werden auf Low gesetzt
    	PORTC &= ~0x0f;
    	PORTD &= ~0xf0;
    
    // Spalten
    	if(col) PORTB |= (1<<4);	/* Danach Einsen hinterherschicken (PB4 = 1) */
    		else PORTB &= ~(1<<4);	/* Bei der ersten Spalte eine 0 ausgeben (PB4 = 0) */
    	PORTB |= (1 << 3);        	/* PB3 = 1 (cl) */
    	PORTB &= ~(1 << 3);        /* PB3 = 0 (!cl) */
    	PORTB |= (1 << 2);         /* PB2 = 1 (str) */
    	PORTB &= ~(1 << 2);        /* PB2 = 0 (!str) */
    
    // Zeilen
    	ledval = bildspeicher[ebene][col]; // y 9 bis 2
    	PORTC = ledval & 0x0f;
    	PORTD = ledval & 0xf0;
    	ledval = bildspeicher[ebene][12+(col>>2)]; // y 1 und 0
    	PORTB = (ledval >> (col%4)*2) & 0x03;
    
    	col++;
    	if(col>11)
    	{
    	   col=0;
    	   ebene++;
    		if(ebene == colors) ebene=0;
    	}
    }
    
    void set(uint8_t x, uint8_t y, uint8_t c)
    {
    	uint8_t ebene;
    
    	y = 9-y;	// Koordinatennullpunkt unten links
    	if(y < 8) // y 9 bis 2
    	   for(ebene=0; ebene<colors; ebene++)
    	   	if(c>ebene) bildspeicher[ebene][x] |= (1 << y);
    	   	   else bildspeicher[ebene][x] &= ~(1 << y);
    	else // y 1 und 0
    		for(ebene=0; ebene<colors; ebene++)
    	   	if(c>ebene) bildspeicher[ebene][12+(x>>2)] |= (1<<((x%4)*2+(y&1)));
    	   		else bildspeicher[ebene][12+(x>>2)] &= ~(1<<((x%4)*2+(y&1)));
    }
    Code:
    Aufbau der 15 Bytes des Bildspeichers:
    
    0000 0000 0011 | Bytenummer
    0123 4567 8901 |
    
    0000 0000 0000 | Bit           \
    1111 1111 1111 |                | Port C
    2222 2222 2222 |                | Pin 0-3
    3333 3333 3333 |               /
    4444 4444 4444 |               \
    5555 5555 5555 |                | Port D
    6666 6666 6666 |                | Pin 4-7
    7777 7777 7777 |               /
    
    0246 0246 0246 | Bit           \ Port B
    1357 1357 1357 |               / Pin 0 und 1
    
    |12| |13| |14| | Bytenummer
    
    Koordinatennullpunkt von set(0,0, Helligkeit)
    ist links unten.
    Code:
    // Einfache Ansteuerung des PingPong-Spiels                        mic 20.12.2010
    
    // Das Programm sollte mit der orginalen Hardware des Spiels funktionieren.
    
    // Es werden drei Funktionen zur Verfügung gestellt:
    
    // cls() löscht das Display
    // set(x, y, c) setzt an x, y eine LED. Werte für c: 0 ist aus, 1 bis 4 die Helligkeit
    // readADC(Kanal) liest den ADC, P2 ist Kanal 6, P3 ist Kanal 7
    
    // Neben den vier Helligkeitsstufen sind die LEDs nun auch dimmbar, allerdings
    // nur alle zusammen. Möglich wird das durch den Timer2, den ich hier im
    // FastPWM-Mode betreibe und eine zusätzliche ISR. Wie gehabt, werden die LEDs beim
    // Bildaufbau über die Überlauf-ISR gesetzt. Gelöscht werden sie nun aber in der
    // Compare-ISR. Diese wird immer dann aufgerufen, wenn das Zählregister des Timers
    // den selben Inhalt wie das OCR2-Register hat.
    
    // Beim Nulldurchgang des Zählregisters werden die LEDs eingeschaltet, bei OCR2
    // wieder ausgeschaltet. Somit ist die Leuchtdauer umso größer, je höher der Wert
    // im OCR2-Register ist. 0 ist dunkel, 255 ist hell.
    
    #include <avr/interrupt.h>
    #include <util/delay.h>
    #include <inttypes.h>
    
    //#define F_CPU 8000000UL // macht KAMAvr automatisch
    #define colors 4 // Anzahl der Farbebenen
    
    void cls(void); // alle LEDs in allen Ebenen aus
    
    // Einen Bildpunkt an x, y setzen. Werte für c: 0 ist aus, 1 ist dunkel, 4 ist hell
    void set(uint8_t x, uint8_t y, uint8_t c);
    
    // Potiwerte einlesen, P2 ist Kanal 6, P3 ist Kanal 7
    uint16_t readADC(uint8_t channel);
    
    volatile uint8_t col = 0;
    uint8_t x, y, z, bildspeicher[colors][15];
    
    int main(void)
    {
    	cli();
    
    	DDRB = 0xff;
    	DDRC = 0x0f;
    	DDRD = 0xf0;
    
    	TCCR2 = (1<<CS21) | (0<<CS20);		// 8-bit Timer mit 1/8 Vorteiler
    	TCCR2 |= (1<<WGM21) | (1<<WGM20); 	// Fast PWM
    	TCCR2 |= (0<<COM21) | (0<<COM20); 	// no OC2-Pin
    	OCR2 = 20;                          // 0=dunkel, 255=hell
    	TIFR = (1<<OCF2) | (1<<TOV2); 		// Clear old flags
    	TIMSK |= (1<<TOIE2) | (1<<OCIE2);	// overflow and compare interrupt
    
     	// A/D Conversion (aus der asuro-Lib)
    	ADCSRA = (1 << ADEN) | (1 << ADPS2) | (1 << ADPS1); // clk/64
    
    	sei();							    	// Interrupts erlauben
    
    	for(x=0; x<12; x++)
    	   for(y=0; y<10; y++)
    			set(x, y, (x/3)%colors+1); // Helligkeitsstufen anzeigen
       _delay_ms(2000);
    
    	DDRD |= (1<<PD2); //GND für Potis
    	PORTD |= (1<<PD2);
    	while(1)
    	{
    	   z=readADC(6)/110+1;
    	   OCR2=readADC(7)/4;
    	   for(x=0; x<12; x++)
    	   {
    		   if(z==x) set(x, 0, 0); else set(x, 0, 2);
    			for(y=1; y<9; y++)
    	         if(z==x-1) set(x, y, 3);
    	         else if(z==x) set(x, y, 4);
    	         else if(z==x+1) set(x, y, 3);
    	         else set(x, y, 1);
    		   if(z==x) set(x, 9, 0); else set(x, 9, 2);
    		}
    		_delay_ms(50);
    	}
    	return (0);
    }
    
    void cls(void)
    {
    	uint8_t x, y;
    	for(x=0; x<15; x++)
    		for(y=0; y<colors; y++)bildspeicher[y][x] = 0;
    }
    void set(uint8_t x, uint8_t y, uint8_t c)
    {
    	uint8_t ebene;
    
    	y = 9-y;	// Koordinatennullpunkt unten links
    	if(y < 8) // y 9 bis 2
    	   for(ebene=0; ebene<colors; ebene++)
    	   	if(c>ebene) bildspeicher[ebene][x] |= (1 << y);
    	   	   else bildspeicher[ebene][x] &= ~(1 << y);
    	else // y 1 und 0
    		for(ebene=0; ebene<colors; ebene++)
    	   	if(c>ebene) bildspeicher[ebene][12+(x>>2)] |= (1<<((x%4)*2+(y&1)));
    	   		else bildspeicher[ebene][12+(x>>2)] &= ~(1<<((x%4)*2+(y&1)));
    }
    uint16_t readADC(uint8_t channel)
    {
    	ADMUX = (1 << REFS0) | (channel & 7);// AVCC reference with external capacitor
    	ADCSRA |= (1 << ADSC);					// Start conversion
    	while (!(ADCSRA & (1 << ADIF)));		// wait for conversion complete
    	ADCSRA |= (1 << ADIF);					// clear ADCIF
    	return(ADC);
    }
    SIGNAL (SIG_OUTPUT_COMPARE2)
    {
    	PORTB &= ~0x03; // Die Pins der Displaymatrix werden auf Low gesetzt
    	PORTC &= ~0x0f;
    	PORTD &= ~0xf4; // PD2 ist GND für Potis!
    }
    SIGNAL (SIG_OVERFLOW2)
    {
    	static uint8_t ebene=0;
    	uint16_t ledval, portb;
    
    // Spalten
    	if(col) PORTB |= (1<<4);	/* Danach Einsen hinterherschicken (PB4 = 1) */
    		else PORTB &= ~(1<<4);	/* Bei der ersten Spalte eine 0 ausgeben (PB4 = 0) */
    	PORTB |= (1 << 3);        	/* PB3 = 1 (cl) */
    	PORTB &= ~(1 << 3);        /* PB3 = 0 (!cl) */
    	PORTB |= (1 << 2);         /* PB2 = 1 (str) */
    	PORTB &= ~(1 << 2);        /* PB2 = 0 (!str) */
    
    // Zeilen
    	ledval = bildspeicher[ebene][12+(col>>2)]; // y 1 und 0
    	portb = (ledval >> (col%4)*2) & 0x03;
    	ledval = bildspeicher[ebene][col]; // y 9 bis 2
    	PORTC |= ledval & 0x0f;
    	PORTD |= ledval & 0xf0;
    	PORTB |= portb;
    
    	col++;
    	if(col>11)
    	{
    		col=0;
    		ebene++;
    		if(ebene == colors) ebene=0;
    	}
    }
    Angehängte Dateien Angehängte Dateien
    Bild hier  
    Atmel’s products are not intended, authorized, or warranted for use
    as components in applications intended to support or sustain life!

Stichworte

Berechtigungen

  • Neue Themen erstellen: Nein
  • Themen beantworten: Nein
  • Anhänge hochladen: Nein
  • Beiträge bearbeiten: Nein
  •  

Labornetzteil AliExpress