AVR GCC Funktionsgenerator


// Titel     : Funktionsgenerator AVR Minimus -- Atmega32u2@16MHz
//----------------------------------------------------------------------
// Funktion  : Schaltet Portpins PD0-PD7 als DAC 8Bit
// Schaltung : R2R Netzwerk an PD0-PD7
//----------------------------------------------------------------------
// Prozessor : ATmega32u2
// Takt      : 16.0 MHz
// Sprache   : C
//----------------------------------------------------------------------
// Hardware  : PD5 LED blau, PD6 LED rot, PD7 Btn

#define  F_CPU 16000000  // Taktfrequenz des myAVR-Boards
#include <avr/io.h>     // AVR Register und Konstantendefinitionen
#include <util/delay.h> // Delay Funktion
#include <avr/pgmspace.h> // Program Space
#include <avr/interrupt.h> // Interruptfunktionen
//----------------------------------------------------------------------

int8_t choice=1;

static const int8_t sinustable[]  PROGMEM = 
{
126, 129, 132, 135, 138, 141, 144, 148,        //Anfang der positiven Halbwelle
151, 154, 157, 160, 163, 166, 168, 171,
174, 177, 180, 183, 185, 188, 191, 193,
196, 199, 201, 204, 206, 208, 211, 213,
215, 217, 219, 221, 223, 225, 227, 229,
231, 232, 234, 236, 237, 239, 240, 241,
242, 244, 245, 246, 247, 247, 248, 249,
250, 250, 251, 251, 251, 252, 252, 252,
252, 252, 252, 252, 251, 251, 251, 250,
250, 249, 248, 247, 247, 246, 245, 244,
242, 241, 240, 239, 237, 236, 234, 232,
231, 229, 227, 225, 223, 221, 219, 217,
215, 213, 211, 208, 206, 204, 201, 199,
196, 193, 191, 188, 185, 183, 180, 177,
174, 171, 168, 166, 163, 160, 157, 154,
151, 148, 144, 141, 138, 135, 132, 129,        //Ende der positiven Halbwelle
126, 123, 120, 117, 114, 111, 108, 104,        //Anfang der negativen Halbwelle
101,  98,  95,  92,  89,  86,  84,  81,
78,  75,  72,  69,  67,  64,  61,  59,
56,  53,  51,  48,  46,  44,  41,  39,
37,  35,  33,  31,  29,  27,  25,  23,
21,  20,  18,  16,  15,  13,  12,  11,
10,   8,   7,   6,   5,   5,   4,   3,
2,   2,   1,   1,   1,   0,   0,   0,
0,   0,   0,   0,   1,   1,   1,   2,
2,   3,   4,   5,   5,   6,   7,   8,
10,  11,  12,  13,  15,  16,  18,  20,
21,  23,  25,  27,  29,  31,  33,  35,
37,  39,  41,  44,  46,  48,  51,  53,
56,  59,  61,  64,  67,  69,  72,  75,
78,  81,  84,  86,  89,  92,  95,  98,
101, 104, 108, 111, 114, 117, 120, 123       //Ende der negativen Halbwelle
};


static const int8_t sawtoothtable[] PROGMEM =
{
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19, //   1-20
20,21,22,23,24,25,26,27,28,29,30,31,32,33,34,35,36,37,38,39, //  21-40
40,41,42,43,44,45,46,47,48,49,50,51,52,53,54,55,56,57,58,59, //  41-60
60,61,62,63,64,65,66,67,68,69,70,71,72,73,74,75,76,77,78,79, //  61-80
80,81,82,83,84,85,86,87,88,89,90,91,92,93,94,95,96,97,98,99, //  81-100
100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,116,117,118,119, // 101-120
120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,136,137,138,139, // 121-140
140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,156,157,158,159, // 141-160
160,161,162,163,164,165,166,167,168,169,170,171,172,173,174,175,176,177,178,179, // 161-180
180,181,182,183,184,185,186,187,188,189,190,191,192,193,194,195,196,197,198,199, // 181-200
200,201,202,203,204,205,206,207,208,209,210,211,212,213,214,215,216,217,218,219, // 201-220
220,221,222,223,224,225,226,227,228,229,230,231,232,233,234,235,236,237,238,239, // 221-240
240,241,242,243,244,245,246,247,248,249,250,251,252,253,254,255                  // 241-255
};

static const int8_t squaretable[] PROGMEM =
{
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //   1-20
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //  21-40
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //  41-60
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //  61-80
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, //  81-100
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 101-120
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 121-140
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 141-160
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 161-180
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 181-200
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 201-220
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, // 221-240
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0            // 241-255
};

