Delay = 0.697 + 5,947/ADC_Result
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 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 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 unsigned int stepper_1_control_value = 0, stepper_2_control_value = 0, stepper_3_control_value = 0; unsigned int old_stepper_1_control_value = 0, old_stepper_2_control_value = 0, old_stepper_3_control_value = 0; unsigned int stepper_1_required_delay_value_in_ms = 0, stepper_2_required_delay_value_in_ms = 0, stepper_3_required_delay_value_in_ms = 0; long counter_1 = 0, counter_2 = 0, counter_3 = 0; char stepper_sequence[4] = {0xC0, 0x06, 0x03, 0x09}; signed char i = 0, j = 0, k = 0; unsigned char myFlags = 0; #define STEPPER_1_PORT LATC #define STEPPER_2_PORT LATC #define STEPPER_3_PORT LATB sbit stepper_1_direction_flag at myFlags.B0; sbit stepper_2_direction_flag at myFlags.B1; sbit stepper_3_direction_flag at myFlags.B2; long map(long x, long in_min, long in_max, long out_min, long out_max) { return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min; } //Timer1 //Prescaler 1:1; TMR1 Preload = 60536; Actual Interrupt Time : 1 ms //Place/Copy this part in declaration section void InitTimer1() { T1CON = 0x01; TMR1IF_bit = 0; TMR1H = 0xEC; TMR1L = 0x78; TMR1IE_bit = 1; INTCON = 0xC0; } void Interrupt() { if(TMR1IF_bit){ //Interrupt every 3 ms TMR1IF_bit = 0; TMR1H = 0xEC; TMR1L = 0x78; //Enter your code here //if adc(0) value is greater than 0 if(stepper_1_control_value > 0) { //if required milli seconds elapsed if(++counter_1 == stepper_1_required_delay_value_in_ms) { //provide the proper step sequence to stepper 1 STEPPER_1_PORT = (STEPPER_1_PORT & 0xF0) | stepper_sequence[i]; //if stepper 1 direction is clockwise if(stepper_1_direction_flag == 1) { i++; //increment the stepper 1 sequence index } //else if stepper 1 direction is anti-clockwise else if(stepper_1_direction_flag == 0) { i--; //decrement the stepper 1 sequence index } if(i < 0)i = 3; if(i > 3)i = 0; counter_1 = 0; } } //if adc(1) value is greater than 0 if(stepper_2_control_value > 0) { //if required milli seconds elapsed if(++counter_2 == stepper_2_required_delay_value_in_ms) { //provide the proper step sequence to stepper 2 STEPPER_2_PORT = (STEPPER_2_PORT & 0xF0) | stepper_sequence[j]; //if stepper 2 direction is clockwise if(stepper_2_direction_flag == 1) { j++; //increment the stepper 2 sequence index } //else if stepper 2 direction is anti-clockwise else if(stepper_2_direction_flag == 0) { j--; //decrement the stepper 2 sequence index } if(j < 0)j = 3; if(j > 3)j = 0; counter_2 = 0; } } //if adc(2) value is greater than 0 if(stepper_3_control_value > 0) { //if required milli seconds elapsed if(++counter_3 == stepper_3_required_delay_value_in_ms) { //provide the proper step sequence to stepper 3 STEPPER_3_PORT = (STEPPER_3_PORT & 0xF0) | stepper_sequence[k]; //if stepper 3 direction is clockwise if(stepper_3_direction_flag == 1) { k++; //increment the stepper 3 sequence index } //else if stepper 3 direction is anti-clockwise else if(stepper_3_direction_flag == 0) { k--; //decrement the stepper 3 sequence index } if(k < 0)k = 3; // if(k > 3)k = 0; counter_3 = 0; } } } } void main() { asm clrwdt TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; asm clrwdt InitTimer1(); Delay_ms(200); asm clrwdt while(1) { asm clrwdt stepper_1_control_value = ADC_Read(0); Delay_ms(20); stepper_2_control_value = ADC_Read(1); Delay_ms(20); stepper_3_control_value = ADC_Read(2); Delay_ms(20); //if adc(0) value changed if(old_stepper_1_control_value != stepper_1_control_value) { //convert adc value to required milli seconds stepper_1_required_delay_value_in_ms = (6138 / stepper_1_control_value); old_stepper_1_control_value = stepper_1_control_value; } //if adc(1) value changed if(old_stepper_2_control_value != stepper_2_control_value) { //convert adc value to required milli seconds stepper_2_required_delay_value_in_ms = (6138 / stepper_2_control_value); old_stepper_2_control_value = stepper_2_control_value; } //if adc(2) value changed if(old_stepper_3_control_value != stepper_3_control_value) { //convert adc value to required milli seconds stepper_3_required_delay_value_in_ms = (6138 / stepper_3_control_value); old_stepper_3_control_value = stepper_3_control_value; } } }
I had problem finding out the function.
...
Here is the code I made for PIC18F. Is this correct ?
How to convert these raw adc values to milli seconds ?
...
The adc value should be converted to millis sec. The function is not linear.
I am having problem using that tool. I have entered the values in the table but clicking execute doesn't show the graph not it is giving the result. What should I do to get the result ?
if(TMR1IF_bit){ //Interrupt every 3 ms
if(TMR1IF_bit){ //Interrupt every 1 ms
STEPPER_2_PORT = (STEPPER_2_PORT & 0xF0) | stepper_sequence[j];
STEPPER_2_PORT = (STEPPER_2_PORT & 0xF0) | (stepper_sequence[j] << 4);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?