#include <90usb1287.h> #include // Standard-Ein-und Ausgang #include // Standardbibliothek #include // Verzoegerungsbibliothek // I2C Bus functions #asm .equ __i2c_port=0x0B; PORTD .equ __sda_bit=1 .equ __scl_bit=0 #endasm #include // i2c der CodeVision-Bibliothek #define CLOCK 16e6 // Main Clock // Declare your global variables here unsigned char regadress, databytes; unsigned char data=0; // Data (1 Byte) aus Lesevorgang aus i2c-Schnittstelle unsigned char ack; // hält die SDA-Leitung auf low, später als DEFINE implementieren int g=0, h=0; int realwerte=0; // Anzahl der Feldelemente entsprechen Anzahl der Number of increments int imaginarwerte=0; // Anzahl der Feldelemente entsprechen Anzahl der Number of increments // External Interrupt 2 service routine interrupt [EXT_INT2] void ext_int2_isr(void) { // Place your code here PORTF^=0x01; } /* // frequency sweep do { i2c_writebyte(0x80,0xD0); // Registeradresse: 0x80, standby mode: D12,D13,D15 setzen i2c_writebyte(0x80,0x10); // Registeradresse: 0x80, initialize mode: D12 setzen i2c_writebyte(0x8B,0xFF); // Registeradresse: 0x8A und 0x8B, Anzahl der Zeitzyklen: Zeit die abgewartet wird, bevor der Messvorgang stattfindet i2c_writebyte(0x8A,0x07); // Registeradresse: 0x8A und 0x8B, Anzahl der Zeitzyklen i2c_writebyte(0x80,0x20); // Registeradresse: 0x80, sweep mode command: D13 setzen (Zyklen des ADC's können in Register 0x8A und 0x8B bevor das Messen programmiert werden i2c_readbyte(0x8F); // Statusregister abfragen } while(!data); if (data) { i2c_readbyte(0x8F); realwerte=data; i2c_readbyte(0x8F); imaginarwerte=data; } */ i2c_writebyte(0x80,0x16); // Registeradresse: 0x80, initialize mode: D12 setzen für undefinierte Zeit, D9&D10 setzen für 1Vpp, D8=0 --> Gain x5 i2c_writebyte(0x81,0x08); // D3=1 --> externer CLK i2c_writebyte(0x8B,0xFF); // Registeradresse: 0x8A und 0x8B, Anzahl der Zeitzyklen: Zeit die abgewartet wird, bevor der Messvorgang stattfindet i2c_writebyte(0x8A,0x07); // Registeradresse: 0x8A und 0x8B, Anzahl der Zeitzyklen } void main(void) { // Declare your local variables here // Crystal Oscillator division factor: 1 #pragma optsize- CLKPR=0x80; CLKPR=0x00; #ifdef _OPTIMIZE_SIZE_ #pragma optsize+ #endif // Reset Source checking if (MCUSR & 1) { // Power-on Reset MCUSR&=0xE0; // Place your code here } else if (MCUSR & 2) { // External Reset MCUSR&=0xE0; // Place your code here } else if (MCUSR & 4) { // Brown-Out Reset MCUSR&=0xE0; // Place your code here } else if (MCUSR & 8) { // Watchdog Reset MCUSR&=0xE0; // Place your code here } else if (MCUSR & 0x10) { // JTAG Reset MCUSR&=0xE0; // Place your code here }; // Input/Output Ports initialization // Port A initialization PORTA=0x00; // Pin3-7: LCD DDRA=0x00; // Port B initialization PORTB=0x00; DDRB=0x00; // Port C initialization PORTC=0x00; // alle LCD DDRC=0x00; // Port D initialization PORTD=0x03; DDRD=0xC3; // Pin 0 und 1 als I²C-Schnittstelle PORTE=0x00; DDRE=0x00; PORTF=0x00; // kleine LEDs zunächst aus DDRF=0x0F; // kleine LEDs als Ausgänge // 2 Wire Bus initialization // Generate Acknowledge Pulse: On // 2 Wire Bus Slave Address: 0h // General Call Recognition: On // Bit Rate: 100,000 kHz TWSR=0x00; TWBR=0x48; TWAR=0x01; TWCR=0x44; // External Interrupt(s) initialization // INT0: Off // INT1: Off // INT2: On // INT2 Mode: Falling Edge // INT3: On // INT3 Mode: Falling Edge // INT4: Off // INT5: Off // INT6: Off // INT7: Off EICRA=0xA0; EICRB=0x00; EIMSK=0x0C; EIFR=0x0C; // PCINT0 interrupt: Off // PCINT1 interrupt: Off // PCINT2 interrupt: Off // PCINT3 interrupt: Off // PCINT4 interrupt: Off // PCINT5 interrupt: Off // PCINT6 interrupt: Off // PCINT7 interrupt: Off PCMSK0=0x00; PCICR=0x00; // Two-Wire-Schnittstelle aktivieren i2c_init(); // I²C initialisieren //TWCR|=0x04; // zusätzlich TowWire Enable (TWEN) setzen //Schema für AD5934-Programmierung: i2c_writebyte(0x80,0x4B); // Controll Register einstellen:(regadresse,databytes) D8=1:PGA=1x; D9=1&D10=0: Output voltage range=200mVpp, D14=1: Repeat frequency i2c_writebyte(0x81,0x08); // D3=1 --> externer CLK // Global enable interrupts #asm("sei") while (1) { i2c_start(); i2c_write('A'); i2c_stop(); } } void i2c_writebyte(unsigned char regadress, unsigned char databytes) { i2c_start(); i2c_write(0x1A); // Standardadresse des AD5934 0b0001101 +0-Bit =schreiben; (spaeter: Sensorarray --> mehrere Adressen!) i2c_read(ack); // Acknowledge-Bit vom AD5934: 0 schreiben auf AD5934; 1 ist lesen von AD5934 i2c_write(regadress); // Registeradresse auf dem Daten geschrieben werden i2c_read(ack); i2c_write(databytes); // databytes= eigentliche Data/Info i2c_read(ack); i2c_stop(); } unsigned char i2c_readbyte(unsigned char regadress) { i2c_start(); i2c_write(0x1B); // Standardadresse des AD5934 0b0001101 +1-Bit =lesen; (spaeter: Sensorarray --> mehrere Adressen!) i2c_read(ack); // Acknowledge-Bit vom AD5934: 0 schreiben auf SDA; 1 ist lesen von AD5934 data=i2c_read(0); // Master empfängt Data Byte i2c_read(!ack); i2c_stop(); return data; // Rückgabewert der gelesenen Bytes aus Ad5934 }