static const int8_t triangletable[] PROGMEM =
{
0,2,4,6,8,10,12,14,16,18,20,22,24,26,28,30,32,34,36,38,                          //   1-20
40,42,44,46,48,50,52,54,56,58,60,62,64,66,68,70,72,74,76,                        //  21-40
78,80,82,84,86,88,90,92,94,96,98,100,102,104,106,108,110,112,114,116,            //  41-60
118,120,122,124,126,128,130,132,134,136,138,140,142,144,146,148,150,152,154,156, //  61-80
158,160,162,164,166,168,170,172,174,176,178,180,182,184,186,188,190,192,194,196, //  81-100
198,200,202,204,206,208,210,212,214,216,218,220,222,224,226,228,230,232,234,236, // 101-120
238,240,242,244,246,248,250,252,254,254,252,250,248,246,244,242,240,238,236,234, // 121-140
232,230,228,226,224,222,220,218,216,214,212,210,208,206,204,202,200,198,196,194, // 141-160
192,190,188,186,184,182,180,178,176,174,172,170,168,166,164,162,160,158,156,154, // 161-180
152,150,148,146,144,142,140,138,136,134,132,130,128,126,124,122,120,118,116,114, // 181-200
112,110,108,106,104,102,100,98,96,94,92,90,88,86,84,82,80,78,76,74,              // 201-220
72,70,68,66,64,62,60,58,56,54,52,50,48,46,44,42,40,38,36,34,                     // 221-240
32,30,28,26,24,22,20,18,16,14,12,10,8,6,4,2                                      // 241-255
};

static const int8_t randomtable[] PROGMEM =
{
188,201,121,216,21,58,99,183,36,81,143,173,116,41,229,254,238,101,126,49,  //   1-20
238,162,42,89,119,50,144,60,168,89,231,51,148,174,60,46,37,13,254,98,      //  21-40
165,173,174,139,165,242,218,107,244,37,61,82,254,52,102,209,23,39,207,216, //  41-60
69,219,35,103,60,92,81,194,200,187,18,15,22,0,234,237,83,40,151,90,        //  61-80
168,131,6,16,199,203,145,132,157,160,65,198,30,72,127,80,135,131,186,68,   //  81-100
103,232,104,47,110,169,243,52,45,218,7,216,43,169,44,167,134,62,58,101,    // 101-120
92,236,100,136,81,245,221,134,61,102,85,61,44,100,136,217,220,139,43,129,  // 121-140
193,85,195,12,0,126,176,177,208,7,228,42,252,80,245,53,174,95,171,239,     // 141-160
254,194,9,95,192,173,33,105,214,139,96,75,232,102,158,198,241,124,136,124, // 161-180
2,165,49,180,197,172,239,16,194,239,34,115,84,6,137,177,196,130,100,233,   // 181-200
244,215,140,239,146,121,80,125,216,64,46,206,50,199,182,181,83,247,231,36, // 201-220
221,243,40,102,250,193,18,218,74,58,148,19,97,191,94,190,9,235,158,5,      // 221-240
158,178,69,62,210,198,135,127,80,230,202,14,140,189,200                    // 241-255
};

void long_delay(uint16_t ms) {
       for(; ms>0; ms--) _delay_ms(1);
 }

ISR(INT7_vect)  //Interrupt 7 Hardwarebutton
{
		choice = choice++;
		PORTD ^= ( 1 << PD6 ); // LED toggle
		if (choice > 5){choice = 0;};
}

main ()
 {
    int16_t count;

    DDRD &= ~(1 << PD7);  	// als Eingang

    DDRD |= (1<<PD5);		// als Ausgang
    DDRD |= (1<<PD6);		// als Ausgang

    DDRB |= (1<<PB0);           // als Ausgang
    DDRB |= (1<<PB1);           // als Ausgang
    DDRB |= (1<<PB2);           // als Ausgang
    DDRB |= (1<<PB3);           // als Ausgang
    DDRB |= (1<<PB4);           // als Ausgang
    DDRB |= (1<<PB5);           // als Ausgang
    DDRB |= (1<<PB6);           // als Ausgang
    DDRB |= (1<<PB7);           // als Ausgang

    EICRB = (1 << ISC70); 
    EIMSK |= (1 << INT7); // INT7 erlauben
    sei();  // Interrupts aktivieren

    while(1)
    {

	switch (choice) 
	{
  	case 1: //Sinustabelle

		for(count = 0;count < 255; count++)
		{
		PORTB = pgm_read_word(&sinustable[count]);
		_delay_us(1);
		}
    	break;

  	case 2: //Sägezahntabelle
		for(count = 0;count < 255; count++)
		{
		PORTB = pgm_read_word(&sawtoothtable[count]);
		_delay_us(1);
		}
    	break;

  	case 3: //Rechtecktabelle
		for(count = 0;count < 255; count++)
		{
		PORTB = pgm_read_word(&squaretable[count]);
		_delay_us(1);
		}
    	break;

  	case 4: //Dreiecktabelle
		for(count = 0;count < 255; count++)
		{
		PORTB = pgm_read_word(&triangletable[count]);
		_delay_us(1);
		}
    	break;

  	case 5: //Zufallstabelle
		for(count = 0;count < 255; count++)
		{
		PORTB = pgm_read_word(&randomtable[count]);
		_delay_us(1);
		}
    	break;

  	default: //Default bei anderem Wert
		PORTD ^= ( 1 << PD5 ); // LED toggle
    	break;

	}

     }
 }
//----------------------------------------------------------------------

Cookie Consent mit Real Cookie Banner