ramp the pwm and stable at final value

Status
Not open for further replies.

mshh

Full Member level 6
Joined
May 30, 2010
Messages
349
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,296
Location
usa
Activity points
3,871
I want to control proportional valve using pwm generated by atmega 8. the pwm changed when changing analog input value. i did this code to make the pwm reached it's final value by ramping it, so i did the matrix K. the problem here is that the output pwm didn't stable at the final value (r) ], it goes up then down and so on
i want to ramp from rold to r


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
void main(void)
{
    float r;
   float rold =0; 
   const unsigned int k[4]={4,6,8,10};  /////slope of ramp  up
  
 
while (1)
      {  
 
  while (r!=rold) // check if the input changed or not equaled
 
   {  
////////////////////// ramp up pwm
if(r>rold)
{
 for(i=0;i<4 ;i++)
 {
 r=read_adc(0);
 x=r/10;   // as i will multiply by K on the next step r from 0-5v
 y=k[i]*x;
  OCR1A=y*2000/1024;
  delay_ms(50);
 }  
 }
           rold=r;
  
   } 
   r=read_adc(0);
      x=r/10;   // as i will multiply by K on the next step     
     y=k[3]*x;//  stable at final value of x
      OCR1A=y*2000/1024;
      }
}

 
Last edited by a moderator:

Where is y variable declared ? Is it unsigned int type ?
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…