That´s a code for making data aquisition from a chip through SPI of a
mega16 and then I want to sent the data through I2C in a mega128. If
you see some "illigal" things or something that is not going well
please let me know. I can´t try to interface it to see if it works
because I need at least 50-70€ to make the pcd. Thanks a lot for your
try and your time!!
Code:#include <mega16.h> // SPI functions #include <spi.h> #include <stdio.h> #include <stdlib.h> // Declare your global variables here //#define TWCR _SFR_IO8(0x36) #define AD7708_CS PORTA.3 #define TWIE 0 #define TWEN 2 #define TWWC 3 #define TWSTO 4 #define TWSTA 5 #define TWEA 6 #define TWINT 7 #define TWI_START 0x08 // START has been transmitted #define SLA_W 0x20 // Slave address 0x10 + 0 write bit #define MT_SLA_ACK 0x18 #define MT_DATA_ACK 0x28 #define TWI_BUFFER_SIZE 100 unsigned char TWI_Buffer[TWI_BUFFER_SIZE+1]; unsigned int TWI_Wr_Index; unsigned int TWI_Rd_Index; unsigned int TWI_Counter; unsigned int h,g; unsigned char c; void SPI_to_BuFF (void) { //unsigned char c; c = SPDR; TWI_Buffer[TWI_Wr_Index++] = c; if (TWI_Wr_Index > TWI_BUFFER_SIZE) TWI_Wr_Index = 0; if (++TWI_Counter > TWI_BUFFER_SIZE) { TWI_Counter = TWI_BUFFER_SIZE; //TWI_Buffer_Overflow = 1; printf("overflow"); }; } unsigned char message (void) { g = h; TWDR = g; } void SPI_MasterDataReceive(void) { SPDR=0x00;//Trigger first transaction while(!(SPSR&0x80)); //wait for transmission c=SPDR; //fetch first 8 bits from the ADC } // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) { // Place your code here int reg1; // // 8 To write to the Communication Register to go to the ADC Control register // SPDR = 0x02; //communications register while((SPSR&0x80)==0); SPDR = 0x7F; // 8 To write to the ADC Control register my parameters while((SPSR&0x80)==0); // // 8 To read the Status register // SPDR = 0x40; //communications register = read of data reg while((SPSR&0x80)==0); SPDR = 0x00; while((SPSR&0x80)==0); reg1 = SPDR; //reg1 the data which come from the Ad7708(to make the data aqui in the Status Reg the flag mustn`t be set) if (reg1<128) { // // 8 To read(dataq) the 8th ch // SPDR = 0x44; //communications register = read of data reg while((SPSR&0x80)==0); SPI_MasterDataReceive(); SPI_to_BuFF(); } } // 2 Wire bus interrupt service routine interrupt [TWI] void twi_isr(void) { if ( TWI_Counter != 0 ) { h = TWI_Buffer[TWI_Rd_Index]; if (++TWI_Rd_Index > TWI_BUFFER_SIZE) TWI_Rd_Index = 0; TWI_Counter--; } resend_again: message(); //TWDR=SPDR TWCR = (1<<TWINT)|(1<<TWEN); /* Clear TWI INT flag to start transmission*/ while (!(TWCR & (1<<TWINT))); if ((TWSR & 0xF8) !=MT_DATA_ACK) { goto resend_again; }; } // Declare your global variables here void main(void) { // Declare your local variables here // Input/Output Ports initialization // Port A initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTA=0x00; DDRA=0x00; // Port B initialization // Func7=Out Func6=In Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In // State7=0 State6=T State5=0 State4=0 State3=T State2=T State1=T State0=T PORTB=0x00; DDRB=0xB0; // Port C initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTC=0x00; DDRC=0x00; // Port D initialization // Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T PORTD=0x00; DDRD=0x00; // Timer/Counter 0 initialization // Clock source: System Clock // Clock value: Timer 0 Stopped // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=0x00; TCNT0=0x00; OCR0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer 1 Stopped // Mode: Normal top=FFFFh // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0x00; TCCR1B=0x00; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x00; ICR1L=0x00; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; // Timer/Counter 2 initialization // Clock source: System Clock // Clock value: Timer 2 Stopped // Mode: Normal top=FFh // OC2 output: Disconnected ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Falling Edge // INT1: Off // INT2: Off GICR|=0x40; MCUCR=0x02; MCUCSR=0x00; GIFR=0x40; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // SPI initialization // SPI Type: Master // SPI Clock Rate: 2*3686,400 kHz // SPI Clock Phase: Cycle Half // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0x50; SPSR=0x01; // 2 Wire Bus initialization // Generate Acknowledge Pulse: On // 2 Wire Bus Slave Address: 10h // General Call Recognition: Off // Bit Rate: 388,042 kHz TWSR=0x00; TWBR=0x0B; TWAR=0x20; TWCR=0x45; // Global enable interrupts #asm("sei") start_again: TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if ((TWSR & 0xF8) != TWI_START) { goto start_again; }; repeat_again: TWDR = SLA_W; TWCR = (1<<TWINT)|(1<<TWEN); while (!(TWCR & (1<<TWINT))); if ((TWSR & 0xF8) != MT_SLA_ACK) { goto repeat_again; }; // // To write to the Communication Register to go to the IO Register // SPDR = 0x00; //reg &= 0x07; PORTA &= ~(1<<AD7708_CS); //CS low SPDR = 0x07; while((SPSR&0x80)==0); //wait for transmition to complete //reg = SPDR; SPDR = 0x02; // To write to the IO Register ???!!!!!!!! while((SPSR&0x80)==0); //reg = SPDR; //PORTA |= (1<<AD7708_CS); //CS high // To write to the Communication Register to go to the Mode Register // //reg &= 0x01; //PORTA &= ~(1<<AD7708_CS); //CS low SPDR = 0x01 ; //communications register while((SPSR&0x80)==0); //reg = SPDR ; SPDR = 0x83; while((SPSR&0x80)==0); //reg = SPDR; //PORTA |= (1<<AD7708_CS); //CS high // // To write to the Communication Register to go to the Filter Register // //reg &= 0x03; //PORTA &= ~(1<<AD7708_CS); //CS low SPDR = 0x03 ; //communications register while((SPSR&0x80)==0); //reg = SPDR ; SPDR = 0x03; while((SPSR&0x80)==0); //reg = SPDR; //PORTA |= (1<<AD7708_CS); //CS high while (1) { // Place your code here }; }
Lesezeichen