View previous topic :: View next topic |
Author |
Message |
roivas
Joined: 28 Jan 2009 Posts: 5
|
LED's on port B responding to AD inputs (picdem 2+) |
Posted: Sun Feb 01, 2009 1:43 pm |
|
|
Hello all.
I am working on a project to fill a container with a specific temperature water. I am using a picdem 2+ (red board) with a 16f877 chip. I am modeling the LED's on port B as valves that control the hot and cold water and drain. I am using modified code from these forums as a foundation for the code. In this code I am simulating a temp input with the installed potentiometer at A0.
My problem is that I can get HOT to come on and the DRN to close when at the appropriate temperature, then, when temp continues to rise, the CW1 and CW2 come on... but the CW1 cycles... and yet the CW2 operates as expected...
Code: |
#include <16F877.H>
#device adc=10
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=4000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
#include "flex_lcd.c"
#define HOT PIN_B0
#define CW1 PIN_B1
#define CW2 PIN_B2
#define DRN PIN_B3
//============================
void main()
{ //main start
int16 adc_value;
float volts;
float temperature;
int8 temp_sp=98;
lcd_init();
setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
set_adc_channel(0);
delay_us(20);
while(1)
{ //while start
adc_value = read_adc();
volts = (adc_value * 5)/1023.0;//(float)
temperature=volts*393.01-867.29;
printf(lcd_putc, "\f%3.3f volts\n", volts);
if (temperature<50)
{//if1 start
printf(lcd_putc, "Low");
}//end if1
else if (temperature>140)
{//elseif2 start
printf(lcd_putc, "High");
}//elseif2 end
else
{//else3 start
printf(lcd_putc, "%3.2f F", temperature);
}//else3 end
delay_ms(500);
if (temperature<temp_sp)
{//if4 start
output_high(HOT); //opens hot water valve and raises temp before fill
}//if4 end
if (temperature>130)//over temperature condition
{//if5 start
output_low(HOT);
output_low(CW1);
output_low(CW2);
output_low(DRN);
}//if5 end
else if ((temperature > temp_sp-5) & (temperature < temp_sp+5))//temp is at sp
{//elseif6 start
output_high(DRN);//closes drain valve
}//elseif6 end
else if (temperature > temp_sp+5)//checks if temp is too high
{
if (!input(CW1))//if cw1 is not energized
{
output_high(CW1);// energize cw1
}
if(!input(CW2)) //if cw2 is not energized
{
output_high(CW2); //energize cw2
}
}
//}
delay_cycles(1);
}//while end
} //main end |
Would anyone have any advice as to why the CW1 on pin B1 would be cycling? (at a constant input temperature being simulated via the potentiometer)
Please be gentle
I am new to C programming and have leaned much just by studying and manipulating code and watching how the system responds. I appreciate all the knowledge I have gained from these forums and hope to learn more. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 01, 2009 4:14 pm |
|
|
I have that board, and both the LEDs on B1 and B2 blinked on and off
together, at the same time. I could not duplicate your problem.
Quote: | else if ((temperature > temp_sp-5) & (temperature < temp_sp+5)) |
Here you are using a bitwise-AND operation, and I'm sure you intend it
to be a logical operation. That's done with a double-ampersand: && |
|
|
roivas
Joined: 28 Jan 2009 Posts: 5
|
|
Posted: Sun Feb 01, 2009 4:21 pm |
|
|
My initial assessment of the LED on pin B2 was wrong, it is cycling as well.
They both turn off at the if(!input(x)) command. Am I turning these off here?? I understood this command to check the status of the pin and not perform an operation.
Edit: they turn off after executing the above command which confuses me more.... if the bit is high it should pass over to the next instruction... that is my understanding. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun Feb 01, 2009 4:27 pm |
|
|
In standard i/o mode, the CCS pin functions change the TRIS on the
pin to the direction required by the operation. So the input() function
changes the TRIS to be an input pin. It no longer acts as an output.
To read a pin without changing the TRIS, use the input_state() function
instead. From the manual:
Quote: | input_state()
Syntax: value = input_state(pin)
Description:
This function reads the level of a pin without changing the direction of the
pin as INPUT() does. |
|
|
|
roivas
Joined: 28 Jan 2009 Posts: 5
|
|
Posted: Sun Feb 01, 2009 4:49 pm |
|
|
Thanks for the response and the info! It now works as I wanted it to... so far
Again, thank you. |
|
|
|