looping


Closed Thread
Results 1 to 7 of 7

Thread: looping

  1. #1
    Join Date
    Sep 2006
    Posts
    747

    Default looping

    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

  2. #2
    Join Date
    Jan 2006
    Location
    Istanbul
    Posts
    1,185


    Did you find this post helpful? Yes | No

    Default

    On TRISB, set PORTB.5 as an Input pin.

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


    ---------------------------------
    "If the Earth were a single state, Istanbul would be its capital." Napoleon Bonaparte

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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,
    Code:
    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.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by lerameur; - 11th February 2007 at 13:15.

  5. #5
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    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
    Code:
    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
    Last edited by mister_e; - 11th February 2007 at 14:16.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  6. #6
    Join Date
    Sep 2006
    Posts
    747


    Did you find this post helpful? Yes | No

    Default

    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
    Last edited by lerameur; - 11th February 2007 at 14:33.

  7. #7
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default

    1K is fine. You can go up to 10K (even if i hear some here saying it can be WAY higher )

    I used 1k and seems to work good
    Sweet!
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

Similar Threads

  1. Replies: 67
    Last Post: - 8th December 2009, 02:27
  2. SerIn Looping problem ???
    By gtvmarty in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 24th September 2009, 00:31
  3. Looping Variable Assignments
    By jcb344 in forum General
    Replies: 3
    Last Post: - 5th July 2008, 09:50
  4. looping / counter
    By tech in forum General
    Replies: 3
    Last Post: - 18th May 2008, 23:31

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts