mshh
Full Member level 6
i wrote this code to control proportional solenoid valve with analog input 0-5 v . i made ramp by multiply the input signal by constant k to change the input signal slightly to the final value. but if the signal changed after running the program it can't go into the for loop so i put a condition while (r!=rold) // check if the input changed .but it gives me error
"
here is the code using codevision
"
Code dot - [expand] 1 2 Warning: D:\Micro Controller\my projects\proportional valve amplifier\pwm2.c(107): local variable 'r' is used before its value is set Warning: D:\Micro Controller\my projects\proportional valve amplifier\pwm2.c(107): local variable 'rold' is used before its value is set
here is the code using codevision
Code dot - [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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 /***************************************************** Chip type : ATmega8 Program type : Application AVR Core Clock frequency: 8.000000 MHz Memory model : Small External RAM size : 0 Data Stack size : 256 *****************************************************/ #include <mega8.h> #include <delay.h> #define ADC_VREF_TYPE 0x00 // Read the AD conversion result 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); // Start the AD conversion ADCSRA|=0x40; // Wait for the AD conversion to complete while ((ADCSRA & 0x10)==0); ADCSRA|=0x10; return ADCW; } // Declare your global variables here void main(void) { // Declare your local variables here long r; long rold; const unsigned int k[4]={4,6,8,10}; int i; // number of words in the Sentence long x; long y; PORTB=0x00; DDRB=0x02; PORTC=0x00; DDRC=0x00; PORTD=0x00; DDRD=0xFF; TCCR0=0x00; TCNT0=0x00; // Timer/Counter 1 initialization // Clock source: System Clock // Clock value: 8000.000 kHz // Mode: Ph. & fr. cor. PWM top=ICR1 // OC1A output: Non-Inv. // OC1B output: Discon. // Noise Canceler: Off // Input Capture on Falling Edge TCCR1A=0x80; TCCR1B=0x11; TCNT1H=0x00; TCNT1L=0x00; ICR1H=0x03; ICR1L=0x20; OCR1AH=0x00; OCR1AL=0x00; OCR1BH=0x00; OCR1BL=0x00; ASSR=0x00; TCCR2=0x00; TCNT2=0x00; OCR2=0x00; MCUCR=0x00; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=0x00; // USART initialization // USART disabled UCSRB=0x00; // Analog Comparator initialization // Analog Comparator: Off // Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80; SFIOR=0x00; // ADC initialization // ADC Clock frequency: 1000.000 kHz // ADC Voltage Reference: AREF pin ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; SPCR=0x00; TWCR=0x00; while (1) { while (r!=rold) // check if the input changed { ///////// ramp up or down for(i=0;i<4 ;i++) { r=read_adc(0); PORTD=r; x=r/10; // as i will multiply by K on the next step r from 0-5v y=k[i]*x; OCR1A=y*800/1023; delay_ms(10); } } 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*800/1023; rold=r; } }
Last edited by a moderator: