Gast
#2016430
Ich soll die am Poti eingestellte analoge Werte des AD-wandler Max 146 über Spi Bus durch Atmega 16 zum Pc in 1 seconde abstand erfassen. Ich muss dabe dad folgendeProgramm(source Fil AVr compiler) ändert so ab, die Datenerfassung in abstand von 1 seconde erfolgt. /Verwendung des MAX146/147 (8 Kanäle, 12 Bit AD-Wandler) //auf ext. Erweiterungsboard (Polli_V2_spi) //zum Einlesen von analogen Daten über SPI //und Ausgabe über RS 232 zum PC //Taste 1 (int0) unterbricht SPI-Aktivität kurzzeitig, LEDs blinken //Compiler: CodeVision 1.24.8 // #include <mega16.h> #include <stdio.h> #include <spi.h> #include <delay.h> // MAX147 externe Spannungsreferenz [mV] - nicht für MAX 146 erf. #define VREF 2500 // MEGA 16 SPI-Leitungen für MAX146/147 #define NCS PORTB.4 // MAX146-Pin 18 ---> 13 CON 1 auf Erweiterungsboard #define DOUT PINB.5 // MAX146-Pin 17 ---> 14 // SCLK MAX146 Pin 19 ---> 19 // DOUT MAX146 Pin 15 ---> 15 union adcu { unsigned char byte[2]; unsigned int word; }; interrupt [EXT_INT0] void ext_int0_isr(void) // Taste 1 auf Polliboard { PORTD=0x60; //Leds ein-aus delay_ms (1000); PORTD=0x00; delay_ms(1000); } void uartinit(void) { UCSRA=0x00; UCSRB=0x58; //TXD enable, 8 Bit bei 90S8535 UCR UBRRL=0x33; //9600 Baud bei 8 MHz Quarz bei 90S8535 UBRR } // Funktion f. eine AD-Wandlung - Wert zurück unsigned int max147_read(unsigned char kan) { union adcu adc_data; unsigned char TB1, RB1, RB2, RB3; //control byte f. Max 147 //TB1=0x8F; Kan. 0 ..., externe clock f. AD TB1=0x8e; //Start, Kan. 7, unipolar, //single ended, interne clock für AD if (kan==0) TB1=0x8e; else if (kan==1) TB1=0xce; else if (kan==2) TB1=0x9e; else if (kan==3) TB1=0xde; else if (kan==4) TB1=0xae; else if (kan==5) TB1=0xee; else if (kan==6) TB1=0xbe; else if (kan==7) TB1=0xfe; NCS=0; //Chip Select f. Max 147 delay_us(100); SPDR=TB1; RB1=spi(0); //SPDR=0x00; RB2=spi(0); //SPDR=0x00; RB3=spi(0); NCS=1; //deselect f. MAX147 delay_us(10); adc_data.byte[1]=RB2; adc_data.byte[0]=RB3; return(adc_data.word>>3)&0xfff; } void main(void) { unsigned n1, n2, n3; // Ergebnis der AD-Wandlung float mittelw; // Einstellung f. printf auf float with precision unsigned char kan; // Input/Output Ports initialization // Port A DDRA=0x00; PORTA=0x00; DDRD=0x60; //Leds // Port B // the /SS pin is set as an output // with level 1, it's required by // the SPI to work in master mode //------ mega16 ------ // //PB.7=SCLK PB.6=MISO PB.5=MOSI PB.4=SS // DDRB=0xB0; PORTB=0xB0; // Port C DDRC=0x00; PORTC=0x00; uartinit(); // SPI initialization des uP 90S8535 // SPI Type: Master // SPI Clock Rate: 921.6 kHz=3.6864 MHz/4 // // SPI Clock Phase: Cycle Half // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR=0x50; //putsf("\rMAX147 Demo\n"); //putsf("\r***********************************************\n"); delay_us(100); while (1) { /* for (kan=0; kan<8; kan++) { n=max147_read(kan); printf("Kanal= %2u N=%4u U=%4umV\r\n",kan, n,(unsigned) ((long) n*VREF/4096)); } */ kan=0; n1=max147_read(kan); printf("\r%4u\r", (unsigned) ((long) n1*VREF/4096)); n2=max147_read(kan); printf("%4u\r", (unsigned) ((long) n2*VREF/4096)); n3=max147_read(kan); printf("%4u\r", (unsigned) ((long) n3*VREF/4096)); mittelw= (float) ((n1+n2+n3)/3); printf("------> Mittelw: %f\r", mittelw*VREF/4096); delay_ms(500); GICR=0x40; MCUCR=0x02; #asm("sei") Kann jemand mir helfen,wo ih das Prog ändert muss um die lösung zu haben. Danke. }; }