PIC16F819

PIC16F819 – Datenblatt – Microchip

interne Oszillator Konfiguration

/*
OSCCON - internal Oscillator Register
=====================================
Bit Nr	Bit7	Bit6	Bit5	Bit4	Bit3	Bit2	Bit1	Bit0
Name	-	IRCF2	IRCF1	IRCF0	-	IOFS	-	-
I/O	-	R/W	R/W	R/W	-	R	-	-
Default	0	0	0	0	0	0	0	0
Project -	1	1 	0 	-	-	-	-

IRCF2:IRCF0: Internal Oscillator Frequency Select bits
111 = 8 MHz (8 MHz source drives clock directly)
110 = 4 MHz
101 = 2 MHz
100 = 1 MHz
011 = 500 kHz
010 = 250 kHz
001 = 125 kHz
000 = 31.25 kHz (INTRC source drives clock directly)

IOFS: INTOSC Frequency Stable bit
1 = Frequency is stable
0 = Frequency is not stable
*/

OSCCON = 0b01100000; // 4 MHz Clock

Teile diesen Beitrag:

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;

	}

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

Teile diesen Beitrag:

AVR GCC Attiny Sensor Modules


#define F_CPU 1000000L

#include <inttypes.h>
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

// global variables

const uint16_t pwmtable_8D[32]  PROGMEM = {0, 1, 2, 2, 2, 3, 3, 4, 5, 6, 7, 8, 10, 11,
                                    13, 16, 19, 23, 27, 32, 38, 45, 54, 64, 76,
                                    91, 108, 128, 152, 181, 215, 255};

int16_t tmp = 0;
int16_t step_time=100;      // delay in millisecond for one fading step
uint16_t result = 0; // Analog Input Value

// long delays

void my_delay(uint16_t milliseconds) {
    for(; milliseconds>0; milliseconds--) _delay_ms(1);
}

// 8-Bit PWM with 32 different settings

void pwm_8_32_up(uint16_t delay){

 if (tmp<=0)
{	
  for(tmp=0; tmp<=31; tmp++){
      OCR0B = pgm_read_word(pwmtable_8D+tmp);
      my_delay(delay);
    }
}

}

void pwm_8_32_down(uint16_t delay){

if (tmp>=31)
{
     for(tmp=31; tmp>=0; tmp--){
      OCR0B = pgm_read_word(pwmtable_8D+tmp);
      my_delay(delay);
    }
}

}

uint16_t readADC(uint8_t channel) {
	uint8_t i;
	uint16_t result = 0;

	// Den ADC aktivieren und Teilungsfaktor auf 64 stellen
	ADCSRA = (1<<ADEN) | (1<<ADPS2) | (1<<ADPS1);

	// Kanal des Multiplexers waehlen
	// Interne Referenzspannung verwenden (also 2,56 V)
	ADMUX = channel | (1<<REFS0);

	// Den ADC initialisieren und einen sog. Dummyreadout machen
	ADCSRA |= (1<<ADSC);
	while(ADCSRA & (1<<ADSC));

	// Jetzt 3x die analoge Spannung and Kanal channel auslesen
	// und dann Durchschnittswert ausrechnen.
	for(i=0; i<3; i++) {
		// Eine Wandlung
		ADCSRA |= (1<<ADSC);
		// Auf Ergebnis warten...
		while(ADCSRA & (1<<ADSC));

		result += ADCW;
	}

	// ADC wieder deaktivieren
	ADCSRA &= ~(1<<ADEN);

	result /= 3;

	return result;
}


int main(void)
{

    DDRB |= (1<<PB1);           // LED uses OC0B
    DDRB |= (1<<PB2);           // IR LED uses PB2

    PORTB = (1<<PB2);           // IR LED on

    // PWM Init
    TCCR0A=0xA3;
    TCCR0B=0x02; 

    while(1) {

        result = readADC(3);	//Auslesen der analogen Spannungen an Pin 3

	if (result > 240)
	{
	    pwm_8_32_up(step_time/8);
	}
	else
	{
	    pwm_8_32_down(step_time/8);
	}
    };

    return 0;
}

Teile diesen Beitrag:

Python

Python Programmierung — Version 2.6

sqlite DB

# Datenbank und Tabelle erzeugen
import sqlite3
connection = sqlite3.connect("lagerverwaltung.db")
cursor = connection.cursor()
cursor.execute("""CREATE TABLE lager (
    fachnummer INTEGER, seriennummer INTEGER,
    komponente TEXT, lieferant TEXT, reserviert INTEGER)""")

# Datensätze eintragen
cursor.execute("""INSERT INTO lager VALUES (
    2, 26071987, 'Gr222', 'FC222', 220)""")

# Datensätze auslesen
cursor.execute("""SELECT * FROM lager""")
row = cursor.fetchone()
while row:
     print row
     row = cursor.fetchone()

# Datensätze speichern
connection.commit()

csv import/export

import csv

# CSV Datei auslesen
reader = csv.reader(open("autos.csv", "rb"), delimiter=';')
for row in reader:
    print row

# CSV Datei schreiben
writer = csv.writer(open("autos.csv", "wb"))
writer.writerow(["marke", "modell", "leistung_in_ps"])
daten = (
    ["Volvo", "P245", "130"], ["Ford", "Focus", "90"],
    ["Mercedes", "CLK", "250"], ["Audi", "A6", "350"],
    )
writer.writerows(daten)

eMail Anzahl von Postfach anzeigen lassen

import poplib

M = poplib.POP3_SSL('pop.gmx.de')
M.user('test@gmx.de')
M.pass_('testpasswd')
status = M.stat()
# Tupel Nachrichten, Groesse
print status

Kodierung ganze einfach ^^

import base64

encpw = base64.b64encode('test')
print encpw

decpw = base64.b64decode(encpw)
print decpw
Teile diesen Beitrag:

AVR GCC

Installation Tools

apt-get install avr-gcc avr-libc avrdude

Fuses Config auslesen

avrdude -P /dev/ttyS0 -c ponyser -p m16 -v

Bsp. Default Fuses Atmega16

avrdude: Device signature = 0x1e9403
avrdude: safemode: lfuse reads as E1
avrdude: safemode: hfuse reads as 99

= 1 MHz RC Osci Takt intern

1tes Testprogramm

//----------------------------------------------------------------------
// Titel     : Beispiel LED an für myAVR-Board
//----------------------------------------------------------------------
// Funktion  : Schaltet alle 3 LEDs an
// Schaltung : PortB 0-2 an LEDs
//----------------------------------------------------------------------
// Prozessor : ATmega8/48/88/168
// Takt      : 3.6864 MHz
// Sprache   : C
//----------------------------------------------------------------------
#define  F_CPU 3686400  // Taktfrequenz des myAVR-Boards
#include <avr/io.h>;     // AVR Register und Konstantendefinitionen
#include <util/delay.h>; // Delay Funktion
//----------------------------------------------------------------------
void long_delay(uint16_t ms) {
       for(; ms>0; ms--) _delay_ms(1);
 }
main ()
 {
    DDRB=0xFF;        //PortB auf Ausgang konfigurieren
    do
    {
       PORTB=0x01;    // PortB High, LEDs on
       long_delay(100);
       PORTB=0x02;
       long_delay(100);
       PORTB=0x04;
       long_delay(100);
       PORTB=0x02;
       long_delay(100);
     } while (1);     // Mainloop
 }
//----------------------------------------------------------------------

Programm compilen und auf den ATMEGA8 flashen

Skript atmegaflash.sh

avr-gcc -O -mmcu=atmega8 -o $1.out $1.c
avr-strip $1.out
avr-objcopy -O ihex $1.out $1.hex
#avrdude -c sp12 -p m8 -P /dev/parport0
avrdude -p m8 -c sp12 -P /dev/parport0 -E noreset,vcc -U flash:w:$1.hex

Programm compilen und auf Atmega32u2 flashen

avr-gcc -O -mmcu=atmega32u2 -o avr-usb-162_led.out avr-usb-162_led.c 
avr-strip avr-usb-162_led.out 
avr-objcopy -O ihex avr-usb-162_led.out avr-usb-162_led.hex 

sudo dfu-programmer atmega32u2 erase 
sudo dfu-programmer atmega32u2 flash avr-usb-162_led.hex 
sudo dfu-programmer atmega32u2 start

Programm auf Atmega32u2 mit Arduino Bootloader flashen

avrdude -v -v -v -v -patmega32u2 -cavr109 -P/dev/ttyACM3 -b57600 -D -Uflash:w:avr32u2dac.hex:i

Atmel AVR AVRISP MKII Treiber wechseln

Zadig – USB driver installation made easy

Programme

A/D Wandler Test

#include <avr/io.h>

int main(void)
{
   DDRB = 0xFF;
   while (1)
   {
        uint8_t i;
        uint16_t result;
        int i2 = 60;
        ADMUX = (0<<REFS1)|(1<<REFS0); // Kanal waehlen
        ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen


        ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);  
	// Frequenzvorteiler
        // setzen auf 8 (1) und ADC aktivieren (1)

        /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
          also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
        ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
        while ( ADCSRA & (1<<ADSC) ) {
          ;     // auf Abschluss der Konvertierung warten
        }
        result = ADCW;  // ADCW muss einmal gelesen werden,
        // sonst wird Ergebnis der nächsten Wandlung
        // nicht übernommen.

        /* Eigentliche Messung - Mittelwert aus 4 aufeinanderfolgenden Wandlungen */
        result = 0;


        for( i=0; i<i2; i++ )
        {
         ADCSRA |= (1<<ADSC);            // eine Wandlung "single conversion"
         while ( ADCSRA & (1<<ADSC) ) {
           ;   // auf Abschluss der Konvertierung warten
         }
         result += ADCW; // Wandlungsergebnisse aufaddieren

        }
        ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)

         result /= i2; // Summe durch vier teilen = arithm. Mittelwert
         if (result < 128)
         {            
            PORTB = 0b10000000;
         }
         else if (result >= 128 && result < 256)   
         {
            PORTB = 0b01000000;
         }
         else if (result >= 256 && result < 384)   
         {
            PORTB = 0b00100000;
         }
         else if (result >= 384 && result < 512)   
         {
            PORTB = 0b00010000;
         }
         else if (result >= 512 && result < 640)   
         {
            PORTB = 0b00001000;
         }
         else if (result >= 640 && result < 768)   
         {
            PORTB = 0b00000100;
         }
         else if (result >= 768 && result < 896)   
         {
            PORTB = 0b00000010;
         }
         else
         {
            PORTB = 0b00000001;
         }
   }
   return 1;
}

Pollin Programmer Board

Flash mit avrdude und compile mit avr-gcc
Anschluss über serielle Schnittstelle

avr-gcc -O -mmcu=atmega8 -o $1.out $1.c
avr-strip $1.out
avr-objcopy -O ihex $1.out $1.hex
avrdude -p m8 -c ponyser -P /dev/ttyS0 -v -U flash:w:$1.hex

LED Muster Generator

//----------------------------------------------------------------------
// Titel     : LED schalten / Poti auswerten / Muster generieren
//----------------------------------------------------------------------
// Funktion  : Schaltet alle 6 LEDs nacheinander in verschiedenen Mustern an 
// Schaltung : PortB 0-5 an LEDs bzw. ULN2803, PortD 0 an Poti
//----------------------------------------------------------------------
// Prozessor : ATmega8
// Takt      : 3.6864 MHz
// Sprache   : C
//----------------------------------------------------------------------
#define  F_CPU 3686400  // Taktfrequenz des myAVR-Boards
#include <avr/io.h>     // AVR Register und Konstantendefinitionen
#include <util/delay.h> // Delay Funktion
//----------------------------------------------------------------------
void long_delay(uint16_t ms) {
       for(; ms>0; ms--) _delay_ms(1);
 }

int main(void)
{
   DDRB = 0xFF;
   int delay = 20;
   while (1)
   {
        uint8_t i;
        uint16_t result;
        int i2 = 60;
        ADMUX = (0<<REFS1)|(1<<REFS0); // Kanal waehlen
        ADMUX |= (1<<REFS1) | (1<<REFS0); // interne Referenzspannung nutzen


        ADCSRA = (1<<ADEN) | (1<<ADPS1) | (1<<ADPS0);  
	// Frequenzvorteiler
        // setzen auf 8 (1) und ADC aktivieren (1)

        /* nach Aktivieren des ADC wird ein "Dummy-Readout" empfohlen, man liest
          also einen Wert und verwirft diesen, um den ADC "warmlaufen zu lassen" */
        ADCSRA |= (1<<ADSC); // eine ADC-Wandlung
        while ( ADCSRA & (1<<ADSC) ) {
          ;     // auf Abschluss der Konvertierung warten
        }
        result = ADCW;  // ADCW muss einmal gelesen werden,
        // sonst wird Ergebnis der nächsten Wandlung
        // nicht übernommen.

        /* Eigentliche Messung - Mittelwert aus 4 aufeinanderfolgenden Wandlungen */
        result = 0;


        for( i=0; i<i2; i++ )
        {
         ADCSRA |= (1<<ADSC);            // eine Wandlung "single conversion"
         while ( ADCSRA & (1<<ADSC) ) {
           ;   // auf Abschluss der Konvertierung warten
         }
         result += ADCW; // Wandlungsergebnisse aufaddieren

        }
        ADCSRA &= ~(1<<ADEN); // ADC deaktivieren (2)

         result /= i2; // Summe durch vier teilen = arithm. Mittelwert
         if (result < 128)
         {            
		PORTB = 0b00000001;
		long_delay(delay);
		PORTB = 0b00000010;
		long_delay(delay);
		PORTB = 0b00000100;
		long_delay(delay);
		PORTB = 0b00001000;
		long_delay(delay);
		PORTB = 0b00010000;
		long_delay(delay);
		PORTB = 0b00100000;
		long_delay(delay);
         }
         else if (result >= 128 && result < 256)   
         {
		PORTB = 0b00000001;
		long_delay(delay);
		PORTB = 0b00000010;
		long_delay(delay);
		PORTB = 0b00000100;
		long_delay(delay);
		PORTB = 0b00001000;
		long_delay(delay);
		PORTB = 0b00010000;
		long_delay(delay);
		PORTB = 0b00100000;
		long_delay(delay);
		PORTB = 0b00010000;
		long_delay(delay);
		PORTB = 0b00001000;
		long_delay(delay);
		PORTB = 0b00000100;
		long_delay(delay);
		PORTB = 0b00000010;
		long_delay(delay);
         }
         else if (result >= 256 && result < 384)   
         {
		PORTB = 0b00111111;
		long_delay(10);
		PORTB = 0b00000000;
		long_delay(10);
         }
         else if (result >= 384 && result < 512)   
         {
		PORTB = 0b00111111;
		long_delay(20);
		PORTB = 0b00000000;
		long_delay(20);
         }
         else if (result >= 512 && result < 640)   
         {
		PORTB = 0b00111111;
		long_delay(30);
		PORTB = 0b00000000;
		long_delay(30);
         }
         else if (result >= 640 && result < 768)   
         {
		PORTB = 0b00000000;
         }
         else if (result >= 768 && result < 896)   
         {
		PORTB = 0b00000000;
         }
         else
         {
		PORTB = 0b11111111;
         }
   }
   return 1;
}

LED Fading

#include <avr/io.h>
#include <stdint.h>

// Analog/Digital Wandler initialisieren
void adc_init(void) 
{
        // externe Referenzspannung und AD-Wandlerkanal 0 (ADC0 / PC0) auswählen
        ADMUX = 0;

        // AD-Wandler einschalten und Prescaler = 64 einstellen (enstpricht 115 khz Wandlertakt)
        ADCSRA = (1<<ADEN)|(1<<ADPS2)|(1<<ADPS1);
}

// Timer einstellen
void timer_init(void)
{
 	// OC1A auf Ausgang
	DDRB = (1 << PB1 );

	// Was die einzelnen Einstellungen genau bedeuten steht im Datenblatt
	//
	// Timer 1 einstellen
        //  
        // Modus 14: Fast PWM, Top von ICR1
        //
        // >> WGM13    WGM12   WGM11    WGM10
        //     	1        1       1        0
        //
        // Timer Vorteiler: 1
	//
        // >> CS12     CS11    CS10
        //      0        0       1
        //
        //  Steuerung des Ausgangsport: Set at BOTTOM, Clear at match
	//
        // >> COM1A1   COM1A0
        //      1        0

	TCCR1A = (1<<COM1A1) | (1<<WGM11);
	TCCR1B = (1<<WGM13) | (1<<WGM12) | (1<<CS10);

	// Den Endwert (TOP) für den Zähler setzen
 	// Der Zähler zählt bis zu diesem Wert
	// Hier kann man unter anderem die Frequenz der PWM einstellen
  	ICR1 = 0x0080;

	// 0x0080 ist eine Abstufung mit 128 Stufen.
	// Problem: Wenn wir einen Motor mir PWM ansteuern vibrieren
	// seine Teile automatisch in der Frequenz mit.
	// Also legen wir diese in den nicht hörbaren Bereich.
	// 4MHz / 128 = 31250 Hz

}  

int main()
{
	// 16bit Variable
  	uint16_t buffer;

	// Timer für PWM initialisieren
	timer_init();

  	// A/D - Wandler initialisieren
  	adc_init();

	// OCR1A ist der Compare Wert
	// Wenn der Zähler diesen Wert erreicht, wird mit
	// obiger Konfiguration der OC1A Ausgang abgeschaltet
	// Sobald der Zähler wieder bei 0 startet, wird der	
	// Ausgang wieder auf 1 gesetzt
	//
	// Durch Verändern dieses Wertes, werden die unterschiedlichen
	// PWM Werte eingestellt.

 	// Startwert = 0. Wird in der while-Schleife gesetzt.
  	OCR1A = 0x0000;


	while( 1 )
	{
	// Wandlung vom analogen Eingang starten
        ADCSRA |= (1<<ADSC);

        // Warten bis die AD-Wandlung abgeschloßen ist
        while ( !(ADCSRA & (1<<ADIF)) )
        	;

        // AD-Wert auslesen.
        // ADCH muss als zweites gelesen werden, da nachdem ADCL gelesen wurde
        // das ADC-Register gesperrt ist bis ADCH auch ausgelesen wurde.
        // buffer = ADCL | (ADCH<<8);

        // Oder einfacher.
        buffer = ADC;

	// Da die PWM eine Auflösung von 8bit hat aber der buffer 10bit gross ist
	// müssen wir noch transformieren

	OCR1A = (buffer>>3);
   } 
}

UART

serielle Schnittstelle unter Linux einstellen:

setserial /dev/ttyS0 uart 16550A skip_test

uart.h

#ifndef _UART_H_
#define _UART_H_

#include <avr/io.h>

extern void uart_init();

static inline int
uart_putc (const uint8_t c)
{
    // Warten, bis UDR bereit ist für einen neuen Wert 
    while (!(UCSRA & (1 << UDRE)))
        ;

    // UDR Schreiben startet die Übertragung 
    UDR = c;

    return 1;
}

static inline int 
uart_puts(char *s){
  while(*s){
    uart_putc(*s);
  s++;
  }
}


static inline uint8_t
uart_getc_wait()
{
    // Warten, bis etwas empfangen wird 
    while (!(UCSRA & (1 << RXC)))
        ;

    // Das empfangene Zeichen zurückliefern 
    return UDR;
}

static inline int
uart_getc_nowait()
{
    // Liefer das empfangene Zeichen, falls etwas empfangen wurde; -1 sonst 
    return (UCSRA & (1 << RXC)) ? (int) UDR : -1;
}

#endif /* _UART_H_  */

uartgps.c

#define F_CPU 1000000 // Takt des MCU
#define BAUDRATE 4800UL //Definition als unsigned long, sonst gibt es Fehler

#include "uart.h"	// Include für serielle Schnittstelle UART
#include <avr/io.h>     // AVR Register und Konstantendefinitionen
#include <util/delay.h> // Delay Funktion
#include <string.h> 	// String Funktionen

void uart_init()
{
    uint16_t ubrr = (uint16_t) ((uint32_t) F_CPU/(16*BAUDRATE) - 1);

    UBRRH = (uint8_t) (ubrr>>8);
    UBRRL = (uint8_t) (ubrr);

    // UART Receiver und Transmitter anschalten 
    // Data mode 8N1, asynchron, no parity 
    UCSRB = (1 << RXEN) | (1 << TXEN);
    // UMSEL 0 = async UMSEL 1 = sync
    // UPM1/UPM0 = 00 Dis, 01 Res, 10 En Even Parity, 11 En Odd Parity
    // USBS Stop Bit 0 = 1 Bit, 1 = 2 Bit
    // UCSZ2/UCSZ1/UCSZ0 = 000 = 5 Bit 001 = 6 Bit 010 = 7 Bit 011 = 8 Bit
    UCSRC = (1 << URSEL) | (0 << UMSEL) | (0 <<UCSZ2) | (1 << UCSZ1) | (1 << UCSZ0) | (0 << UPM1) | (0 << UPM0) | (0 << USBS);

    // Flush Receive-Buffer (entfernen evtl. vorhandener ungültiger Werte) 
    do
    {
        UDR;
    }
    while (UCSRA & (1 << RXC));
}

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


