Hello.
I am trying to make a pulse delay using a potentiometer (using a ADC). The ADC works, the delay function work, but it's not a linear delay.
What i want to say is that when the pot is at 0, i get no delay. When it's somewhere else, i get some delay, however, after a certain point, the delay begins to drop, until it almost get to 0 again, however the pot gets to full range.
This is the code. I hope someone can help me with this.
#include<p18f4550.h> // Always include the header file
#include <delays.h>
#define REMAPPED_RESET_VECTOR_ADDRESS 0x1000
/***************Bootloader************************************/
#pragma code REMAPPED_RESET_VECTOR = REMAPPED_RESET_VECTOR_ADDRESS
extern void _startup (void);
void _reset (void)
{
_asm goto _startup _endasm
}
#pragma code
void Delay(int x)
{
int i; //Post:Delay for x*10ms.
for (i=0; i<x; i++){
Delay10TCYx (10); // gives a delay of 10 x 60 x 1/12 = 600/12 = 50 us
}
}
void ADCInit()
{
ADCON1 = 0b00001110;//VSS,VDD ref. AN0 analog only
ADCON0 = 0x00;//clear ADCON0 to select channel 0 (AN0)
ADCON2 = 0b10001000;//ADCON2 setup: Right justified, Tacq=2Tad, Tad=2*Tosc (or Fosc/2)
ADCON0bits.ADON = 0x01;//Enable A/D module
}
void main(void)
{
unsigned int delay;
int ad_val;
TRISB=0b00000001;
PORTB=0b00000000;
PORTBbits.RB2=1;
ADCInit();
while(1)
{
if(PORTBbits.RB2 == 0)PORTBbits.RB2 = 1;
if(PORTBbits.RB0 == 0) //test button press
{
ADCON0bits.GO_DONE = 1; //Start A/D Conversion
while(ADCON0bits.GO_DONE != 0); //Loop here until A/D conversion completes
ad_val=((ADRESH<<8)+ADRESL); //get value of 10 bit adc
Delay100TCYx (ad_val); //insert number in delay function
PORTBbits.RB2 = 0; //change port status
Delay10TCYx (120); //delay for a bit
PORTBbits.RB2 = 1; //change back to initial port status
}
}
}