lectura del adc de atmega y envío a labview por puerto ser…

Upload: inalac2

Post on 13-Oct-2015

40 views

Category:

Documents


0 download

TRANSCRIPT

  • LECTURA DE ADC Y ENVIO A LABVIEW Por Arnoldo Ulises Villalobos Guerra Instituto Tecnolgico de Zamora

    Este tutorial est enfocado a la lectura de un voltaje en el ADC y enviar la conversin a labview con la finalidad de que usted pueda leer el voltaje de un sensor y mandar la informacin a labview.

    PROGRAMA: leer el voltaje de la derivacin central de un potencimetro por el ADC5 y mandar los datos convertidos a labview, en ste se graficarn y se compararn con un valor, si el valor del voltaje recibido es mayor al valor comparado entonces se enviar un dato para que se prenda un led conectado a PB3.

    A continuacin se muestra la configuracin en el wizard

  • Programa en el codevision

    /***************************************************** This program was produced by the CodeWizardAVR V2.03.6 Evaluation Automatic Program Generator Copyright 1998-2008 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com

    Project : TUTO2 Version : 1 Date : 31/05/2009 Author : Freeware, for evaluation and non-commercial use only Company : VIGA Comments: LECTURA DE VOLTAJE

    Chip type : ATmega48 Clock frequency : 16,000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 128 *****************************************************/

    #include

    // Standard Input/Output functions #include

    #include

    #define ADC_VREF_TYPE 0x60

    // Read the 8 most significant bits // of the AD conversion result unsigned char read_adc(unsigned char adc_input) { ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Delay needed for the stabilization of the ADC input voltage delay_us(10); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCH; }

    // Declare your global variables here unsigned char x;

    #pragma vector=19 // vector de interrupcin por recepcin completa interrupt [USART_RXC] void recepcion (void) // funcin de interrupcin { PORTB=PINB|0X80; // PONE EN UNO EL BIT3 DEL PUERTO B delay_ms(1); // RETARDO DE 1 MILISEGUNDO PARA APRECIAR EL LED PRENDIDO PORTB=PINB&0X80; // PONE EN CERO EL BIT3 DEL PUERTO B }

    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

    // Input/Output Ports initialization // Port B initialization // Func7=In Func6=In Func5=In Func4=In Func3=Out Func2=In Func1=In Func0=In // State7=T State6=T State5=T State4=T State3=0 State2=T State1=T State0=T PORTB=0x00;

  • DDRB=0x08;

    // Port C initialization // Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In // 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 // OC0A output: Disconnected // OC0B output: Disconnected TCCR0A=0x00; TCCR0B=0x00; TCNT0=0x00; OCR0A=0x00; OCR0B=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 // Timer 1 Overflow Interrupt: Off // Input Capture Interrupt: Off // Compare A Match Interrupt: Off // Compare B Match Interrupt: Off 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 // OC2A output: Disconnected // OC2B output: Disconnected ASSR=0x00; TCCR2A=0x00; TCCR2B=0x00; TCNT2=0x00; OCR2A=0x00; OCR2B=0x00;

    // External Interrupt(s) initialization // INT0: Off // INT1: Off // Interrupt on any change on pins PCINT0-7: Off // Interrupt on any change on pins PCINT8-14: Off // Interrupt on any change on pins PCINT16-23: Off EICRA=0x00; EIMSK=0x00; PCICR=0x00;

    // Timer/Counter 0 Interrupt(s) initialization TIMSK0=0x00; // Timer/Counter 1 Interrupt(s) initialization TIMSK1=0x00; // Timer/Counter 2 Interrupt(s) initialization

  • TIMSK2=0x00;

    // USART initialization // Communication Parameters: 8 Data, 1 Stop, No Parity // USART Receiver: On // USART Transmitter: On // USART0 Mode: Asynchronous // USART Baud Rate: 9600 UCSR0A=0x00; UCSR0B=0x18; UCSR0C=0x06; UBRR0H=0x00; UBRR0L=0x67;

    // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; ADCSRB=0x00;

    // ADC initialization // ADC Clock frequency: 500,000 kHz // ADC Voltage Reference: AVCC pin // ADC Auto Trigger Source: None // Only the 8 most significant bits of // the AD conversion result are used // Digital input buffers on ADC0: On, ADC1: On, ADC2: On, ADC3: On // ADC4: On, ADC5: On DIDR0=0x00; ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x85;

    UCSR0B=UCSR0B|0x80; // SE HABILITA LA INTERRUPCIN POR RECEPCIN #asm("sei")

    while (1) {x=UDR0; // COMIENZA LA RECEPCIN while((UCSR0A&0x20)==0x20) // S UDR0 ESTA VACIO ENTRA AL WHILE { UDR0=read_adc(5); // CONVIERTE EL VALOR Y LO EMPIEZA A TRANSMITIR } }; }

    Programa en labview

    Realice el siguiente programa para la recepcin en labview (similar al programa de lectura y escritura de pines)

    Vaya el panel frontal y ponga un indicador grfico tipo chart y un indicador numrico.

    Quite el auto escala en Y en el indicador grfico dando click con el botn derecho del mouse sobre l. Ver figura siguiente

  • Recuerde que el microntrolador enva el valor del ADC convertido, entonces hay que hacer lo inverso para conocer el valor del voltaje. Vaya al diagrama de bloques y utilice una Formula Node para convertir el dato (byte) al valor numrico del voltaje. Compare el resultado con la funcin Greater (mayor que) que est en: Programming>>Comparison>>Greater. Conecte como a continuacin se muestra.

    Observe que se cre una constante de 3 en la comparacin mayor que esto es para mandar un dato al micro y que ste prenda el led conectado a PB3 si el valor de voltaje es mayor a tres.

    Utilice la funcin Case Structure que est en Programming>>Structures>>Case Structure

    Coloque dentro de la estructura la funcin VISA Write y conecte como se muestra.

  • De la anterior manera la estructura Case mandar un dato si resulta que el voltaje es mayor a 3 y no har nada (porque no se meti nada en el caso False) si el resultado es falso de la comparacin con 3.

    Para mandar el dato ponga dentro del caso True la funcin Byte Array to String y en su entrada cree una constante y conecte como a continuacin se muestra.

    Observe que el dato que se mandar en este caso es uno.

    Por ltimo ponemos el ciclo While como se muestra a continuacin.

  • Ahora conecte el circuito y pruebe todo.