mshh
Full Member level 6
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
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: