|
|
View previous topic :: View next topic |
Author |
Message |
nrmeyer34 Guest
|
Using RB0 as Input not working |
Posted: Thu Dec 28, 2006 2:10 pm |
|
|
I have set up a program where i am using RB0 as a digital input pin. However when i run my program the pin goes high. I have tris set up for it to be a input and i also have port_b pullups set to false. What am i missing in the set up? |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 28, 2006 2:31 pm |
|
|
Are you using the output_b() function in standard i/o mode ?
If so, the compiler will change the TRIS for all pins on Port B
to be outputs. |
|
|
Guest
|
|
Posted: Thu Dec 28, 2006 2:41 pm |
|
|
here is the code that i am working with. I should have attached it before.
#include <16F819.h>
#device adc=10
#use delay(clock=4000000)
#fuses WDT,INTRC_IO, PUT, MCLR, NOBROWNOUT, NOLVP, NOCPD, NOWRT, NODEBUG, NOPROTECT
#use i2c(Master,Fast,sda=PIN_B1,scl=PIN_B4)
#include <math.h>
#use fast_io(A)
#define th PIN_A0 //heater thermistor
#define isns PIN_A2 //current sense
#define vbat PIN_A3 //battery voltage sense
#define ch_off PIN_A6 //disable charge circuit output
#define dis PIN_A7 //discharge control output
#use fast_io(B)
#define enable PIN_B0 //input line from host
#define SMBD PIN_B1 //data line
#define status PIN_B2 //output on state of charger
#define SMBC PIN_B4 //clock line
//#define INTS_PER_SECOND 76
#define INTS_PER_SECOND 15
//#byte port_b=6
//#byte INTCON=0x0B
////////////////////////////////////////////////////////////////////////////////
char i,loop=1,int_count;
long htr_temp,pack_temp,time1,time2,seconds,current,v_bat,percent,duty;
////////////////////////////////////////////////////////////////////////////////
#int_rtcc
clock_isr()
{
if(--int_count==0) //decrement counter until 0
{
++seconds; //count up seconds
int_count=INTS_PER_SECOND; //reset counter
if (seconds>=65000)
seconds=65000;
}
}
void main()
{
output_low(ch_off); //charger enabled
output_low(dis); //heater off
output_float(SMBC); //release data line
output_float(SMBD); //release clock line
setup_oscillator(osc_4mhz);
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_DIV_8);
setup_spi(FALSE);
setup_counters(RTCC_INTERNAL,RTCC_DIV_256);
set_rtcc(0);
enable_interrupts(INT_RTCC);
enable_interrupts(GLOBAL);
//disable_interrupts(GLOBAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DIV_BY_1,25,1);
setup_ccp1(CCP_PWM);
port_b_pullups(false); //added to see if this could be used on chg
set_tris_a(0b00011111);
set_tris_b(0b00000001);
duty=94;
output_low(ch_off); //charger enabled
output_low(dis); //heater off
output_float(SMBC); //release data line
output_float(SMBD); //release clock line
loop=1; //initialize all vairables
int_count=INTS_PER_SECOND; //used for timer
seconds=0; //clear out variable
time1=120; //counter for time, 8 hours
time2=240;
for (i=0;i<=100;i++) //half second delay
{
delay_ms(10); //delay of 1 sec
restart_wdt();
}
while (TRUE)
{
set_adc_channel(2); //read pack thermistor voltage(chagne back to 2)
delay_us(500);
restart_wdt();
current=read_adc();
seconds=0;
set_adc_channel(3); //read pack thermistor voltage
delay_us(500);
restart_wdt();
v_bat=read_adc();
seconds=0;
/*i2c_start();
i2c_write(0x16);
i2c_write(0x0E);
i2c_start();
i2c_write(0x17);
percent=i2c_read(0);
i2c_stop();*/
for (i=0;i<=200;i++) //half second delay
{
delay_ms(10); //delay of 1 sec
restart_wdt();
}
set_pwm1_duty(duty);
if(input(enable))
output_low(ch_off);
if(!input(enable))
output_low(ch_off);
}
} |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Dec 28, 2006 3:25 pm |
|
|
Please use the 'code' buttons when posting code in order to preserve the formatting of your program.
What is connected to input B0? Is there an active circuit connected that will ensure the input is low when not active? For example an open collector output would require a pull-down resistor.
Then, how do you 'know' the input is going high? How did you meassure this behaviour?
I had a quick look at your program and the following code seems incorrect to me Code: | if(input(enable))
output_low(ch_off);
if(!input(enable))
output_low(ch_off); | In both cases, a high or low on input B0, you perform the same action.
Just a tip, you can tell the compiler to trigger the watchdog while inside the delay routines by changing Code: | #use delay(clock=4000000) | to Code: | #use delay(clock=4000000, RESTART_WDT) |
Then instead of Code: | for (i=0;i<=200;i++) //half second delay
{
delay_ms(10); //delay of 1 sec
restart_wdt();
} |
your code reduces toMuch easier to read and no more confusions for is it a 'half second delay', 'delay of 1 sec' or a '2 second' delay. |
|
|
nrmeyer34 Guest
|
|
Posted: Thu Dec 28, 2006 3:44 pm |
|
|
All is ok. I found that it was a hardware problem. I did know that the "if" statements both had the same output, i was messing with the code at that point trying to figure out what was wrong.
Thanks for the tip on the WDT. That will help reduce code and improve clarity. |
|
|
|
|
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
|