PDA

View Full Version : looping



lerameur
- 11th February 2007, 05:47
Hello,
I wrote a small program :
I think its self explanatory. Push a button, then do Sub_MicroControllerBatteryCheck function, otherwise just print constant.
The problem is this. The lCD keep jumping from function to function without me pressing anything
Someone have an idea why ?
Ken


portB = 0
loop:

if portB.5 = 1 then
goto Sub_MicroControllerBatteryCheck
else
goto print
endif

print:
Lcdout $fe, 1 'Clear screen
Lcdout "Left: ", Dec 534
Lcdout $fe, $C0, "Right: ", Dec 999
portB.7 = 1
pause 150
portB.7 = 0
goto loop

Sub_MicroControllerBatteryCheck:
adcin 6,battery1
battery1 = 100-( 10*(1024-battery1) )/20

Lcdout $fe, 1 'Clear screen
Lcdout "Battery1: ", Dec battery1
portB.6 = 1
Pause 150
portB.6 = 0
goto loop

End

sayzer
- 11th February 2007, 06:01
On TRISB, set PORTB.5 as an Input pin.

OR just add "INPUT PORTB.5" after "portB = 0".


---------------------------------

mister_e
- 11th February 2007, 09:24
ICD are sometime useful... sometime a pain to deal with... most of the time, too slow to be efficient anyways.... one of the reason why i don't use them.

BTW,


Loop:

if portB.5 = 1 then
goto Sub_MicroControllerBatteryCheck
else
goto print
endif

print:
Even if you don't push on the button, you'll jump to Print right?

Assuming you have place a Pull-down resistor, LVP is set to off, MCLR is set to VDD and there's no multiplexed analog stuff on PORTB, it have to work.

P.S.: As we can't always remind which PIC you're using, you should say us and post your whole code + Crystal speed + Config Fuses setting.

lerameur
- 11th February 2007, 13:10
Ok so here the whole code. I was setting the portB.5 to input.
Yes it should just show the constant hen I am not pressing the button. But now it is flipping from to the other without me doing anything.
k

INCLUDE "modedefs.bas"
@ DEVICE PIC16F877a , HS_OSC , WDT_OFF , PWRT_ON , BOD_ON , LVP_OFF , PROTECT_OFF
'HS 20mhz, watchdog off, powerup timer on, mclr external, brown out detect on, low volt program off , code protect off
CMCON = 9 : ADCON1 = 2
DEFINE OSC 20 'use external 20mhz crystal

DEFINE LCD_DREG PORTD ' Set LCD Data port
DEFINE LCD_DBIT 4 ' Set starting Data bit (0 or 4) if 4-bit bus
DEFINE LCD_RSREG PORTC ' Set LCD Register Select port
DEFINE LCD_RSBIT 6 ' Set LCD Register Select bit
DEFINE LCD_EREG PORTC ' Set LCD Enable port
DEFINE LCD_EBIT 7 ' Set LCD Enable bit ' Set LCD Enable bit
DEFINE LCD_BITS 4 ' Set LCD bus size (4 or 8 bits)
DEFINE LCD_LINES 2 ' Set number of lines on LCD
DEFINE LCD_COMMANDUS 2500
DEFINE LCD_DATAUS 250
DEFINE CHAR_PACING 2000

Pause 2000 ' Wait 2 second

' Define ADCIN parameters
Define ADC_BITS 10 ' Set number of bits in result
Define ADC_CLOCK 3 ' Set clock source (3=rc)
Define ADC_SAMPLEUS 50 ' Set sampling time in uS

DEFINE CCP1_REG PORTC 'Hpwm 1 pin port, RIGHT
DEFINE CCP1_BIT 2 'Hpwm 1 pin bit
DEFINE CCP2_REG PORTC 'Hpwm 2 pin port, LEFT; to enable A
DEFINE CCP2_BIT 1 'Hpwm 2 pin bit
DEFINE HPWM1_TIMER 1
DEFINE HPWM2_TIMER 10

TRISA = %11111111 ' Set PORTA to all input
TRISB = %00111111
TRISC = %00010000
TRISD = %00000000
TRISE = %00000111

ADCON1 = %10000010

battery1 var word

portB = 0

'************************************************* ***************************************
loop:

if portB.5 = 1 then
goto Sub_MicroControllerBatteryCheck
else
goto print
endif

print:
Lcdout $fe, 1 'Clear screen
Lcdout "Left: ", Dec 534
Lcdout $fe, $C0, "Right: ", Dec 999
portB.7 = 1
pause 150
portB.7 = 0
goto loop

Sub_MicroControllerBatteryCheck:
adcin 6,battery1
battery1 = 100-( 10*(1024-battery1) )/20

Lcdout $fe, 1 'Clear screen
Lcdout "Battery1: ", Dec battery1
portB.6 = 1
Pause 150
portB.6 = 0
goto loop


End

mister_e
- 11th February 2007, 14:10
Nah... it have to work. To me it's because you don't have any pull-down resistor attached to your PORTB.5 pin... right?

in worst case you could still use the internal PULL-UP... wich never turned me on... but usefull when you feel lazy ;)

OPTION_REG.7 = 0 ' Active les pull-up de tapette ;o)

And then, you just need to change the IF PORTB.5=1 to = 0

lerameur
- 11th February 2007, 14:28
I never had to use pull down resistors.
So what value whould I use for that (1k fine ?)
and then use IF PORTB.5=1 to = 0 ?

I used 1k and seems to work good
thanks

k

mister_e
- 11th February 2007, 14:41
1K is fine. You can go up to 10K (even if i hear some here saying it can be WAY higher :D )


I used 1k and seems to work good


Sweet!
http://www.mister-e.org/Pics/JumpingSmiley.gif