main () 
{
	DDRA = 0xFF;
	uart_init();

	char rxzeichen;	
	int count = 0;

	while(1)
		{
			rxzeichen = uart_getc_wait();
			if (strcmp(rxzeichen,'$'))
			{
			    PORTA = (1 << PA0);
			    PORTA = (0 << PA0);
			}
		        uart_putc(rxzeichen);
		};
 }

GPS an serieller Schnittstelle ttyS0

stty -F /dev/ttyS0

stty -F /dev/ttyS0 4800

cat /dev/ttyS0 | hd

Navilock EM406a Protokollausschnitt

00000000  24 50 53 52 46 54 58 54  2c 56 65 72 73 69 6f 6e  |$PSRFTXT,Version|
00000010  3a 47 53 57 33 2e 32 2e  34 5f 33 2e 31 2e 30 30  |:GSW3.2.4_3.1.00|
00000020  2e 31 32 2d 53 44 4b 30  30 33 50 31 2e 30 30 61  |.12-SDK003P1.00a|
00000030  20 0a 0a 24 50 53 52 46  54 58 54 2c 56 65 72 73  | ..$PSRFTXT,Vers|
00000040  69 6f 6e 32 3a 46 2d 47  50 53 2d 30 33 2d 30 37  |ion2:F-GPS-03-07|
00000050  30 31 33 31 33 0a 0a 24  50 53 52 46 54 58 54 2c  |01313..$PSRFTXT,|
00000060  56 65 72 73 69 6f 6e 33  3a 4e 41 56 49 4c 4f 43  |Version3:NAVILOC|
00000070  4b 0a 0a 24 50 53 52 46  54 58 54 2c 57 41 41 53  |K..$PSRFTXT,WAAS|
00000080  20 44 69 73 61 62 6c 65  0a 0a 24 50 53 52 46 54  | Disable..$PSRFT|
00000090  58 54 2c 54 4f 57 3a 20  20 31 31 38 34 34 33 0a  |XT,TOW:  118443.|
000000a0  0a 24 50 53 52 46 54 58  54 2c 57 4b 3a 20 20 20  |.$PSRFTXT,WK:   |
000000b0  31 35 35 35 0a 0a 24 50  53 52 46 54 58 54 2c 50  |1555..$PSRFTXT,P|
000000c0  4f 53 3a 20 20 36 33 37  38 31 33 37 20 30 20 30  |OS:  6378137 0 0|
000000d0  0a 0a 24 50 53 52 46 54  58 54 2c 43 4c 4b 3a 20  |..$PSRFTXT,CLK: |
000000e0  20 39 36 32 35 30 0a 0a  24 50 53 52 46 54 58 54  | 96250..$PSRFTXT|
000000f0  2c 43 48 4e 4c 3a 20 31  32 0a 0a 24 50 53 52 46  |,CHNL: 12..$PSRF|
00000100  54 58 54 2c 42 61 75 64  20 72 61 74 65 3a 20 34  |TXT,Baud rate: 4|
00000110  38 30 30 20 0a 0a 24 47  50 47 47 41 2c 30 38 35  |800 ..$GPGGA,085|
00000120  33 35 30 2e 35 39 31 2c  2c 2c 2c 2c 30 2c 30 30  |350.591,,,,,0,00|
00000130  2c 2c 2c 4d 2c 30 2e 30  2c 4d 2c 2c 30 30 30 30  |,,,M,0.0,M,,0000|
00000140  2a 35 30 0a 0a 24 47 50  47 53 41 2c 41 2c 31 2c  |*50..$GPGSA,A,1,|
00000150  2c 2c 2c 2c 2c 2c 2c 2c  2c 2c 2c 2c 2c 2c 2a 31  |,,,,,,,,,,,,,,*1|
00000160  45 0a 0a 24 47 50 52 4d  43 2c 30 38 35 33 35 30  |E..$GPRMC,085350|
00000170  2e 35 39 31 2c 56 2c 2c  2c 2c 2c 2c 2c 32 36 31  |.591,V,,,,,,,261|
00000180  30 30 39 2c 2c 2a 32 35  0a 0a 24 47 50 56 54 47  |009,,*25..$GPVTG|
00000190  2c 2c 54 2c 2c 4d 2c 2c  4e 2c 2c 4b 2a 34 45 0a  |,,T,,M,,N,,K*4E.|
000001a0  0a 24 47 50 47 47 41 2c  30 38 35 33 35 31 2e 35  |.$GPGGA,085351.5|
000001b0  33 36 2c 2c 2c 2c 2c 30  2c 30 30 2c 2c 2c 4d 2c  |36,,,,,0,00,,,M,|
000001c0  30 2e 30 2c 4d 2c 2c 30  30 30 30 2a 35 43 0a 0a  |0.0,M,,0000*5C..|
000001d0  24 47 50 47 53 41 2c 41  2c 31 2c 2c 2c 2c 2c 2c  |$GPGSA,A,1,,,,,,|
000001e0  2c 2c 2c 2c 2c 2c 2c 2c  2c 2a 31 45 0a 0a 24 47  |,,,,,,,,,*1E..$G|
000001f0  50 52 4d 43 2c 30 38 35  33 35 31 2e 35 33 36 2c  |PRMC,085351.536,|
00000200  56 2c 2c 2c 2c 2c 2c 2c  32 36 31 30 30 39 2c 2c  |V,,,,,,,261009,,|
00000210  2a 32 39 0a 0a 24 47 50  56 54 47 2c 2c 54 2c 2c  |*29..$GPVTG,,T,,|
00000220  4d 2c 2c 4e 2c 2c 4b 2a  34 45 0a 0a 24 47 50 47  |M,,N,,K*4E..$GPG|
00000230  47 41 2c 30 38 35 33 35  32 2e 35 33 37 2c 2c 2c  |GA,085352.537,,,|
00000240  2c 2c 30 2c 30 30 2c 2c  2c 4d 2c 30 2e 30 2c 4d  |,,0,00,,,M,0.0,M|
00000250  2c 2c 30 30 30 30 2a 35  45 0a 0a 24 47 50 47 53  |,,0000*5E..$GPGS|
00000260  41 2c 41 2c 31 2c 2c 2c  2c 2c 2c 2c 2c 2c 2c 2c  |A,A,1,,,,,,,,,,,|
00000270  2c 2c 2c 2c 2a 31 45 0a  0a 24 47 50 52 4d 43 2c  |,,,,*1E..$GPRMC,|
00000280  30 38 35 33 35 32 2e 35  33 37 2c 56 2c 2c 2c 2c  |085352.537,V,,,,|
00000290  2c 2c 2c 32 36 31 30 30  39 2c 2c 2a 32 42 0a 0a  |,,,261009,,*2B..|
000002a0  24 47 50 56 54 47 2c 2c  54 2c 2c 4d 2c 2c 4e 2c  |$GPVTG,,T,,M,,N,|
000002b0  2c 4b 2a 34 45 0a 0a 24  47 50 47 47 41 2c 30 38  |,K*4E..$GPGGA,08|
000002c0  35 33 35 33 2e 35 33 39  2c 2c 2c 2c 2c 30 2c 30  |5353.539,,,,,0,0|
000002d0  30 2c 2c 2c 4d 2c 30 2e  30 2c 4d 2c 2c 30 30 30  |0,,,M,0.0,M,,000|
000002e0  30 2a 35 31 0a 0a 24 47  50 47 53 41 2c 41 2c 31  |0*51..$GPGSA,A,1|
000002f0  2c 2c 2c 2c 2c 2c 2c 2c  2c 2c 2c 2c 2c 2c 2c 2a  |,,,,,,,,,,,,,,,*|
00000300  31 45 0a 0a 24 47 50 52  4d 43 2c 30 38 35 33 35  |1E..$GPRMC,08535|
00000310  33 2e 35 33 39 2c 56 2c  2c 2c 2c 2c 2c 2c 32 36  |3.539,V,,,,,,,26|
00000320  31 30 30 39 2c 2c 2a 32  34 0a 0a 24 47 50 56 54  |1009,,*24..$GPVT|
00000330  47 2c 2c 54 2c 2c 4d 2c  2c 4e 2c 2c 4b 2a 34 45  |G,,T,,M,,N,,K*4E|
00000340  0a 0a 24 47 50 47 47 41  2c 30 38 35 33 35 34 2e  |..$GPGGA,085354.|
00000350  35 34 33 2c 2c 2c 2c 2c  30 2c 30 30 2c 2c 2c 4d  |543,,,,,0,00,,,M|
00000360  2c 30 2e 30 2c 4d 2c 2c  30 30 30 30 2a 35 42 0a  |,0.0,M,,0000*5B.|
00000370  0a 24 47 50 47 53 41 2c  41 2c 31 2c 2c 2c 2c 2c  |.$GPGSA,A,1,,,,,|
00000380  2c 2c 2c 2c 2c 2c 2c 2c  2c 2c 2a 31 45 0a 0a 24  |,,,,,,,,,,*1E..$|
00000390  47 50 47 53 56 2c 33 2c  31 2c 31 32 2c 32 30 2c  |GPGSV,3,1,12,20,|
000003a0  30 30 2c 30 30 30 2c 2c  31 30 2c 30 30 2c 30 30  |00,000,,10,00,00|
000003b0  30 2c 2c 33 31 2c 30 30  2c 30 30 30 2c 2c 32 37  |0,,31,00,000,,27|
000003c0  2c 30 30 2c 30 30 30 2c  2a 37 43 0a 0a 24 47 50  |,00,000,*7C..$GP|
000003d0  47 53 56 2c 33 2c 32 2c  31 32 2c 31 39 2c 30 30  |GSV,3,2,12,19,00|
000003e0  2c 30 30 30 2c 2c 30 37  2c 30 30 2c 30 30 30 2c  |,000,,07,00,000,|
000003f0  2c 30 34 2c 30 30 2c 30  30 30 2c 2c 32 34 2c 30  |,04,00,000,,24,0|
00000400  30 2c 30 30 30 2c 2a 37  36 0a 0a 24 47 50 47 53  |0,000,*76..$GPGS|
00000410  56 2c 33 2c 33 2c 31 32  2c 31 36 2c 30 30 2c 30  |V,3,3,12,16,00,0|
00000420  30 30 2c 2c 32 38 2c 30  30 2c 30 30 30 2c 2c 32  |00,,28,00,000,,2|
00000430  36 2c 30 30 2c 30 30 30  2c 2c 32 39 2c 30 30 2c  |6,00,000,,29,00,|
00000440  30 30 30 2c 2a 37 38 0a  0a 24 47 50 52 4d 43 2c  |000,*78..$GPRMC,|
00000450  30 38 35 33 35 34 2e 35  34 33 2c 56 2c 2c 2c 2c  |085354.543,V,,,,|
00000460  2c 2c 2c 32 36 31 30 30  39 2c 2c 2a 32 45 0a 0a  |,,,261009,,*2E..|
00000470  24 47 50 56 54 47 2c 2c  54 2c 2c 4d 2c 2c 4e 2c  |$GPVTG,,T,,M,,N,|
00000480  2c 4b 2a 34 45 0a 0a 24  47 50 47 47 41 2c 30 38  |,K*4E..$GPGGA,08|
00000490  35 33 35 35 2e 35 34 31  2c 2c 2c 2c 2c 30 2c 30  |5355.541,,,,,0,0|
000004a0  30 2c 2c 2c 4d 2c 30 2e  30 2c 4d 2c 2c 30 30 30  |0,,,M,0.0,M,,000|
000004b0  30 2a 35 38 0a 0a 24 47  50 47 53 41 2c 41 2c 31  |0*58..$GPGSA,A,1|
000004c0  2c 2c 2c 2c 2c 2c 2c 2c  2c 2c 2c 2c 2c 2c 2c 2a  |,,,,,,,,,,,,,,,*|
000004d0  31 45 0a 0a 24 47 50 52  4d 43 2c 30 38 35 33 35  |1E..$GPRMC,08535|
000004e0  35 2e 35 34 31 2c 56 2c  2c 2c 2c 2c 2c 2c 32 36  |5.541,V,,,,,,,26|
000004f0  31 30 30 39 2c 2c 2a 32  44 0a 0a 24 47 50 56 54  |1009,,*2D..$GPVT|
00000500  47 2c 2c 54 2c 2c 4d 2c  2c 4e 2c 2c 4b 2a 34 45  |G,,T,,M,,N,,K*4E|
00000510  0a 0a 24 47 50 47 47 41  2c 30 38 35 33 35 36 2e  |..$GPGGA,085356.|
00000520  35 33 37 2c 2c 2c 2c 2c  30 2c 30 30 2c 2c 2c 4d  |537,,,,,0,00,,,M|
00000530  2c 30 2e 30 2c 4d 2c 2c  30 30 30 30 2a 35 41 0a  |,0.0,M,,0000*5A.|
00000540  0a 24 47 50 47 53 41 2c  41 2c 31 2c 2c 2c 2c 2c  |.$GPGSA,A,1,,,,,|
00000550  2c 2c 2c 2c 2c 2c 2c 2c  2c 2c 2a 31 45 0a 0a 24  |,,,,,,,,,,*1E..$|
00000560  47 50 52 4d 43 2c 30 38  35 33 35 36 2e 35 33 37  |GPRMC,085356.537|
00000570  2c 56 2c 2c 2c 2c 2c 2c  2c 32 36 31 30 30 39 2c  |,V,,,,,,,261009,|
00000580  2c 2a 32 46 0a 0a 24 47  50 56 54 47 2c 2c 54 2c  |,*2F..$GPVTG,,T,|
00000590  2c 4d 2c 2c 4e 2c 2c 4b  2a 34 45 0a 0a 24 47 50  |,M,,N,,K*4E..$GP|
000005a0  47 47 41 2c 30 38 35 33  35 37 2e 35 34 33 2c 2c  |GGA,085357.543,,|
000005b0  2c 2c 2c 30 2c 30 30 2c  2c 2c 4d 2c 30 2e 30 2c  |,,,0,00,,,M,0.0,|
000005c0  4d 2c 2c 30 30 30 30 2a  35 38 0a 0a 24 47 50 47  |M,,0000*58..$GPG|
000005d0  53 41 2c 41 2c 31 2c 2c  2c 2c 2c 2c 2c 2c 2c 2c  |SA,A,1,,,,,,,,,,|
000005e0  2c 2c 2c 2c 2c 2a 31 45  0a 0a 24 47 50 52 4d 43  |,,,,,*1E..$GPRMC|
000005f0  2c 30 38 35 33 35 37 2e  35 34 33 2c 56 2c 2c 2c  |,085357.543,V,,,|
00000600  2c 2c 2c 2c 32 36 31 30  30 39 2c 2c 2a 32 44 0a  |,,,,261009,,*2D.|
00000610  0a 24 47 50 56 54 47 2c  2c 54 2c 2c 4d 2c 2c 4e  |.$GPVTG,,T,,M,,N|
00000620  2c 2c 4b 2a 34 45 0a 0a  24 47 50 47 47 41 2c 30  |,,K*4E..$GPGGA,0|
00000630  38 35 33 35 38 2e 35 33  37 2c 2c 2c 2c 2c 30 2c  |85358.537,,,,,0,|
00000640  30 30 2c 2c 2c 4d 2c 30  2e 30 2c 4d 2c 2c 30 30  |00,,,M,0.0,M,,00|
00000650  30 30 2a 35 34 0a 0a 24  47 50 47 53 41 2c 41 2c  |00*54..$GPGSA,A,|
00000660  31 2c 2c 2c 2c 2c 2c 2c  2c 2c 2c 2c 2c 2c 2c 2c  |1,,,,,,,,,,,,,,,|
00000670  2a 31 45 0a 0a 24 47 50  52 4d 43 2c 30 38 35 33  |*1E..$GPRMC,0853|
00000680  35 38 2e 35 33 37 2c 56  2c 2c 2c 2c 2c 2c 2c 32  |58.537,V,,,,,,,2|
00000690  36 31 30 30 39 2c 2c 2a  32 31 0a 0a 24 47 50 56  |61009,,*21..$GPV|
000006a0  54 47 2c 2c 54 2c 2c 4d  2c 2c 4e 2c 2c 4b 2a 34  |TG,,T,,M,,N,,K*4|
000006b0  45 0a 0a 24 47 50 47 47  41 2c 30 38 35 33 35 39  |E..$GPGGA,085359|
000006c0  2e 35 33 37 2c 2c 2c 2c  2c 30 2c 30 30 2c 2c 2c  |.537,,,,,0,00,,,|
000006d0  4d 2c 30 2e 30 2c 4d 2c  2c 30 30 30 30 2a 35 35  |M,0.0,M,,0000*55|
000006e0  0a 0a 24 47 50 47 53 41  2c 41 2c 31 2c 2c 2c 2c  |..$GPGSA,A,1,,,,|

