shifting problem


Closed Thread
Results 1 to 2 of 2
  1. #1
    Join Date
    Aug 2007
    Posts
    6

    Default shifting problem

    hi everyone here, i'm newbie in pbp, i wrote a program so that when i shift 1 bit to left in portb, it will increase the pwm, while shift 1 bit to the right in portb will decrease the pwm. when i run my project, it seem doesnt run well, somemtimes it work, sometimes it's not. can you please let me know what problems i have? thank for your help.
    my code shown below:


    DEFINE OSC 4
    DEFINE CCP1_REG PORTC
    DEFINE CCP1_BIT 2
    DEFINE CCP2_REG PORTC
    DEFINE CCP2_BIT 1
    trisb = %11111111
    trisc = %11110000
    CCP1CON = %00001100
    CCP2CON = %00001100
    T2CON = %00000100
    L VAR BYTE
    M VAR BYTE
    a var byte
    b var byte
    c var byte
    d var byte
    e var byte
    f var byte
    g var byte
    h var byte
    m = 80
    l = 0
    a = %00000001
    b = %00000010
    c = %00000100
    d = %00001000
    e = %00010000
    f = %00100000
    g = %01000000
    h = %10000000

    main:
    hpwm 1,0,1000
    hpwm 2,0,1000
    if portc.7 == 1 then 'selection button press, go to led
    pause 850
    goto LED
    endif
    goto main
    end

    '---------------------------------------------------------------------------

    LED:
    low portc.5
    high portc.4
    gosub LEDCOMPARE
    if portc.7 == 1 then
    pause 850
    goto motor
    else
    goto LED
    endif

    '--------------------------------------------------------------------------

    MOTOR:
    low portc.4
    high portc.5
    GOSUB MOTORCOMPARE
    if portc.7 == 1 then
    pause 850
    goto led
    else
    goto motor
    endif

    '-----------------------------------------------------------------------------
    LEDCOMPARE:

    if (portb == a) then
    pause 500
    if (portb == b) then
    gosub ledincrease
    endif
    else
    if (portb == h) then
    gosub leddecrease
    endif
    endif

    if (portb == b) then
    pause 500
    if (portb == c) then
    gosub ledincrease
    endif
    else
    if (portb == a) then
    gosub leddecrease
    endif
    endif

    if (portb == c) then
    pause 500
    if (portb == d) then
    gosub ledincrease
    endif
    else
    if (portb == b) then
    gosub leddecrease
    endif
    endif

    if (portb == d) then
    pause 500
    if (portb == e) then
    gosub ledincrease
    endif
    else
    if (portb == c) then
    gosub leddecrease
    endif
    endif

    if (portb == e) then
    pause 500
    if (portb == f) then
    gosub ledincrease
    endif
    else
    if (portb == d) then
    gosub leddecrease
    endif
    endif

    if (portb == f) then
    pause 500
    if (portb == g) then
    gosub ledincrease
    endif
    else
    if (portb == e) then
    gosub leddecrease
    endif
    endif

    if (portb == g) then
    pause 500
    if (portb == h) then
    gosub ledincrease
    endif
    else
    if (portb == f) then
    gosub leddecrease
    endif
    endif

    if (portb == h) then
    pause 500
    if (portb == a) then
    gosub ledincrease
    endif
    else
    if (portb == g) then
    gosub leddecrease
    endif
    endif
    return

    '--------------------------------------------------------------------------

    MOTORCOMPARE:
    if (portb == a) then
    pause 500
    if (portb == b) then
    gosub MOTORincrease
    endif
    else
    if (portb == h) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == b) then
    pause 500
    if (portb == c) then
    gosub MOTORincrease
    endif
    else
    if (portb == a) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == c) then
    pause 500
    if (portb == d) then
    gosub MOTORincrease
    endif
    else
    if (portb == b) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == d) then
    pause 500
    if (portb == e) then
    gosub MOTORincrease
    endif
    else
    if (portb == c) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == e) then
    pause 500
    if (portb == f) then
    gosub MOTORincrease
    endif
    else
    if (portb == d) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == f) then
    pause 500
    if (portb == g) then
    gosub MOTORincrease
    endif
    else
    if (portb == e) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == g) then
    pause 500
    if (portb == h) then
    gosub MOTORincrease
    endif
    else
    if (portb == f) then
    gosub MOTORdecrease
    endif
    endif

    if (portb == h) then
    pause 500
    if (portb == a) then
    gosub MOTORincrease
    endif
    else
    if (portb == g) then
    gosub MOTORdecrease
    endif
    endif
    return

    '-----------------------------------------------------------------------------

    ledincrease:
    high portc.4
    l = l + 16
    if (l >= 240 ) then
    hpwm 1,255,1000
    else
    IF (L < 240) THEN
    HPWM 1,L,1000
    endif
    endif
    return

    leddecrease:
    high portc.4
    l = l - 16
    if (l <= 0) then
    hpwm 1,0,1000 '
    else
    if (l > 0) then
    hpwm 1,l,1000
    endif
    endif
    return

    '--------------------------------------------------------------------------

    motorincrease:
    high portc.5
    if (m >= 245) then
    hpwm 2,255,1000
    ELSE
    IF (m < 245) THEN
    m = m + 11
    HPWM 2,m,1000
    endif
    ENDIF
    return

    motordecrease:
    high portc.5
    if (m <= 80) then
    hpwm 2,0,1000
    ELSE
    if (m > 80) then
    m = m - 11
    hpwm 2,m,1000
    endif
    ENDIF
    return

  2. #2
    Join Date
    Feb 2003
    Location
    Salt Lake City, Utah USA
    Posts
    517


    Did you find this post helpful? Yes | No

    Default

    Quote Originally Posted by helmut View Post
    ledincrease:
    high portc.4
    l = l + 16
    if (l >= 240 ) then
    hpwm 1,255,1000
    else
    IF (L < 240) THEN
    HPWM 1,L,1000
    endif
    endif
    return
    Let's just look at this subroutine .. what happens if L = 226 coming into this routine. L is changed to 240, HPWM is set to 255, and then you exit. What happens if you return to this subroutine again. L is increased from 240 to 0 (240+16 = 256 = 1 0000 0000, but as a byte = 0000 0000). HPWM is set to 0.

    See my reply in your earlier related thread for the answer (the answer: do not update L if L is equal to 240)
    http://www.picbasic.co.uk/forum/showthread.php?t=6989
    You also should check the decrease subroutine - I did not check it this time.
    Paul Borgmeier
    Salt Lake City, UT
    USA
    __________________

Similar Threads

  1. Problem with ASM IRQ using FSR0 on PIC18F252
    By mytekcontrols in forum mel PIC BASIC Pro
    Replies: 10
    Last Post: - 11th March 2008, 20:48
  2. USART Problem , but don't know where, in pc? or in PIC?
    By precision in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 15th July 2007, 08:12
  3. Hardware problem or what ?
    By Steve S. in forum mel PIC BASIC Pro
    Replies: 8
    Last Post: - 4th March 2007, 21:39
  4. PicBasic Pro Math Problem??
    By Glen65 in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 5th March 2006, 04:36
  5. 1 slave 1 master 1 MAX232 1 problem ?
    By SuB-ZeRo in forum mel PIC BASIC Pro
    Replies: 19
    Last Post: - 31st July 2005, 22:59

Members who have read this thread : 1

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