Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] PWM adc ADC not running simultaneously on ATmega8

Status
Not open for further replies.

Pushpkant

Member level 1
Joined
May 17, 2014
Messages
39
Helped
2
Reputation
4
Reaction score
2
Trophy points
8
Location
India
Activity points
451
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 C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<avr/io.h>
#include<avr/interrupt.h>
 
void adcinit()
{
    ADMUX &= ~(1 << REFS0);
    ADMUX &= ~(1 << REFS1); // Aref is selected, internal vref is disabled
    ADMUX &= ~(1 << MUX3);
    ADMUX &= ~(1 << MUX2);
    ADMUX &= ~(1 << MUX1);
    ADMUX &= ~(1 << MUX0);
 
    ADCSRA |= (1 << ADEN);  // enable the adc
    ADCSRA &= ~(1 << ADSC);
    ADCSRA &= ~(1 << ADFR);
    ADCSRA &= ~(1 << ADIF);
    ADCSRA &= ~(1 << ADIE);
    ADCSRA |= (1 << ADPS2);
    ADCSRA |= (1 << ADPS1);
    ADCSRA |= (1 << ADPS0); // division factor 128
 
    return;
}
 
unsigned int adcread(unsigned char ch)
{
    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.
 
    ADCSRA |= (1 << ADSC);
 
    while( ADCSRA & ( 1 << ADSC )); // wait for conversion completes
 
    return ADC;
}
    
 
void msdelay(unsigned int itime)
{
    unsigned int i,j;
 
    for(i=0;i<itime;i++)
    {
        for(j=0;j<100;j++)
        {
        }
    }
    return;
}
 
void PWM_on()
{
    TCCR2 |= (1 << COM21);  // set pwm on non inverting mode
 
    TCCR2 |= (1 << WGM21) | (1 << WGM20);
    // set fast pwm mode
 
    TCCR2 |= (1 << CS20);
    // start the pwm with prescalar 1
    
    return;
}
 
 
int main()
{
    unsigned char duty=0;
    unsigned int feedback=0;
 
    DDRB=0xFF;
    DDRD=0xFF;
 
    adcinit();
    PWM_on();
 
    OCR2=128;
 
    while(1)
    {
        feedback=adcread(0);
 
        if(feedback<200)
        {
            duty++;
        }
 
        if(feedback>220)
        {
            duty--;
        }
 
        OCR2=duty;
 
        msdelay(500);
 
    }
    return 0;
}

 

For PWM better used an ISR function take a logic from that and excure the code
 

Where have you set the direction of ADC input pin using something like DDRA = 0x00? (I assume PORTA0 is ADC0 pin)
 

hii, embRTS and milan thanks for replay,

milan I have PORTC0 is ADC0 in atmega8, I have just tried to set the DDRC=0x00, to make it as input pin but still it didn't work.

embRTS I have also tried the ISR for the PWM generation but that didn't work so I moved to the timer2 wave generator mode. Now I am retrying the ISR for PWM generation. I will get back soon after testing.

If you need I can upload the schematic?
Thank again for help...
 

The problem is solved, there is missing a filter capacitor at the input of the power supply of design.
any way thank milan and embRTS for helping.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top