Connect with us

problem with ATmega 16

Discussion in 'Microcontrollers, Programming and IoT' started by hosseinkargar, Oct 9, 2015.

  1. hosseinkargar

    hosseinkargar

    2
    0
    Oct 9, 2015
    hello guys

    i have a serious problem with ADC

    i enabled 3 ADC on my microcontroller but there is a lot of noise i mean when ADC(0) is enable every thing is good but
    after enable ADC(1) noise is come . i useing AVR codevision . i went to code wizard and changed ADC clock to 125/000 khz
    and set usart to 8 data 1 stop parity : even and noise is gone. but when i enabled ADC(2) every thing has been messed up

    note : i checked the circuit with multi meter an every thing is good .

    please help me to find the problem , tanks a lot


    while (1)
    {
    adc0=readAdc(0);
    delay_ms(50);
    adc1=readAdc(1);
    delay_ms(50);
    adc2=readAdc(2);
    delay_ms(50);
    if(i<5)
    {
    a=adc0;
    b=adc1;
    n=adc2;
    i++;
    }
    else
    {
    i=0;
    //knob1
    printf(" knob1= ");
    for(j=0;j<5;j++)
    {
    printf("%d",a[j]);
    printf("-");
    }
    printf(" ");
    delay_ms(1000);

    //knob2
    printf(" knob2= ");
    for(j=0;j<5;j++)
    {
    printf("%d",b[j]);
    printf("-");
    }
    printf(" ");
    delay_ms(1000);
    //konb3
    printf(" knob3= ");
    for(j=0;j<5;j++)
    {
    printf("%d",n[j]);
    printf("-");
    }
    printf(" ");
    delay_ms(1000);
    }
    }
    }

    int readAdc(int anyAdc)
    {
    int x;
    float y;
    y=read_adc(anyAdc);
    x=(y/1023)*127;
    return x;
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    9,396
    1,919
    Nov 17, 2011
    The AVR cntrollers have a noise-reduction mode for A/D-conversion, see e.g. here.
    Basically it reduces all operations of the controler that can contribute to noise on the ADC.
     
    hosseinkargar likes this.
  3. hosseinkargar

    hosseinkargar

    2
    0
    Oct 9, 2015
    hello my friend i read the post and i enabled noise canceler

    but i still have the problem :(

    last day i Bought a CD4051BE multiplexer but not helped

    here the code :

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

    Project :
    Version :
    Date : 13/10/2015
    Author : Hossein
    Company :
    Comments:


    Chip type : ATmega16
    Program type : Application
    AVR Core Clock frequency: 16/000000 MHz
    Memory model : Small
    External RAM size : 0
    Data Stack size : 256
    *****************************************************/

    #include <mega16.h>

    #include <delay.h>

    #ifndef RXB8
    #define RXB8 1
    #endif

    #ifndef TXB8
    #define TXB8 0
    #endif

    #ifndef UPE
    #define UPE 2
    #endif

    #ifndef DOR
    #define DOR 3
    #endif

    #ifndef FE
    #define FE 4
    #endif

    #ifndef UDRE
    #define UDRE 5
    #endif

    #ifndef RXC
    #define RXC 7
    #endif

    #define FRAMING_ERROR (1<<FE)
    #define PARITY_ERROR (1<<UPE)
    #define DATA_OVERRUN (1<<DOR)
    #define DATA_REGISTER_EMPTY (1<<UDRE)
    #define RX_COMPLETE (1<<RXC)

    // Get a character from the USART Receiver
    #ifndef _DEBUG_TERMINAL_IO_
    #define _ALTERNATE_GETCHAR_
    #pragma used+
    char getchar(void)
    {
    char status,data;
    while (1)
    {
    while (((status=UCSRA) & RX_COMPLETE)==0);
    data=UDR;
    if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
    return data;
    }
    }
    #pragma used-
    #endif

    // Write a character to the USART Transmitter
    #ifndef _DEBUG_TERMINAL_IO_
    #define _ALTERNATE_PUTCHAR_
    #pragma used+
    void putchar(char c)
    {
    while ((UCSRA & DATA_REGISTER_EMPTY)==0);
    UDR=c;
    }
    #pragma used-
    #endif

    // Standard Input/Output functions
    #include <stdio.h>

    unsigned int adc_data;
    #define ADC_VREF_TYPE 0x40

    // ADC interrupt service routine
    interrupt [ADC_INT] void adc_isr(void)
    {
    // Read the AD conversion result
    adc_data=ADCW;
    }

    // Read the AD conversion result
    // with noise canceling
    unsigned int 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);
    #asm
    in r30,mcucr
    cbr r30,__sm_mask
    sbr r30,__se_bit | __sm_adc_noise_red
    out mcucr,r30
    sleep
    cbr r30,__se_bit
    out mcucr,r30
    #endasm
    return adc_data;
    }

    int knob1,knob2;
    float y;


    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=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=Out Func0=Out
    // State7=T State6=T State5=T State4=T State3=T State2=0 State1=0 State0=0
    PORTB=0x00;
    DDRB=0x07;

    // 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=0xFF
    // OC0 output: Disconnected
    TCCR0=0x00;
    TCNT0=0x00;
    OCR0=0x00;

    // Timer/Counter 1 initialization
    // Clock source: System Clock
    // Clock value: Timer1 Stopped
    // Mode: Normal top=0xFFFF
    // OC1A output: Discon.
    // OC1B output: Discon.
    // Noise Canceler: Off
    // Input Capture on Falling Edge
    // Timer1 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: Timer2 Stopped
    // Mode: Normal top=0xFF
    // OC2 output: Disconnected
    ASSR=0x00;
    TCCR2=0x00;
    TCNT2=0x00;
    OCR2=0x00;

    // External Interrupt(s) initialization
    // INT0: Off
    // INT1: Off
    // INT2: Off
    MCUCR=0x00;
    MCUCSR=0x00;

    // Timer(s)/Counter(s) Interrupt(s) initialization
    TIMSK=0x00;

    // USART initialization
    // Communication Parameters: 8 Data, 1 Stop, Even Parity
    // USART Receiver: On
    // USART Transmitter: On
    // USART Mode: Asynchronous
    // USART Baud Rate: 9600
    UCSRA=0x00;
    UCSRB=0x18;
    UCSRC=0xA6;
    UBRRH=0x00;
    UBRRL=0x67;

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

    // ADC initialization
    // ADC Clock frequency: 125/000 kHz
    // ADC Voltage Reference: AVCC pin
    // ADC Auto Trigger Source: ADC Stopped
    ADMUX=ADC_VREF_TYPE & 0xff;
    ADCSRA=0x8F;

    // SPI initialization
    // SPI disabled
    SPCR=0x00;

    // TWI initialization
    // TWI disabled
    TWCR=0x00;

    // Global enable interrupts
    #asm("sei")

    while (1)
    {
    PORTB.0=0;
    PORTB.1=0;
    PORTB.2=0;
    delay_ms(30);
    y=read_adc(0);
    knob1=(y/1023)*127;
    printf(" knob1= ");
    printf("%d",knob1);

    PORTB.0=0;
    PORTB.1=0;
    PORTB.2=1;
    delay_ms(30);
    y=read_adc(0);
    knob2=(y/1023)*127;
    delay_ms(1000);
    printf(" knob2= ");
    printf("%d",knob2);
    delay_ms(1000);
    }
    }

    i set every 2 knobs to 127
    and this is the output :

    [​IMG]
     
    Last edited: Oct 13, 2015
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    9,396
    1,919
    Nov 17, 2011
    This is not sufficient. A multimeter is an integrating measuring device and will remove noise by averaging/integrating the measured value over time. Look at the ADC's input signals with an oscilloscope. Or, if you don't have access to an oscilloscope, use the multimeter in mV AC range.

    If you do not require instant measured values, you can do some averaging of teh measured values in your software. Read e.g. 6 ADC values and sume them up, then divide by eight (I used eight because the divide is then a shift to the right by 3 places):
    ADC_mean = (ADC(0)+ADC(1)+ADC(2)+ADC(3)+ADC(4)+ADC(5)+ADC(6)+ADC(7))>>3;
     
    hosseinkargar likes this.
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-