|
|
View previous topic :: View next topic |
Author |
Message |
boogajamooga
Joined: 24 Mar 2011 Posts: 5
|
Flashing LED's |
Posted: Sun Mar 27, 2011 7:22 pm |
|
|
Hi, I am trying to implement this logic where the the PINs of the PIC16f877a will output high and turn on some LED's based on an if statement check. The If statement is comparing the analog input that I am inputting using a function generator. I am using a 5V peak to peak w/ offset 2.5v, so the lowest voltage ouputted from the function generator is 0v and the highest is 5v in a square wave function. This input is connected to pin RA0 of the PIC, which is an analog input port, and I am sending this input through the PIC's ADC, which I am setting up with the following code:
Code: |
setup_adc_ports(ALL_ANALOG); //RA0 port is set to analog w/ Vcc as ref voltage to receive source tank sensor data
setup_adc(ADC_CLOCK_DIV_32); //use the external 20mhz clock
set_adc_channel(0); //sets it to read the analog value of the source tank at pin AN0
delay_ms(20); //allow some delay time in order for the PIC to set up the ADC after initializing the ports/clock
|
and this is my adc function that compares the analog input and returns a 0 if it is below the threshold, and a 1 if it above the threshold.
Code: |
//Returns 0 if the current value is below the parameter theshold value. 1 otherwise
int ADC_threshold(int thresh){ //unsigned char makes it an 16-bit value, int makes the threshold variable a 16-bit value
if(read_adc() < thresh) //compare the converted analog value to the middle value in hex between 0v and 5v
return 0; //sets the variable to which it returns to a digital low
else
return 1; //sets the variable to which it returns to a digital high
}
|
Next, in main, I am sending it through a while loop and then using an if statement where this adc function returns its result and outputting to the pins based on this. So, basically when the ADC determines that the analog input is below the threshold, it should output the pin to high, when it is below, it shoulnd't do anything, but the first time they go high, i never implemented code to turn the pins low so I am confused as to why they start to blink
Code: |
int main(){
while(1){
if(ADC_threshold(THRESHOLD)){ //check to see if source tank is low. if it is, don't even poll receiver channels and turn ALL motors off
output_high(PIN_D2); //turn motor 0 off
output_high(PIN_D3); //turn motor 1 off
output_high(PIN_C4); //turn motor 2 off
output_high(PIN_C5); //turn motor 3 off
output_high(PIN_D4); //turn motor 4 off
output_high(PIN_D5); //turn motor 5 off
output_high(PIN_D6); //turn motor 6 off
output_high(PIN_D7); //turn motor 7 off
}
} //end of while loop
return 0;
}//end of main loop
|
Also, my function generator is cutting power to your output pins when the signal goes low. How would you fix that problem? |
|
|
SherpaDoug
Joined: 07 Sep 2003 Posts: 1640 Location: Cape Cod Mass USA
|
|
Posted: Mon Mar 28, 2011 5:51 am |
|
|
I assume you are powering the PIC from +5V? If it is 3V make sure the analog input never goes above the supply voltage.
Even at 5V I would still set the function generator to output 1V to 4V to start with just to make sure. It would also be good to put a 1000 Ohm resistor in the function generator output so weird stuff doesn't happen if the input pin gets set to an output. _________________ The search for better is endless. Instead simply find very good and get the job done. |
|
|
demonspells
Joined: 06 Jan 2011 Posts: 26
|
|
Posted: Mon Mar 28, 2011 12:53 pm |
|
|
your threshold is int, that means you should set the adc to 8bits
and the threshold should be between 0-255
to set the ADC to 8-Bits add this line to the beginning of your code
_________________ Badr Ghatasheh |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|