PWM adc ADC not running simultaneously on ATmega8

Discussion in 'Embedded Systems and Microcontrollers' started by Pushpkant, May 18, 2014.

  1. Pushpkant

    Thread Starter New Member

    May 18, 2014
    1
    0
    The PWM and ADC are working separately fine, but not working simultaneously.

    I am using AVR studio 4 with compiler optimization disabled.

    The program is running timer 2 fast PWM mode in non-inverting mode, and adc channel 0 for feedback from the design, the multimeter shows correct feedback voltage value but controller gives flicker in the design.

    The OCR2 must be in the range of 200 to 220 for the correct operation of the design.

    below is my code:

    Code ( (Unknown Language)):
    1.  
    2. #include<avr/io.h>
    3. #include<avr/interrupt.h>
    4.  
    5. void adcinit()
    6. {
    7.     ADMUX &= ~(1 << REFS0);
    8.     ADMUX &= ~(1 << REFS1); // Aref is selected, internal vref is disabled
    9.     ADMUX &= ~(1 << MUX3);
    10.     ADMUX &= ~(1 << MUX2);
    11.     ADMUX &= ~(1 << MUX1);
    12.     ADMUX &= ~(1 << MUX0);
    13.  
    14.     ADCSRA |= (1 << ADEN);  // enable the adc
    15.     ADCSRA &= ~(1 << ADSC);
    16.     ADCSRA &= ~(1 << ADFR);
    17.     ADCSRA &= ~(1 << ADIF);
    18.     ADCSRA &= ~(1 << ADIE);
    19.     ADCSRA |= (1 << ADPS2);
    20.     ADCSRA |= (1 << ADPS1);
    21.     ADCSRA |= (1 << ADPS0); // division factor 128
    22.  
    23.     return;
    24. }
    25.  
    26. unsigned int adcread(unsigned char ch)
    27. {
    28.     ADMUX = (ch & 0x07); // if all the pins in admux are zero no need to apply a bitwise or operation, while a masking is already done by a  bitwise and operation.
    29.  
    30.     ADCSRA |= (1 << ADSC);
    31.  
    32.     while( ADCSRA & ( 1 << ADSC )); // wait for conversion completes
    33.  
    34.     return ADC;
    35. }
    36.    
    37.  
    38. void msdelay(unsigned int itime)
    39. {
    40.     unsigned int i,j;
    41.  
    42.     for(i=0;i<itime;i++)
    43.     {
    44.         for(j=0;j<100;j++)
    45.         {
    46.         }
    47.     }
    48.     return;
    49. }
    50.  
    51. void PWM_on()
    52. {
    53.     TCCR2 |= (1 << COM21);  // set pwm on non inverting mode
    54.  
    55.     TCCR2 |= (1 << WGM21) | (1 << WGM20);
    56.     // set fast pwm mode
    57.  
    58.     TCCR2 |= (1 << CS20);
    59.     // start the pwm with prescalar 1
    60.    
    61.     return;
    62. }
    63.  
    64.  
    65. int main()
    66. {
    67.     unsigned char duty=0;
    68.     unsigned int feedback=0;
    69.  
    70.     DDRB=0xFF;
    71.     DDRD=0xFF;
    72.  
    73.     adcinit();
    74.     PWM_on();
    75.  
    76.     OCR2=128;
    77.  
    78.     while(1)
    79.     {
    80.         feedback=adcread(0);
    81.  
    82.         if(feedback<200)
    83.         {
    84.             duty++;
    85.         }
    86.  
    87.         if(feedback>220)
    88.         {
    89.             duty--;
    90.         }
    91.  
    92.         OCR2=duty;
    93.  
    94.         msdelay(500);
    95.  
    96.     }
    97.     return 0;
    98. }
    99.  
     
    Last edited by a moderator: May 18, 2014
Loading...