LCD Routine

#include <avr/io.h>
#define TAKT 1000000ul
#define LCDPEN PORTD
#define LCDEN PD3
#define LCDPRS PORTD
#define LCDRS PD2
#define LCDPDAT PORTD
#define LCDBUS 'h'

unsigned char curpos = 0;
unsigned char i = 0;
unsigned int count = 0;
unsigned int j = 0;

void wartexms(unsigned int faktor)
{
	for (j = 0; j < faktor; j++)
	{
		for(count = TAKT/4000ul; count > 0; count--);
	}
}

void lcd4com(unsigned char x)
{
	#if LCDBUS == 'l'
	x = (x << 4) | (x >> 4);
	#endif
	LCDPDAT = x;
	LCDPRS &= ~(1 << LCDRS);
	LCDPEN |= (1 << LCDEN);
	wartexms(5);
	LCDPEN &= ~(1 << LCDEN);
	x = (x << 4) | (x >> 4);
	LCDPDAT = x;
	LCDPRS &= ~(1 << LCDRS);
	LCDPEN |= (1 << LCDEN);
	wartexms(5);
	LCDPEN &= ~(1 << LCDEN);
	LCDPDAT = 0;
	wartexms(10);
}

void lcd4put(unsigned char x)
{
	#if LCDBUS == 'l'
	x = (x << 4) | (x >> 4);
	#endif
	LCDPDAT = x;
	LCDPRS |= (1 << LCDRS);
	LCDPEN |= (1 << LCDEN);
	wartexms(5);
	LCDPEN &= ~(1 << LCDEN);
	x = (x << 4) | (x >> 4);
	LCDPDAT = x;
	LCDPRS |= (1 << LCDRS);
        LCDPEN |= (1 << LCDEN);
	wartexms(5);
	LCDPEN &= ~(1 << LCDEN);
	LCDPDAT = 0;
	wartexms(5);
}

void lcd4ini(void)
{
	wartexms(500);
	lcd4com(0x33);
	lcd4com(0x32);
	lcd4com(0x28);
	lcd4com(0x0e);
	lcd4com(0x06);
	lcd4com(0x02);
	lcd4com(0x01);
}

void lcd4cur(void)
{
	curpos++;
	if (curpos == 0x10) {curpos = 0x40; lcd4com(0x80 | 0x40); }
	if (curpos == 0x50) {curpos = 0x10; lcd4com(0x80 | 0x10); }
	if (curpos == 0x20) {curpos = 0x50; lcd4com(0x80 | 0x50); }
	if (curpos == 0x60) {curpos = 0x00; lcd4com(0x80 | 0x00); }
}

void lcd4puts(unsigned char *zeiger)
{
	while(*zeiger != 0) {lcd4put(*zeiger++); lcd4cur();}
}

int main(void)
{
	unsigned char meldung [] = "Willkommen";
	DDRD = 0xff;
	LCDPEN &= ~(1 << LCDEN);
	DDRD |= (1 << LCDEN);
	lcd4ini();
	lcd4puts(meldung);
	while(1)
	{
	}
	return 0;
}

Sinuswerte

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

Funktionsgenerator mit Atmega32u2 Sinus / Dreieck

//----------------------------------------------------------------------
// 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
//----------------------------------------------------------------------
#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
//----------------------------------------------------------------------

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

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

main ()
 {
    int8_t count;

    DDRD |= (1<<PD0);           // als Ausgang
    DDRD |= (1<<PD1);           // als Ausgang
    DDRD |= (1<<PD2);           // als Ausgang
    DDRD |= (1<<PD3);           // als Ausgang
    DDRD |= (1<<PD4);           // als Ausgang
    DDRD |= (1<<PD5);           // als Ausgang
    DDRD |= (1<<PD6);           // als Ausgang
    DDRD |= (1<<PD7);           // als Ausgang

    do
    {
	 for(count = 0; count < 255;count++)
	{
		PORTD = pgm_read_word(&sawtoothtable[count]); 
	};
     } while (1);     // Mainloop
 }
//----------------------------------------------------------------------
Teilspannungen R2R Netzwerk			T. Lorenz	23.10.2013							

Auflösung	8	Bit									
V Referenz	5	Volt									

Bit 0	0,01953125	V									
Bit 1	0,0390625	V									
Bit 2	0,078125	V									
Bit 3	0,15625	V									
Bit 4	0,3125	V									
Bit 5	0,625	V									
Bit 6	1,25	V									
Bit 7	2,5	V									


Wert-Nr.	Bit 0	Bit 1	Bit 2	Bit 3	Bit 4	Bit 5	Bit 6	Bit 7	Dezimal	Spannungswert	Einheit
0	0	0	0	0	0	0	0	0	0	0,000	V
1	1	0	0	0	0	0	0	0	1	0,020	V
2	0	1	0	0	0	0	0	0	2	0,039	V
3	1	1	0	0	0	0	0	0	3	0,059	V
4	0	0	1	0	0	0	0	0	4	0,078	V
5	1	0	1	0	0	0	0	0	5	0,098	V
6	0	1	1	0	0	0	0	0	6	0,117	V
7	1	1	1	0	0	0	0	0	7	0,137	V
8	0	0	0	1	0	0	0	0	8	0,156	V
9	1	0	0	1	0	0	0	0	9	0,176	V
10	0	1	0	1	0	0	0	0	10	0,195	V
11	1	1	0	1	0	0	0	0	11	0,215	V
12	0	0	1	1	0	0	0	0	12	0,234	V
13	1	0	1	1	0	0	0	0	13	0,254	V
14	0	1	1	1	0	0	0	0	14	0,273	V
15	1	1	1	1	0	0	0	0	15	0,293	V
16	0	0	0	0	1	0	0	0	16	0,313	V
17	1	0	0	0	1	0	0	0	17	0,332	V
18	0	1	0	0	1	0	0	0	18	0,352	V
19	1	1	0	0	1	0	0	0	19	0,371	V
20	0	0	1	0	1	0	0	0	20	0,391	V
21	1	0	1	0	1	0	0	0	21	0,410	V
22	0	1	1	0	1	0	0	0	22	0,430	V
23	1	1	1	0	1	0	0	0	23	0,449	V
24	0	0	0	1	1	0	0	0	24	0,469	V
25	1	0	0	1	1	0	0	0	25	0,488	V
26	0	1	0	1	1	0	0	0	26	0,508	V
27	1	1	0	1	1	0	0	0	27	0,527	V
28	0	0	1	1	1	0	0	0	28	0,547	V
29	1	0	1	1	1	0	0	0	29	0,566	V
30	0	1	1	1	1	0	0	0	30	0,586	V
31	1	1	1	1	1	0	0	0	31	0,605	V
32	0	0	0	0	0	1	0	0	32	0,625	V
33	1	0	0	0	0	1	0	0	33	0,645	V
34	0	1	0	0	0	1	0	0	34	0,664	V
35	1	1	0	0	0	1	0	0	35	0,684	V
36	0	0	1	0	0	1	0	0	36	0,703	V
37	1	0	1	0	0	1	0	0	37	0,723	V
38	0	1	1	0	0	1	0	0	38	0,742	V
39	1	1	1	0	0	1	0	0	39	0,762	V
40	0	0	0	1	0	1	0	0	40	0,781	V
41	1	0	0	1	0	1	0	0	41	0,801	V
42	0	1	0	1	0	1	0	0	42	0,820	V
43	1	1	0	1	0	1	0	0	43	0,840	V
44	0	0	1	1	0	1	0	0	44	0,859	V
45	1	0	1	1	0	1	0	0	45	0,879	V
46	0	1	1	1	0	1	0	0	46	0,898	V
47	1	1	1	1	0	1	0	0	47	0,918	V
48	0	0	0	0	1	1	0	0	48	0,938	V
49	1	0	0	0	1	1	0	0	49	0,957	V
50	0	1	0	0	1	1	0	0	50	0,977	V
51	1	1	0	0	1	1	0	0	51	0,996	V
52	0	0	1	0	1	1	0	0	52	1,016	V
53	1	0	1	0	1	1	0	0	53	1,035	V
54	0	1	1	0	1	1	0	0	54	1,055	V
55	1	1	1	0	1	1	0	0	55	1,074	V
56	0	0	0	1	1	1	0	0	56	1,094	V
57	1	0	0	1	1	1	0	0	57	1,113	V
58	0	1	0	1	1	1	0	0	58	1,133	V
59	1	1	0	1	1	1	0	0	59	1,152	V
60	0	0	1	1	1	1	0	0	60	1,172	V
61	1	0	1	1	1	1	0	0	61	1,191	V
62	0	1	1	1	1	1	0	0	62	1,211	V
63	1	1	1	1	1	1	0	0	63	1,230	V
64	0	0	0	0	0	0	1	0	64	1,250	V
65	1	0	0	0	0	0	1	0	65	1,270	V
66	0	1	0	0	0	0	1	0	66	1,289	V
67	1	1	0	0	0	0	1	0	67	1,309	V
68	0	0	1	0	0	0	1	0	68	1,328	V
69	1	0	1	0	0	0	1	0	69	1,348	V
70	0	1	1	0	0	0	1	0	70	1,367	V
71	1	1	1	0	0	0	1	0	71	1,387	V
72	0	0	0	1	0	0	1	0	72	1,406	V
73	1	0	0	1	0	0	1	0	73	1,426	V
74	0	1	0	1	0	0	1	0	74	1,445	V
75	1	1	0	1	0	0	1	0	75	1,465	V
76	0	0	1	1	0	0	1	0	76	1,484	V
77	1	0	1	1	0	0	1	0	77	1,504	V
78	0	1	1	1	0	0	1	0	78	1,523	V
79	1	1	1	1	0	0	1	0	79	1,543	V
80	0	0	0	0	1	0	1	0	80	1,563	V
81	1	0	0	0	1	0	1	0	81	1,582	V
82	0	1	0	0	1	0	1	0	82	1,602	V
83	1	1	0	0	1	0	1	0	83	1,621	V
84	0	0	1	0	1	0	1	0	84	1,641	V
85	1	0	1	0	1	0	1	0	85	1,660	V
86	0	1	1	0	1	0	1	0	86	1,680	V
87	1	1	1	0	1	0	1	0	87	1,699	V
88	0	0	0	1	1	0	1	0	88	1,719	V
89	1	0	0	1	1	0	1	0	89	1,738	V
90	0	1	0	1	1	0	1	0	90	1,758	V
91	1	1	0	1	1	0	1	0	91	1,777	V
92	0	0	1	1	1	0	1	0	92	1,797	V
93	1	0	1	1	1	0	1	0	93	1,816	V
94	0	1	1	1	1	0	1	0	94	1,836	V
95	1	1	1	1	1	0	1	0	95	1,855	V
96	0	0	0	0	0	1	1	0	96	1,875	V
97	1	0	0	0	0	1	1	0	97	1,895	V
98	0	1	0	0	0	1	1	0	98	1,914	V
99	1	1	0	0	0	1	1	0	99	1,934	V
100	0	0	1	0	0	1	1	0	100	1,953	V
101	1	0	1	0	0	1	1	0	101	1,973	V
102	0	1	1	0	0	1	1	0	102	1,992	V
103	1	1	1	0	0	1	1	0	103	2,012	V
104	0	0	0	1	0	1	1	0	104	2,031	V
105	1	0	0	1	0	1	1	0	105	2,051	V
106	0	1	0	1	0	1	1	0	106	2,070	V
107	1	1	0	1	0	1	1	0	107	2,090	V
108	0	0	1	1	0	1	1	0	108	2,109	V
109	1	0	1	1	0	1	1	0	109	2,129	V
110	0	1	1	1	0	1	1	0	110	2,148	V
111	1	1	1	1	0	1	1	0	111	2,168	V
112	0	0	0	0	1	1	1	0	112	2,188	V
113	1	0	0	0	1	1	1	0	113	2,207	V
114	0	1	0	0	1	1	1	0	114	2,227	V
115	1	1	0	0	1	1	1	0	115	2,246	V
116	0	0	1	0	1	1	1	0	116	2,266	V
117	1	0	1	0	1	1	1	0	117	2,285	V
118	0	1	1	0	1	1	1	0	118	2,305	V
119	1	1	1	0	1	1	1	0	119	2,324	V
120	0	0	0	1	1	1	1	0	120	2,344	V
121	1	0	0	1	1	1	1	0	121	2,363	V
122	0	1	0	1	1	1	1	0	122	2,383	V
123	1	1	0	1	1	1	1	0	123	2,402	V
124	0	0	1	1	1	1	1	0	124	2,422	V
125	1	0	1	1	1	1	1	0	125	2,441	V
126	0	1	1	1	1	1	1	0	126	2,461	V
127	1	1	1	1	1	1	1	0	127	2,480	V
128	0	0	0	0	0	0	0	1	128	2,500	V
129	1	0	0	0	0	0	0	1	129	2,520	V
130	0	1	0	0	0	0	0	1	130	2,539	V
131	1	1	0	0	0	0	0	1	131	2,559	V
132	0	0	1	0	0	0	0	1	132	2,578	V
133	1	0	1	0	0	0	0	1	133	2,598	V
134	0	1	1	0	0	0	0	1	134	2,617	V
135	1	1	1	0	0	0	0	1	135	2,637	V
136	0	0	0	1	0	0	0	1	136	2,656	V
137	1	0	0	1	0	0	0	1	137	2,676	V
138	0	1	0	1	0	0	0	1	138	2,695	V
139	1	1	0	1	0	0	0	1	139	2,715	V
140	0	0	1	1	0	0	0	1	140	2,734	V
141	1	0	1	1	0	0	0	1	141	2,754	V
142	0	1	1	1	0	0	0	1	142	2,773	V
143	1	1	1	1	0	0	0	1	143	2,793	V
144	0	0	0	0	1	0	0	1	144	2,813	V
145	1	0	0	0	1	0	0	1	145	2,832	V
146	0	1	0	0	1	0	0	1	146	2,852	V
147	1	1	0	0	1	0	0	1	147	2,871	V
148	0	0	1	0	1	0	0	1	148	2,891	V
149	1	0	1	0	1	0	0	1	149	2,910	V
150	0	1	1	0	1	0	0	1	150	2,930	V
151	1	1	1	0	1	0	0	1	151	2,949	V
152	0	0	0	1	1	0	0	1	152	2,969	V
153	1	0	0	1	1	0	0	1	153	2,988	V
154	0	1	0	1	1	0	0	1	154	3,008	V
155	1	1	0	1	1	0	0	1	155	3,027	V
156	0	0	1	1	1	0	0	1	156	3,047	V
157	1	0	1	1	1	0	0	1	157	3,066	V
158	0	1	1	1	1	0	0	1	158	3,086	V
159	1	1	1	1	1	0	0	1	159	3,105	V
160	0	0	0	0	0	1	0	1	160	3,125	V
161	1	0	0	0	0	1	0	1	161	3,145	V
162	0	1	0	0	0	1	0	1	162	3,164	V
163	1	1	0	0	0	1	0	1	163	3,184	V
164	0	0	1	0	0	1	0	1	164	3,203	V
165	1	0	1	0	0	1	0	1	165	3,223	V
166	0	1	1	0	0	1	0	1	166	3,242	V
167	1	1	1	0	0	1	0	1	167	3,262	V
168	0	0	0	1	0	1	0	1	168	3,281	V
169	1	0	0	1	0	1	0	1	169	3,301	V
170	0	1	0	1	0	1	0	1	170	3,320	V
171	1	1	0	1	0	1	0	1	171	3,340	V
172	0	0	1	1	0	1	0	1	172	3,359	V
173	1	0	1	1	0	1	0	1	173	3,379	V
174	0	1	1	1	0	1	0	1	174	3,398	V
175	1	1	1	1	0	1	0	1	175	3,418	V
176	0	0	0	0	1	1	0	1	176	3,438	V
177	1	0	0	0	1	1	0	1	177	3,457	V
178	0	1	0	0	1	1	0	1	178	3,477	V
179	1	1	0	0	1	1	0	1	179	3,496	V
180	0	0	1	0	1	1	0	1	180	3,516	V
181	1	0	1	0	1	1	0	1	181	3,535	V
182	0	1	1	0	1	1	0	1	182	3,555	V
183	1	1	1	0	1	1	0	1	183	3,574	V
184	0	0	0	1	1	1	0	1	184	3,594	V
185	1	0	0	1	1	1	0	1	185	3,613	V
186	0	1	0	1	1	1	0	1	186	3,633	V
187	1	1	0	1	1	1	0	1	187	3,652	V
188	0	0	1	1	1	1	0	1	188	3,672	V
189	1	0	1	1	1	1	0	1	189	3,691	V
190	0	1	1	1	1	1	0	1	190	3,711	V
191	1	1	1	1	1	1	0	1	191	3,730	V
192	0	0	0	0	0	0	1	1	192	3,750	V
193	1	0	0	0	0	0	1	1	193	3,770	V
194	0	1	0	0	0	0	1	1	194	3,789	V
195	1	1	0	0	0	0	1	1	195	3,809	V
196	0	0	1	0	0	0	1	1	196	3,828	V
197	1	0	1	0	0	0	1	1	197	3,848	V
198	0	1	1	0	0	0	1	1	198	3,867	V
199	1	1	1	0	0	0	1	1	199	3,887	V
200	0	0	0	1	0	0	1	1	200	3,906	V
201	1	0	0	1	0	0	1	1	201	3,926	V
202	0	1	0	1	0	0	1	1	202	3,945	V
203	1	1	0	1	0	0	1	1	203	3,965	V
204	0	0	1	1	0	0	1	1	204	3,984	V
205	1	0	1	1	0	0	1	1	205	4,004	V
206	0	1	1	1	0	0	1	1	206	4,023	V
207	1	1	1	1	0	0	1	1	207	4,043	V
208	0	0	0	0	1	0	1	1	208	4,063	V
209	1	0	0	0	1	0	1	1	209	4,082	V
210	0	1	0	0	1	0	1	1	210	4,102	V
211	1	1	0	0	1	0	1	1	211	4,121	V
212	0	0	1	0	1	0	1	1	212	4,141	V
213	1	0	1	0	1	0	1	1	213	4,160	V
214	0	1	1	0	1	0	1	1	214	4,180	V
215	1	1	1	0	1	0	1	1	215	4,199	V
216	0	0	0	1	1	0	1	1	216	4,219	V
217	1	0	0	1	1	0	1	1	217	4,238	V
218	0	1	0	1	1	0	1	1	218	4,258	V
219	1	1	0	1	1	0	1	1	219	4,277	V
220	0	0	1	1	1	0	1	1	220	4,297	V
221	1	0	1	1	1	0	1	1	221	4,316	V
222	0	1	1	1	1	0	1	1	222	4,336	V
223	1	1	1	1	1	0	1	1	223	4,355	V
224	0	0	0	0	0	1	1	1	224	4,375	V
225	1	0	0	0	0	1	1	1	225	4,395	V
226	0	1	0	0	0	1	1	1	226	4,414	V
227	1	1	0	0	0	1	1	1	227	4,434	V
228	0	0	1	0	0	1	1	1	228	4,453	V
229	1	0	1	0	0	1	1	1	229	4,473	V
230	0	1	1	0	0	1	1	1	230	4,492	V
231	1	1	1	0	0	1	1	1	231	4,512	V
232	0	0	0	1	0	1	1	1	232	4,531	V
233	1	0	0	1	0	1	1	1	233	4,551	V
234	0	1	0	1	0	1	1	1	234	4,570	V
235	1	1	0	1	0	1	1	1	235	4,590	V
236	0	0	1	1	0	1	1	1	236	4,609	V
237	1	0	1	1	0	1	1	1	237	4,629	V
238	0	1	1	1	0	1	1	1	238	4,648	V
239	1	1	1	1	0	1	1	1	239	4,668	V
240	0	0	0	0	1	1	1	1	240	4,688	V
241	1	0	0	0	1	1	1	1	241	4,707	V
242	0	1	0	0	1	1	1	1	242	4,727	V
243	1	1	0	0	1	1	1	1	243	4,746	V
244	0	0	1	0	1	1	1	1	244	4,766	V
245	1	0	1	0	1	1	1	1	245	4,785	V
246	0	1	1	0	1	1	1	1	246	4,805	V
247	1	1	1	0	1	1	1	1	247	4,824	V
248	0	0	0	1	1	1	1	1	248	4,844	V
249	1	0	0	1	1	1	1	1	249	4,863	V
250	0	1	0	1	1	1	1	1	250	4,883	V
251	1	1	0	1	1	1	1	1	251	4,902	V
252	0	0	1	1	1	1	1	1	252	4,922	V
253	1	0	1	1	1	1	1	1	253	4,941	V
254	0	1	1	1	1	1	1	1	254	4,961	V
255	1	1	1	1	1	1	1	1	255	4,980	V

Teile diesen Beitrag:

C# Libnodave Siemens LOGO! 0BA7

Libnodave — LOGO 0BA7

  • Netzwerkfähige LOGO 0BA7
  • Visual C# Express
  • Libnodave Devel Archiv
  • libnodave.dll
  • libnodave.net.dll
  • LOGO Netzwerkeinstellungen VM/Ethernet Verbindung/Server
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LOGOTest_libnodave
{
    public partial class Form1 : Form
    {
        static libnodave.daveOSserialType fds;
        static libnodave.daveInterface di;
        static libnodave.daveConnection dc;
        static int rack = 0; // LOGO
        static int slot = 0; // LOGO
        int LOGOInputs = 0, LOGOOutputs = 0, res;

        public Form1()
        {
            InitializeComponent();
            connectlogo();
            timer1.Interval = 500;
            timer1.Start();
        }

        public static bool CheckBitSet(byte b, int BitNumber)
        {
            //Kleine Fehlerbehandlung
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (b & (1 << BitNumber)) > 0;
            }
            else
            {
                throw new InvalidOperationException(
                "Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
            }

        }

        public void connectlogo()
        {
            fds.rfd = libnodave.openSocket(102, "192.168.1.111");

            fds.wfd = fds.rfd;

            if (fds.rfd > 0)
            {

                di = new libnodave.daveInterface(fds, "IF1", 0, libnodave.daveProtoISOTCP, libnodave.daveSpeed187k);

                di.setTimeout(1000000);

                dc = new libnodave.daveConnection(di, 0, rack, slot);

                dc.connectPLC();
            }
        }

            public void disconnectlogo()
            {
                dc.disconnectPLC();
                libnodave.closeSocket(fds.rfd);
            }

        public void readlogo()
        {
                    // Eingänge lesen
                    res = dc.readBytes(libnodave.daveInputs, 0, 0, 2, null);

                    if (res == 0)
                    {

                        LOGOInputs = dc.getU8();

                        cbIn1.Checked = CheckBitSet((byte)LOGOInputs, 0);
                        cbIn2.Checked = CheckBitSet((byte)LOGOInputs, 1);
                        cbIn3.Checked = CheckBitSet((byte)LOGOInputs, 2);
                        cbIn4.Checked = CheckBitSet((byte)LOGOInputs, 3);
                        cbIn5.Checked = CheckBitSet((byte)LOGOInputs, 4);
                        cbIn6.Checked = CheckBitSet((byte)LOGOInputs, 5);
                        cbIn7.Checked = CheckBitSet((byte)LOGOInputs, 6);
                        cbIn8.Checked = CheckBitSet((byte)LOGOInputs, 7);

                    }
                    else
                        Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));

                    // Ausgänge lesen
                    res = dc.readBytes(libnodave.daveOutputs, 0, 0, 2, null);

                    if (res == 0)
                    {
                        LOGOOutputs = dc.getU8();

                        cbOut1.Checked = CheckBitSet((byte)LOGOOutputs, 0);
                        if (cbOut1.Checked) { tbOut1.BackColor = Color.Yellow; } else { tbOut1.BackColor = Color.Green; };
                        cbOut2.Checked = CheckBitSet((byte)LOGOOutputs, 1);
                        if (cbOut2.Checked) { tbOut2.BackColor = Color.Yellow; } else { tbOut2.BackColor = Color.Green; };
                        cbOut3.Checked = CheckBitSet((byte)LOGOOutputs, 2);
                        if (cbOut3.Checked) { tbOut3.BackColor = Color.Yellow; } else { tbOut3.BackColor = Color.Green; };
                        cbOut4.Checked = CheckBitSet((byte)LOGOOutputs, 3);
                        if (cbOut4.Checked) { tbOut4.BackColor = Color.Yellow; } else { tbOut4.BackColor = Color.Green; };

                    }
                    else
                        Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        private void button1_Click(object sender, EventArgs e)
        {
            MessageBox.Show("libnodave & LOGO! 0BA7 -- Tom Lorenz 06/2013");
        }

        private void pictureBox1_Click(object sender, EventArgs e)
        {

        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {

        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            readlogo();
        }

    }
}
Teile diesen Beitrag:

