use of potentiometer as delay

Status
Not open for further replies.

jorginho1877

New Member
Hello everyone

I am doing the programming of some step by step motors, but I have a doubt in which I want to control the delay with a potentiometer, but I have not been able to do it since the resolution values are not well defined to work with the potentiometer

this is the code

#include <16f877A.h>
#fuses XT, NOWDT
#use delay (clock = 4000000)
#use fast_io(B)
#use standard_io(C)
#use standard_io(D)
#use standard_io(A)

void main()
{

int POT= (input (PIN_A1), 0, 1023, 0, 500); ;

set_tris_B(0b00000000);

while (1){

if (input (PIN_A0)== 1){

output_B(0b00000001);
delay_ms(POT);
output_B(0b00000010);
delay_ms(POT);
output_B(0b00000100);
delay_ms(POT);
output_B(0b00001000);
delay_ms(POT);


}
}

}
 
I'm not familiar with that compiler but I'm assuming that the pot value is remapped to 0 to 500. If so can you change it to delay uS?
You also need to move the pot read inside the while loop and make sure it's reading the ADC.

Mike.
BTW, when posting code use code tags so it keeps it's formatting,
Code:
void main(){
	int POT= (input (PIN_A1), 0, 1023, 0, 500); ;
	set_tris_B(0b00000000);
	while (1){
		if (input (PIN_A0)== 1){
			output_B(0b00000001);
			delay_ms(POT);
			output_B(0b00000010);
			delay_ms(POT); 
			output_B(0b00000100);
			delay_ms(POT); 
			output_B(0b00001000);
			delay_ms(POT); 
		}
	}
}
 
If you are using XC8 or CCS, the inbuilt delay isn't allowed to use variables..

I always wrap the delay...

use it like this:-
C:
void DelayMs(int dly)
   {
    while(dly--)
     delay_us(989);
   }

you have to tweak delay_us to get the correct value... It depends on frequency.. while(); takes 11clock cycles.. the call take two..
 
Ian, I'm not familiar with that compiler (or don't recognize it) but it looks like the line if (input (PIN_A0)== 1){ is reading A0 as digital. So the delay will be either 0mS or 500mS.

OP, what processor/compiler is this?

Mike.
 
Also the analogue isn't read in the while loop, which makes the variable pot superfluous... In fact it isn't read at all..
I have just looked at the documentation.. CCS C compiler and the delay_ms(); does indeed take a variable..
 
CCS C compiler and the delay_ms(); does indeed take a variable..

As does yours above

As does mine as well, although I just rather crudely 'call delay_ms(1);' in the loop - I'll hang my head in shame
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…