Auf ATmega32: Master: ------------------------------------------------------------------------------------------------- /***************************************************** This program was produced by the CodeWizardAVR V2.03.4 Standard Automatic Program Generator © Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com Project : Version :V3 Date : 25.10.2010 Author : Company : Comments: Chip type : ATmega32 Program type : Application Clock frequency : 16,000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 512 *****************************************************/ #include #include #include //Definition for TWI //Defintion for TWCR (Control Register) #define TWINT 7 //TWI Interrupt Flag #define TWEA 6 //TWI Enable Acknowledge Bit #define TWSTA 5 //TWI START Condition Bit #define TWSTO 4 //TWI STOP Condition Bit #define TWWC 3 //TWI Write Collision Flag #define TWEN 2 //TWI Enable Bit #define TWIE 0 //TWI Interrupt Enable //Variables int slave_adress=0; //Adress of ADC-Slave int ADCH0=0; //High-byte of PA0 int ADCL0=0; //Low-byte of PA0 int ADCH1=0; //High-byte of PA1 int ADCL1=0; //Low-byte of PA1 int run=0; //run counter unsigned long int ADC0=0; //Full Byte of PA0 unsigned long int ADC1=0; //Full Byte of PA1 unsigned long int U0=0; unsigned long int U1=0; //Functions void get_data(void) { printf("\rStart get data\r"); run=0; //clears start counter TWCR =(1< #include #include //Defintion for TWCR (Control Register) #define TWINT 7 //TWI Interrupt Flag #define TWEA 6 //TWI Enable Acknowledge Bit #define TWSTA 5 //TWI START Condition Bit #define TWSTO 4 //TWI STOP Condition Bit #define TWWC 3 //TWI Write Collision Flag #define TWEN 2 //TWI Enable Bit #define TWIE 0 //TWI Interrupt Enable //Variables int ADCH0=0; //High-Byte of ADC-PA0 result int ADCL0=0; //Low-Byte of ADC-PA0 result int ADCH1=0; //High-Byte of ADC-PA1 result int ADCL1=0; //Low-Byte of ADC-PA1 result int run=0; // Function to read ADC-Value void read_adc(int channel) //Arguments: 0xC0 or 64 for PA0 or 0xC1 or 65 for PA1 as Input { ADMUX=channel; //select reference Source AVCC, Channel ADO = PA0, AD1 = PA1 delay_us(10); //delay time to get reference Voltage stable ADCSRA=(ADCSRA | 0x40); //Sets Start Conversation Bit while ((ADCSRA & 0x10)==0); //waits until Interupt flag for completed adc-conversation is set ADCSRA=(ADCSRA | 0x10); //clears interrupt flag switch(channel) //depending on selcted Input-channel, different variables are written { case 0xC0: ADCH0=(ADCW&0xFF00)/256; //copies high-byte conversation result of PA0 to variable ADCH0 ADCL0=ADCW&0xFF; //copies low-byte conversation result of PA0 to variable ADCH0 break; case 0xC1: ADCH1=(ADCW&0xFF00)/256; //copies high-byte conversation result of PA1 to variable ADCH1 ADCL1=ADCW&0xFF; //copies low-byte conversation result of PA1 to variable ADCH1 break; } } // TWI interrupt service routine interrupt [TWI] void twi_isr(void) //Interrupt is set by TWI { switch (TWSR & 0b11111000) //masks status register bits { case 0xA8: // Own SLA+R has been received; ACK has been returned printf("\rOwn SLA+R has been received; ACK has been returned\r"); run=0; //sets run counter to zero read_adc(0xC0); //reads ADC from Port PA0 TWDR = ADCH0; //Writes adc-Value high byte from port PA0 to TWI Data Register TWCR = (1< #include //Definition for TWI //Defintion for TWCR (Control Register) #define TWINT 7 //TWI Interrupt Flag #define TWEA 6 //TWI Enable Acknowledge Bit #define TWSTA 5 //TWI START Condition Bit #define TWSTO 4 //TWI STOP Condition Bit #define TWWC 3 //TWI Write Collision Flag #define TWEN 2 //TWI Enable Bit #define TWIE 0 //TWI Interrupt Enable //Variables int slave_adress=0; //Adress of ADC-Slave int ADCH0=0; //High-byte of PA0 int ADCL0=0; //Low-byte of PA0 int ADCH1=0; //High-byte of PA1 int ADCL1=0; //Low-byte of PA1 int run=0; //run counter unsigned long int ADC0=0; //Full Byte of PA0 unsigned long int ADC1=0; //Full Byte of PA1 unsigned long int U0=0; unsigned long int U1=0; //Functions void get_data(void) { printf("\rStart get data\r"); run=0; //clears start counter TWCR =(1< #include #include //Defintion for TWCR (Control Register) #define TWINT 7 //TWI Interrupt Flag #define TWEA 6 //TWI Enable Acknowledge Bit #define TWSTA 5 //TWI START Condition Bit #define TWSTO 4 //TWI STOP Condition Bit #define TWWC 3 //TWI Write Collision Flag #define TWEN 2 //TWI Enable Bit #define TWIE 0 //TWI Interrupt Enable //Variables int ADCH0=0; //High-Byte of ADC-PA0 result int ADCL0=0; //Low-Byte of ADC-PA0 result int ADCH1=0; //High-Byte of ADC-PA1 result int ADCL1=0; //Low-Byte of ADC-PA1 result int run=0; // Function to read ADC-Value void read_adc(int channel) //Arguments: 0xC0 or 64 for PA0 or 0xC1 or 65 for PA1 as Input { ADMUX=channel; //select reference Source AVCC, Channel ADO = PA0, AD1 = PA1 delay_us(10); //delay time to get reference Voltage stable ADCSRA=(ADCSRA | 0x40); //Sets Start Conversation Bit while ((ADCSRA & 0x10)==0); //waits until Interupt flag for completed adc-conversation is set ADCSRA=(ADCSRA | 0x10); //clears interrupt flag switch(channel) //depending on selcted Input-channel, different variables are written { case 0xC0: ADCH0=(ADCW&0xFF00)/256; //copies high-byte conversation result of PA0 to variable ADCH0 ADCL0=ADCW&0xFF; //copies low-byte conversation result of PA0 to variable ADCH0 break; case 0xC1: ADCH1=(ADCW&0xFF00)/256; //copies high-byte conversation result of PA1 to variable ADCH1 ADCL1=ADCW&0xFF; //copies low-byte conversation result of PA1 to variable ADCH1 break; } } // TWI interrupt service routine interrupt [TWI] void twi_isr(void) //Interrupt is set by TWI { switch (TWSR & 0b11111000) //masks status register bits { case 0xA8: // Own SLA+R has been received; ACK has been returned printf("\rOwn SLA+R has been received; ACK has been returned\r"); run=0; //sets run counter to zero read_adc(0xC0); //reads ADC from Port PA0 TWDR = ADCH0; //Writes adc-Value high byte from port PA0 to TWI Data Register TWCR = (1<