Assembler „Hello World“

Assembler Programmierung unter Linux

Quellen:
http://docs.cs.up.ac.za/programming/asm/derick_tut/#maindiff
http://runtime-basic.net/Assembler:Funktionen:Beschreibung-Kurz-CPU

Hello World in Assembler

;Hallo-Welt in Assembler unter Linux

SECTION .text
GLOBAL _start

text1     DB 'Hallo Welt!',10
text1ende:


;Programmeinstiegspunkt
_start:

;Text ausgeben
MOV EDX,text1ende - text1  ;Laenge des Textes
MOV ECX,text1              ;auszugebender Text
MOV EBX,1                  ;Handle fuer stdout
MOV EAX,4                  ;Funktionsnummer: Datei schreiben
INT 80h                    ;Kernel-Funktion aufrufen

;Programm beenden
MOV EAX,1                  ;Funktionsnummer: Programm beenden
XOR EBX,EBX                ;Returncode 0
INT 80h                    ;Kernel-Funktion aufrufen

Übersetzen & Linken

nasm -f elf hello.asm
ld -s -o hello hello.o
Teile diesen Beitrag:

Siemens LOGO! vs. Visual Studio Express

Siemens LOGO! 0BA7 auslesen

WICHTIG: Vorher in Parameter VM Zuordnung die im DB gewünschten Werte festlegen
WICHTIG: Netzwerkverbindung mit TSAP 01.00 und mit Operator Panel [x] konfigurieren

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace LOGO_Remote
{
    public partial class Form1 : Form
    {
        static libnodave.daveOSserialType fds;
        static libnodave.daveInterface di;
        static libnodave.daveConnection dc;

        string ip = "192.168.1.111"; // Siemens LOGO! 0BA7
        int port = 102; // Siemens LOGO! 0BA7
        int protokoll = libnodave.daveProtoISOTCP;

        static int mpi = 0;     // MPI
        static int rack = 1;    // Racknummer
        static int slot = 0;    // Slotnummer

        int res;
        int LOGODInputs = 0;
        int LOGODOutputs = 0;
        int LOGOAInputs = 0;
        int LOGOAOutputs = 0;
        int LOGODB = 0;
        int LOGOFlags = 0;

        public Form1()
        {
            InitializeComponent();
        }

        public static bool CheckBitSet(byte b, int BitNumber)
        {
            //Kleine Fehlerbehandlung
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (b & (1 << BitNumber)) > 0;
            }
            else
            {
                throw new InvalidOperationException(
                "Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
            }

        }

        public void connectlogo()
        {
            fds.rfd = libnodave.openSocket(port, ip);

            fds.wfd = fds.rfd;

            if (fds.rfd > 0)
            {

                di = new libnodave.daveInterface(fds, "IF1", 0, protokoll, libnodave.daveSpeed187k);

                di.setTimeout(1000000);

                dc = new libnodave.daveConnection(di, mpi, rack, slot);

                dc.connectPLC();
            }
            else
                Console.WriteLine("Connect Error");
        }

        public void disconnectlogo()
        {
            dc.disconnectPLC();
            libnodave.closeSocket(fds.rfd);
        }

        public void readlogodi()
        {
            // Digitale Eingänge lesen
            res = dc.readBytes(libnodave.daveInputs, 0, 0, 1, null);

            if (res == 0)
            {
                LOGODInputs = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogodo()
        {
            // Digitale Eingänge lesen
            res = dc.readBytes(libnodave.daveOutputs, 0, 0, 1, null);

            if (res == 0)
            {
                LOGODOutputs = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoai()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveAnaIn, 0, 0, 2, null);

            if (res == 0)
            {
                LOGOAInputs = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoao()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveAnaOut, 0, 0, 2, null);

            if (res == 0)
            {
                LOGOAOutputs = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogodb()
        {
            // Analoge Eingänge lesen
            res = dc.readBytes(libnodave.daveDB, 0, 0, 2, null);

            if (res == 0)
            {
                LOGODB = dc.getU16();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        public void readlogoflags()
        {
            // Merker lesen
            res = dc.readBytes(libnodave.daveFlags, 0, 0, 1, null);

            if (res == 0)
            {
                LOGOFlags = dc.getU8();
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }


        //Write a sequence of bytes from a buffer to PLC memory. int daveWriteBytes(daveConnection * dc, int area, int DB, int start, int len, void * buffer);
        //Parameters:

        //dc: A pointer to a daveConnection structure representing an established connection.
        //area: A constant that specifies a memory area in the PLC.
        //DB: The number of a data block. Only meaningful if area is daveDB. Use 0 oterwise.
        //start: The address of the first byte in the block.
        //len: The number of bytes to write.
        //buffer: A pointer to some memory space with the writedata. 

        public void writelogoflags()
        {
              // Merker schreiben
              byte[] buf;
              buf = new byte[1];

             int Adr;
             bool Par = false;

             int InputNum = 0; //
             int BitNum = 24;   // 

             if(Par == true)
             {
                 buf[0] = 255; // TRUE
             }
             else
             {
                buf[0] = 0; // FALSE
             }

             Adr = InputNum * 8 + BitNum;

            Console.WriteLine("InputNum: " + InputNum);
            Console.WriteLine("ByteNum: " + BitNum);
            Console.WriteLine("Adr: " + Adr);

             //write Merker M0.0
             res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            //readlogoai();
            readlogodb();
            progressBar1.Value = LOGODB;
        }

        private void btnInfo_Click(object sender, EventArgs e)
        {
            MessageBox.Show("libnodave & LOGO! 0BA7 -- Tom Lorenz 09/2014");
        }

        private void btnConnect_Click(object sender, EventArgs e)
        {
            connectlogo();
        }

        private void btnDisconnect_Click(object sender, EventArgs e)
        {
            disconnectlogo();
        }

        private void btnReadAI_Click(object sender, EventArgs e)
        {
            readlogoai();
            tbTesttext.Text = LOGOAInputs.ToString();
        }

        private void btnReadAO_Click(object sender, EventArgs e)
        {
            readlogoao();
            tbTesttext.Text = LOGOAOutputs.ToString();
        }

        private void btnReadDI_Click(object sender, EventArgs e)
        {
            readlogodi();
            tbTesttext.Text = LOGODInputs.ToString();
        }

        private void btnReadDO_Click(object sender, EventArgs e)
        {
            readlogodo();
            tbTesttext.Text = LOGODOutputs.ToString();
        }

        private void btnReadDB_Click(object sender, EventArgs e)
        {
            readlogodb();
            tbTesttext.Text = LOGODB.ToString();
        }

        private void btnReadFlags_Click(object sender, EventArgs e)
        {
            readlogoflags();
            tbTesttext.Text = LOGOFlags.ToString();
        }

        private void btnWriteFlags_Click(object sender, EventArgs e)
        {
            writelogoflags();
        }

        private void btnLOGOLightOn_Click(object sender, EventArgs e)
        {
            // Merker schreiben
            byte[] buf;
            buf = new byte[1];

            int Adr;
            bool Par = true;

            int InputNum = 0; //
            int BitNum = 24;   // 

            if (Par == true)
            {
                buf[0] = 255; // TRUE
            }
            else
            {
                buf[0] = 0; // FALSE
            }

            Adr = InputNum * 8 + BitNum;

            //write Merker M25
            res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

        private void btnLOGOLightOff_Click(object sender, EventArgs e)
        {
            // Merker schreiben
            byte[] buf;
            buf = new byte[1];

            int Adr;
            bool Par = false;

            int InputNum = 0; //
            int BitNum = 24;   // 

            if (Par == true)
            {
                buf[0] = 255; // TRUE
            }
            else
            {
                buf[0] = 0; // FALSE
            }

            Adr = InputNum * 8 + BitNum;

            //write Merker M25
            res = dc.writeBits(libnodave.daveFlags, 0, Adr, 1, buf);

            if (res == 0)
            {
                //
            }
            else
                Console.WriteLine("Error " + res + " " + libnodave.daveStrerror(res));
        }

    }
}

Siemens S7-300 Eingänge auslesen

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.IO;

namespace S7300_Statistik
{
    public partial class Form1 : Form
    {

        static libnodave.daveOSserialType davesocket;
        static libnodave.daveInterface daveiface;
        static libnodave.daveConnection daveconnection;

        //string ip = "192.168.1.113"; // Siemens LOGO! 0BA7
        //int port = 102; // Siemens LOGO! 0BA7
        //int portokoll = libnodave.daveprotoTCP;

        //string ip = "192.168.0.236"; // S7-300 IP Altbau
        //int port = 1099; // S7-300 IP Altbau
        //int protokoll = libnodave.daveProtoMPI_IBH;

        string ip = "192.168.1.207";    // S7-300 IP Haustechnik
        int port = 7777;                // S7-300 IP Haustechnik
        int protokoll = 230;            // libnodave.daveProtoNLpro = 230;

        static int rack = 0;    // S7-300 Racknummer
        static int slot = 2;    // S7-300 Slotnummer
        static int mpi = 2;     // S7-300 MPI -- WICHTIG!!
        string logfilename = "";

        //-----------------------------------------------------------------------

        int res=0;
        int daveinputs = 0;
        int connectionestablished = 0;
        double readcounter = 0;
        DateTime recorddate = DateTime.Now;

        public Form1()
        {
            InitializeComponent();
            timermomentankwh.Interval = 1000;
            timerrecord.Interval = 30000;
        }

        private void Form1_FormClosed(object sender, FormClosedEventArgs e)
        {
            disconnects7300();
        }

        public static bool CheckBitSet(byte b, int BitNumber)
        {
            // Fehlerbehandlung
            if (BitNumber < 8 && BitNumber > -1)
            {
                return (b & (1 << BitNumber)) > 0;
            }
            else
            {
                throw new InvalidOperationException(
                "Der Wert für BitNumber " + BitNumber.ToString() + " war nicht im zulässigen Bereich! (BitNumber = (min)0 - (max)7)");
            }
        }

        public void connects7300()
        {
            connectionestablished = 0;
            tbLog.AppendText("Socket öffnen\n");
            davesocket.rfd = libnodave.openSocket(port, ip);
            tbLog.AppendText("Socket erfolgreich\n");
            davesocket.wfd = davesocket.rfd;

            if (davesocket.rfd > 0)
                {
                tbLog.AppendText("Interface öffnen\n");
                daveiface = new libnodave.daveInterface(davesocket, "IF1", 0, protokoll, libnodave.daveSpeed187k);
                daveiface.initAdapter();
                tbLog.AppendText("Adapter erfolgreich\n");
                daveiface.setTimeout(100000);
                tbLog.AppendText("Interface erfolgreich\n");

                if (res == 0)
                {
                    tbLog.AppendText("Connection öffnen\n");
                    daveconnection = new libnodave.daveConnection(daveiface, mpi, rack, slot);

                    daveconnection.connectPLC();
                    tbLog.AppendText("Connection erfolgreich\n");
                    connectionestablished = 1;
                }
                else
                    tbLog.AppendText("Status: " + res + " " + libnodave.daveStrerror(res));
                }
            else
                {
                    tbLog.AppendText("Verbindungsaufbau abgebrochen\n");
                }
        }

        public void disconnects7300()
        {
            if (connectionestablished == 1)
            {
                daveiface.disconnectAdapter();
                tbLog.AppendText("Disconnect Adapter erfolgreich\n");
                daveconnection.disconnectPLC();
                libnodave.closeSocket(davesocket.rfd);
                tbLog.AppendText("Disconnect PLC erfolgreich\n");
                connectionestablished = 0;
            }
        }

        public void readdbkw()
        {
            readcounter++;
            recorddate = DateTime.Now;
            /*
            // Datenbaustein Momentanwert DB30 DW0 lesen 
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 30, 0, 4, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " DB30-0 erfolgreich\n");

            if (res == 0)
            {
                daveinputs = daveconnection.getU32();
                lblkw0.Text = daveinputs.ToString();

            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
            */

            // Datenbaustein Momentanwert DB30 DW8 lesen --- Easyshow !!!! AKTUELLER WERT
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 30, 8, 4, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " DB30-8 erfolgreich\n");

            if (res == 0)
            {
                daveinputs = daveconnection.getU32();
                lblkw8.Text = daveinputs.ToString();
                pbmomentankw.Value = daveinputs;
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }

            /*
            // Datenbaustein Momentanwert DB1 x lesen --- Easyshow !!!! AKTUELLER WERT
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveDB, 1, 0, 1, null);
            tbLog.AppendText("Funktion ReadDB " + readcounter + " Tick erfolgreich\n");

            daveinputs = daveconnection.getU8();

            if (res == 0)
            {
                if (CheckBitSet((byte)daveinputs,0))
                    {
                        panelkwtick.BackColor = Color.Yellow;
                    }
                else
                    {
                        panelkwtick.BackColor = Color.DarkGray;
                    }
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
            */
        }

        public void readtime()
        {
            DateTime Uhrzeit = DateTime.Now;

            lblplcyear.Text = Uhrzeit.Year.ToString();
            lblplcmonth.Text = Uhrzeit.Month.ToString();
            lblplcday.Text = Uhrzeit.Day.ToString();
            lblplcstunde.Text = Uhrzeit.Hour.ToString();
            lblplcminute.Text = Uhrzeit.Minute.ToString();
            lblplcsekunde.Text = Uhrzeit.Second.ToString();
            lblplcmillis.Text = Uhrzeit.Millisecond.ToString();
        }

        public void recordkwh()
        {
            // Record kWh
            // 13.12.2013;08:23:42;45

            String appendstring =  lblplcday.Text+"."+lblplcmonth.Text+"."+lblplcyear.Text+";"+lblplcstunde.Text+":"+lblplcminute.Text+":"+lblplcsekunde.Text+";"+lblkw8.Text;

            AppendToTextFile(logfilename, appendstring);
        }

        private void AppendToTextFile(string strPath, string strLines)
        {
            using (StreamWriter streamWriter = new StreamWriter(strPath, true))
            {
                streamWriter.WriteLine(strLines);
            }
        }

        private void InitiateTextFile(string strPath, string strLines)
        {
            using (StreamWriter streamWriter = new StreamWriter(strPath))
            {
                streamWriter.WriteLine(strLines);
            }
        }

        // Actual total zoom value
        int deltaScrollTotal;
        private void kwchart_MouseWheel(object sender, MouseEventArgs e)
        {
            int maxChangeRange = 21;
            int minChangeRange = -1;

            int deltaScroll = e.Delta / Math.Abs(e.Delta);
            deltaScrollTotal += deltaScrollTotal + deltaScroll > minChangeRange
                             && deltaScrollTotal + deltaScroll < maxChangeRange
                              ? deltaScroll : 0;
            // Additional calculation in order to obtain pseudo
            // "positional zoom" feature
            double minXScale = (double)e.X / (double)kwchart.Width;
            double maxXScale = 1 - minXScale;
            double minYScale = (double)e.Y / (double)kwchart.Height;
            double maxYScale = 1 - minYScale;

            // Max and min values into which axis need to be scaled/zoomed
            double maxX = kwchart.ChartAreas[0].AxisX.Maximum
                        - deltaScrollTotal * maxXScale;
            double minX = kwchart.ChartAreas[0].AxisX.Minimum
                        + deltaScrollTotal * minXScale;
            double maxY = kwchart.ChartAreas[0].AxisY.Maximum
                        - deltaScrollTotal * minYScale;
            double minY = kwchart.ChartAreas[0].AxisY.Minimum
                        + deltaScrollTotal * maxYScale;

            kwchart.ChartAreas[0].AxisX.ScaleView.Zoom(minX, maxX);
            kwchart.ChartAreas[0].AxisY.ScaleView.Zoom(minY, maxY);
        }

        private void btnstoplogging_Click(object sender, EventArgs e)
        {
            timermomentankwh.Stop();
        }

        private void btnreadplctime_Click(object sender, EventArgs e)
        {
            // PLC Time auslesen
            daveinputs = 0;
            res = daveconnection.readBytes(libnodave.daveTimer, 0, 110, 2, null);
            tbLog.AppendText("Funktion ReadTimer erfolgreich\n");

            if (res == 0)
            {
                tbLog.AppendText("------Timer101--------\n");
                daveinputs = daveconnection.getU8();
                tbLog.AppendText("Ergebnis T101: " + daveinputs + "\n");
                daveinputs = daveconnection.getU8();
                tbLog.AppendText("Ergebnis T101: " + daveinputs + "\n");
            }
            else
            {
                tbLog.AppendText("Fehler Read\n");
            }
        }

        private void btnstart_Click(object sender, EventArgs e)
        {
            connects7300();
            timermomentankwh.Start();

            kwchart.ChartAreas[0].AxisX.MajorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisY.MajorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisX.MinorGrid.LineWidth = 1;
            kwchart.ChartAreas[0].AxisY.MinorGrid.LineWidth = 1;

            kwchart.ChartAreas[0].AxisY.MajorGrid.Interval = 100;
            kwchart.ChartAreas[0].AxisY.MinorGrid.Interval = 10;

            kwchart.ChartAreas[0].AxisY.MinorGrid.LineColor = Color.LightGray;

            kwchart.ChartAreas[0].AxisX.MajorGrid.Enabled = false;
            kwchart.ChartAreas[0].AxisX.MinorGrid.Enabled = false;
            kwchart.ChartAreas[0].AxisY.MajorGrid.Enabled = true;
            kwchart.ChartAreas[0].AxisY.MinorGrid.Enabled = true;

            kwchart.Series["kWh"].Color = Color.DarkGreen;

            kwchart.Series["kWh"].ToolTip = "#VALX{G}, #VALY kW/h";

            kwchart.Series["kWh"].IsXValueIndexed = true;
        }

        private void btnstop_Click(object sender, EventArgs e)
        {
            timerrecord.Stop();
            timermomentankwh.Stop();
            disconnects7300();
            pbmomentankw.Value = 0;
        }

        private void btnrecord_Click(object sender, EventArgs e)
        {
            DateTime logtime = DateTime.Now;

            logfilename = "Logfile_" + logtime.Year.ToString() + logtime.Month.ToString() + logtime.Day.ToString() + logtime.Hour.ToString() + logtime.Minute.ToString() + logtime.Second.ToString() + ".txt";

            InitiateTextFile(logfilename, "Datum;Zeit;kWh");

            readdbkw();
            readtime();
            kwchart.Series["kWh"].Points.AddXY(recorddate.ToOADate(), pbmomentankw.Value);

            timerrecord.Start();
        }

        private void timermomentankwh_Tick(object sender, EventArgs e)
        {
            readdbkw();
            readtime();
        }

        private void timerrecord_Tick(object sender, EventArgs e)
        {
            recordkwh();
            kwchart.Series["kWh"].Points.AddXY(recorddate.ToOADate(), pbmomentankw.Value);
        }

        private void kwchart_MouseMove(object sender, MouseEventArgs e)
        {
            //kwchart.Series["kWh"].ToolTip = "#VALX, #VALY";
        }

        private void btnsavechart_Click(object sender, EventArgs e)
        {
            this.kwchart.SaveImage(logfilename + ".png", System.Drawing.Imaging.ImageFormat.Png);
        }
    }
}

PT100 Widerstandswert in Temperatur umrechnen

aivalue = Widerstandswert
tmpcalc = Temperatur

tmpcalc = ((3.90802*0.1)/(2*5.802*0.00001)) - Math.Sqrt(((0.390802*0.390802)/(4*((5.802*0.00001)*(5.802*0.00001)))) - ((aivalue-100)/(5.802*0.00001))); 
Teile diesen Beitrag:
Cookie Consent mit Real Cookie Banner